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