View Javadoc

1   package org.codehaus.classworlds.uberjar.boot;
2   
3   /*
4    $Id: InitialClassLoader.java,v 1.1.1.1 2004/07/01 13:59:19 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  /***
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      //     Instance members
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      //     Constructors
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 }