1 29 38 39 package org.planetamessenger.security; 40 41 import org.planetamessenger.io.*; 42 import java.util.jar.*; 43 import java.util.*; 44 import java.io.*; 45 import java.net.*; 46 47 48 public class JJarClassLoader extends java.lang.ClassLoader { 49 50 private final java.lang.String SUFFIX_CLASS = ".class"; 51 52 private JarFile jarFile = null; 53 private Manifest manifest = null; 54 private HashMap<String , String > depList = null; 55 56 57 62 public JJarClassLoader() { 63 64 this( java.lang.ClassLoader.getSystemClassLoader() ); 65 } 66 67 73 public JJarClassLoader( java.lang.ClassLoader parent ) { 74 75 super( parent ); 76 77 depList = new HashMap<String , String >(); 78 } 79 80 88 public JJarClassLoader( String strJarFile, boolean bLoadIt ) throws JarException { 89 90 this(); 91 92 try { 93 open( strJarFile ); 94 95 if( bLoadIt ) 96 if( !load() ) 97 throw new JarException( "JJarClassLoader() - Exception in jar " + strJarFile + " file. The jar content could not be loaded" ); 98 } catch( java.util.jar.JarException e ) { 99 throw new JarException( "JJarClassLoader() - Exception in jar " + strJarFile + " file " + e ); 100 } 101 } 102 103 108 public void addDependency( String strJarFile ) { 109 110 if( !depList.containsKey( strJarFile ) ) 111 depList.put( strJarFile, strJarFile ); 112 } 113 114 120 public synchronized Manifest getManifestFromFile( String strJarFile ) throws JarException { 121 122 try { 123 124 JarFile tmpJarFile = new JarFile( strJarFile ); 125 return tmpJarFile.getManifest(); 126 127 } catch( java.io.IOException e ) { 128 throw new JarException( "JJarClassLoader.getManifestFromFile() - Exception in jar " + strJarFile + " file " + e ); 129 } 130 } 131 132 136 public synchronized void open( String strJarFile ) throws JarException { 137 138 try { 139 close(); 140 jarFile = new JarFile( strJarFile ); 141 manifest = jarFile.getManifest(); 142 } catch( java.io.IOException e ) { 143 throw new JarException( "JJarClassLoader.open() - Exception in jar " + strJarFile + " file " + e ); 144 } 145 } 146 147 150 public synchronized void close() throws JarException { 151 152 try { 153 if( jarFile != null ) { 154 jarFile.close(); 155 jarFile = null; 156 manifest = null; 157 } 158 } catch( java.io.IOException e ) { 159 throw new JarException( "JJarClassLoader.close() - " + e ); 160 } 161 } 162 163 167 public synchronized boolean load() { 168 169 if( jarFile != null ) { 170 java.util.Enumeration entries = jarFile.entries(); 171 java.lang.String strName; 172 173 174 while( entries.hasMoreElements() ) { 175 176 strName = ( ( JarEntry ) entries.nextElement() ).getName(); 177 178 try { 179 180 if( strName.endsWith( SUFFIX_CLASS ) ) 181 loadClass( strName, true ); 182 183 } catch( java.lang.ClassNotFoundException e ) { 184 System.err.println( "JJarClassLoader.load() - " + e ); 185 return false; 186 } 187 } 188 189 return true; 190 } 191 192 return false; 193 } 194 195 198 public Manifest getManifest() { 199 200 return manifest; 201 } 202 203 208 public URL getResource( String strResName ) { 209 210 try { 211 URL url = new URL( "jar:file:" + jarFile.getName() + "!/" + strResName ); 212 213 return url; 214 } catch( MalformedURLException e ) { 215 System.err.println( "JJarClassloader.getResource() - " + e ); 216 return null; 217 } 218 } 219 220 229 public InputStream getResourceAsStream( String strResName ) { 230 231 System.err.println( "JJarClassLoader.getResourceAsStream() - Getting " + strResName ); 232 233 InputStream inStream = null; 234 235 236 try { 237 238 JarEntry jarEntry = ( jarFile != null ? jarFile.getJarEntry( strResName ) : null ); 239 240 if( jarEntry != null ) 242 inStream = jarFile.getInputStream( jarEntry ); 243 else { 244 245 JarFile tmpJarFile = null; 246 java.lang.Object depArray[] = depList.values().toArray(); 247 248 249 for( int nCount = 0; nCount < depArray.length; nCount++ ) { 251 try { 252 tmpJarFile = new JarFile( ( String ) depArray[nCount] ); 253 jarEntry = tmpJarFile.getJarEntry( strResName ); 254 255 if( jarEntry != null ) { 256 inStream = tmpJarFile.getInputStream( jarEntry ); 257 break; 258 } 259 } catch( java.io.IOException ioe ) { 260 System.err.println( "JJarClassLoader.getResourceAsStream() - " + ioe ); 261 } 262 } 263 264 if( inStream == null ) { 266 ClassLoader parent = getParent(); 267 268 if( parent != null ) 269 inStream = parent.getResourceAsStream( strResName ); 270 271 if( inStream == null ) 273 inStream = java.lang.ClassLoader.getSystemClassLoader().getResourceAsStream( strResName ); 274 } 275 } 276 } catch( IOException e ) { 277 System.err.println( "JJarClassLoader.getResourceAsStream() - " + e ); 278 } 279 280 return inStream; 281 } 282 283 289 protected String replaceClassName( String strClassName ) { 290 291 int nLastIndex = strClassName.indexOf( SUFFIX_CLASS ); 292 293 294 if( nLastIndex < 0 ) 295 return strClassName; 296 297 java.lang.String strNewClassName = strClassName.substring( 0, nLastIndex ); 298 299 if( strNewClassName.indexOf( '/' ) >= 0 ) 300 return strNewClassName.replace( '/', '.' ); 301 else 302 if( strNewClassName.indexOf( '\\' ) >= 0 ) 303 return strNewClassName.replace( '\\', '.' ); 304 else 305 return strClassName; 306 } 307 308 314 protected String replaceDottedName( String strClassName ) { 315 316 int nDotPos = strClassName.indexOf( '.' ); 317 318 319 if( nDotPos < 0 ) 320 return strClassName; 321 322 java.lang.String strClassEntry = strClassName.replace( '.', '/' ) + SUFFIX_CLASS; 323 324 return strClassEntry; 325 } 326 327 339 protected Class findClass( String strName ) throws ClassNotFoundException { 340 341 java.io.InputStream inStream = null; 342 JarEntry jarEntry = null; 343 JarFile tmpJarFile = null; 344 java.lang.Class theClass = null; 345 346 347 348 strName = replaceClassName( strName ); 349 theClass = findLoadedClass( strName ); 350 351 System.err.println( "JJarClassLoader.findClass() - " + strName + ( ( theClass == null ) ? " not " : " " ) + " in cache" ); 352 353 if( theClass == null ) { 355 356 java.lang.Object depArray[] = depList.values().toArray(); 358 359 360 for( int nCount = 0; nCount < depArray.length; nCount++ ) { 361 try { 362 tmpJarFile = new JarFile( ( String ) depArray[nCount] ); 363 jarEntry = tmpJarFile.getJarEntry( replaceDottedName( strName ) ); 364 365 if( jarEntry != null ) { 366 inStream = tmpJarFile.getInputStream( jarEntry ); 367 break; 368 } 369 370 } catch( java.io.IOException ioe ) { 371 System.err.println( "JJarClassLoader.findClass.findSystemClass() - " + ioe ); 372 } 373 } 374 375 if( jarEntry == null ) { 377 jarEntry = ( jarFile != null ? jarFile.getJarEntry( replaceDottedName( strName ) ) : null ); 379 380 if( jarEntry != null ) { 381 try { 382 inStream = jarFile.getInputStream( jarEntry ); 383 } catch( java.io.IOException ioe ) { 384 System.err.println( "JJarClassLoader.findClass.findSystemClass() - " + ioe ); 385 throw new ClassNotFoundException ( strName ); 386 } 387 } 388 } 389 390 if( jarEntry == null ) { 391 ClassLoader parent = getParent(); 392 393 try { 395 return findSystemClass( strName ); 396 } catch( java.lang.NoClassDefFoundError nce ) { 397 System.err.println( "JJarClassLoader.findClass.findSystemClass() - " + nce ); 398 399 if( parent != null ) { 401 try { 402 return parent.loadClass( strName ); 403 } catch( java.lang.ClassNotFoundException e ) { 404 System.err.println( "JJarClassLoader.findClass.loadClass() - " + e ); 405 throw new ClassNotFoundException ( strName ); 406 } 407 } 408 } 409 } 410 411 try { 413 int nLen = ( int ) jarEntry.getSize(); 414 byte byteCodes[] = new byte[nLen]; 415 int nOffset = 0; 416 int nLoaded = 0; 417 418 419 while( nLoaded < nLen ) { 420 nLen-=nLoaded; 421 nOffset+=nLoaded; 422 423 nLoaded = inStream.read( byteCodes, nOffset, nLen ); 424 425 if( nLoaded == -1 ) { 426 inStream.close(); 427 throw new java.lang.ClassNotFoundException ( strName ); 428 } 429 } 430 431 inStream.close(); 432 433 if( tmpJarFile != null ) { 435 tmpJarFile.close(); 436 tmpJarFile = null; 437 } 438 439 try { 440 theClass = defineClass( strName, byteCodes, 0, byteCodes.length ); 441 } catch( java.lang.ClassFormatError e ) { 442 System.err.println( "JJarClassLoader.findClass.defineClass() - " + e ); 443 } 444 445 } catch( java.io.IOException e ) { 446 System.err.println( "JJarClassLoader.findClass.getInputStream - " + e ); 447 theClass = null; 448 } 449 } 450 451 if( theClass == null ) 452 throw new ClassNotFoundException ( strName ); 453 else 454 return theClass; 455 } 456 } 457 458 | Popular Tags |