1 22 package org.jboss.mx.loading; 23 24 import java.net.URL ; 25 import java.io.InputStream ; 26 import java.io.IOException ; 27 import java.util.zip.ZipInputStream ; 28 import java.util.zip.ZipEntry ; 29 import java.util.regex.Pattern ; 30 import java.util.regex.Matcher ; 31 import java.util.ArrayList ; 32 import java.util.Arrays ; 33 34 import org.jboss.logging.Logger; 35 36 77 public class ClassPreloadService 78 { 79 static Logger log = Logger.getLogger(ClassPreloadService.class); 80 81 private String [] includePattern = {}; 82 83 private String [] excludePattern = {}; 84 85 private boolean simpleMatch; 86 boolean trace; 87 88 public String [] getIncludePatterns() 89 { 90 return includePattern; 91 } 92 public void setIncludePatterns(String [] includePattern) 93 { 94 this.includePattern = includePattern; 95 } 96 97 public String [] getExcludePatterns() 98 { 99 return excludePattern; 100 } 101 public void setExcludePatterns(String [] excludePattern) 102 { 103 this.excludePattern = excludePattern; 104 } 105 106 public boolean isSimpleMatch() 107 { 108 return simpleMatch; 109 } 110 public void setSimpleMatch(boolean simpleMatch) 111 { 112 this.simpleMatch = simpleMatch; 113 } 114 115 public URL [] getRawClassPath() 116 { 117 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 118 URL [] fullCP = ClassLoaderUtils.getClassLoaderURLs(loader); 119 return fullCP; 120 } 121 122 126 public void start() 127 { 128 trace = log.isTraceEnabled(); 129 log.debug("Starting, includes="+ Arrays.asList(includePattern) 130 +", excludes="+excludePattern); 131 Pattern [] includes = compileIncludes(); 133 Pattern [] excludes = compileExcludes(); 134 135 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 136 URL [] rawCP = ClassLoaderUtils.getClassLoaderURLs(loader); 137 URL [] cp = filterCP(rawCP, includes, excludes); 138 139 int loadedClasses = 0; 140 int loadErrors = 0; 141 for(int n = 0; n < cp.length; n ++) 142 { 143 URL u = cp[n]; 144 try 145 { 146 InputStream is = u.openStream(); 147 ZipInputStream zis = new ZipInputStream (is); 148 ZipEntry ze = zis.getNextEntry(); 149 while (ze != null) 150 { 151 String name = ze.getName(); 152 if (name.endsWith(".class")) 153 { 154 int length = name.length(); 155 String cname = name.replace('/', '.').substring(0, length - 6); 156 try 157 { 158 Class c = loader.loadClass(cname); 159 loadedClasses ++; 160 if (trace) 161 log.trace("loaded class: " + cname); 162 } 163 catch (Throwable e) 164 { 165 loadErrors ++; 166 if( trace ) 167 log.trace("Failed to load class, "+e.getMessage()); 168 } 169 } 170 ze = zis.getNextEntry(); 171 } 172 zis.close(); 173 } 174 catch (IOException ignore) 175 { 176 } 178 } 179 log.info("Loaded "+loadedClasses+" classes, "+loadErrors+" CNFEs"); 180 } 181 182 public Pattern [] compileIncludes() 183 { 184 ArrayList tmp = new ArrayList (); 185 int count = this.includePattern != null ? includePattern.length : 0; 186 for(int n = 0; n < count; n ++) 187 { 188 String p = includePattern[n]; 189 Pattern pat = Pattern.compile(p); 190 tmp.add(pat); 191 } 192 Pattern [] includes = new Pattern [tmp.size()]; 193 tmp.toArray(includes); 194 return includes; 195 } 196 public Pattern [] compileExcludes() 197 { 198 ArrayList tmp = new ArrayList (); 199 int count = this.excludePattern != null ? excludePattern.length : 0; 200 for(int n = 0; n < count; n ++) 201 { 202 String p = excludePattern[n]; 203 Pattern pat = Pattern.compile(p); 204 tmp.add(pat); 205 } 206 Pattern [] includes = new Pattern [tmp.size()]; 207 tmp.toArray(includes); 208 return includes; 209 } 210 211 public URL [] filterCP(URL [] rawCP, Pattern [] includes, Pattern [] excludes) 212 { 213 if( trace ) 214 log.trace("filterCP, rawCP="+Arrays.asList(rawCP)); 215 ArrayList tmp = new ArrayList (); 216 int count = rawCP != null ? rawCP.length : 0; 217 for(int m = 0; m < count; m ++) 218 { 219 URL pathURL = rawCP[m]; 220 String path = pathURL.toString(); 221 boolean excluded = false; 222 223 for(int n = 0; n < excludes.length; n ++) 225 { 226 Pattern p = excludes[n]; 227 Matcher matcher = p.matcher(path); 228 if( simpleMatch && path.endsWith(p.pattern()) ) 229 { 230 excluded = true; 231 break; 232 } 233 else if( matcher.matches() ) 234 { 235 excluded = true; 236 break; 237 } 238 } 239 if( excluded ) 240 { 241 log.debug("Excluded: "+pathURL); 242 continue; 243 } 244 245 boolean included = includes.length == 0; 247 for(int n = 0; n < includes.length; n ++) 248 { 249 Pattern p = includes[n]; 250 Matcher matcher = p.matcher(path); 251 if( simpleMatch && path.endsWith(p.pattern()) ) 252 tmp.add(pathURL); 253 else if( matcher.matches() ) 254 tmp.add(pathURL); 255 } 256 if( included ) 257 { 258 log.debug("Included: "+pathURL); 259 tmp.add(pathURL); 260 } 261 } 262 URL [] cp = new URL [tmp.size()]; 263 tmp.toArray(cp); 264 return cp; 265 } 266 } 267 | Popular Tags |