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