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<File> fileList = new ArrayList<File>();
82  
83              File[] fileArray = FileUtil.sortFiles(directory.listFiles());
84  
85              for (File file : fileArray) {
86                  if (file.isDirectory()) {
87                      _compile(file);
88                  }
89                  else if (file.getName().endsWith(".java")) {
90                      fileList.add(file);
91                  }
92              }
93  
94              _compile(directory.getPath(), fileList);
95          }
96      }
97  
98      private void _compile(String sourcePath, List<File> files)
99          throws Exception {
100 
101         if (files.size() == 0) {
102             return;
103         }
104 
105         System.out.println(sourcePath);
106 
107         for (File file : files) {
108             String classDestination = _directory;
109 
110             String cmd =
111                 _compiler + " -classpath " + _classPath +
112                 " -d " + classDestination + " " +
113                 file.toString();
114 
115             File classFile = new File(
116                 sourcePath + File.separator +
117                 StringUtil.replace(file.getName(), ".java", ".class"));
118 
119             if (!classFile.exists() ||
120                 (_checkTimeStamp &&
121                  (file.lastModified() > classFile.lastModified()))) {
122 
123                 Runtime rt = Runtime.getRuntime();
124 
125                 Process p = rt.exec(cmd);
126 
127                 BufferedReader br = new BufferedReader(
128                     new InputStreamReader(p.getErrorStream()));
129 
130                 StringMaker sm = new StringMaker();
131                 String line = null;
132 
133                 while ((line = br.readLine()) != null) {
134                     sm.append(line).append("\n");
135                 }
136 
137                 br.close();
138 
139                 p.waitFor();
140                 p.destroy();
141 
142                 if (!classFile.exists()) {
143                     throw new Exception(sm.toString());
144                 }
145             }
146         }
147     }
148 
149     private String _compiler;
150     private String _classPath;
151     private String _directory;
152     private boolean _checkTimeStamp;
153 
154 }