1   /**
2    * Copyright (c) 2000-2007 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.util.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.util.FileUtil;
29  
30  import java.io.File;
31  
32  import java.util.ArrayList;
33  import java.util.List;
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              _compile(new File(appDir));
58          }
59          catch (Exception e) {
60              e.printStackTrace();
61          }
62      }
63  
64      private void _compile(File directory) throws Exception {
65          if (directory.exists() && directory.isDirectory()) {
66              List fileList = new ArrayList();
67  
68              File[] fileArray = FileUtil.sortFiles(directory.listFiles());
69  
70              for (int i = 0; i < fileArray.length; i++) {
71                  File file = fileArray[i];
72  
73                  if (file.isDirectory()) {
74                      _compile(fileArray[i]);
75                  }
76                  else if (file.getName().endsWith(".jsp")) {
77                      fileList.add(file);
78                  }
79              }
80  
81              _compile(directory.getPath(), fileList);
82          }
83      }
84  
85      private void _compile(String sourcePath, List files) throws Exception {
86          if (files.size() == 0) {
87              return;
88          }
89  
90          System.out.println(sourcePath);
91  
92          for (int i = 0; i < files.size(); i++) {
93              File file = (File)files.get(i);
94  
95              String fileName = file.toString();
96  
97              String[] args = new String[] {
98                  "-app-dir", _appDir, "-class-dir", _classDir, fileName
99              };
100 
101             MethodWrapper methodWrapper = new MethodWrapper(
102                 "com.caucho.jsp.JspCompiler", "main", new Object[] {args});
103 
104             try {
105                 MethodInvoker.invoke(methodWrapper);
106             }
107             catch (Exception e) {
108                 FileUtil.write(
109                     fileName + ".jspc_error", StackTraceUtil.getStackTrace(e));
110             }
111         }
112     }
113 
114     private String _appDir;
115     private String _classDir;
116 
117 }