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.util.L10N;
033:
034: import java.io.IOException;
035: import java.util.logging.Logger;
036:
037: /**
038: * Compiles Java source, returning the loaded class.
039: */
040: abstract public class AbstractJavaCompiler implements Runnable {
041: protected static final L10N L = new L10N(AbstractJavaCompiler.class);
042: protected static final Logger log = Log
043: .open(AbstractJavaCompiler.class);
044:
045: private ClassLoader _loader;
046:
047: protected JavaCompiler _compiler;
048: private volatile boolean _isDone;
049:
050: // path of source files
051: private String[] _path;
052: private LineMap _lineMap;
053:
054: private Throwable _exception;
055:
056: public AbstractJavaCompiler(JavaCompiler compiler) {
057: _loader = Thread.currentThread().getContextClassLoader();
058:
059: _compiler = compiler;
060: }
061:
062: /**
063: * Sets the path of files to compile.
064: */
065: public void setPath(String[] path) {
066: _path = path;
067: }
068:
069: /**
070: * Sets the LineMap for the file
071: */
072: public void setLineMap(LineMap lineMap) {
073: _lineMap = lineMap;
074: }
075:
076: /**
077: * Returns any compile exception.
078: */
079: public Throwable getException() {
080: return _exception;
081: }
082:
083: /**
084: * Returns true when the compilation is done.
085: */
086: public boolean isDone() {
087: return _isDone;
088: }
089:
090: /**
091: * runs the compiler.
092: */
093: public void run() {
094: try {
095: Thread.currentThread().setContextClassLoader(_loader);
096:
097: compileInt(_path, _lineMap);
098: } catch (Throwable e) {
099: _exception = e;
100: } finally {
101: Thread.currentThread().setContextClassLoader(null);
102:
103: synchronized (this ) {
104: _isDone = true;
105:
106: notifyAll();
107: }
108: }
109: }
110:
111: /**
112: * Quit the compilation.
113: */
114: public void abort() {
115: }
116:
117: /**
118: * Compile the configured file.
119: *
120: * @param path the path to the java source.
121: * @param lineMap mapping from the generated source to the original files.
122: */
123: abstract protected void compileInt(String[] path, LineMap lineMap)
124: throws IOException;
125: }
|