1
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
44 public class JSPCompiler {
45
46 public static void main(String[] args) {
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(String appServerType, String compiler, String classPath,
61 String directory, boolean checkTimeStamp) {
62
63 try {
64 _compiler = compiler;
65
66 if (!_compiler.equals("jikes")) {
67 _compiler = "javac";
68 }
69
70 _classPath = StringUtil.replace(
71 classPath, ";", System.getProperty("path.separator"));
72 _directory = directory;
73 _checkTimeStamp = checkTimeStamp;
74
75 _compile(new File(directory));
76 }
77 catch (Exception e) {
78 e.printStackTrace();
79 }
80 }
81
82 private void _compile(File directory) throws Exception {
83 if (directory.exists() && directory.isDirectory()) {
84 List fileList = new ArrayList();
85
86 File[] fileArray = FileUtil.sortFiles(directory.listFiles());
87
88 for (int i = 0; i < fileArray.length; i++) {
89 File file = fileArray[i];
90
91 if (file.isDirectory()) {
92 _compile(fileArray[i]);
93 }
94 else if (file.getName().endsWith(".java")) {
95 fileList.add(file);
96 }
97 }
98
99 _compile(directory.getPath(), fileList);
100 }
101 }
102
103 private void _compile(String sourcePath, List files) throws Exception {
104 if (files.size() == 0) {
105 return;
106 }
107
108 System.out.println(sourcePath);
109
110 for (int i = 0; i < files.size(); i++) {
111 File file = (File)files.get(i);
112
113 String classDestination = _directory;
114
115 String cmd =
116 _compiler + " -classpath " + _classPath +
117 " -d " + classDestination + " " +
118 file.toString();
119
120 File classFile = new File(
121 sourcePath + File.separator +
122 StringUtil.replace(file.getName(), ".java", ".class"));
123
124 if (!classFile.exists() ||
125 (_checkTimeStamp &&
126 (file.lastModified() > classFile.lastModified()))) {
127
128 Runtime rt = Runtime.getRuntime();
129
130 try {
131 Process p = rt.exec(cmd);
132
133 BufferedReader br = new BufferedReader(
134 new InputStreamReader(p.getErrorStream()));
135
136 StringMaker sm = new StringMaker();
137 String line = null;
138
139 while ((line = br.readLine()) != null) {
140 sm.append(line).append("\n");
141 }
142
143 br.close();
144
145 p.waitFor();
146 p.destroy();
147
148 if (!classFile.exists()) {
149 FileUtil.write(
150 classFile.toString() + ".jspc_error",
151 sm.toString());
152 }
153 }
154 catch (Exception e) {
155 e.printStackTrace();
156 }
157 }
158 }
159 }
160
161 private String _compiler;
162 private String _classPath;
163 private String _directory;
164 private boolean _checkTimeStamp;
165
166 }