1
2
3
4
5
6
7
8
9
10
11
12
13 package org.abstracthorizon.extend.server;
14
15 import java.io.BufferedReader;
16 import java.io.File;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.InputStreamReader;
20 import java.lang.reflect.Constructor;
21 import java.lang.reflect.InvocationTargetException;
22 import java.lang.reflect.Method;
23 import java.net.JarURLConnection;
24 import java.net.MalformedURLException;
25 import java.net.URL;
26 import java.net.URLClassLoader;
27 import java.util.ArrayList;
28 import java.util.Collection;
29 import java.util.Enumeration;
30 import java.util.jar.JarEntry;
31 import java.util.jar.JarFile;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 public class Bootstrap {
61
62
63
64
65
66
67 public static void main(String[] args) throws Exception {
68 URL homeURL = getHomeLocation();
69 URL libURL = new URL(homeURL.getProtocol(), homeURL.getHost(), homeURL.getPort(), homeURL.getFile() + "lib/");
70
71 String serverDir = "default";
72
73 int i = 0;
74 while (i < args.length) {
75 if ("-c".equals(args[i])) {
76 i = i + 1;
77 if (i >= args.length) {
78 throw new IllegalArgumentException("-c must be followed with name of sub-directory from server directory");
79 }
80 serverDir = args[i];
81 }
82
83 i = i + 1;
84 }
85
86 URL serverURL = null;
87 File local = new File(homeURL.getFile() + "server/" + serverDir);
88 if (local.exists()) {
89 serverURL = new URL(homeURL.getProtocol(), homeURL.getHost(), homeURL.getPort(), homeURL.getFile() + "server/" + serverDir + "/");
90 } else {
91 serverURL = new URL(homeURL.getProtocol(), homeURL.getHost(), homeURL.getPort(), serverDir + "/");
92 }
93
94 System.out.println("home = " + homeURL);
95 System.out.println("lib = " + libURL);
96
97 URLClassLoader classLoader = createClassLoader(Thread.currentThread().getContextClassLoader(), libURL);
98
99 Class<?> serverClass = classLoader.loadClass("org.abstracthorizon.extend.support.spring.server.SpringBasedServer");
100
101 Constructor<?> constructor = serverClass.getConstructor(new Class[]{URL.class, URL.class});
102 Object server = constructor.newInstance(new Object[]{homeURL, serverURL});
103
104 invokeMethod(server, "create", true);
105 invokeMethod(server, "start", true);
106
107 }
108
109
110
111
112
113
114 public static URL getHomeLocation() throws MalformedURLException {
115 ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
116 String className = Bootstrap.class.getName().replace('.', '/') + ".class";
117 URL root = contextClassLoader.getResource(className);
118 if ("file".equals(root.getProtocol())) {
119 String file = root.getFile();
120 file = file.substring(0, file.length() - className.length() - 1);
121 if (file.endsWith("bin")) {
122 file = file.substring(0, file.length() - 3);
123 }
124 return new File(file).toURI().toURL();
125 } else if ("jar".equals(root.getProtocol())) {
126 String file = root.getFile();
127
128
129
130 int i = file.lastIndexOf('!');
131 file = file.substring(0, i);
132 File f = new File(file);
133 file = f.getParent();
134 if (file.endsWith("bin")) {
135 file = file.substring(0, file.length() - 3);
136 }
137 return new URL(file);
138
139 } else {
140 throw new RuntimeException("Cannot handle protocol from where this class is loaded; " + root);
141 }
142 }
143
144
145
146
147
148
149
150
151
152 public static URLClassLoader createClassLoader(ClassLoader parent, URL lib) throws IOException {
153 Collection<URL> urls = collectFiles(lib);
154 urls.add(lib);
155 URL[] us = new URL[urls.size()];
156 us = urls.toArray(us);
157 URLClassLoader classLoader = new URLClassLoader(us, parent);
158 return classLoader;
159 }
160
161
162
163
164
165
166
167 public static Collection<URL> collectFiles(URL url) throws IOException {
168 String protocol = url.getProtocol();
169 if ("file".equals(protocol)) {
170 return collectFilesFromPath(new File(url.getFile()));
171 } else if ("jar".equals(protocol)) {
172 JarURLConnection jarURLConnection = (JarURLConnection)url.openConnection();
173 return collectFilesFromJar(url, jarURLConnection.getJarFile());
174 } else {
175 return collectFilesFromURL(url);
176 }
177 }
178
179
180
181
182
183
184
185 public static Collection<URL> collectFilesFromPath(File path) throws IOException {
186 ArrayList<URL> urls = new ArrayList<URL>();
187 if (path.exists()) {
188 if (path.isDirectory()) {
189 File[] files = path.listFiles();
190 for (File file : files) {
191 if (file.isFile()) {
192 urls.add(file.toURI().toURL());
193 }
194 }
195 }
196 }
197 return urls;
198 }
199
200
201
202
203
204
205
206
207 public static Collection<URL> collectFilesFromJar(URL original, JarFile jarFile) throws IOException {
208 ArrayList<URL> urls = new ArrayList<URL>();
209
210 String originalFile = original.getFile();
211 String prefix = originalFile.substring(originalFile.lastIndexOf('!') + 1);
212 if (prefix.endsWith("/")) {
213 if (prefix.startsWith("/")) {
214 prefix = prefix.substring(1);
215 }
216 Enumeration<JarEntry> en = jarFile.entries();
217 while (en.hasMoreElements()) {
218 JarEntry entry = en.nextElement();
219 String path = entry.getName();
220 if (path.startsWith(prefix)) {
221 path = path.substring(prefix.length());
222 if (path.length() > 0) {
223 int i = path.indexOf('/');
224 if ((i < 0)
225 URL url = new URL(original.getProtocol(), original.getHost(), original.getPort(), originalFile + path);
226 urls.add(url);
227 }
228 }
229 }
230 }
231 }
232 return urls;
233 }
234
235
236
237
238
239
240
241 public static Collection<URL> collectFilesFromURL(URL url) throws IOException {
242 ArrayList<URL> urls = new ArrayList<URL>();
243 InputStream is = url.openStream();
244 try {
245 BufferedReader input = new BufferedReader(new InputStreamReader(is));
246 String file = input.readLine();
247 while (file != null) {
248 if (file.length() > 0) {
249 URL newUrl = new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getFile() + "/" + file);
250 urls.add(newUrl);
251 }
252 file = input.readLine();
253 }
254 } finally {
255 is.close();
256 }
257 return urls;
258 }
259
260
261
262
263
264
265
266
267
268
269
270
271 public static void invokeMethod(Object object, String methodName, boolean mustBePresent) {
272 try {
273 Method method = object.getClass().getMethod(methodName, new Class[]{});
274 method.invoke(object, new Object[]{});
275 } catch (NoSuchMethodException e) {
276 if (mustBePresent) {
277 throw new RuntimeException(e);
278 }
279 } catch (IllegalArgumentException e) {
280 throw new RuntimeException(e);
281 } catch (IllegalAccessException e) {
282 throw new RuntimeException(e);
283 } catch (InvocationTargetException e) {
284 throw new RuntimeException(e);
285 }
286 }
287 }