001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.tools;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024: import com.liferay.portal.kernel.util.StringMaker;
025: import com.liferay.portal.kernel.util.StringUtil;
026: import com.liferay.util.FileUtil;
027:
028: import java.io.BufferedReader;
029: import java.io.File;
030: import java.io.InputStreamReader;
031:
032: import java.util.ArrayList;
033: import java.util.List;
034:
035: /**
036: * <a href="JSPCompiler.java.html"><b><i>View Source</i></b></a>
037: *
038: * @author Brian Wing Shun Chan
039: * @author Henrik Bentel
040: *
041: */
042: public class JSPCompiler {
043:
044: public static void main(String[] args) throws Exception {
045: if (args.length == 4) {
046: new JSPCompiler(args[0], args[1], args[2], args[3], false);
047: } else if (args.length == 5) {
048: new JSPCompiler(args[0], args[1], args[2], args[3],
049: GetterUtil.getBoolean(args[4]));
050: } else {
051: throw new IllegalArgumentException();
052: }
053: }
054:
055: public JSPCompiler(String appServerType, String compiler,
056: String classPath, String directory, boolean checkTimeStamp)
057: throws Exception {
058:
059: _compiler = compiler;
060:
061: if (!_compiler.equals("jikes")) {
062: _compiler = "javac";
063: }
064:
065: _classPath = StringUtil.replace(classPath, ";", System
066: .getProperty("path.separator"));
067: _directory = directory;
068: _checkTimeStamp = checkTimeStamp;
069:
070: _compile(new File(directory));
071: }
072:
073: private void _compile(File directory) throws Exception {
074: if (directory.exists() && directory.isDirectory()) {
075: List fileList = new ArrayList();
076:
077: File[] fileArray = FileUtil
078: .sortFiles(directory.listFiles());
079:
080: for (int i = 0; i < fileArray.length; i++) {
081: File file = fileArray[i];
082:
083: if (file.isDirectory()) {
084: _compile(fileArray[i]);
085: } else if (file.getName().endsWith(".java")) {
086: fileList.add(file);
087: }
088: }
089:
090: _compile(directory.getPath(), fileList);
091: }
092: }
093:
094: private void _compile(String sourcePath, List files)
095: throws Exception {
096: if (files.size() == 0) {
097: return;
098: }
099:
100: System.out.println(sourcePath);
101:
102: for (int i = 0; i < files.size(); i++) {
103: File file = (File) files.get(i);
104:
105: String classDestination = _directory;
106:
107: String cmd = _compiler + " -classpath " + _classPath
108: + " -d " + classDestination + " " + file.toString();
109:
110: File classFile = new File(sourcePath
111: + File.separator
112: + StringUtil.replace(file.getName(), ".java",
113: ".class"));
114:
115: if (!classFile.exists()
116: || (_checkTimeStamp && (file.lastModified() > classFile
117: .lastModified()))) {
118:
119: Runtime rt = Runtime.getRuntime();
120:
121: Process p = rt.exec(cmd);
122:
123: BufferedReader br = new BufferedReader(
124: new InputStreamReader(p.getErrorStream()));
125:
126: StringMaker sm = new StringMaker();
127: String line = null;
128:
129: while ((line = br.readLine()) != null) {
130: sm.append(line).append("\n");
131: }
132:
133: br.close();
134:
135: p.waitFor();
136: p.destroy();
137:
138: if (!classFile.exists()) {
139: throw new Exception(sm.toString());
140: }
141: }
142: }
143: }
144:
145: private String _compiler;
146: private String _classPath;
147: private String _directory;
148: private boolean _checkTimeStamp;
149:
150: }
|