1   /**
2    * Copyright (c) 2000-2008 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.portal.tools;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.StringMaker;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.util.FileUtil;
29  
30  import java.io.BufferedReader;
31  import java.io.File;
32  import java.io.InputStreamReader;
33  
34  import java.util.ArrayList;
35  import java.util.List;
36  
37  /**
38   * <a href="JSPCompiler.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   * @author Henrik Bentel
42   *
43   */
44  public class JSPCompiler {
45  
46      public static void main(String[] args) throws Exception {
47          if (args.length == 4) {
48              new JSPCompiler(args[0], args[1], args[2], args[3], false);
49          }
50          else if (args.length == 5) {
51              new JSPCompiler(
52                  args[0], args[1], args[2], args[3],
53                  GetterUtil.getBoolean(args[4]));
54          }
55          else {
56              throw new IllegalArgumentException();
57          }
58      }
59  
60      public JSPCompiler(
61              String appServerType, String compiler, String classPath,
62              String directory, boolean checkTimeStamp)
63          throws Exception {
64  
65          _compiler = compiler;
66  
67          if (!_compiler.equals("jikes")) {
68              _compiler = "javac";
69          }
70  
71          _classPath = StringUtil.replace(
72              classPath, ";", System.getProperty("path.separator"));
73          _directory = directory;
74          _checkTimeStamp = checkTimeStamp;
75  
76          _compile(new File(directory));
77      }
78  
79      private void _compile(File directory) throws Exception {
80          if (directory.exists() && directory.isDirectory()) {
81              List fileList = new ArrayList();
82  
83              File[] fileArray = FileUtil.sortFiles(directory.listFiles());
84  
85              for (int i = 0; i < fileArray.length; i++) {
86                  File file = fileArray[i];
87  
88                  if (file.isDirectory()) {
89                      _compile(fileArray[i]);
90                  }
91                  else if (file.getName().endsWith(".java")) {
92                      fileList.add(file);
93                  }
94              }
95  
96              _compile(directory.getPath(), fileList);
97          }
98      }
99  
100     private void _compile(String sourcePath, List files) throws Exception {
101         if (files.size() == 0) {
102             return;
103         }
104 
105         System.out.println(sourcePath);
106 
107         for (int i = 0; i < files.size(); i++) {
108             File file = (File)files.get(i);
109 
110             String classDestination = _directory;
111 
112             String cmd =
113                 _compiler + " -classpath " + _classPath +
114                 " -d " + classDestination + " " +
115                 file.toString();
116 
117             File classFile = new File(
118                 sourcePath + File.separator +
119                 StringUtil.replace(file.getName(), ".java", ".class"));
120 
121             if (!classFile.exists() ||
122                 (_checkTimeStamp &&
123                  (file.lastModified() > classFile.lastModified()))) {
124 
125                 Runtime rt = Runtime.getRuntime();
126 
127                 Process p = rt.exec(cmd);
128 
129                 BufferedReader br = new BufferedReader(
130                     new InputStreamReader(p.getErrorStream()));
131 
132                 StringMaker sm = new StringMaker();
133                 String line = null;
134 
135                 while ((line = br.readLine()) != null) {
136                     sm.append(line).append("\n");
137                 }
138 
139                 br.close();
140 
141                 p.waitFor();
142                 p.destroy();
143 
144                 if (!classFile.exists()) {
145                     throw new Exception(sm.toString());
146                 }
147             }
148         }
149     }
150 
151     private String _compiler;
152     private String _classPath;
153     private String _directory;
154     private boolean _checkTimeStamp;
155 
156 }