1 package org.codehaus.classworlds.uberjar.boot;
2
3 /*
4 $Id: InitialClassLoader.java,v 1.1 2003/09/23 18:11:30 jvanzyl Exp $
5
6 Copyright 2002 (C) The Werken Company. All Rights Reserved.
7
8 Redistribution and use of this software and associated documentation
9 ("Software"), with or without modification, are permitted provided
10 that the following conditions are met:
11
12 1. Redistributions of source code must retain copyright
13 statements and notices. Redistributions must also contain a
14 copy of this document.
15
16 2. Redistributions in binary form must reproduce the
17 above copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
20
21 3. The name "classworlds" must not be used to endorse or promote
22 products derived from this Software without prior written
23 permission of The Werken Company. For written permission,
24 please contact bob@werken.com.
25
26 4. Products derived from this Software may not be called "classworlds"
27 nor may "classworlds" appear in their names without prior written
28 permission of The Werken Company. "classworlds" is a registered
29 trademark of The Werken Company.
30
31 5. Due credit should be given to The Werken Company.
32 (http://classworlds.werken.com/).
33
34 THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS
35 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
36 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
37 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
38 THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
39 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
41 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
45 OF THE POSSIBILITY OF SUCH DAMAGE.
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 /*** Initial bootstrapping <code>ClassLoader</code>.
59 *
60 * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
61 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
62 *
63 * @version $Id: InitialClassLoader.java,v 1.1 2003/09/23 18:11:30 jvanzyl Exp $
64 */
65 public class InitialClassLoader
66 extends SecureClassLoader
67 {
68 // ----------------------------------------------------------------------
69 // Instance members
70 // ----------------------------------------------------------------------
71
72 /*** Class index. */
73 private Map index;
74
75 /*** Classworlds jar URL. */
76 private URL classworldsJarUrl;
77
78 // ----------------------------------------------------------------------
79 // Constructors
80 // ----------------------------------------------------------------------
81
82 /*** Construct.
83 *
84 * @throws Exception If an error occurs while attempting to perform
85 * bootstrap initialization.
86 */
87 public InitialClassLoader()
88 throws Exception
89 {
90 this.index = new HashMap();
91
92 ClassLoader cl = Thread.currentThread().getContextClassLoader();
93
94 URL classUrl = getClass().getResource( "InitialClassLoader.class" );
95
96 String urlText = classUrl.toExternalForm();
97
98 int bangLoc = urlText.indexOf( "!" );
99
100 System.setProperty( "classworlds.lib",
101 urlText.substring( 0,
102 bangLoc ) + "!/WORLDS-INF/lib" );
103
104 this.classworldsJarUrl = new URL( urlText.substring( 0,
105 bangLoc ) + "!/WORLDS-INF/classworlds.jar" );
106 }
107
108 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
109 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
110
111 /*** @see ClassLoader
112 */
113 public synchronized Class findClass(String className)
114 throws ClassNotFoundException
115 {
116 String classPath = className.replace( '.',
117 '/' ) + ".class";
118
119 if ( this.index.containsKey( classPath ) )
120 {
121 return (Class) this.index.get( classPath );
122 }
123
124 try
125 {
126 JarInputStream in = new JarInputStream( this.classworldsJarUrl.openStream() );
127
128 try
129 {
130 JarEntry entry = null;
131
132 while ((entry = in.getNextJarEntry()) != null)
133 {
134 if ( entry.getName().equals( classPath ) )
135 {
136 ByteArrayOutputStream out = new ByteArrayOutputStream();
137
138 try
139 {
140 byte[] buffer = new byte[ 2048 ];
141
142 int read = 0;
143
144 while ( in.available() > 0 )
145 {
146 read = in.read( buffer,
147 0,
148 buffer.length );
149
150 if ( read < 0 )
151 {
152 break;
153 }
154
155 out.write( buffer,
156 0,
157 read );
158 }
159
160 buffer = out.toByteArray();
161
162 Class cls = defineClass( className,
163 buffer,
164 0,
165 buffer.length );
166
167 this.index.put( className,
168 cls );
169
170 return cls;
171 }
172 finally
173 {
174 out.close();
175 }
176 }
177 }
178 }
179 finally
180 {
181 in.close();
182 }
183 }
184 catch (IOException e)
185 {
186 e.printStackTrace();
187 throw new ClassNotFoundException( "io error reading stream for: " + className );
188 }
189
190 throw new ClassNotFoundException( className );
191 }
192 }
This page was automatically generated by Maven