1 package org.codehaus.classworlds.uberjar.boot;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 import java.io.ByteArrayOutputStream;
50 import java.io.IOException;
51 import java.net.URL;
52 import java.security.SecureClassLoader;
53 import java.util.Map;
54 import java.util.HashMap;
55 import java.util.jar.JarInputStream;
56 import java.util.jar.JarEntry;
57
58 /***
59 * Initial bootstrapping <code>ClassLoader</code>.
60 *
61 * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
62 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
63 * @version $Id: InitialClassLoader.java,v 1.1.1.1 2004/07/01 13:59:19 jvanzyl Exp $
64 */
65 public class InitialClassLoader
66 extends SecureClassLoader
67 {
68
69
70
71
72 /***
73 * Class index.
74 */
75 private Map index;
76
77 /***
78 * Classworlds jar URL.
79 */
80 private URL classworldsJarUrl;
81
82
83
84
85
86 /***
87 * Construct.
88 *
89 * @throws Exception If an error occurs while attempting to perform
90 * bootstrap initialization.
91 */
92 public InitialClassLoader()
93 throws Exception
94 {
95 this.index = new HashMap();
96
97 ClassLoader cl = Thread.currentThread().getContextClassLoader();
98
99 URL classUrl = getClass().getResource( "InitialClassLoader.class" );
100
101 String urlText = classUrl.toExternalForm();
102
103 int bangLoc = urlText.indexOf( "!" );
104
105 System.setProperty( "classworlds.lib",
106 urlText.substring( 0,
107 bangLoc ) + "!/WORLDS-INF/lib" );
108
109 this.classworldsJarUrl = new URL( urlText.substring( 0,
110 bangLoc ) + "!/WORLDS-INF/classworlds.jar" );
111 }
112
113
114
115
116 /***
117 * @see ClassLoader
118 */
119 public synchronized Class findClass( String className )
120 throws ClassNotFoundException
121 {
122 String classPath = className.replace( '.',
123 '/' ) + ".class";
124
125 if ( this.index.containsKey( classPath ) )
126 {
127 return (Class) this.index.get( classPath );
128 }
129
130 try
131 {
132 JarInputStream in = new JarInputStream( this.classworldsJarUrl.openStream() );
133
134 try
135 {
136 JarEntry entry = null;
137
138 while ( ( entry = in.getNextJarEntry() ) != null )
139 {
140 if ( entry.getName().equals( classPath ) )
141 {
142 ByteArrayOutputStream out = new ByteArrayOutputStream();
143
144 try
145 {
146 byte[] buffer = new byte[2048];
147
148 int read = 0;
149
150 while ( in.available() > 0 )
151 {
152 read = in.read( buffer,
153 0,
154 buffer.length );
155
156 if ( read < 0 )
157 {
158 break;
159 }
160
161 out.write( buffer,
162 0,
163 read );
164 }
165
166 buffer = out.toByteArray();
167
168 Class cls = defineClass( className,
169 buffer,
170 0,
171 buffer.length );
172
173 this.index.put( className,
174 cls );
175
176 return cls;
177 }
178 finally
179 {
180 out.close();
181 }
182 }
183 }
184 }
185 finally
186 {
187 in.close();
188 }
189 }
190 catch ( IOException e )
191 {
192 e.printStackTrace();
193 throw new ClassNotFoundException( "io error reading stream for: " + className );
194 }
195
196 throw new ClassNotFoundException( className );
197 }
198 }