001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.java;
031:
032: import com.caucho.loader.EnvironmentClassLoader;
033: import com.caucho.log.Log;
034: import com.caucho.util.CharBuffer;
035: import com.caucho.vfs.*;
036:
037: import java.io.IOException;
038: import java.io.InputStream;
039: import java.io.PrintWriter;
040: import java.lang.reflect.InvocationTargetException;
041: import java.lang.reflect.Method;
042: import java.util.ArrayList;
043: import java.util.logging.Level;
044: import java.util.logging.Logger;
045:
046: /**
047: * Compiles Java source, returning the loaded class.
048: */
049: public class InternalCompiler extends AbstractJavaCompiler {
050: private static final Logger log = Log.open(InternalCompiler.class);
051:
052: private static boolean _hasCompiler; // already tested for compiler
053:
054: Process _process;
055: String _userPrefix;
056:
057: boolean _isDead;
058:
059: public InternalCompiler(JavaCompiler compiler) {
060: super (compiler);
061: }
062:
063: protected void compileInt(String[] path, LineMap lineMap)
064: throws IOException, JavaCompileException {
065: if (!_hasCompiler) {
066: JavaCompileException exn = null;
067: try {
068: ClassLoader loader = ClassLoader.getSystemClassLoader();
069: Class
070: .forName("com.sun.tools.javac.Main", false,
071: loader);
072:
073: _hasCompiler = true;
074: } catch (Exception e) {
075: }
076:
077: try {
078: EnvironmentClassLoader env;
079: env = new EnvironmentClassLoader(ClassLoader
080: .getSystemClassLoader());
081:
082: Path javaHome = Vfs.lookup(System
083: .getProperty("java.home"));
084: Path jar = javaHome.lookup("./lib/tools.jar");
085: env.addJar(jar);
086: jar = javaHome.lookup("../lib/tools.jar");
087: env.addJar(jar);
088:
089: Class.forName("com.sun.tools.javac.Main", false, env);
090:
091: _hasCompiler = true;
092: } catch (ClassNotFoundException e) {
093: throw new JavaCompileException(
094: L
095: .l(
096: "Resin can't load com.sun.tools.javac.Main. Usually this means that the JDK tools.jar is missing from the classpath, possibly because of using a JRE instead of the JDK. You can either add tools.jar to the classpath or change the compiler to an external one with <java compiler='javac'/> or jikes.\n\n{0}",
097: String.valueOf(e)), e);
098: }
099: }
100:
101: executeInt(path, lineMap);
102: }
103:
104: /**
105: * Compiles the names files.
106: */
107: private void executeInt(String[] path, LineMap lineMap)
108: throws JavaCompileException, IOException {
109: MemoryStream tempStream = new MemoryStream();
110: WriteStream error = new WriteStream(tempStream);
111:
112: try {
113: // String parent = javaPath.getParent().getNativePath();
114:
115: ArrayList<String> argList = new ArrayList<String>();
116: argList.add("-d");
117: argList.add(_compiler.getClassDirName());
118: if (_compiler.getEncoding() != null) {
119: String encoding = Encoding.getJavaName(_compiler
120: .getEncoding());
121: if (encoding != null && !encoding.equals("ISO8859_1")) {
122: argList.add("-encoding");
123: argList.add(_compiler.getEncoding());
124: }
125: }
126: argList.add("-classpath");
127: argList.add(_compiler.getClassPath());
128: ArrayList<String> args = _compiler.getArgs();
129: if (args != null)
130: argList.addAll(args);
131:
132: for (int i = 0; i < path.length; i++) {
133: Path javaPath = _compiler.getSourceDir()
134: .lookup(path[i]);
135: argList.add(javaPath.getNativePath());
136: }
137:
138: if (log.isLoggable(Level.FINER)) {
139: CharBuffer msg = new CharBuffer();
140: msg.append("javac(int)");
141: for (int i = 0; i < argList.size(); i++) {
142: msg.append(" ");
143: msg.append(argList.get(i));
144: }
145:
146: log.finer(msg.toString());
147: }
148:
149: String[] argArray = argList.toArray(new String[argList
150: .size()]);
151:
152: int status = -1;
153:
154: Thread thread = Thread.currentThread();
155: ClassLoader oldLoader = thread.getContextClassLoader();
156:
157: EnvironmentClassLoader env;
158: env = new EnvironmentClassLoader(ClassLoader
159: .getSystemClassLoader());
160:
161: Path javaHome = Vfs.lookup(System.getProperty("java.home"));
162: Path jar = javaHome.lookup("./lib/tools.jar");
163: env.addJar(jar);
164: jar = javaHome.lookup("../lib/tools.jar");
165: env.addJar(jar);
166:
167: try {
168: thread.setContextClassLoader(env);
169:
170: try {
171: Class cl = Class.forName(
172: "com.sun.tools.javac.Main", false, env);
173: Object compiler = cl.newInstance();
174: Method compile = null;
175:
176: Object value = null;
177:
178: try {
179: compile = cl.getMethod("compile", new Class[] {
180: String[].class, PrintWriter.class });
181: value = compile.invoke(compiler, new Object[] {
182: argArray, error.getPrintWriter() });
183:
184: } catch (Throwable e) {
185: log.log(Level.FINER, e.toString(), e);
186: }
187:
188: if (compile == null) {
189: compile = cl.getMethod("compile",
190: new Class[] { String[].class });
191: value = compile.invoke(compiler,
192: new Object[] { argArray });
193: }
194:
195: if (value instanceof Integer)
196: status = ((Integer) value).intValue();
197: } catch (ClassNotFoundException e) {
198: e.printStackTrace();
199: throw new JavaCompileException(
200: L
201: .l("Can't find internal Java compiler. Either configure an external compiler with <javac> or use a JDK which contains a Java compiler."),
202: e);
203: } catch (NoSuchMethodException e) {
204: throw new JavaCompileException(e);
205: } catch (InstantiationException e) {
206: throw new JavaCompileException(e);
207: } catch (IllegalAccessException e) {
208: throw new JavaCompileException(e);
209: } catch (InvocationTargetException e) {
210: throw new IOExceptionWrapper(e);
211: }
212:
213: error.close();
214: tempStream.close();
215: } finally {
216: thread.setContextClassLoader(oldLoader);
217: }
218:
219: ReadStream read = tempStream.openReadAndSaveBuffer();
220: JavacErrorParser parser = new JavacErrorParser(_compiler
221: .getEncoding());
222:
223: String errors = parser.parseErrors((InputStream) read,
224: lineMap);
225: read.close();
226:
227: if (errors != null)
228: errors = errors.trim();
229:
230: if (log.isLoggable(Level.FINE)) {
231: read = tempStream.openReadAndSaveBuffer();
232: CharBuffer cb = new CharBuffer();
233: int ch;
234: while ((ch = read.read()) >= 0) {
235: cb.append((char) ch);
236: }
237: read.close();
238:
239: log.fine(cb.toString());
240: } else if (status == 0 && errors != null
241: && !errors.equals("")) {
242: final String msg = errors;
243:
244: new com.caucho.loader.ClassLoaderContext(_compiler
245: .getClassLoader()) {
246: public void run() {
247: log.warning(msg);
248: }
249: };
250: }
251:
252: if (status != 0)
253: throw new JavaCompileException(errors);
254: } finally {
255: tempStream.destroy();
256: }
257: }
258: }
|