1 29 30 package com.caucho.java; 31 32 import com.caucho.loader.EnvironmentClassLoader; 33 import com.caucho.log.Log; 34 import com.caucho.util.CharBuffer; 35 import com.caucho.vfs.Encoding; 36 import com.caucho.vfs.IOExceptionWrapper; 37 import com.caucho.vfs.MemoryStream; 38 import com.caucho.vfs.Path; 39 import com.caucho.vfs.ReadStream; 40 import com.caucho.vfs.WriteStream; 41 42 import java.io.IOException ; 43 import java.io.InputStream ; 44 import java.io.PrintWriter ; 45 import java.lang.reflect.InvocationTargetException ; 46 import java.lang.reflect.Method ; 47 import java.util.ArrayList ; 48 import java.util.logging.Level ; 49 import java.util.logging.Logger ; 50 51 54 public class InternalCompiler extends AbstractJavaCompiler { 55 private static final Logger log = Log.open(InternalCompiler.class); 56 57 private static boolean _hasCompiler; 59 Process _process; 60 String _userPrefix; 61 62 boolean _isDead; 63 64 public InternalCompiler(JavaCompiler compiler) 65 { 66 super(compiler); 67 } 68 69 protected void compileInt(String []path, LineMap lineMap) 70 throws IOException , JavaCompileException 71 { 72 if (! _hasCompiler) { 73 try { 74 Class.forName("com.sun.tools.javac.Main", 75 false, Thread.currentThread().getContextClassLoader()); 76 77 _hasCompiler = true; 78 } catch (Exception e) { 79 e.printStackTrace(); 80 throw new JavaCompileException(L.l("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}", String.valueOf(e))); 81 } 82 } 83 84 executeInt(path, lineMap); 85 } 86 87 90 private void executeInt(String []path, LineMap lineMap) 91 throws JavaCompileException, IOException 92 { 93 MemoryStream tempStream = new MemoryStream(); 94 WriteStream error = new WriteStream(tempStream); 95 96 try { 97 99 ArrayList <String > argList = new ArrayList <String >(); 100 106 argList.add("-d"); 107 argList.add(_compiler.getClassDirName()); 108 if (_compiler.getEncoding() != null) { 109 String encoding = Encoding.getJavaName(_compiler.getEncoding()); 110 if (encoding != null && ! encoding.equals("ISO8859_1")) { 111 argList.add("-encoding"); 112 argList.add(_compiler.getEncoding()); 113 } 114 } 115 argList.add("-classpath"); 116 argList.add(_compiler.getClassPath()); 117 ArrayList <String > args = _compiler.getArgs(); 118 if (args != null) 119 argList.addAll(args); 120 121 for (int i = 0; i < path.length; i++) { 122 Path javaPath = _compiler.getSourceDir().lookup(path[i]); 123 argList.add(javaPath.getNativePath()); 124 } 125 126 if (log.isLoggable(Level.FINE)) { 127 CharBuffer msg = new CharBuffer(); 128 msg.append("javac(int)"); 129 for (int i = 0; i < argList.size(); i++) { 130 msg.append(" "); 131 msg.append(argList.get(i)); 132 } 133 134 log.fine(msg.toString()); 135 } 136 137 String []argArray = argList.toArray(new String [argList.size()]); 138 139 int status = -1; 140 141 Thread thread = Thread.currentThread(); 142 ClassLoader oldLoader = thread.getContextClassLoader(); 143 try { 144 EnvironmentClassLoader env; 145 env = new EnvironmentClassLoader(ClassLoader.getSystemClassLoader()); 146 thread.setContextClassLoader(env); 147 148 try { 149 Class cl = Class.forName("com.sun.tools.javac.Main", false, env); 150 Object compiler = cl.newInstance(); 151 Method compile = null; 152 153 Object value = null; 154 155 try { 156 compile = cl.getMethod("compile", new Class [] { String [].class , PrintWriter .class }); 157 value = compile.invoke(compiler, new Object [] { argArray, error.getPrintWriter() }); 158 159 } catch (Throwable e) { 160 log.log(Level.FINER, e.toString(), e); 161 } 162 163 if (compile == null) { 164 compile = cl.getMethod("compile", new Class [] { String [].class }); 165 value = compile.invoke(compiler, new Object [] { argArray }); 166 } 167 168 if (value instanceof Integer ) 169 status = ((Integer ) value).intValue(); 170 } catch (ClassNotFoundException e) { 171 throw new JavaCompileException(L.l("Can't find internal Java compiler. Either configure an external compiler with <javac> or use a JDK which contains a Java compiler.")); 172 } catch (NoSuchMethodException e) { 173 throw new JavaCompileException(e); 174 } catch (InstantiationException e) { 175 throw new JavaCompileException(e); 176 } catch (IllegalAccessException e) { 177 throw new JavaCompileException(e); 178 } catch (InvocationTargetException e) { 179 throw new IOExceptionWrapper(e); 180 } 181 182 error.close(); 183 tempStream.close(); 184 } finally { 185 thread.setContextClassLoader(oldLoader); 186 } 187 188 ReadStream read = tempStream.openRead(); 189 JavacErrorParser parser = new JavacErrorParser(_compiler.getEncoding()); 190 191 String errors = parser.parseErrors((InputStream ) read, lineMap); 192 read.close(); 193 194 if (errors != null) 195 errors = errors.trim(); 196 197 if (log.isLoggable(Level.FINE)) { 198 read = tempStream.openRead(); 199 CharBuffer cb = new CharBuffer(); 200 int ch; 201 while ((ch = read.read()) >= 0) { 202 cb.append((char) ch); 203 } 204 read.close(); 205 206 log.fine(cb.toString()); 207 } 208 else if (status == 0 && errors != null && ! errors.equals("")) { 209 final String msg = errors; 210 211 new com.caucho.loader.ClassLoaderContext(_compiler.getClassLoader()) { 212 public void run() 213 { 214 log.warning(msg); 215 } 216 }; 217 } 218 219 if (status != 0) 220 throw new JavaCompileException(errors); 221 } finally { 222 tempStream.destroy(); 223 } 224 } 225 } 226 | Popular Tags |