1 23 24 package com.sun.appserv.server.util; 25 26 import java.util.ArrayList ; 27 import java.util.Enumeration ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.io.IOException ; 31 import java.net.URL ; 32 import java.security.SecureClassLoader ; 33 34 42 public class ClassLoaderChain extends SecureClassLoader { 43 private List <ClassLoader > classLoaderList = new ArrayList <ClassLoader >(); 44 private ClassLoader parentCL = null; 45 private String nameOfCL; 46 47 public ClassLoaderChain(){ 48 super(); 49 } 50 51 public ClassLoaderChain(ClassLoader parent){ 52 super(parent); 53 this.parentCL = parent; 54 } 55 56 public void setName(String n) { 57 this.nameOfCL = n; 58 } 59 60 public String getName() { 61 return this.nameOfCL; 62 } 63 64 65 public void addToList(ClassLoader cl) { 66 this.classLoaderList.add(cl); 67 } 68 69 protected Class <?> findClass(String name) throws ClassNotFoundException { 70 throw new ClassNotFoundException (name); 71 } 72 73 84 protected Class loadClass(String name, ClassLoader toSkip) 85 throws ClassNotFoundException { 86 for (Iterator <ClassLoader > iter = classLoaderList.iterator(); 88 iter.hasNext();) { 89 ClassLoader element = iter.next(); 90 91 if(element.equals(toSkip)) { 92 continue; 93 } 94 95 Class clz = null; 96 try { 97 clz = element.loadClass(name); 99 if (clz != null) { 100 return clz; 101 } 102 } catch (ClassNotFoundException e) { 103 } 106 } 107 throw new ClassNotFoundException (name); 109 } 110 111 112 117 @Override 118 public Class <?> loadClass(String className,boolean resolve) 119 throws ClassNotFoundException { 120 Class c = findLoadedClass(className); 122 if( c != null ) { 123 return c; 124 } 125 if( parentCL instanceof ClassLoaderChain ) { 127 throw new RuntimeException ("ClassLoader " + this.toString() 129 + " parent is a chain " + parentCL.toString() ); 130 } 131 if (parentCL != null) { 132 try { 133 c = parentCL.loadClass(className); 135 if (c != null) { 136 if (resolve) { 137 resolveClass(c); 138 } 139 } 140 } catch(ClassNotFoundException e) { 141 } 143 if(c != null) { 144 return c; 146 } 147 } 148 149 for (ClassLoader element : classLoaderList) { 151 if(element == null) 152 continue; 153 154 Class clz = null; 155 try { 156 clz = element.loadClass(className); 157 if (clz != null) { 158 if(resolve) { 159 resolveClass(clz); 160 } 161 return clz; 162 } 163 } catch (ClassNotFoundException e) { 165 } 167 } 168 throw new ClassNotFoundException (className); 170 } 171 172 @Override 173 protected URL findResource(String name) { 174 for(ClassLoader cl:classLoaderList) { 175 URL res = cl.getResource(name); 176 if (res != null) return res; 177 } 178 return null; 179 } 180 181 @Override 182 protected Enumeration <URL > findResources(String name) throws IOException { 183 for(ClassLoader cl:classLoaderList) { 184 Enumeration <URL > res = cl.getResources(name); 185 List <URL > al = new ArrayList <URL >(); 186 while(res.hasMoreElements()) { 187 al.add(res.nextElement()); 188 } 189 if (al.size() != 0) { 190 return ((new java.util.Vector <URL >(al)).elements()); 191 } 192 } 193 return ((new java.util.Vector <URL >()).elements()); 194 } 195 196 @Override 197 public String toString() { 198 String s = this.nameOfCL + " parentCL :: " + this.parentCL + 199 " constituent CLs :: \n"; 200 for(ClassLoader cl:this.classLoaderList) { 201 String nameofCL1 = null; 202 if (cl instanceof ASURLClassLoader) { 203 nameofCL1 = ((ASURLClassLoader)cl).getName(); 205 } else if (cl instanceof ClassLoaderChain) { 206 nameofCL1 = ((ClassLoaderChain)cl).getName(); 207 } 208 s += " :: " + nameofCL1; 209 } 210 s += "\n"; 211 return s; 212 } 213 } 214 215 216 | Popular Tags |