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