KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > loader > CompilingClassEntry


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.loader;
31
32 import com.caucho.java.CompileClassNotFound;
33 import com.caucho.log.Log;
34 import com.caucho.server.util.CauchoSystem;
35 import com.caucho.util.L10N;
36 import com.caucho.util.ThreadPool;
37 import com.caucho.vfs.Path;
38
39 import java.io.IOException JavaDoc;
40 import java.security.CodeSource JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 class CompilingClassEntry extends ClassEntry {
44   private static final L10N L = new L10N(CompilingClassEntry.class);
45   private static final Logger JavaDoc log = Log.open(CompilingClassEntry.class);
46   
47   private CompilingLoader _loader;
48   private boolean _compileIsModified;
49     
50   public CompilingClassEntry(CompilingLoader compilingLoader,
51                  DynamicClassLoader loader,
52                  String JavaDoc name, Path sourcePath,
53                  Path classPath,
54                  CodeSource JavaDoc codeSource)
55   {
56     super(loader, name, sourcePath, classPath, codeSource);
57
58     _loader = compilingLoader;
59   }
60
61   public void preLoad()
62     throws ClassNotFoundException JavaDoc
63   {
64     String JavaDoc javaName = getName().replace('.', '/') + ".java";
65     
66     Path javaFile = getSourcePath();
67     Path classFile = getClassPath();
68     
69     if (javaFile.getLastModified() <= classFile.getLastModified()) {
70       log.fine(L.l("loading pre-compiled class {0} from {1}",
71            getName(), classFile));
72       return;
73     }
74
75     if (! javaFile.canRead())
76       return;
77     
78     try {
79       classFile.remove();
80     } catch (IOException JavaDoc e) {
81     }
82       
83     String JavaDoc sourcePath = _loader.prefixClassPath(_loader.getLoader().getSourcePath());
84
85     // deal with windows case nuttiness
86
if (CauchoSystem.isWindows() &&
87     ! _loader.checkSource(_loader.getSource(), javaName))
88       return;
89
90     _loader.compileClass(javaFile, classFile, sourcePath, false);
91
92     if (classFile.canRead()) {
93       log.fine(L.l("loading compiled class {0} from {1}",
94            getName(), classFile));
95     }
96     else if (javaFile.exists())
97       throw new CompileClassNotFound(L.l("compiling {0} didn't produce a {1}. class",
98                      javaFile, getName()));
99
100     setDependPath(classFile);
101   }
102
103   /**
104    * Returns true if the compile doesn't avoid the dependency.
105    */

106   public boolean compileIsModified()
107   {
108     if (_compileIsModified)
109       return true;
110
111     CompileThread compileThread = new CompileThread();
112     ThreadPool.getThreadPool().start(compileThread);
113
114     try {
115       synchronized (compileThread) {
116     if (! compileThread.isDone())
117       compileThread.wait(5000);
118       }
119
120       if (_compileIsModified)
121     return true;
122       else if (compileThread.isDone()) {
123     setDependPath(getClassPath());
124     
125     return reloadIsModified();
126       }
127       else
128     return true;
129     } catch (Throwable JavaDoc e) {
130     }
131
132     return false;
133   }
134
135   class CompileThread implements Runnable JavaDoc {
136     private volatile boolean _isDone;
137
138     public boolean isDone()
139     {
140       return _isDone;
141     }
142     public void run() {
143       Path sourcePath = getSourcePath();
144       
145       long length = sourcePath.getLength();
146       long lastModified = sourcePath.getLastModified();
147
148       try {
149     _loader.compileClass(getSourcePath(), getClassPath(),
150                  getSourcePath().getPath(), false);
151
152     setSourceLength(length);
153     setSourceLastModified(lastModified);
154       } catch (Throwable JavaDoc e) {
155     _compileIsModified = true;
156       }
157
158       _isDone = true;
159
160       synchronized (this) {
161     notifyAll();
162       }
163     }
164   }
165
166   public String JavaDoc toString()
167   {
168     return "CompilingClassEntry[" + getClassPath() + ", SRC=" + getSourcePath() + "]";
169   }
170
171 }
172
Popular Tags