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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.java;
030:
031: import com.caucho.log.Log;
032: import com.caucho.server.util.CauchoSystem;
033: import com.caucho.util.CharBuffer;
034: import com.caucho.vfs.IOExceptionWrapper;
035: import com.caucho.vfs.Path;
036: import com.caucho.vfs.Vfs;
037:
038: import java.io.IOException;
039: import java.lang.reflect.Method;
040: import java.util.ArrayList;
041: import java.util.logging.Logger;
042:
043: /**
044: * Compiles Groovy source, returning the loaded class.
045: */
046: public class GroovyCompiler extends AbstractJavaCompiler {
047: protected static final Logger log = Log.open(GroovyCompiler.class);
048:
049: private final static String GROOVY_COMPILER = "org.codehaus.groovy.tools.FileSystemCompiler";
050:
051: private static Class _groovyCompilerClass;
052: private static Method _setOutputDir;
053: private static Method _setClasspath;
054: private static Method _compile;
055:
056: String _userPrefix;
057:
058: public GroovyCompiler(JavaCompiler compiler) {
059: super (compiler);
060: }
061:
062: /**
063: * Compile the configured file.
064: *
065: * @param path the path to the java source.
066: * @param lineMap mapping from the generated source to the original files.
067: */
068: protected void compileInt(String[] paths, LineMap lineMap)
069: throws IOException {
070: Thread thread = Thread.currentThread();
071: ClassLoader loader = thread.getContextClassLoader();
072:
073: if (_groovyCompilerClass == null) {
074: try {
075: _groovyCompilerClass = Class.forName(GROOVY_COMPILER,
076: false, loader);
077: _setClasspath = _groovyCompilerClass.getMethod(
078: "setClasspath", new Class[] { String.class });
079:
080: _setOutputDir = _groovyCompilerClass.getMethod(
081: "setOutputDir", new Class[] { String.class });
082:
083: _compile = _groovyCompilerClass.getMethod("compile",
084: new Class[] { String[].class });
085:
086: } catch (Exception e) {
087: throw new RuntimeException(e);
088: }
089: }
090:
091: Object compiler;
092: try {
093: compiler = _groovyCompilerClass.newInstance();
094: } catch (Exception e) {
095: throw new RuntimeException(e);
096: }
097:
098: try {
099: String sourceExt = _compiler.getSourceExtension();
100:
101: String path = paths[0];
102: int tail = path.length() - sourceExt.length();
103: String className = path.substring(0, tail);
104: Path classFile = _compiler.getClassDir().lookup(
105: className + ".class");
106:
107: String cp = normalizeClassPath(_compiler.getClassPath(),
108: false);
109:
110: _setClasspath.invoke(compiler, new Object[] { cp });
111:
112: String dest = normalizePath(_compiler.getClassDirName(),
113: false);
114:
115: _setOutputDir.invoke(compiler, new Object[] { dest });
116:
117: ArrayList<String> argList = new ArrayList<String>();
118: for (int i = 0; i < paths.length; i++) {
119: Path javaPath = _compiler.getSourceDir().lookup(
120: paths[i]);
121: argList.add(javaPath.getNativePath());
122: }
123:
124: String[] files = new String[argList.size()];
125:
126: argList.toArray(files);
127:
128: _compile.invoke(compiler, new Object[] { files });
129: } catch (Exception e) {
130: e.printStackTrace();
131: throw new IOExceptionWrapper(e);
132: }
133: }
134:
135: /**
136: * Converts any relative classpath references to the full path.
137: */
138: String normalizeClassPath(String classPath, boolean generateRelative) {
139: char sep = CauchoSystem.getPathSeparatorChar();
140: int head = 0;
141: int tail = 0;
142:
143: CharBuffer cb = CharBuffer.allocate();
144:
145: while (head < classPath.length()) {
146: tail = classPath.indexOf(sep, head);
147: if (tail < 0)
148: tail = classPath.length();
149:
150: if (tail > head) {
151: String segment = classPath.substring(head, tail);
152:
153: if (cb.length() != 0)
154: cb.append(sep);
155:
156: cb.append(normalizePath(segment, generateRelative));
157: }
158:
159: head = tail + 1;
160: }
161:
162: return cb.close();
163: }
164:
165: /**
166: * Normalizes a path.
167: */
168: String normalizePath(String segment, boolean generateRelative) {
169: if (_userPrefix == null) {
170: Path userPath = Vfs.lookup(CauchoSystem.getUserDir());
171: char sep = CauchoSystem.getFileSeparatorChar();
172: _userPrefix = userPath.getNativePath();
173:
174: if (_userPrefix.length() == 0
175: || _userPrefix.charAt(_userPrefix.length() - 1) != sep) {
176: _userPrefix = _userPrefix + sep;
177: }
178: }
179:
180: Path path = Vfs.lookup(segment);
181: String nativePath = path.getNativePath();
182:
183: if (!generateRelative)
184: return nativePath;
185:
186: if (nativePath.startsWith(_userPrefix))
187: nativePath = nativePath.substring(_userPrefix.length());
188:
189: if (nativePath.equals(""))
190: return ".";
191: else
192: return nativePath;
193: }
194:
195: public static class CompilerThread implements Runnable {
196: private volatile boolean _isDone;
197:
198: public void run() {
199: try {
200: } finally {
201: _isDone = true;
202:
203: synchronized (this) {
204: notifyAll();
205: }
206: }
207: }
208: }
209: }
|