1 28 29 package com.caucho.java; 30 31 import com.caucho.log.Log; 32 import com.caucho.server.util.CauchoSystem; 33 import com.caucho.util.CharBuffer; 34 import com.caucho.vfs.IOExceptionWrapper; 35 import com.caucho.vfs.Path; 36 import com.caucho.vfs.Vfs; 37 38 import java.io.IOException ; 39 import java.lang.reflect.Method ; 40 import java.util.ArrayList ; 41 import java.util.logging.Logger ; 42 43 46 public class GroovyCompiler extends AbstractJavaCompiler { 47 protected static final Logger log = Log.open(GroovyCompiler.class); 48 49 private final static String GROOVY_COMPILER = 50 "org.codehaus.groovy.tools.FileSystemCompiler"; 51 52 private static Class _groovyCompilerClass; 53 private static Method _setOutputDir; 54 private static Method _setClasspath; 55 private static Method _compile; 56 57 String _userPrefix; 58 59 public GroovyCompiler(JavaCompiler compiler) 60 { 61 super(compiler); 62 } 63 64 70 protected void compileInt(String []paths, LineMap lineMap) 71 throws IOException 72 { 73 Thread thread = Thread.currentThread(); 74 ClassLoader loader = thread.getContextClassLoader(); 75 76 if (_groovyCompilerClass == null) { 77 try { 78 _groovyCompilerClass = Class.forName(GROOVY_COMPILER, false, loader); 79 _setClasspath = 80 _groovyCompilerClass.getMethod("setClasspath", 81 new Class [] { String .class }); 82 83 _setOutputDir = 84 _groovyCompilerClass.getMethod("setOutputDir", 85 new Class [] { String .class }); 86 87 _compile = 88 _groovyCompilerClass.getMethod("compile", 89 new Class [] { String [].class }); 90 91 } catch (Exception e) { 92 throw new RuntimeException (e); 93 } 94 } 95 96 Object compiler; 97 try { 98 compiler = _groovyCompilerClass.newInstance(); 99 } catch (Exception e) { 100 throw new RuntimeException (e); 101 } 102 103 try { 104 String sourceExt = _compiler.getSourceExtension(); 105 106 String path = paths[0]; 107 int tail = path.length() - sourceExt.length(); 108 String className = path.substring(0, tail); 109 Path classFile = _compiler.getClassDir().lookup(className + ".class"); 110 111 String cp = normalizeClassPath(_compiler.getClassPath(), false); 112 113 _setClasspath.invoke(compiler, new Object [] { cp }); 114 115 String dest = normalizePath(_compiler.getClassDirName(), false); 116 117 _setOutputDir.invoke(compiler, new Object [] { dest }); 118 119 ArrayList <String > argList = new ArrayList <String >(); 120 for (int i = 0; i < paths.length; i++) { 121 Path javaPath = _compiler.getSourceDir().lookup(paths[i]); 122 argList.add(javaPath.getNativePath()); 123 } 124 125 String []files = new String [argList.size()]; 126 127 argList.toArray(files); 128 129 _compile.invoke(compiler, new Object [] {files}); 130 } catch (Exception e) { 131 e.printStackTrace(); 132 throw new IOExceptionWrapper(e); 133 } 134 } 135 136 139 String normalizeClassPath(String classPath, boolean generateRelative) 140 { 141 char sep = CauchoSystem.getPathSeparatorChar(); 142 int head = 0; 143 int tail = 0; 144 145 CharBuffer cb = CharBuffer.allocate(); 146 147 while (head < classPath.length()) { 148 tail = classPath.indexOf(sep, head); 149 if (tail < 0) 150 tail = classPath.length(); 151 152 if (tail > head) { 153 String segment = classPath.substring(head, tail); 154 155 if (cb.length() != 0) 156 cb.append(sep); 157 158 cb.append(normalizePath(segment, generateRelative)); 159 } 160 161 head = tail + 1; 162 } 163 164 return cb.close(); 165 } 166 169 String normalizePath(String segment, boolean generateRelative) 170 { 171 if (_userPrefix == null) { 172 Path userPath = Vfs.lookup(CauchoSystem.getUserDir()); 173 char sep = CauchoSystem.getFileSeparatorChar(); 174 _userPrefix = userPath.getNativePath(); 175 176 if (_userPrefix.length() == 0 || 177 _userPrefix.charAt(_userPrefix.length() - 1) != sep) { 178 _userPrefix = _userPrefix + sep; 179 } 180 } 181 182 Path path = Vfs.lookup(segment); 183 String nativePath = path.getNativePath(); 184 185 if (! generateRelative) 186 return nativePath; 187 188 if (nativePath.startsWith(_userPrefix)) 189 nativePath = nativePath.substring(_userPrefix.length()); 190 191 if (nativePath.equals("")) 192 return "."; 193 else 194 return nativePath; 195 } 196 197 public static class CompilerThread implements Runnable { 198 private volatile boolean _isDone; 199 200 public void run() 201 { 202 try { 203 } finally { 204 _isDone = true; 205 206 synchronized (this) { 207 notifyAll(); 208 } 209 } 210 } 211 } 212 } 213 | Popular Tags |