1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.tools.jspc.resin;
24  
25  import com.liferay.portal.kernel.util.MethodInvoker;
26  import com.liferay.portal.kernel.util.MethodWrapper;
27  import com.liferay.portal.kernel.util.StackTraceUtil;
28  import com.liferay.portal.util.FileImpl;
29  
30  import java.util.ArrayList;
31  import java.util.Arrays;
32  import java.util.List;
33  
34  import org.apache.tools.ant.DirectoryScanner;
35  
36  /**
37   * <a href="BatchJspCompiler.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class BatchJspCompiler {
43  
44      public static void main(String[] args) {
45          if (args.length == 2) {
46              new BatchJspCompiler(args[0], args[1]);
47          }
48          else {
49              throw new IllegalArgumentException();
50          }
51      }
52  
53      public BatchJspCompiler(String appDir, String classDir) {
54          try {
55              _appDir = appDir;
56              _classDir = classDir;
57  
58              DirectoryScanner ds = new DirectoryScanner();
59  
60              ds.setBasedir(appDir);
61              ds.setIncludes(new String[] {"**\\*.jsp"});
62  
63              ds.scan();
64  
65              String[] files = ds.getIncludedFiles();
66  
67              Arrays.sort(files);
68  
69              List<String> fileNames = new ArrayList<String>();
70  
71              for (int i = 0; i < files.length; i++) {
72                  String fileName = files[i];
73  
74                  fileNames.add(fileName);
75  
76                  if (((i > 0) && ((i % 200) == 0)) ||
77                      ((i + 1) == files.length)) {
78  
79                      _compile(fileNames);
80  
81                      fileNames.clear();
82                  }
83              }
84          }
85          catch (Exception e) {
86              e.printStackTrace();
87          }
88      }
89  
90      private void _compile(List<String> fileNames) throws Exception {
91          if (fileNames.size() == 0) {
92              return;
93          }
94  
95          List<String> args = new ArrayList<String>();
96  
97          args.add("-app-dir");
98          args.add(_appDir);
99          args.add("-class-dir");
100         args.add(_classDir);
101         args.addAll(fileNames);
102 
103         MethodWrapper methodWrapper = new MethodWrapper(
104             "com.caucho.jsp.JspCompiler", "main",
105             new Object[] {args.toArray(new String[args.size()])});
106 
107         try {
108             MethodInvoker.invoke(methodWrapper);
109         }
110         catch (Exception e) {
111             _fileUtil.write(
112                 _appDir + "/../jspc_error", StackTraceUtil.getStackTrace(e));
113         }
114     }
115 
116     private static FileImpl _fileUtil = FileImpl.getInstance();
117 
118     private String _appDir;
119     private String _classDir;
120 
121 }