1 16 17 18 package org.apache.tomcat.util.loader; 19 20 21 import java.io.BufferedReader ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.InputStreamReader ; 25 import java.net.URL ; 26 import java.util.Enumeration ; 27 import java.util.Hashtable ; 28 import java.util.Vector ; 29 30 31 47 public class Repository { 48 49 private static final boolean DEBUG=true; 51 private static final boolean USE_IDX=true; 55 private Vector loaders=new Vector (); 56 private String name; 57 private Vector grpModules=new Vector (); 58 private transient Loader loader; 59 60 private Repository parent; 61 62 private transient ModuleClassLoader groupClassLoader; 63 private Hashtable prefixes=new Hashtable (); 64 65 public Repository() { 66 } 67 68 public Repository(Loader loader) { 69 if( loader== null ) throw new NullPointerException (); 70 this.loader=loader; 71 } 72 73 public Loader getLoader() { 74 return loader; 75 } 76 77 public void addModule( Module mod ) { 78 mod.setRepository( this ); 79 80 grpModules.addElement(mod); 81 if( loader.listener!=null ) { 82 loader.listener.moduleAdd(mod); 83 } 84 85 if(! mod.isStarted()) { 86 mod.start(); 87 } else { 89 } 91 92 try { 93 if( USE_IDX ) { 94 processJarIndex(mod); 95 writeCacheIdx(); 96 } 97 } catch (Exception e) { 98 e.printStackTrace(); 100 } 101 102 } 103 104 public Enumeration getModules() { 105 return grpModules.elements(); 106 } 107 108 public Repository getParent() { 109 return parent; 110 } 111 112 116 public void setParent(Repository parent) { 117 this.parent = parent; 118 } 119 120 130 void addClassLoader(ClassLoader cl ) { 131 if( ( cl instanceof ModuleClassLoader )) { 132 ((ModuleClassLoader)cl).setRepository(this); 133 } 134 loaders.addElement(cl); 135 } 137 138 public String getName() { 139 return name; 140 } 141 142 public void removeClassLoader(ClassLoader cl) { 143 int oldSize=loaders.size(); 144 loaders.removeElement(cl); 145 146 if(DEBUG) log("removed " + loaders.size() + "/" + oldSize + ": " + cl); 147 } 148 149 154 public ClassLoader getClassLoader() { 155 if( groupClassLoader==null ) { 156 if( parent == null ) { 157 groupClassLoader=new ModuleClassLoader(new URL [0]); 158 } else { 159 groupClassLoader=new ModuleClassLoader(new URL [0], parent.getClassLoader()); 160 } 161 groupClassLoader.start(); 162 groupClassLoader.setRepository(this); 163 } 164 return groupClassLoader; 165 } 166 176 public Class findClass(ClassLoader caller, String classN ) { 177 Class clazz=null; 178 179 if( USE_IDX ) { 181 int lastIdx=classN.lastIndexOf("."); 182 String prefix=(lastIdx>0) ? classN.substring(0, lastIdx) : classN; 183 Object mO=prefixes.get(prefix.replace('.', '/')); 184 if( mO!=null ) { 185 if( mO instanceof Module ) { 186 Module m=(Module)mO; 187 try { 188 Class c=((ModuleClassLoader)m.getClassLoader()).findLocalClass(classN); 189 return c; 191 } catch (Exception e) { 192 } 195 } else { 196 Module mA[]=(Module[])mO; 197 for( int i=0; i<mA.length; i++ ) { 198 Module m=mA[i]; 199 try { 200 Class c=((ModuleClassLoader)m.getClassLoader()).findLocalClass(classN); 201 return c; 203 } catch (Exception e) { 204 } 207 } 208 } 209 } 210 } 211 212 for( int i=loaders.size()-1; i>=0; i-- ) { 214 215 ModuleClassLoader cl=(ModuleClassLoader)loaders.elementAt(i); 218 if( cl== caller ) continue; 221 try { 225 if( cl instanceof ModuleClassLoader ) { 226 clazz=((ModuleClassLoader)cl).findLocalClass(classN ); 227 } else { 228 clazz=cl.findClass(classN); 229 } 230 231 return clazz; 233 } catch (ClassNotFoundException e) { 234 } 237 } 238 return null; 239 } 240 241 246 public URL findResource(ModuleClassLoader caller, String classN) { 247 URL url=null; 248 249 for( int i=loaders.size()-1; i>=0; i-- ) { 250 ModuleClassLoader cl=(ModuleClassLoader)loaders.elementAt(i); 253 if( cl== caller ) continue; 254 url=((ModuleClassLoader)cl).findResource(classN ); 255 if( url!=null ) 256 return url; 257 } 258 return null; 259 } 260 261 private void log(String s) { 262 System.err.println("Repository (" + name + "): " + s ); 263 } 264 265 268 public void setName(String name2) { 269 this.name=name2; 270 } 271 272 281 282 288 private void processJarIndex(Module m) throws Exception { 289 ModuleClassLoader cl=(ModuleClassLoader)m.createClassLoader(); 290 String cp=m.getClasspathString(); 292 if( ! cp.endsWith(".jar")) return; 293 URL urlIdx=cl.findResource("META-INF/INDEX.LIST"); 294 if( urlIdx == null ) { 295 log("INDEX.LIST not found, run: jar -i " + m.getClasspathString()); 296 return; 297 } 298 try { 299 InputStream is=urlIdx.openStream(); 300 if( is==null ) { 301 log("Can't read " + urlIdx + " " + m.getClasspathString()); 302 return; 303 } 304 BufferedReader br=new BufferedReader ( new InputStreamReader (is) ); 305 String line=br.readLine(); 306 if( line==null ) return; 307 if( ! line.startsWith( "JarIndex-Version:") || 308 ! line.endsWith("1.0")) { 309 log("Invalid signature " + line + " " + m.getClasspathString()); 310 } 311 br.readLine(); 313 while( readSection(br, m) ) { 314 } 315 316 m.hasIndex=true; 317 } catch (IOException e) { 318 e.printStackTrace(); 320 } 321 } 322 323 private boolean readSection( BufferedReader br, Module m) throws IOException { 324 String jarName=br.readLine(); 325 if( jarName==null ) return false; if( "".equals( jarName )) { 327 log("Invalid jarName " + jarName + " " + m.getClasspathString() ); 328 return false; 329 } 330 String prefix=null; 332 while( ((prefix=br.readLine()) != null ) && 333 (! "".equals( prefix )) ) { 334 Object o=prefixes.get(prefix); 336 if( o == null ) { 337 prefixes.put(prefix, m); 338 } else { 339 Module mA[]=null; 340 if( o instanceof Module ) { 341 mA=new Module[2]; 342 mA[0]=(Module)o; 343 mA[1]=m; 344 } else { 345 Object oldA[]=(Module[])o; 346 mA=new Module[oldA.length + 1]; 347 System.arraycopy(oldA, 0, mA, 0, oldA.length); 348 mA[oldA.length]=m; 349 } 350 prefixes.put( prefix, mA); 351 353 } 354 } 355 356 return prefix!=null; 357 } 358 359 360 367 private void readCachedIdx() throws IOException { 368 369 } 370 371 377 private void checkCacheIdx() throws IOException { 378 379 } 380 381 382 private void writeCacheIdx() throws IOException { 383 384 } 385 } 386 | Popular Tags |