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.MethodHandler;
018    import com.liferay.portal.kernel.util.MethodKey;
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> arguments = new ArrayList<String>();
085    
086                    arguments.add("-app-dir");
087                    arguments.add(_appDir);
088                    arguments.add("-class-dir");
089                    arguments.add(_classDir);
090                    arguments.addAll(fileNames);
091    
092                    MethodKey methodKey = new MethodKey(
093                            "com.caucho.jsp.JspCompiler", "main", String[].class);
094    
095                    MethodHandler methodHandler = new MethodHandler(
096                            methodKey,
097                            (Object)arguments.toArray(new String[arguments.size()]));
098    
099                    try {
100                            methodHandler.invoke(false);
101                    }
102                    catch (Exception e) {
103                            _fileUtil.write(
104                                    _appDir + "/../jspc_error", StackTraceUtil.getStackTrace(e));
105                    }
106            }
107    
108            private static FileImpl _fileUtil = FileImpl.getInstance();
109    
110            private String _appDir;
111            private String _classDir;
112    
113    }