1
-
2
- /*
1
+ /*
3
2
* Adito
4
3
*
5
4
* Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
17
16
* License along with this program; if not, write to the Free Software
18
17
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
18
*/
20
-
21
19
package com .adito .boot ;
22
20
23
21
import java .io .File ;
38
36
39
37
/**
40
38
* ALL CHANGED - REWRITE
41
- *
42
- *
39
+ *
40
+ *
43
41
* Bootstrap an Adito server implementation. The first argument passed to
44
42
* {@link #main(String[])} should be the class name of the server
45
43
* implementation.
48
46
* <code>setBootLoader(ClassLoader bootLoader)</code> and
49
47
* <code>main(String[] args)</code>.
50
48
* <p>
51
- * A classpath.properties file in the
49
+ * A classpath.properties file in the
52
50
* <p>
53
51
* The server will be loaded using a new class loader that scans the
54
52
* <b>serverlib</b> directory for any jars specific to this implementation. The
55
53
* parent of this classloader is the <i>Boot Loader</i>
56
- *
57
- * The <i>Boot Loader</i> is the class loader that loads all of the boot
58
- * classes and anything in lib.
54
+ *
55
+ * The <i>Boot Loader</i> is the class loader that loads all of the boot classes
56
+ * and anything in lib.
59
57
* <p>
60
- * The server implementation should use the <i>Boot Loader</i> as the parent
61
- * for the Adito web application. This hides the server implementation
62
- * from the Adito core and any of its extensions.
63
- *
58
+ * The server implementation should use the <i>Boot Loader</i> as the parent for
59
+ * the Adito web application. This hides the server implementation from the
60
+ * Adito core and any of its extensions.
61
+ *
64
62
*/
65
63
public class Bootstrap {
66
64
67
- // Private instance variables
65
+ /**
66
+ * Entry point. First argument should be the server implementation class
67
+ * name. Remaining arguments are passed to create server.
68
+ *
69
+ * @param args arguments
70
+ * @throws Exception
71
+ */
72
+ public static void main (final String [] args ) throws Exception {
73
+ final List <String > argList = new ArrayList <String >(Arrays .asList (args ));
74
+ if (argList .size () > 1 && argList .get (0 ).equals ("--serverImpl" )) {
75
+ argList .remove (0 );
76
+ argList .remove (0 );
77
+ }
78
+
79
+ // Look for --conf argument and add that to that classpath
80
+ File conf = new File ("conf" );
81
+ for (String arg : argList ) {
82
+ if (arg .startsWith ("--conf=" )) {
83
+ conf = new File (arg .substring (7 ));
84
+ }
85
+ }
68
86
87
+ // Create the bootstrap, configure it and start the server
88
+ new Bootstrap ().withClassLoaders (conf )
89
+ .start (argList .toArray (new String [argList .size ()]));
90
+ }
69
91
private ClassLoader serverLoader ;
70
- private ClassLoader bootLoader ;
92
+ private ClassLoader bootLoader ;
71
93
private File conf ;
72
94
private Properties classpath ;
73
95
74
96
/**
75
97
* Start the server implementation.
76
- *
77
- * @param serverImplClassName server implementation
98
+ *
78
99
* @param args arguments
79
100
* @throws ClassNotFoundException
80
101
* @throws SecurityException
@@ -83,21 +104,29 @@ public class Bootstrap {
83
104
* @throws IllegalAccessException
84
105
* @throws InvocationTargetException
85
106
*/
86
- public void start (String serverImplClassName , String [] args ) throws ClassNotFoundException , SecurityException ,
87
- NoSuchMethodException , IllegalArgumentException , IllegalAccessException , InvocationTargetException {
88
- Class <?> serverClass = Class .forName (serverImplClassName , true , serverLoader );
89
- Method setBootLoaderMethod = serverClass .getMethod ("setBootLoader" , new Class [] { ClassLoader .class });
90
- setBootLoaderMethod .invoke (null , new Object [] { bootLoader });
91
- Method mainMethod = serverClass .getMethod ("main" , new Class [] { String [].class });
92
- mainMethod .invoke (null , new Object [] { args });
107
+ public void start (final String [] args ) throws ClassNotFoundException , NoSuchMethodException , IllegalArgumentException ,
108
+ InvocationTargetException , IllegalAccessException {
109
+
110
+ if (serverLoader == null || bootLoader == null ) {
111
+ throw new IllegalStateException ("Needed classloaders not configured!" );
112
+ }
113
+
114
+ Thread .currentThread ().setContextClassLoader (serverLoader );
115
+ Class .forName (ServerStarter .class .getName (), true , serverLoader )
116
+ .getMethod ("start" , ClassLoader .class , String [].class ).invoke (null , bootLoader , args );
117
+ }
118
+
119
+ public Bootstrap withClassLoaders (final File conf ) throws IOException , URISyntaxException {
120
+ configureClassLoaders (conf );
121
+ return this ;
93
122
}
94
123
95
124
/**
96
125
* Configure the class loaders.
97
- *
126
+ *
98
127
* @param conf conf directory
99
128
* @throws IOException
100
- * @throws URISyntaxException
129
+ * @throws URISyntaxException
101
130
*/
102
131
public void configureClassLoaders (File conf ) throws IOException , URISyntaxException {
103
132
this .conf = conf ;
@@ -114,29 +143,29 @@ public void configureClassLoaders(File conf) throws IOException, URISyntaxExcept
114
143
serverLoader = new URLClassLoader (serverLibs .toArray (new URL [serverLibs .size ()]), bootLoader );
115
144
116
145
}
117
-
146
+
118
147
private void addJarPaths (String propertyName , List <URL > libs ) throws MalformedURLException , IOException {
119
148
String paths = classpath .getProperty (propertyName );
120
- if (paths != null ) {
149
+ if (paths != null ) {
121
150
StringTokenizer t = new StringTokenizer (paths , "," );
122
- while (t .hasMoreTokens ()) {
151
+ while (t .hasMoreTokens ()) {
123
152
addDirLibs (new File (t .nextToken ()), libs );
124
153
}
125
154
}
126
155
}
127
-
156
+
128
157
private void addDirPaths (String propertyName , List <URL > libs ) throws MalformedURLException , IOException {
129
158
String paths = classpath .getProperty (propertyName );
130
- if (paths != null ) {
159
+ if (paths != null ) {
131
160
StringTokenizer t = new StringTokenizer (paths , "," );
132
- while (t .hasMoreTokens ()) {
161
+ while (t .hasMoreTokens ()) {
133
162
addDir (new File (t .nextToken ()), libs );
134
163
}
135
164
}
136
165
}
137
166
138
167
private void addDirLibs (File dir , List <URL > libs ) throws MalformedURLException , IOException {
139
- if (!dir .exists () || !dir .isDirectory ()) {
168
+ if (!dir .exists () || !dir .isDirectory ()) {
140
169
return ;
141
170
}
142
171
for (File jar : dir .listFiles (new FileFilter () {
@@ -150,59 +179,25 @@ public boolean accept(File pathname) {
150
179
151
180
private URL [] debugClasspath (String string , URL [] urls ) throws URISyntaxException {
152
181
System .out .println ("Classloader " + string );
153
- for (int i = 0 ; i < urls .length ; i ++) {
182
+ for (int i = 0 ; i < urls .length ; i ++) {
154
183
System .out .println (new File (urls [i ].toURI ()).getAbsolutePath ());
155
184
}
156
185
return urls ;
157
186
}
158
187
159
188
private void addDir (File dir , List <URL > libs ) throws MalformedURLException , IOException {
160
- if (dir .exists () || !dir .isDirectory ()) {
189
+ if (dir .exists () || !dir .isDirectory ()) {
161
190
libs .add (dir .getCanonicalFile ().toURI ().toURL ());
162
- }
191
+ }
163
192
}
164
-
193
+
165
194
private void loadClasspathConfiguration () throws IOException {
166
195
FileInputStream fin = new FileInputStream (new File (conf , "classpath.properties" ));
167
196
try {
168
197
classpath = new Properties ();
169
198
classpath .load (fin );
199
+ } finally {
200
+ fin .close ();
170
201
}
171
- finally {
172
- if (fin != null ) {
173
- fin .close ();
174
- }
175
- }
176
- }
177
-
178
- /**
179
- * Entry point. First argument should be the server implementation class
180
- * name. Remaining arguments are passed to create server.
181
- *
182
- * @param args arguments
183
- * @throws Exception
184
- */
185
- public static void main (String [] args ) throws Exception {
186
- List <String > argList = new ArrayList <String >(Arrays .asList (args ));
187
- String serverImplClassName = "com.adito.server.Main" ;
188
- if (argList .size () > 1 && argList .get (0 ).equals ("--serverImpl" )) {
189
- argList .remove (0 );
190
- serverImplClassName = argList .get (0 );
191
- argList .remove (0 );
192
- }
193
- args = argList .toArray (new String [argList .size ()]);
194
-
195
- // Look for --conf argument and add that to that classpath
196
- File conf = new File ("conf" );
197
- for (String arg : argList ) {
198
- if (arg .startsWith ("--conf=" )) {
199
- conf = new File (arg .substring (7 ));
200
- }
201
- }
202
-
203
- // Create the bootstrap, configure it and start the server
204
- Bootstrap bootstrap = new Bootstrap ();
205
- bootstrap .configureClassLoaders (conf );
206
- bootstrap .start (serverImplClassName , args );
207
202
}
208
203
}
0 commit comments