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.
5 package org.ibex.jinetd;
7 import org.ibex.util.*;
12 import java.util.zip.*;
14 public class TreeClassLoader extends URLClassLoader {
16 private final File root;
17 private final File lib;
18 private Hashtable cache = new Hashtable();
20 public TreeClassLoader(File root, ClassLoader parent) {
21 super(new URL[] { }, parent);
23 this.lib = new File(root.getAbsolutePath() + File.separatorChar + "lib");
26 public Enumeration getLoadedClassNames() { return cache.keys(); }
28 // Classloading //////////////////////////////////////////////////////////////////////////////
30 public URL[] getURLs() {
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]);
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) {
54 private synchronized Class defineClass(String name) throws ClassNotFoundException {
55 InputStream is = null;
57 is = getClassInputStream(name);
58 if (is == null) return null;
59 byte[] b = InputStreamToByteArray.convert(is);
60 Class ret = defineClass(b, 0, b.length);
63 } catch (Exception e) {
65 throw new ClassNotFoundException();
67 if (is != null) try { is.close(); } catch (Exception e) { Log.error(this, e); }
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);
78 // Filesystem Methods //////////////////////////////////////////////////////////////////////////////
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()); } };
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);
92 public InputStream getResourceAsStream(String name) {
93 InputStream ret = getInputStream(name);
94 if (ret == null) ret = getParent().getResourceAsStream(name);
98 public InputStream getClassInputStream(String classname) { return getInputStream(classname.replace('.', '/')+".class"); }
99 public InputStream getInputStream(String name) {
101 File f = new File(root.getAbsolutePath() + File.separatorChar + name);
102 if (f.exists()) return new FileInputStream(f);
103 } catch (IOException e) { /* DELIBERATE */ }
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);
113 } catch (Exception ex) { Log.warn(this, ex); }
117 // Experimental //////////////////////////////////////////////////////////////////////////////
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());
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]);