licensing update to APSL 2.0
[org.ibex.jinetd.git] / src / org / ibex / jinetd / Loader.java
index 5c933c7..012a935 100644 (file)
@@ -1,25 +1,49 @@
+// Copyright 2000-2005 the Contributors, as shown in the revision logs.
+// Licensed under the Apache Public Source License 2.0 ("the License").
+// You may not use this file except in compliance with the License.
+
 package org.ibex.jinetd;
+import org.ibex.io.*;
 import org.ibex.util.*;
 import java.io.*;
 import java.util.*;
+import java.text.*;
+import java.net.*;
 import java.util.zip.*;
 
 /** 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();
+
+    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 = new TreeClassLoader();
-    public ClassLoader getClassLoader() {
+    private TreeClassLoader classloader = null;
+    public synchronized void scan() throws IOException { super.scan(); }
+    public synchronized ClassLoader getClassLoader() {
         ClassLoader classloader = this.classloader;
         if (classloader == null) {
-            classloader = this.classloader = new TreeClassLoader();
-            Log.warn(this, "getting classloader...");
+            String s = getClassPath();
+            StringTokenizer st = new StringTokenizer(s, File.pathSeparatorChar+"");
+            URL[] urls = new URL[st.countTokens()];
             try {
-                compileSource();
-            } catch (Exception e) {
+                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;
     }
@@ -35,19 +59,31 @@ public class Loader extends Watcher {
                 fill(vec, new File(dir.getAbsolutePath() + File.separatorChar + list[i]));
         }
     }
+
+
+    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();
-            File libdir = new File(this.path + File.separatorChar + "LIB");
-            String classpath = System.getProperty("java.class.path");
-            String [] l = new File("/jinetd/LIB/").list();
-            for(int i=0; i<l.length; i++) {
-                if (!l[i].endsWith(".jar")) continue;
-                classpath += File.pathSeparatorChar;
-                classpath += "/jinetd/LIB/" + l[i];
-            }
             String bootclasspath = System.getProperty("sun.boot.class.path", "");
             Vec args = new Vec();
             args.addElement("/usr/bin/jikes");
@@ -55,10 +91,8 @@ public class Loader extends Watcher {
             args.addElement("-nowarn");
             args.addElement("-bootclasspath");
             args.addElement(bootclasspath);
-            args.addElement("-extdirs");
-            args.addElement(libdir.getAbsolutePath());
             args.addElement("-classpath");
-            args.addElement(classpath);
+            args.addElement(getClassPath());
             args.addElement("-sourcepath");
             args.addElement(srcdir.getAbsolutePath());
             args.addElement("-d");
@@ -81,6 +115,33 @@ public class Loader extends Watcher {
         } 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
@@ -91,81 +152,95 @@ public class Loader extends Watcher {
                 path.endsWith(".jar") ) ? super.slash(path) : null; }
 
 
+    private void nuke() {
+        if (tg.activeCount() == 0) return;
+        Log.info(this, "killing all threads for: " + path);
+        Log.info(this, "   thread count before interrupt: " + tg.activeCount());
+        tg.interrupt();
+        try { Thread.sleep(3000); } catch (Exception e) { Log.error(this, e); }
+        Log.info(this, "   thread count before kill: " + tg.activeCount());
+        Thread[] all = new Thread[tg.activeCount()];
+        tg.enumerate(all, true);
+        for(int i=0; i<all.length; i++) Stream.kill(all[i]);
+        try { Thread.sleep(3000); } catch (Exception e) { Log.error(this, e); }
+        Log.info(this, "   thread count after kill: " + tg.activeCount());
+        if (tg.activeCount() > 0) {
+            Log.warn(this, "    annoying threads:");
+            Thread[] annoying = new Thread[tg.activeCount()];
+            tg.enumerate(annoying, true);
+            for(int i=0; i<annoying.length; i++) {
+                Log.warn(this, "      " + annoying[i]);
+                StackTraceElement[] stack = annoying[i].getStackTrace();
+                for(int j=0; j<stack.length; j++) Log.warn(this, "        " + stack[j]);
+                Log.warn(this, " ");
+            }
+        }
+    }
+
     // dump the classloader if anything changes
     public void changed(Watched w) {
         if (w.path.indexOf("BIN") != -1) return;
         if (classloader != null) {
-            Log.info(this, "Reloading all classes: " + path);
+            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;
         }
     }
     
-    private class TreeClassLoader extends ClassLoader {
-        private Hashtable cache = new Hashtable();
+    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) {
-            // first see if it's just sitting there
-            File classFile = slash("BIN").slash(name.replace('.', File.separatorChar) + ".class");
-            if (classFile.exists()) {
+            try {
+                InputStream is = null;
+                byte[] b = null;
                 try {
-                    FileInputStream fis = new FileInputStream(classFile);
-                    byte[] b = InputStreamToByteArray.convert(fis);
-                    fis.close();
-                    Log.debug(this, "  loading " + name + " from " + classFile.getAbsolutePath());
-                    return defineClass(b, 0, b.length);
-                } catch (Exception e) {
-                    Log.warn(this, e);
-                }
+                    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 {
-                    String[] paths = lib.list();
+            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());
+                            Log.debug(this, "  scanning " + f.getAbsolutePath() + " for " + name);
                             ZipFile zf = new ZipFile(f);
-                            ZipEntry ze = zf.getEntry(name.replace('.', File.separatorChar) + ".class");
-                            if (ze != null) {
-                                byte[] b = InputStreamToByteArray.convert(zf.getInputStream(ze));
-                                Log.debug(this, "    loading " + name + " from " + f.getAbsolutePath());
-                                zf.close();
-                                return defineClass(b, 0, b.length);
-                            }
+                            ZipEntry ze = zf.getEntry(name);
+                            if (ze != null) return zf.getInputStream(ze);
                             zf.close();
                         }
                     }
-                } catch (Exception e) {
-                    Log.warn(this, e);
+                    if (!first) break;
+                    first = false;
                 }
+            } catch (Exception e) { Log.warn(this, e); }
 
-            // finally scan ourselves
-                try {
-                    String[] paths = list();
-                    for(int i=0; i<paths.length; i++) {
-                        if (paths[i].endsWith(".jar")) {
-                            File f = new File(getAbsolutePath()+File.separatorChar+paths[i]);
-                            //Log.debug(this, "  scanning " + f.getAbsolutePath());
-                            ZipFile zf = new ZipFile(f);
-                            ZipEntry ze = zf.getEntry(name.replace('.', File.separatorChar) + ".class");
-                            if (ze != null) {
-                                byte[] b = InputStreamToByteArray.convert(zf.getInputStream(ze));
-                                Log.debug(this, "    loading " + name + " from " + f.getAbsolutePath());
-                                zf.close();
-                                return defineClass(b, 0, b.length);
-                            }
-                            zf.close();
-                        }
-                    }
-                } 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
@@ -174,19 +249,15 @@ public class Loader extends Watcher {
         }
 
         public synchronized Class loadClass(String name, boolean resolve) throws ClassNotFoundException {
-            try {
-                Class c = findSystemClass(name);
-                if (c != null) { if (resolve) resolveClass(c); return c; }
-            } catch (ClassNotFoundException e) { /* DELIBERATE */ }
             Class c = (Class)cache.get(name);
-            if (c == null) {
-                //Log.info(this, "looking for class " + name);
-                c = defineClass(name);
-                if (c == null) throw new ClassNotFoundException();
-                cache.put(name, c);
-            }
+            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;
         }
+        */
     }
 }