1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.gridsystems.launcher;
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.lang.reflect.Method;
22 import java.lang.reflect.Modifier;
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 import java.util.logging.Level;
33 import java.util.logging.Logger;
34
35
36
37
38
39
40
41
42
43
44 public class CLBuilder {
45
46
47
48 private static Logger log = Logger.getLogger("CLBuilder");
49
50
51
52
53 private ClassLoader parent;
54
55
56
57
58 private Collection classpath = new ArrayList();
59
60
61
62
63 public CLBuilder() {
64 this(null);
65 }
66
67
68
69
70
71
72 public CLBuilder(ClassLoader parent) {
73 this.parent = parent;
74 }
75
76
77
78
79
80
81 public synchronized void setParent(ClassLoader parent) {
82 this.parent = parent;
83 }
84
85
86
87
88
89
90 public synchronized void addClassPath(ClassLoader cl) {
91 try {
92 Enumeration enumeration = cl.getResources("");
93 addEnumeration(enumeration);
94
95 enumeration = cl.getResources("META-INF");
96 addEnumeration(enumeration);
97 } catch (Exception ignore) { }
98 }
99
100
101
102
103 public synchronized void addContextClassPath() {
104 addClassPath(Thread.currentThread().getContextClassLoader());
105 }
106
107
108
109
110
111
112 public synchronized void addDir(File dir) {
113 try {
114 if (check(dir) && dir.isDirectory()) {
115 addUrl(dir.toURI().toURL());
116 }
117 } catch (MalformedURLException e) {
118 log.log(Level.WARNING, "Malformed path: {0}", dir);
119 }
120 }
121
122
123
124
125
126
127
128 private void addEnumeration(Enumeration enumeration) throws Exception {
129 while (enumeration.hasMoreElements()) {
130 URL url = (URL)enumeration.nextElement();
131 if (url.getProtocol().startsWith("jar")) {
132 JarURLConnection con = (JarURLConnection)url.openConnection();
133 URL jar = con.getJarFileURL();
134 addUrl(jar);
135 } else if (url.getProtocol().startsWith("file")) {
136 addUrl(url);
137 }
138 }
139 }
140
141
142
143
144
145
146
147 public synchronized void addFile(File f) {
148 try {
149 if (check(f)) {
150 if (f.isFile() && f.getName().endsWith(".jar")) {
151 addUrl(f.toURI().toURL());
152 }
153 }
154 } catch (MalformedURLException e) {
155 log.log(Level.WARNING, "Malformed path: {0}", f);
156 }
157 }
158
159
160
161
162
163
164
165
166 public synchronized void addJars(File f, boolean recursive) {
167 try {
168 if (!check(f)) {
169 return;
170 }
171
172 File[] contents = f.listFiles();
173 for (int i = 0; i < contents.length; i++) {
174 File ff = contents[i];
175 if (ff.getName().endsWith(".jar")) {
176 addUrl(ff.toURI().toURL());
177 } else if (recursive && ff.isDirectory()) {
178 addJars(ff, true);
179 }
180 }
181 } catch (MalformedURLException e) { }
182 }
183
184
185
186
187
188
189 public synchronized void addUrl(URL url) {
190 if (!classpath.contains(url)) {
191 log.log(Level.CONFIG, "Adding {0}", url);
192 classpath.add(url);
193 }
194 }
195
196
197
198
199
200
201
202 public synchronized void addWebApp(File f) {
203 if (!check(f) || !f.isDirectory()) {
204 return;
205 }
206
207 File webinf = new File(f, "WEB-INF");
208 if (webinf.exists()) {
209 File classes = new File(webinf, "classes");
210 if (classes.exists()) {
211 addDir(classes);
212 }
213
214 File libs = new File(webinf, "lib");
215 if (libs.exists()) {
216 addJars(libs, false);
217 }
218 }
219 }
220
221
222
223
224
225
226 public synchronized void addWar(File f) {
227 if (!check(f) || f.isDirectory() || !f.getName().endsWith(".war")) {
228 return;
229 }
230
231 try {
232 String urlBase = "jar:" + f.toURI().toURL() + "!/";
233
234 JarFile jf = new JarFile(f, true);
235 JarEntry entry = jf.getJarEntry("WEB-INF/classes");
236 if (entry != null) {
237 URL url = new URL(urlBase + "WEB-INF/classes/");
238 addUrl(url);
239 }
240
241 Enumeration enumeration = jf.entries();
242 while (enumeration.hasMoreElements()) {
243 entry = (JarEntry)enumeration.nextElement();
244 String entryName = entry.getName();
245 if (entryName.startsWith("WEB-INF/lib/") && entryName.endsWith(".jar")) {
246 URL url = new URL(urlBase + entryName);
247 addUrl(url);
248 }
249 }
250 } catch (IOException e) {
251 log.log(Level.WARNING, "Error reading {0}", f.getPath());
252 }
253 }
254
255
256
257
258
259
260
261 private boolean check(File f) {
262 if (f == null) {
263 return false;
264 } else if (!f.exists()) {
265 log.log(Level.CONFIG, "Path not found: {0}", f);
266 return false;
267 }
268 return true;
269 }
270
271
272
273
274
275
276 public synchronized ClassLoader getClassLoader() {
277 URL[] urls = new URL[classpath.size()];
278 classpath.toArray(urls);
279
280 return new URLClassLoader(urls, parent);
281 }
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297 public Object invoke(String clazz, String method, Class[] types, Object[] args)
298 throws Exception {
299 ClassLoader cl = getClassLoader();
300 Thread.currentThread().setContextClassLoader(cl);
301
302 Class c = cl.loadClass(clazz);
303 Method m = c.getMethod(method, types);
304 Object target = null;
305 if (!Modifier.isStatic(m.getModifiers())) {
306 target = c.newInstance();
307 }
308 return c.getMethod(method, types).invoke(target, args);
309 }
310 }