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) 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 }