001    /**
002     * Copyright (c) 2000-2013 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.StackTraceUtil;
018    import com.liferay.portal.util.FileImpl;
019    
020    import java.lang.reflect.Method;
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[] fileNames = ds.getIncludedFiles();
055    
056                            Arrays.sort(fileNames);
057    
058                            _compile(fileNames);
059                    }
060                    catch (Exception e) {
061                            e.printStackTrace();
062                    }
063            }
064    
065            private void _compile(String[] fileNames) throws Exception {
066                    if (fileNames.length == 0) {
067                            return;
068                    }
069    
070                    List<String> arguments = new ArrayList<String>();
071    
072                    arguments.add("-app-dir");
073                    arguments.add(_appDir);
074                    arguments.add("-class-dir");
075                    arguments.add(_classDir);
076                    arguments.addAll(Arrays.asList(fileNames));
077    
078                    Class<?> clazz = Class.forName("com.caucho.jsp.JspCompiler");
079    
080                    Method method = clazz.getMethod("main", String[].class);
081    
082                    try {
083                            method.invoke(
084                                    null, (Object)arguments.toArray(new String[arguments.size()]));
085                    }
086                    catch (Exception e) {
087                            _fileUtil.write(
088                                    _appDir + "/../jspc_error", StackTraceUtil.getStackTrace(e));
089                    }
090            }
091    
092            private static FileImpl _fileUtil = FileImpl.getInstance();
093    
094            private String _appDir;
095            private String _classDir;
096    
097    }