arranged for per-host classloaders
[org.ibex.jinetd.git] / src / org / ibex / jinetd / TreeClassLoader.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.jinetd;
6 import org.ibex.io.*;
7 import org.ibex.util.*;
8 import java.io.*;
9 import java.util.*;
10 import java.text.*;
11 import java.net.*;
12 import java.util.zip.*;
13
14 public class TreeClassLoader extends URLClassLoader {
15
16     private final File root;
17     private final File lib;
18     private Hashtable cache = new Hashtable();
19
20     public TreeClassLoader(File root, ClassLoader parent) {
21         super(new URL[] { }, parent);
22         this.root = root;
23         this.lib = new File(root.getAbsolutePath() + File.separatorChar + "lib");
24     }
25
26     public Enumeration getLoadedClassNames() { return cache.keys(); }
27
28     // Classloading //////////////////////////////////////////////////////////////////////////////
29
30     public URL[] getURLs() {
31         try {
32             Vec v = new Vec();
33             if (getParent() != null && getParent() instanceof URLClassLoader) {
34                 URL[] parentUrls = ((URLClassLoader)getParent()).getURLs();
35                 for(int i=0; i<parentUrls.length; i++) v.addElement(parentUrls[i]);
36             }
37             String vmClasspath =
38                 System.getProperty("java.class.path") +
39                 File.pathSeparatorChar +
40                 System.getProperty("sun.boot.class.path");
41             StringTokenizer st = new StringTokenizer(vmClasspath, File.pathSeparatorChar+"");
42             while(st.hasMoreTokens()) v.addElement(new URL("file:" + st.nextToken()));
43             v.addElement(new URL("file:" + root.getAbsolutePath()));
44             v.addElement(new URL("file:" + root.getAbsolutePath() + File.separatorChar + "classes"));
45             for(Enumeration e = enumerateJarFiles(); e.hasMoreElements(); )
46                 v.addElement(new URL("file:" + ((File)e.nextElement()).getAbsolutePath()));
47             return (URL[])v.copyInto(new URL[v.size()]);
48         } catch (MalformedURLException e) {
49             Log.error(this, e);
50             return null;
51         }
52     }
53
54     private synchronized Class defineClass(String name) throws ClassNotFoundException {
55         InputStream is = null;
56         try {
57             is = getClassInputStream(name);
58             if (is == null) return null;
59             byte[] b = InputStreamToByteArray.convert(is);
60             Class ret = defineClass(b, 0, b.length);
61             cache.put(name, ret);
62             return ret;
63         } catch (Exception e) {
64             Log.error(this, e);
65             throw new ClassNotFoundException();
66         } finally {
67             if (is != null) try { is.close(); } catch (Exception e) { Log.error(this, e); }
68         }
69     }
70
71     public synchronized Class findClass(String name) throws ClassNotFoundException {
72         Class c = (Class)cache.get(name);
73         if (c==null) c = defineClass(name);
74         if (c==null) throw new ClassNotFoundException(name);
75         return c;
76     }
77
78     // Filesystem Methods //////////////////////////////////////////////////////////////////////////////
79
80     private static final FilenameFilter jarFilter =
81         new FilenameFilter() { public boolean accept(File f, String s) {return s.endsWith(".jar");}};
82     public Enumeration enumerateJarFiles() {
83         Enumeration rootJars =
84             new Misc.ArrayEnumeration(root.list(jarFilter)) {
85                 public Object nextElement() { return new File(root.getAbsolutePath()+File.separatorChar+super.nextElement()); } };
86         Enumeration libJars =
87             new Misc.ArrayEnumeration(lib.list(jarFilter)) {
88                 public Object nextElement() { return new File(lib.getAbsolutePath()+File.separatorChar+super.nextElement()); } };
89         return new Misc.JoinEnumeration(rootJars, libJars);
90     }
91
92     public InputStream getResourceAsStream(String name) {
93         InputStream ret = getInputStream(name);
94         if (ret == null) ret = getParent().getResourceAsStream(name);
95         return ret;
96     }
97
98     public InputStream getClassInputStream(String classname) { return getInputStream(classname.replace('.', '/')+".class"); }
99     public InputStream getInputStream(String name) {
100         try {
101             File f = new File(root.getAbsolutePath() + File.separatorChar + name);
102             if (f.exists()) return new FileInputStream(f);
103         } catch (IOException e) { /* DELIBERATE */ }
104         try {
105             File f = new File("classes" + File.separatorChar + root.getAbsolutePath() + File.separatorChar + name);
106             if (f.exists()) return new FileInputStream(f);
107         } catch (IOException e) { /* DELIBERATE */ }
108         for(Enumeration e = enumerateJarFiles(); e.hasMoreElements();) try {
109             ZipFile zf = new ZipFile((File)e.nextElement());
110             ZipEntry ze = zf.getEntry(name);
111             if (ze != null) return zf.getInputStream(ze);
112             zf.close();
113         } catch (Exception ex) { Log.warn(this, ex); }
114         return null;
115     }
116
117     // Experimental //////////////////////////////////////////////////////////////////////////////
118
119     protected ThreadGroup tg = null;
120     private void nuke() {
121         if (tg.activeCount() == 0) return;
122         Log.info(this, "killing all threads for: " + root.getAbsolutePath());
123         Log.info(this, "   thread count before interrupt: " + tg.activeCount());
124         tg.interrupt();
125         try { Thread.sleep(3000); } catch (Exception e) { Log.error(this, e); }
126         Log.info(this, "   thread count before kill: " + tg.activeCount());
127         Thread[] all = new Thread[tg.activeCount()];
128         tg.enumerate(all, true);
129         for(int i=0; i<all.length; i++) Stream.kill(all[i]);
130         try { Thread.sleep(3000); } catch (Exception e) { Log.error(this, e); }
131         Log.info(this, "   thread count after kill: " + tg.activeCount());
132         if (tg.activeCount() > 0) {
133             Log.warn(this, "    annoying threads:");
134             Thread[] annoying = new Thread[tg.activeCount()];
135             tg.enumerate(annoying, true);
136             for(int i=0; i<annoying.length; i++) {
137                 Log.warn(this, "      " + annoying[i]);
138                 StackTraceElement[] stack = annoying[i].getStackTrace();
139                 for(int j=0; j<stack.length; j++) Log.warn(this, "        " + stack[j]);
140                 Log.warn(this, " ");
141             }
142         }
143     }
144
145 }