1 package org.jboss.cache.marshall; 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 35 public class SelectedClassnameClassLoader extends ClassLoader { 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 super(parent); 71 this.includedClasses = includedClasses; 72 this.excludedClasses = excludedClasses; 73 } 74 75 100 public SelectedClassnameClassLoader(String [] includedClasses, 101 String [] excludedClasses, 102 String [] notFoundClasses, 103 ClassLoader parent) { 104 super(parent); 105 this.includedClasses = includedClasses; 106 this.excludedClasses = excludedClasses; 107 this.notFoundClasses = notFoundClasses; 108 } 109 110 protected synchronized Class loadClass(String name, boolean resolve) 111 throws ClassNotFoundException 112 { 113 log.info("In SelectedClassnameClassLoader.loadClass("+name+","+resolve+")"); 114 if (isIncluded(name) && (isExcluded(name) == false)) { 115 Class c = findClass(name); 116 117 if (resolve) { 118 resolveClass(c); 119 } 120 return c; 121 } 122 else { 123 return super.loadClass(name, resolve); 124 } 125 } 126 127 protected Class findClass(String name) throws ClassNotFoundException { 128 129 log.info("In SelectedClassnameClassLoader.findClass()"); 130 Class result = (Class )classes.get(name); 131 if(result != null){ 132 return result; 133 } 134 135 if (isIncluded(name) && (isExcluded(name) == false)) { 136 try { 137 InputStream is = getResourceAsStream( name.replace('.','/').concat(".class")); 138 byte[] bytes = new byte[1024]; 139 ByteArrayOutputStream baos = new ByteArrayOutputStream (1024); 140 int read; 141 while ((read = is.read(bytes)) > -1) { 142 baos.write(bytes, 0, read); 143 } 144 bytes = baos.toByteArray(); 145 result = this.defineClass(name, bytes, 0, bytes.length); 146 } catch (FileNotFoundException e) { 147 throw new ClassNotFoundException ("cannot find " + name, e); 148 } catch (IOException e) { 149 throw new ClassNotFoundException ("cannot read " + name, e); 150 } 151 } 152 else if (isNotFound(name)) { 153 throw new ClassNotFoundException (name + " is discarded"); 154 } 155 else { 156 result = super.findClass(name); 157 } 158 159 classes.put(name, result); 160 161 return result; 162 } 163 164 private boolean isIncluded(String className) { 165 166 if (includedClasses != null) { 167 for (int i = 0; i < includedClasses.length; i++) { 168 if (className.startsWith(includedClasses[i])) { 169 return true; 170 } 171 } 172 } 173 174 return false; 175 } 176 177 private boolean isExcluded(String className) { 178 179 if (excludedClasses != null) { 180 for (int i = 0; i < excludedClasses.length; i++) { 181 if (className.startsWith(excludedClasses[i])) { 182 return true; 183 } 184 } 185 } 186 187 return false; 188 } 189 190 private boolean isNotFound(String className) { 191 192 if (notFoundClasses != null) { 193 for (int i = 0; i < notFoundClasses.length; i++) { 194 if (className.startsWith(notFoundClasses[i])) { 195 return true; 196 } 197 } 198 } 199 200 return false; 201 } 202 } 203 | Popular Tags |