/** represents a file or directory which is scanned for updates */
public class Loader extends Watcher {
- ClassLoader parentClassLoader = null;
- static final ClassLoader mycl = Loader.class.getClassLoader();
+ public /*synchronized*/ void scan() throws IOException { super.scan(); }
- protected ThreadGroup tg = new ThreadGroup(getAbsolutePath());
-
- public Loader(String path) { super(path); }
- //public Loader(String path, ClassLoader pcl) { super(path); this.parentClassLoader = pcl; }
-
- private TreeClassLoader classloader = null;
- public synchronized void scan() throws IOException { super.scan(); }
- public synchronized ClassLoader getClassLoader() {
- ClassLoader classloader = this.classloader;
- if (classloader == null) {
- String s = getClassPath();
- StringTokenizer st = new StringTokenizer(s, File.pathSeparatorChar+"");
- URL[] urls = new URL[st.countTokens()];
- try {
- for(int i=0; i<urls.length; i++) {
- String us = st.nextToken();
- //if (us.endsWith(".jar")) us = "jar:file:"+us+"!/";
- if (us.endsWith(".jar")) us = "file:"+us;
- else us = "file:"+us+"/";
- urls[i] = new URL(us);
- }
- } catch (MalformedURLException e) {
- Log.error(this, e);
- return null;
- }
- classloader = this.classloader = new TreeClassLoader(urls/*, parentClassLoader*/);
- try { compileSource(); } catch (Exception e) { Log.error(this, e); }
- }
- return classloader;
- }
+ private final TreeClassLoader classloader;
+ public synchronized ClassLoader getClassLoader() { return classloader; }
+ public Loader(String path) { this(path, Loader.class.getClassLoader()); }
+ public Loader(String path, ClassLoader parent) { super(path); classloader = new TreeClassLoader(this, parent); }
+ public void changed(Watched w) { /*FIXME*/ }
private void fill(Vec vec, File dir) {
if (!dir.exists()) return;
}
}
-
- public String getClassPath() {
- String classpath = System.getProperty("java.class.path");
- String [] l = new File(Root.root + "/LIB/").list();
- for(int i=0; l != null && i<l.length; i++) {
- if (!l[i].endsWith(".jar")) continue;
- classpath += File.pathSeparatorChar;
- classpath += Root.root + "/LIB/" + l[i];
- }
- l = new File(this.path + File.separatorChar + "LIB").list();
- for(int i=0; l!=null && i<l.length; i++) {
- if (!l[i].endsWith(".jar")) continue;
- classpath += File.pathSeparatorChar;
- classpath += this.path + "/LIB/" + l[i];
- }
- return classpath + File.pathSeparatorChar + this.path + "/BIN";
- }
-
- private void compileSource() throws Exception {
- File srcdir = new File(this.path + File.separatorChar + "SRC");
- if (!srcdir.exists()) return;
- /*
- if (new File("/usr/bin/jikes").exists()) {
- File bindir = new File(this.path + File.separatorChar + "BIN"); bindir.mkdirs();
- String bootclasspath = System.getProperty("sun.boot.class.path", "");
- Vec args = new Vec();
- args.addElement("/usr/bin/jikes");
- args.addElement("+E");
- args.addElement("-nowarn");
- args.addElement("-bootclasspath");
- args.addElement(bootclasspath);
- args.addElement("-classpath");
- args.addElement(getClassPath());
- args.addElement("-sourcepath");
- args.addElement(srcdir.getAbsolutePath());
- args.addElement("-d");
- args.addElement(bindir.getAbsolutePath());
- fill(args, srcdir);
- String[] all = new String[args.size()];
- args.copyInto(all);
- Log.info(this, "invoking jikes");
- for(int i=0; i<all.length; i++) Log.info(this, " " + all[i]);
- final Process jikes = Runtime.getRuntime().exec(all);
- final BufferedReader out = new BufferedReader(new InputStreamReader(jikes.getInputStream()));
- final BufferedReader err = new BufferedReader(new InputStreamReader(jikes.getErrorStream()));
- new Thread() { public void run() {
- try { for(String s = out.readLine(); s != null; s = out.readLine()) Log.info("jikes[stdout]", s); }
- catch (Exception e) { Log.warn("jikes", e); } } }.start();
- new Thread() { public void run() {
- try { for(String s = err.readLine(); s != null; s = err.readLine()) Log.info("jikes[stderr]", s); }
- catch (Exception e) { Log.warn("jikes", e); } } }.start();
- jikes.waitFor();
- } else {
- Log.error(this, "ACK! jikes not found, javac not (yet) supported");
- }
- */
-
- File bindir = new File(this.path + File.separatorChar + "BIN"); bindir.mkdirs();
- Vec args = new Vec();
- args.addElement("/usr/bin/javac");
- args.addElement("-nowarn");
- args.addElement("-classpath");
- args.addElement(getClassPath());
- args.addElement("-sourcepath");
- args.addElement(srcdir.getAbsolutePath());
- args.addElement("-d");
- args.addElement(bindir.getAbsolutePath());
- fill(args, srcdir);
- String[] all = new String[args.size()];
- args.copyInto(all);
- Log.info(this, "invoking javac for " + srcdir.getAbsolutePath());
- final Process javac = Runtime.getRuntime().exec(all, new String[] { "PATH=/bin:/usr/bin" });
- final BufferedReader out = new BufferedReader(new InputStreamReader(javac.getInputStream()));
- final BufferedReader err = new BufferedReader(new InputStreamReader(javac.getErrorStream()));
- new Thread() { public void run() {
- try { for(String s = out.readLine(); s != null; s = out.readLine()) Log.info("javac [stdout]", s); }
- catch (Exception e) { Log.warn("javac", e); } } }.start();
- new Thread() { public void run() {
- try { for(String s = err.readLine(); s != null; s = err.readLine()) Log.info("javac [stderr]", s); }
- catch (Exception e) { Log.warn("javac", e); } } }.start();
- javac.waitFor();
-
- }
-
- // only watch SRC and LIB for changes
- public Watched slash(String path) {
- return (path.equals("LIB") ||
- path.equals("BIN") ||
- path.equals("SRC") ||
- path.endsWith(".jar") ) ? super.slash(path) : null; }
-
-
+ protected ThreadGroup tg = new ThreadGroup(getAbsolutePath());
private void nuke() {
if (tg.activeCount() == 0) return;
Log.info(this, "killing all threads for: " + path);
}
}
- // dump the classloader if anything changes
- public void changed(Watched w) {
- if (w.path.indexOf("BIN") != -1) return;
- if (classloader != null) {
- for(int i=0; i<3; i++) nuke();
- tg = new ThreadGroup(getAbsolutePath());
- Log.info(this, "scheduling classes for reload due to change in: " + w.path);
- classloader = null;
- }
- }
-
- public class TreeClassLoader extends java.net.URLClassLoader {
-
- public String getClassPath() { return Loader.this.getClassPath(); }
-
- public TreeClassLoader(java.net.URL[] urls) { super(urls); }
- //private Hashtable cache = new Hashtable();
- /*
- public InputStream getResourceAsStream(String name) { return getInputStream(name); }
-
- private synchronized Class defineClass(String name) {
- try {
- InputStream is = null;
- byte[] b = null;
- try {
- is = getInputStream(name.replace('.', File.separatorChar) + ".class");
- if (is == null) return null;
- b = InputStreamToByteArray.convert(is);
- } finally { if (is != null) is.close(); }
- return defineClass(b, 0, b.length);
- } catch (Exception e) {
- Log.error(this, e);
- return null;
- }
- }
-
- private InputStream getInputStream(String name) {
- // first see if it's just sitting there
- File classFile = slash("BIN").slash(name);
- if (classFile.exists()) try {
- return new FileInputStream(classFile);
- } catch (Exception e) { Log.warn(this, e); }
-
- // then scan the jarfiles for it
- File lib = slash("LIB");
- if (lib.exists() && lib.isDirectory()) try {
- boolean first = true;
- while(true) {
- String[] paths = first ? lib.list() : list();
- for(int i=0; i<paths.length; i++) {
- if (paths[i].endsWith(".jar")) {
- File f = new File(getAbsolutePath()+File.separatorChar+"LIB"+File.separatorChar+paths[i]);
- Log.debug(this, " scanning " + f.getAbsolutePath() + " for " + name);
- ZipFile zf = new ZipFile(f);
- ZipEntry ze = zf.getEntry(name);
- if (ze != null) return zf.getInputStream(ze);
- zf.close();
- }
- }
- if (!first) break;
- first = false;
- }
- } catch (Exception e) { Log.warn(this, e); }
-
- // finally, resort to compiling it if we have to
- //File src = new File(getAbsolutePath() + File.separatorChar + "SRC");
- // FIXME
- //if (!sourcebuilt) buildSource();
- return null;
- }
-
- public synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
- Class c = (Class)cache.get(name);
- if (c == null) try { c = findSystemClass(name); } catch (ClassNotFoundException cfe) { }
- if (c == null) try { c = Class.forName(name); } catch (ClassNotFoundException cfe) { }
- if (c == null) c = defineClass(name);
- if (c == null) throw new ClassNotFoundException();
- cache.put(name, c);
- if (resolve) resolveClass(c);
- return c;
- }
- */
- }
+
}