1 package org.jboss.cache.pojo.memory; 2 3 import org.apache.commons.logging.Log; 4 import org.apache.commons.logging.LogFactory; 5 6 import java.io.ByteArrayOutputStream ; 7 import java.io.FileNotFoundException ; 8 import java.io.IOException ; 9 import java.io.InputStream ; 10 import java.util.Map ; 11 12 34 public class SelectedClassnameClassLoader extends ClassLoader 35 { 36 37 private String [] includedClasses = null; 38 private String [] excludedClasses = null; 39 private String [] notFoundClasses = null; 40 private Log log = LogFactory.getLog(SelectedClassnameClassLoader.class); 41 42 private Map classes = new java.util.HashMap (); 43 44 67 public SelectedClassnameClassLoader(String [] includedClasses, 68 String [] excludedClasses, 69 ClassLoader parent) 70 { 71 super(parent); 72 this.includedClasses = includedClasses; 73 this.excludedClasses = excludedClasses; 74 } 75 76 101 public SelectedClassnameClassLoader(String [] includedClasses, 102 String [] excludedClasses, 103 String [] notFoundClasses, 104 ClassLoader parent) 105 { 106 super(parent); 107 this.includedClasses = includedClasses; 108 this.excludedClasses = excludedClasses; 109 this.notFoundClasses = notFoundClasses; 110 } 111 112 protected synchronized Class loadClass(String name, boolean resolve) 113 throws ClassNotFoundException 114 { 115 log.info("In SelectedClassnameClassLoader.loadClass(" + name + "," + resolve + ")"); 116 if (isIncluded(name) && (isExcluded(name) == false)) 117 { 118 Class c = findClass(name); 119 120 if (resolve) 121 { 122 resolveClass(c); 123 } 124 return c; 125 } else 126 { 127 return super.loadClass(name, resolve); 128 } 129 } 130 131 protected Class findClass(String name) throws ClassNotFoundException 132 { 133 134 log.info("In SelectedClassnameClassLoader.findClass()"); 135 Class result = (Class ) classes.get(name); 136 if (result != null) 137 { 138 return result; 139 } 140 141 if (isIncluded(name) && (isExcluded(name) == false)) 142 { 143 try 144 { 145 InputStream is = getResourceAsStream(name.replace('.', '/').concat(".class")); 146 byte[] bytes = new byte[1024]; 147 ByteArrayOutputStream baos = new ByteArrayOutputStream (1024); 148 int read; 149 while ((read = is.read(bytes)) > -1) 150 { 151 baos.write(bytes, 0, read); 152 } 153 bytes = baos.toByteArray(); 154 result = this.defineClass(name, bytes, 0, bytes.length); 155 } catch (FileNotFoundException e) 156 { 157 throw new ClassNotFoundException ("cannot find " + name, e); 158 } catch (IOException e) 159 { 160 throw new ClassNotFoundException ("cannot read " + name, e); 161 } 162 } else if (isNotFound(name)) 163 { 164 throw new ClassNotFoundException (name + " is discarded"); 165 } else 166 { 167 result = super.findClass(name); 168 } 169 170 classes.put(name, result); 171 172 return result; 173 } 174 175 private boolean isIncluded(String className) 176 { 177 178 if (includedClasses != null) 179 { 180 for (int i = 0; i < includedClasses.length; i++) 181 { 182 if (className.startsWith(includedClasses[i])) 183 { 184 return true; 185 } 186 } 187 } 188 189 return false; 190 } 191 192 private boolean isExcluded(String className) 193 { 194 195 if (excludedClasses != null) 196 { 197 for (int i = 0; i < excludedClasses.length; i++) 198 { 199 if (className.startsWith(excludedClasses[i])) 200 { 201 return true; 202 } 203 } 204 } 205 206 return false; 207 } 208 209 private boolean isNotFound(String className) 210 { 211 212 if (notFoundClasses != null) 213 { 214 for (int i = 0; i < notFoundClasses.length; i++) 215 { 216 if (className.startsWith(notFoundClasses[i])) 217 { 218 return true; 219 } 220 } 221 } 222 223 return false; 224 } 225 } 226 | Popular Tags |