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