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: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.loader;
031:
032: import com.caucho.java.CompileClassNotFound;
033: import com.caucho.log.Log;
034: import com.caucho.server.util.CauchoSystem;
035: import com.caucho.util.L10N;
036: import com.caucho.util.ThreadPool;
037: import com.caucho.vfs.Path;
038:
039: import java.io.IOException;
040: import java.security.CodeSource;
041: import java.util.logging.Logger;
042: import java.util.logging.Level;
043:
044: class CompilingClassEntry extends ClassEntry {
045: private static final L10N L = new L10N(CompilingClassEntry.class);
046: private static final Logger log = Log
047: .open(CompilingClassEntry.class);
048:
049: private CompilingLoader _loader;
050: private boolean _compileIsModified;
051:
052: public CompilingClassEntry(CompilingLoader compilingLoader,
053: DynamicClassLoader loader, String name, Path sourcePath,
054: Path classPath, CodeSource codeSource) {
055: super (loader, name, sourcePath, classPath, codeSource);
056:
057: _loader = compilingLoader;
058: }
059:
060: public void preLoad() throws ClassNotFoundException {
061: String javaName = getName().replace('.', '/') + ".java";
062:
063: Path javaFile = getSourcePath();
064: Path classFile = getClassPath();
065:
066: if (javaFile.getLastModified() <= classFile.getLastModified()) {
067: log.finest(L.l("loading pre-compiled class {0} from {1}",
068: getName(), classFile));
069: return;
070: }
071:
072: if (!javaFile.canRead())
073: return;
074:
075: try {
076: classFile.remove();
077: } catch (IOException e) {
078: }
079:
080: String sourcePath = _loader.prefixClassPath(_loader.getLoader()
081: .getSourcePath());
082:
083: // deal with windows case nuttiness
084: if (CauchoSystem.isWindows()
085: && !_loader.checkSource(_loader.getSource(), javaName))
086: return;
087:
088: _loader.compileClass(javaFile, classFile, sourcePath, false);
089:
090: if (classFile.canRead()) {
091: log.fine(L.l("loading compiled class {0} from {1}",
092: getName(), classFile));
093: } else if (javaFile.exists())
094: throw new CompileClassNotFound(L.l(
095: "compiling {0} didn't produce a {1}. class",
096: javaFile, getName()));
097:
098: setDependPath(classFile);
099: }
100:
101: /**
102: * Returns true if the compile doesn't avoid the dependency.
103: */
104: public boolean compileIsModified() {
105: if (_compileIsModified)
106: return true;
107:
108: CompileThread compileThread = new CompileThread();
109: ThreadPool.getThreadPool().start(compileThread);
110:
111: try {
112: synchronized (compileThread) {
113: if (!compileThread.isDone())
114: compileThread.wait(5000);
115: }
116:
117: if (_compileIsModified)
118: return true;
119: else if (compileThread.isDone()) {
120: setDependPath(getClassPath());
121:
122: return reloadIsModified();
123: } else
124: return true;
125: } catch (Throwable e) {
126: }
127:
128: return false;
129: }
130:
131: class CompileThread implements Runnable {
132: private volatile boolean _isDone;
133:
134: public boolean isDone() {
135: return _isDone;
136: }
137:
138: public void run() {
139: Path sourcePath = getSourcePath();
140:
141: long length = sourcePath.getLength();
142: long lastModified = sourcePath.getLastModified();
143:
144: try {
145: _loader.compileClass(getSourcePath(), getClassPath(),
146: getSourcePath().getPath(), false);
147:
148: setSourceLength(length);
149: setSourceLastModified(lastModified);
150: } catch (Throwable e) {
151: log.log(Level.FINE, e.toString(), e);
152:
153: _compileIsModified = true;
154: }
155:
156: _isDone = true;
157:
158: synchronized (this ) {
159: notifyAll();
160: }
161: }
162: }
163:
164: public String toString() {
165: return "CompilingClassEntry[" + getClassPath() + ", src="
166: + getSourcePath() + "]";
167: }
168:
169: }
|