View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2000 Michael Powers
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
17  */
18  
19  package net.wotonomy.ui.swing.util;
20  
21  import java.io.BufferedInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.FileInputStream;
24  import java.io.InputStream;
25  import java.util.Hashtable;
26  
27  /***
28   * ClassGrabber is a class loader used by WindowGrabber.
29   * It simply loads classes by filename and nothing more.
30   * It exists mainly because the java 1.1 class loading
31   * framework doesn't easily allow the creation of class
32   * loaders nor the loading of arbitrary classes.
33   *
34   * @author michael@mpowers.net
35   * @version $Revision: 904 $
36   * $Date: 2006-02-18 23:19:05 +0000 (Sat, 18 Feb 2006) $
37   */
38  public class ClassGrabber extends ClassLoader
39  {
40  	Hashtable classMap = new Hashtable();
41  
42  	public ClassGrabber()
43  	{
44  		super();
45  	}
46  
47  	protected Class loadClass(String name, boolean resolve)
48  		throws ClassNotFoundException
49  	{
50  		Class c = (Class) classMap.get( name );
51  
52  		if ( c != null ) return c;
53  
54  		try
55  		{
56  			c = findSystemClass( name );
57  		}
58  		catch ( Exception exc1 )
59  		{
60  			// System.err.print( "findSystemClass: " + name + ": " );
61  			// System.err.println( exc1 );
62  		}
63  
64  		if ( c != null ) return c;
65  
66  		try
67  		{
68  			c = findLoadedClass( name );
69  		}
70  		catch ( Exception exc1 )
71  		{
72  			// System.err.print( "findLoadedClass: " + name + ": " );
73  			// System.err.println( exc1 );
74  		}
75  		
76  		if ( c != null ) return c;
77  		
78  		try
79  		{
80  			InputStream input = new BufferedInputStream( new FileInputStream( name ) );
81  			ByteArrayOutputStream output = new ByteArrayOutputStream( 200 );
82  			int ch;
83  			while ( ( ch = input.read() ) != -1 )
84  			{
85  				output.write( ch );
86  			}
87  			byte[] data = output.toByteArray();
88  			c = defineClass( null, data, 0, data.length );				
89  		} 
90  		catch (	Exception exc )
91  		{
92  			System.err.print( "getResource: " + name + ": " );
93  			System.err.println( exc );
94              c = null;
95  		}
96  
97  		if ( c != null )
98  		{
99  			classMap.put( name, c );
100     		if ( resolve ) resolveClass( c );
101 		}
102 
103 		return c;
104 	}
105 }
106 
107 /*
108  * $Log$
109  * Revision 1.2  2006/02/18 23:19:05  cgruber
110  * Update imports and maven dependencies.
111  *
112  * Revision 1.1  2006/02/16 13:22:22  cgruber
113  * Check in all sources in eclipse-friendly maven-enabled packages.
114  *
115  * Revision 1.2  2003/08/06 23:07:53  chochos
116  * general code cleanup (mostly, removing unused imports)
117  *
118  * Revision 1.1.1.1  2000/12/21 15:51:18  mpowers
119  * Contributing wotonomy.
120  *
121  * Revision 1.2  2000/12/20 16:25:45  michael
122  * Added log to all files.
123  *
124  *
125  */
126