001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools.jspc.resin;
016    
017    import com.liferay.portal.kernel.util.MethodInvoker;
018    import com.liferay.portal.kernel.util.MethodWrapper;
019    import com.liferay.portal.kernel.util.StackTraceUtil;
020    import com.liferay.portal.util.FileImpl;
021    
022    import java.util.ArrayList;
023    import java.util.Arrays;
024    import java.util.List;
025    
026    import org.apache.tools.ant.DirectoryScanner;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class BatchJspCompiler {
032    
033            public static void main(String[] args) {
034                    if (args.length == 2) {
035                            new BatchJspCompiler(args[0], args[1]);
036                    }
037                    else {
038                            throw new IllegalArgumentException();
039                    }
040            }
041    
042            public BatchJspCompiler(String appDir, String classDir) {
043                    try {
044                            _appDir = appDir;
045                            _classDir = classDir;
046    
047                            DirectoryScanner ds = new DirectoryScanner();
048    
049                            ds.setBasedir(appDir);
050                            ds.setIncludes(new String[] {"**\\*.jsp"});
051    
052                            ds.scan();
053    
054                            String[] files = ds.getIncludedFiles();
055    
056                            Arrays.sort(files);
057    
058                            List<String> fileNames = new ArrayList<String>();
059    
060                            for (int i = 0; i < files.length; i++) {
061                                    String fileName = files[i];
062    
063                                    fileNames.add(fileName);
064    
065                                    if (((i > 0) && ((i % 200) == 0)) ||
066                                            ((i + 1) == files.length)) {
067    
068                                            _compile(fileNames);
069    
070                                            fileNames.clear();
071                                    }
072                            }
073                    }
074                    catch (Exception e) {
075                            e.printStackTrace();
076                    }
077            }
078    
079            private void _compile(List<String> fileNames) throws Exception {
080                    if (fileNames.size() == 0) {
081                            return;
082                    }
083    
084                    List<String> args = new ArrayList<String>();
085    
086                    args.add("-app-dir");
087                    args.add(_appDir);
088                    args.add("-class-dir");
089                    args.add(_classDir);
090                    args.addAll(fileNames);
091    
092                    MethodWrapper methodWrapper = new MethodWrapper(
093                            "com.caucho.jsp.JspCompiler", "main",
094                            new Object[] {args.toArray(new String[args.size()])});
095    
096                    try {
097                            MethodInvoker.invoke(methodWrapper);
098                    }
099                    catch (Exception e) {
100                            _fileUtil.write(
101                                    _appDir + "/../jspc_error", StackTraceUtil.getStackTrace(e));
102                    }
103            }
104    
105            private static FileImpl _fileUtil = FileImpl.getInstance();
106    
107            private String _appDir;
108            private String _classDir;
109    
110    }