1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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.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> args = new ArrayList<String>();
87  
88          args.add("-app-dir");
89          args.add(_appDir);
90          args.add("-class-dir");
91          args.add(_classDir);
92          args.addAll(fileNames);
93  
94          MethodWrapper methodWrapper = new MethodWrapper(
95              "com.caucho.jsp.JspCompiler", "main",
96              new Object[] {args.toArray(new String[args.size()])});
97  
98          try {
99              MethodInvoker.invoke(methodWrapper);
100         }
101         catch (Exception e) {
102             _fileUtil.write(
103                 _appDir + "/../jspc_error", StackTraceUtil.getStackTrace(e));
104         }
105     }
106 
107     private static FileImpl _fileUtil = FileImpl.getInstance();
108 
109     private String _appDir;
110     private String _classDir;
111 
112 }