1 18 package org.apache.beehive.netui.util.internal; 19 20 import java.net.URLClassLoader ; 21 import java.net.URL ; 22 import java.io.File ; 23 import java.io.FileInputStream ; 24 import java.io.ByteArrayOutputStream ; 25 import java.io.FileNotFoundException ; 26 import java.io.BufferedInputStream ; 27 import java.io.IOException ; 28 import org.apache.beehive.netui.util.internal.concurrent.InternalConcurrentHashMap; 29 import java.util.Map ; 30 import java.util.Iterator ; 31 import java.lang.reflect.Method ; 32 import java.security.SecureClassLoader ; 33 34 import org.apache.beehive.netui.util.logging.Logger; 35 36 37 41 public class BouncyClassLoader 42 extends SecureClassLoader 43 { 44 private static final Logger _log = Logger.getInstance( BouncyClassLoader.class ); 45 46 private InternalConcurrentHashMap _timestamps = new InternalConcurrentHashMap(); 47 private File [] _classDirs; 48 49 50 public BouncyClassLoader( File [] classDirs, ClassLoader parent ) 51 { 52 super( parent ); 53 _classDirs = classDirs; 54 } 55 56 public Class loadClass( String name ) throws ClassNotFoundException 57 { 58 Class ret = findLoadedClass( name ); 59 if ( ret != null ) return ret; 60 61 String baseName = File.separatorChar + name.replace( '.', File.separatorChar ).concat( ".class" ); 62 63 for ( int i = 0; i < _classDirs.length; ++i ) 64 { 65 File file = new File ( _classDirs[i].getPath() + baseName ); 66 67 if ( file.exists() ) 68 { 69 _timestamps.put( file, new Long ( file.lastModified() ) ); 70 byte[] bytes = getBytes( file ); 71 if ( bytes != null ) return super.defineClass( name, bytes, 0, bytes.length ); 72 } 73 } 74 75 return super.loadClass( name ); 76 } 77 78 byte[] getBytes( File file ) 79 { 80 try 81 { 82 BufferedInputStream in = new BufferedInputStream ( new FileInputStream ( file ) ); 83 84 try 85 { 86 ByteArrayOutputStream out = new ByteArrayOutputStream (); 87 int c; 88 89 while ( ( c = in.read() ) != -1 ) 90 { 91 out.write( c ); 92 } 93 94 return out.toByteArray(); 95 } 96 finally 97 { 98 in.close(); 99 } 100 } 101 catch ( FileNotFoundException e ) 102 { 103 _log.error( "Could not read class file " + file, e ); 104 } 105 catch ( IOException e ) 106 { 107 _log.error( "Error while reading class file " + file, e ); 108 } 109 110 return null; 111 } 112 113 public boolean isStale() 114 { 115 for ( Iterator i = _timestamps.entrySet().iterator(); i.hasNext(); ) 116 { 117 Map.Entry entry = ( Map.Entry ) i.next(); 118 if ( ( ( File ) entry.getKey() ).lastModified() > ( ( Long ) entry.getValue() ).longValue() ) return true; 119 } 120 121 return false; 122 } 123 } 124 | Popular Tags |