1 package org.codehaus.classworlds.uberjar.boot;
2
3 /*
4 $Id: Bootstrapper.java,v 1.2 2003/09/23 18:42:21 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.lang.reflect.Method;
50 import java.lang.reflect.Modifier;
51
52 /*** Bootstrapping entry-point.
53 *
54 * <p>
55 * The <code>Bootstrapper</code> is to be used for standalone jars
56 * which carry all dependency jars within them. The layout for
57 * the dependency jar should be similar to:
58 * </p>
59 *
60 * <pre>
61 * myjar/
62 * classworlds.conf
63 * org/
64 * codehaus/
65 * classworlds/
66 * boot/
67 * protocol/
68 * lib/
69 * myapp.jar
70 * depOne.jar
71 * depTwo.jar
72 * </pre>
73 *
74 * @author <a href="mailto:jason@zenplex.com">Jason van Zyl</a>
75 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
76 *
77 * @version $Id: Bootstrapper.java,v 1.2 2003/09/23 18:42:21 jvanzyl Exp $
78 */
79 public class Bootstrapper
80 {
81 // ----------------------------------------------------------------------
82 // Constants
83 // ----------------------------------------------------------------------
84
85 /*** Main classworlds entry class. */
86 public static final String LAUNCHER_CLASS_NAME = "org.codehaus.classworlds.Launcher";
87
88 // ----------------------------------------------------------------------
89 // Instance members
90 // ----------------------------------------------------------------------
91
92 /*** Command-line args. */
93 private String[] args;
94
95 /*** Initial bootstrapping classloader. */
96 private InitialClassLoader classLoader;
97
98 // ----------------------------------------------------------------------
99 // Class methods
100 // ----------------------------------------------------------------------
101
102 /*** Main entry-point.
103 *
104 * @param args Command-line arguments.
105 *
106 * @throws Exception If an error occurs.
107 */
108 public static void main(String[] args)
109 throws Exception
110 {
111 System.setProperty( "java.protocol.handler.pkgs",
112 "org.codehaus.classworlds.uberjar.protocol" );
113
114 Bootstrapper bootstrapper = new Bootstrapper( args );
115
116 bootstrapper.bootstrap();
117 }
118
119 // ----------------------------------------------------------------------
120 // Constructors
121 // ----------------------------------------------------------------------
122
123 /*** Construct.
124 *
125 * @param args Command-line arguments.
126 *
127 * @throws Exception If an error occurs attempting to perform
128 * bootstrap initialization.
129 */
130 public Bootstrapper(String[] args)
131 throws Exception
132 {
133 this.args = args;
134 this.classLoader = new InitialClassLoader();
135 }
136
137 // ----------------------------------------------------------------------
138 // Instance methods
139 // ----------------------------------------------------------------------
140
141 /*** Retrieve the initial bootstrapping <code>ClassLoader</code>.
142 *
143 * @return The classloader.
144 */
145 protected ClassLoader getInitialClassLoader()
146 {
147 return this.classLoader;
148 }
149
150 /*** Perform bootstrap.
151 *
152 * @throws Exception If an error occurs while bootstrapping.
153 */
154 public void bootstrap()
155 throws Exception
156 {
157 ClassLoader cl = getInitialClassLoader();
158
159 Class launcherClass = cl.loadClass( LAUNCHER_CLASS_NAME );
160
161 Method[] methods = launcherClass.getMethods();
162 Method mainMethod = null;
163
164 for ( int i = 0 ; i < methods.length ; ++i )
165 {
166 if ( ! "main".equals( methods[i].getName() ) )
167 {
168 continue;
169 }
170
171 int modifiers = methods[i].getModifiers();
172
173 if ( !( Modifier.isStatic( modifiers )
174 &&
175 Modifier.isPublic( modifiers ) ) )
176 {
177 continue;
178 }
179
180 if ( methods[i].getReturnType() != Void.TYPE )
181 {
182 continue;
183 }
184
185 Class[] paramTypes = methods[i].getParameterTypes();
186
187 if (paramTypes.length != 1)
188 {
189 continue;
190 }
191
192 if (paramTypes[0] != String[].class)
193 {
194 continue;
195 }
196
197 mainMethod = methods[i];
198 break;
199 }
200
201 if ( mainMethod == null )
202 {
203 throw new NoSuchMethodException( LAUNCHER_CLASS_NAME + "::main(String[] args)" );
204 }
205
206 System.setProperty( "classworlds.bootstrapped",
207 "true" );
208
209 mainMethod.invoke( launcherClass,
210 new Object[] { this.args } );
211 }
212 }
This page was automatically generated by Maven