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