1   /**
2    * Copyright (c) 2000-2008 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.io.File;
31  
32  import java.util.ArrayList;
33  import java.util.Arrays;
34  import java.util.List;
35  
36  import org.apache.tools.ant.DirectoryScanner;
37  
38  /**
39   * <a href="BatchJspCompiler.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class BatchJspCompiler {
45  
46      public static void main(String[] args) {
47          if (args.length == 2) {
48              new BatchJspCompiler(args[0], args[1]);
49          }
50          else {
51              throw new IllegalArgumentException();
52          }
53      }
54  
55      public BatchJspCompiler(String appDir, String classDir) {
56          try {
57              _appDir = appDir;
58              _classDir = classDir;
59  
60              DirectoryScanner ds = new DirectoryScanner();
61  
62              ds.setBasedir(appDir);
63              ds.setIncludes(new String[] {"**\\*.jsp"});
64  
65              ds.scan();
66  
67              String[] files = ds.getIncludedFiles();
68  
69              Arrays.sort(files);
70  
71              List<String> fileNames = new ArrayList<String>();
72  
73              for (int i = 0; i < files.length; i++) {
74                  File file = new File(appDir + "/" + files[i]);
75  
76                  fileNames.add(file.toString());
77  
78                  if (((i > 0) && ((i % 200) == 0)) ||
79                      ((i + 1) == files.length)) {
80  
81                      _compile(fileNames);
82  
83                      fileNames.clear();
84                  }
85              }
86          }
87          catch (Exception e) {
88              e.printStackTrace();
89          }
90      }
91  
92      private void _compile(List<String> fileNames) throws Exception {
93          if (fileNames.size() == 0) {
94              return;
95          }
96  
97          List<String> args = new ArrayList<String>();
98  
99          args.add("-app-dir");
100         args.add(_appDir);
101         args.add("-class-dir");
102         args.add(_classDir);
103         args.addAll(fileNames);
104 
105         MethodWrapper methodWrapper = new MethodWrapper(
106             "com.caucho.jsp.JspCompiler", "main",
107             new Object[] {args.toArray(new String[args.size()])});
108 
109         try {
110             MethodInvoker.invoke(methodWrapper);
111         }
112         catch (Exception e) {
113             _fileUtil.write(
114                 _appDir + "/../jspc_error", StackTraceUtil.getStackTrace(e));
115         }
116     }
117 
118     private static FileImpl _fileUtil = FileImpl.getInstance();
119 
120     private String _appDir;
121     private String _classDir;
122 
123 }