1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class BatchJspCompiler {
44  
45      public static void main(String[] args) {
46          if (args.length == 2) {
47              new BatchJspCompiler(args[0], args[1]);
48          }
49          else {
50              throw new IllegalArgumentException();
51          }
52      }
53  
54      public BatchJspCompiler(String appDir, String classDir) {
55          try {
56              _appDir = appDir;
57              _classDir = classDir;
58  
59              DirectoryScanner ds = new DirectoryScanner();
60  
61              ds.setBasedir(appDir);
62              ds.setIncludes(new String[] {"**\\*.jsp"});
63  
64              ds.scan();
65  
66              String[] files = ds.getIncludedFiles();
67  
68              Arrays.sort(files);
69  
70              List<String> fileNames = new ArrayList<String>();
71  
72              for (int i = 0; i < files.length; i++) {
73                  File file = new File(appDir + "/" + files[i]);
74  
75                  fileNames.add(file.toString());
76  
77                  if (((i > 0) && ((i % 200) == 0)) ||
78                      ((i + 1) == files.length)) {
79  
80                      _compile(fileNames);
81  
82                      fileNames.clear();
83                  }
84              }
85          }
86          catch (Exception e) {
87              e.printStackTrace();
88          }
89      }
90  
91      private void _compile(List<String> fileNames) throws Exception {
92          if (fileNames.size() == 0) {
93              return;
94          }
95  
96          List<String> args = new ArrayList<String>();
97  
98          args.add("-app-dir");
99          args.add(_appDir);
100         args.add("-class-dir");
101         args.add(_classDir);
102         args.addAll(fileNames);
103 
104         MethodWrapper methodWrapper = new MethodWrapper(
105             "com.caucho.jsp.JspCompiler", "main",
106             new Object[] {args.toArray(new String[args.size()])});
107 
108         try {
109             MethodInvoker.invoke(methodWrapper);
110         }
111         catch (Exception e) {
112             _fileUtil.write(
113                 _appDir + "/../jspc_error", StackTraceUtil.getStackTrace(e));
114         }
115     }
116 
117     private static FileImpl _fileUtil = FileImpl.getInstance();
118 
119     private String _appDir;
120     private String _classDir;
121 
122 }