1 29 30 package com.caucho.java; 31 32 import com.caucho.loader.EnvironmentClassLoader; 33 import com.caucho.util.CharBuffer; 34 import com.caucho.vfs.Encoding; 35 import com.caucho.vfs.IOExceptionWrapper; 36 import com.caucho.vfs.MemoryStream; 37 import com.caucho.vfs.Path; 38 import com.caucho.vfs.ReadStream; 39 import com.caucho.vfs.WriteStream; 40 41 import java.io.IOException ; 42 import java.io.InputStream ; 43 import java.io.PrintWriter ; 44 import java.lang.reflect.Constructor ; 45 import java.lang.reflect.InvocationTargetException ; 46 import java.lang.reflect.Method ; 47 import java.util.ArrayList ; 48 import java.util.logging.Level ; 49 50 53 public class EclipseCompiler extends AbstractJavaCompiler { 54 private static boolean _hasCompiler; 56 private static final String COMPILER 57 = "org.eclipse.jdt.internal.compiler.batch.Main"; 58 59 Process _process; 60 String _userPrefix; 61 62 boolean _isDead; 63 64 public EclipseCompiler(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(COMPILER, false, 75 Thread.currentThread().getContextClassLoader()); 76 77 _hasCompiler = true; 78 } catch (Exception e) { 79 e.printStackTrace(); 80 throw new JavaCompileException(L.l("Resin can't load org.eclipse.jdt.core.JDTCompilerAdapter. 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 = CharBuffer.allocate(); 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 log.fine(msg.close()); 134 } 135 136 String []argArray = argList.toArray(new String [argList.size()]); 137 138 int status = -1; 139 140 Thread thread = Thread.currentThread(); 141 ClassLoader oldLoader = thread.getContextClassLoader(); 142 try { 143 EnvironmentClassLoader env; 144 env = new EnvironmentClassLoader(ClassLoader.getSystemClassLoader()); 145 thread.setContextClassLoader(env); 146 147 try { 148 Class cl = Class.forName(COMPILER, false, env); 149 Constructor xtor = cl.getConstructor(new Class [] { PrintWriter .class, PrintWriter .class, boolean.class }); 150 151 Object value = xtor.newInstance(error.getPrintWriter(), error.getPrintWriter(), Boolean.FALSE); 152 153 Method compile = cl.getMethod("compile", new Class [] { String [].class }); 154 155 Object result = compile.invoke(value, new Object [] { argArray }); 156 157 status = Boolean.TRUE.equals(result) ? 0 : -1; 158 } catch (ClassNotFoundException e) { 159 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.")); 160 } catch (NoSuchMethodException e) { 161 throw new JavaCompileException(e); 162 } catch (InstantiationException e) { 163 throw new JavaCompileException(e); 164 } catch (IllegalAccessException e) { 165 throw new JavaCompileException(e); 166 } catch (InvocationTargetException e) { 167 throw new IOExceptionWrapper(e); 168 } 169 170 error.close(); 171 tempStream.close(); 172 } finally { 173 thread.setContextClassLoader(oldLoader); 174 } 175 176 ReadStream read = tempStream.openRead(); 177 JavacErrorParser parser = new JavacErrorParser(); 178 179 String errors = parser.parseErrors((InputStream ) read, lineMap); 180 read.close(); 181 182 if (errors != null) 183 errors = errors.trim(); 184 185 if (log.isLoggable(Level.FINE)) { 186 read = tempStream.openRead(); 187 CharBuffer cb = new CharBuffer(); 188 int ch; 189 while ((ch = read.read()) >= 0) { 190 cb.append((char) ch); 191 } 192 read.close(); 193 194 log.fine(cb.toString()); 195 } 196 208 209 if (status != 0) 210 throw new JavaCompileException(errors); 211 } finally { 212 tempStream.destroy(); 213 } 214 } 215 } 216 | Popular Tags |