1 package com.ca.commons.cbutil; 2 3 import java.io.InputStream ; 4 import java.net.MalformedURLException ; 5 import java.net.URL ; 6 import java.util.Hashtable ; 7 import java.util.logging.Logger ; 8 import java.util.zip.ZipException ; 9 10 19 20 public class CBClassLoader extends ClassLoader 21 { 22 23 26 27 protected Hashtable classes = new Hashtable (); 28 protected Hashtable lowerCaseClasses = new Hashtable (); 29 30 33 34 protected CBResourceLoader resourceLoader; 35 36 37 private static Logger log = Logger.getLogger(CBClassLoader.class.getName()); 38 39 43 44 public CBClassLoader(CBResourceLoader loader) 45 { 46 log.fine("Started CBClassLoader"); 47 48 resourceLoader = loader; 49 } 50 51 55 56 protected String translateClassName(String name) 57 { 58 if (name.endsWith(".class")) 59 name = name.replace('.', '/'); 60 else 61 name = name.replace('.', '/') + ".class"; 62 63 log.finer("looking for class: " + name); 64 65 return name; 66 } 67 68 72 73 private byte[] getClassFromResourceFiles(String className) 74 throws ZipException 75 { 76 className = translateClassName(className); 77 return resourceLoader.getResource(className); 78 } 79 80 85 public Class findClass(String className) throws ClassNotFoundException 86 { 87 return (findClass(className, true)); 88 } 89 90 void addClass(Class c) 91 { 92 log.finer("adding class " + c.toString()); 93 } 95 96 101 102 public synchronized Class findClass(String className, boolean resolveIt) 103 throws ClassNotFoundException 104 { 105 Class result; 106 byte classData[]; 107 108 log.finer(" >>>>>> Load class : " + className); 109 110 111 Object local = classes.get(className); 112 if (local != null) 113 { 114 if (local instanceof String && "".equals((String ) local)) 115 { 116 log.finer(" >>>>>> ignoring '" + className + "' (failed to load previously)."); 117 throw new ClassNotFoundException ("ignoring class '" + className + "' (failed to load previously)."); 118 } 119 log.finer(" >>>>>> returning cached result."); 120 return (Class ) local; 121 } 122 123 124 try 125 { 126 result = super.findSystemClass(className); 127 log.finer(" >>>>>> returning system class (in CLASSPATH)."); 128 return result; 129 } 130 catch (ClassNotFoundException e) 131 { 132 log.finer(" >>>>>> Not a system class - looking in zip files."); 133 } 134 135 136 try 137 { 138 classData = getClassFromResourceFiles(className); 139 } 140 catch (ZipException e) 141 { 142 classes.put(className, ""); lowerCaseClasses.put(className.toLowerCase(), ""); 144 throw new ClassNotFoundException ("Error getting className: '" + className + "' : " + e); 145 } 146 147 if (classData == null) 148 { 149 classes.put(className, ""); lowerCaseClasses.put(className.toLowerCase(), ""); 151 throw new ClassNotFoundException (); 152 } 153 154 155 result = defineClass(className, classData, 0, classData.length); 156 if (result == null) 157 { 158 classes.put(className, ""); lowerCaseClasses.put(className.toLowerCase(), ""); 160 throw new ClassFormatError (); 161 } 162 163 if (resolveIt) 164 { 165 resolveClass(result); 166 } 167 168 classes.put(className, result); 169 lowerCaseClasses.put(className.toLowerCase(), result); 170 log.finer(" >>>>>> Returning newly loaded zipped class. " + className); 171 return result; 172 } 173 174 public URL getResource(String name) 175 { 176 URL bloop = super.getResource(name); 177 return bloop; 178 } 179 180 188 189 protected URL findResource(String name) 190 { 191 log.finer("CLASSLOADER MAGIC: looking for: " + name); 192 CBJarResource container = resourceLoader.getJarContainingResource(name); 193 log.finer("CLASSLOADER MAGIC: found container: " + ((container == null) ? "null" : container.getZipFileName())); 194 if (container == null) 195 return null; 196 197 String zipFile = container.getZipFileName(); 198 String url = "jar:file:" + zipFile + "!/" + name; 199 log.finer("CLASSLOADER MAGIC: constructed url: " + url); 200 201 try 202 { 203 return new URL (url); 204 } 205 catch (MalformedURLException e) 206 { 207 log.warning("Unable to construct url: " + url + "\n -> due to " + e); 208 return null; 209 } 210 } 211 212 public String toString() 213 { 214 return "CBClassLoader"; 215 } 216 217 public InputStream getResourceAsStream(String name) 218 { 219 return super.getResourceAsStream(name); 220 } 221 222 } | Popular Tags |