KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > util > internal > BouncyClassLoader


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.util.internal;
19
20 import java.net.URLClassLoader JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.ByteArrayOutputStream JavaDoc;
25 import java.io.FileNotFoundException JavaDoc;
26 import java.io.BufferedInputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28 import org.apache.beehive.netui.util.internal.concurrent.InternalConcurrentHashMap;
29 import java.util.Map JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.security.SecureClassLoader JavaDoc;
33
34 import org.apache.beehive.netui.util.logging.Logger;
35
36
37 /**
38  * ClassLoader that takes precendence over its parent for loading classes, and which is aware of timestamps on its
39  * binaries (and thus knows when it needs to be bounced.
40  */

41 public class BouncyClassLoader
42         extends SecureClassLoader JavaDoc
43 {
44     private static final Logger _log = Logger.getInstance( BouncyClassLoader.class );
45     
46     private InternalConcurrentHashMap/*< File, Long >*/ _timestamps = new InternalConcurrentHashMap/*< File, Long >*/();
47     private File JavaDoc[] _classDirs;
48     
49     
50     public BouncyClassLoader( File JavaDoc[] classDirs, ClassLoader JavaDoc parent )
51     {
52         super( parent );
53         _classDirs = classDirs;
54     }
55     
56     public Class JavaDoc loadClass( String JavaDoc name ) throws ClassNotFoundException JavaDoc
57     {
58         Class JavaDoc ret = findLoadedClass( name );
59         if ( ret != null ) return ret;
60         
61         String JavaDoc baseName = File.separatorChar + name.replace( '.', File.separatorChar ).concat( ".class" );
62         
63         for ( int i = 0; i < _classDirs.length; ++i )
64         {
65             File JavaDoc file = new File JavaDoc( _classDirs[i].getPath() + baseName );
66             
67             if ( file.exists() )
68             {
69                 _timestamps.put( file, new Long JavaDoc( 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 JavaDoc file )
79     {
80         try
81         {
82             BufferedInputStream JavaDoc in = new BufferedInputStream JavaDoc( new FileInputStream JavaDoc( file ) );
83             
84             try
85             {
86                 ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
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 JavaDoc e )
102         {
103             _log.error( "Could not read class file " + file, e );
104         }
105         catch ( IOException JavaDoc 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 JavaDoc i = _timestamps.entrySet().iterator(); i.hasNext(); )
116         {
117             Map.Entry entry = ( Map.Entry ) i.next();
118             if ( ( ( File JavaDoc ) entry.getKey() ).lastModified() > ( ( Long JavaDoc ) entry.getValue() ).longValue() ) return true;
119         }
120         
121         return false;
122     }
123 }
124
Popular Tags