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.FileInputStream;
21 import java.io.IOException;
22 import java.lang.reflect.InvocationTargetException;
23 import java.net.MalformedURLException;
24 import java.net.URL;
25 import java.util.Iterator;
26 import java.util.Map;
27 import java.util.Properties;
28 import java.util.Stack;
29
30 import javax.xml.parsers.ParserConfigurationException;
31 import javax.xml.parsers.SAXParser;
32 import javax.xml.parsers.SAXParserFactory;
33
34 import org.xml.sax.Attributes;
35 import org.xml.sax.InputSource;
36 import org.xml.sax.SAXException;
37 import org.xml.sax.SAXParseException;
38 import org.xml.sax.helpers.DefaultHandler;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138 public class Launcher extends DefaultHandler {
139
140
141
142 private CLBuilder clb = new CLBuilder();
143
144
145
146
147 private File baseDir = getBaseDir();
148
149
150
151
152 private Stack context = new Stack();
153
154
155
156
157 private String className = null;
158
159
160
161
162 private String methodName = "main";
163
164
165
166
167 public Launcher() {
168 }
169
170
171
172
173
174
175
176
177
178
179 public void launch(String[] args) throws LaunchException {
180 ClassLoader cl = clb.getClassLoader();
181 Thread.currentThread().setContextClassLoader(cl);
182 try {
183 Class c = cl.loadClass(className);
184 Class[] types = { String[].class };
185 Object[] params = { args };
186 c.getMethod(methodName, types).invoke(null, params);
187 } catch (ClassNotFoundException cnfe) {
188 throw new LaunchException("Class " + className + " not found", cnfe);
189 } catch (NoSuchMethodException nsme) {
190 throw new LaunchException("Method main(String[]) not found in " + className, nsme);
191 } catch (IllegalAccessException iae) {
192 throw new LaunchException("Method main(String[]) not public in " + className, iae);
193 } catch (InvocationTargetException ite) {
194 Throwable t = ite.getCause();
195 throw new LaunchException("Unhandled exception: " + t.getMessage(), t);
196 }
197 }
198
199
200
201
202
203
204 public void setClassName(String className) {
205 this.className = className;
206 }
207
208
209
210
211
212
213
214
215
216 public void setMethodName(String methodName) {
217 this.methodName = methodName == null ? "main" : methodName;
218 }
219
220
221
222
223
224
225
226
227
228 public File getBaseDir() {
229 if (this.baseDir == null) {
230 String base = System.getProperty("launcher.basedir");
231 if (base != null) {
232 this.baseDir = new File(base);
233 } else {
234 String resName = System.getProperty("launcher.file", "launcher.xml");
235 File f = new File(resName);
236 if (f.exists()) {
237 this.baseDir = f.getParentFile();
238 } else {
239 this.baseDir = new File(".");
240 }
241 }
242 }
243 return this.baseDir;
244 }
245
246
247
248
249
250
251
252 public void setBaseDir(File dir) {
253 this.baseDir = dir;
254 }
255
256
257
258
259
260
261 public void doInherit() {
262 clb.setParent(Thread.currentThread().getContextClassLoader());
263 }
264
265
266
267
268
269
270 public void addDir(String path) {
271 clb.addDir(getFile(path));
272 }
273
274
275
276
277
278
279 public void addFile(String path) {
280 clb.addFile(getFile(path));
281 }
282
283
284
285
286
287
288
289
290
291
292 public void addJars(String path, boolean recursive) {
293 clb.addJars(getFile(path), recursive);
294 }
295
296
297
298
299
300
301
302 public void addUrl(String surl) throws SAXException {
303 try {
304 URL url = new URL(surl);
305 clb.addUrl(url);
306 } catch (Exception e) {
307 throw new SAXException("Malformed URL: " + surl, e);
308 }
309 }
310
311
312
313
314
315
316 public void addUrl(URL url) {
317 clb.addUrl(url);
318 }
319
320
321
322
323
324
325
326
327
328
329
330
331
332 public void addWebApp(String path) {
333 clb.addWebApp(getFile(path));
334 }
335
336
337
338
339
340
341 public void addWar(String path) {
342 clb.addWar(getFile(path));
343 }
344
345
346
347
348
349
350
351
352 public void loadProperties(String path) throws IOException {
353 File f = getFile(path);
354 if (f.exists() && f.isFile()) {
355 FileInputStream fis = null;
356 try {
357 fis = new FileInputStream(f);
358 Properties p = new Properties();
359 p.load(fis);
360
361 for (Iterator it = p.entrySet().iterator(); it.hasNext();) {
362 Map.Entry entry = (Map.Entry)it.next();
363 System.setProperty((String)entry.getKey(), (String)entry.getValue());
364 }
365 } finally {
366 try {
367 fis.close();
368 } catch (Exception ignore) {
369 }
370 }
371 }
372 }
373
374
375
376
377
378
379 public static void main(String[] args) {
380 URL descriptor = getDescriptorUrl();
381
382 if (descriptor == null) {
383 System.err.println("FATAL: Launcher descriptor not found");
384 System.exit(1);
385 }
386
387 Launcher launcher = new Launcher();
388
389 try {
390
391 String driver = System.getProperty("org.xml.sax.driver");
392 if (driver == null) {
393 driver = "org.apache.crimson.parser.XMLReaderImpl";
394 }
395
396
397 SAXParserFactory factory = SAXParserFactory.newInstance();
398 factory.setValidating(true);
399 SAXParser parser = factory.newSAXParser();
400
401
402 parser.parse(descriptor.openStream(), launcher);
403 } catch (IOException ie) {
404 System.err.println("Error reading descriptor:");
405 ie.printStackTrace();
406 System.exit(1);
407 } catch (SAXException e) {
408 System.err.println("Error parsing descriptor:");
409 e.printStackTrace();
410 System.exit(1);
411 } catch (ParserConfigurationException pce) {
412 System.err.println("Parser configuration exception: ");
413 pce.printStackTrace();
414 System.exit(1);
415 }
416
417
418 try {
419 launcher.launch(args);
420 } catch (LaunchException e) {
421 System.err.println("FATAL: " + e.getMessage());
422 e.printStackTrace();
423 System.exit(1);
424 }
425 }
426
427
428
429
430
431
432 private static URL getDescriptorUrl() {
433 String resName = System.getProperty("launcher.file", "launcher.xml");
434
435
436 try {
437 return new URL(resName);
438 } catch (MalformedURLException e) {
439 }
440
441
442 File f = new File(resName);
443 if (f.exists()) {
444 try {
445 return f.toURI().toURL();
446 } catch (Exception e) {
447 }
448 }
449
450
451
452
453 return ClassLoader.getSystemResource(resName);
454 }
455
456
457
458
459
460
461
462
463
464
465
466 private File getFile(String path) {
467
468 path = parse(path);
469
470 if (path.equals("JDK_TOOLS")) {
471 String jrePath = System.getProperty("java.home");
472 File f = new File(jrePath, "../lib/tools.jar");
473 return f;
474 } else {
475 File f = new File(path);
476 if (!f.isAbsolute()) {
477 f = new File(baseDir, path);
478 }
479 return f;
480 }
481 }
482
483
484
485
486
487
488
489
490
491 private String parse(String s) {
492 int pos = s.indexOf("${");
493 while (pos != -1) {
494 int pos2 = s.indexOf("}", pos + 2);
495 if (pos2 != -1) {
496 String name = s.substring(pos + 2, pos2);
497 String value = System.getProperty(name);
498 if (value != null) {
499 s = s.substring(0, pos) + value + s.substring(pos2 + 1);
500 }
501 } else {
502 break;
503 }
504 }
505 return s;
506 }
507
508
509
510
511
512
513
514
515 public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
516 if ("-//GridSystems//launcher".equals(publicId)
517 || "http://www.gridsystems.com/dtds/launcher.dtd".equals(systemId)) {
518 try {
519 URL url = getClass().getResource("launcher.dtd");
520 return new InputSource(url.openStream());
521 } catch (IOException e) {
522 throw new SAXException("I/O Error", e);
523 }
524 }
525 return null;
526 }
527
528
529
530
531
532
533
534
535 public void endElement(String namespaceURI, String localName, String qName)
536 throws SAXException {
537 context.pop();
538 }
539
540
541
542
543 public void startElement(String ns, String localName, String qName, Attributes atts)
544 throws SAXException {
545 String ctx = (context.size() == 0) ? null : (String)context.peek();
546
547 if ("".equals(localName)) {
548 localName = qName;
549 }
550
551 if ("launcher".equals(ctx)) {
552 parseLauncher(localName, atts);
553 } else if ("application".equals(ctx)) {
554 parseApplication(localName, atts);
555 } else if ("classpath".equals(ctx)) {
556 parseClasspath(localName, atts);
557 }
558
559 context.push(localName);
560 }
561
562
563
564
565
566
567
568 private void parseLauncher(String localName, Attributes atts) {
569 if ("classpath".equals(localName)) {
570 String inherit = atts.getValue("inherit");
571 if ("true".equals(inherit)) {
572 doInherit();
573 }
574 } else if ("application".equals(localName)) {
575 setClassName(atts.getValue("class"));
576
577 setMethodName(atts.getValue("method"));
578 }
579 }
580
581
582
583
584
585
586 private void parseApplication(String localName, Attributes atts) {
587 if ("property".equals(localName)) {
588 String path = atts.getValue("file");
589 if (path != null) {
590 try {
591 loadProperties(path);
592 } catch (IOException e) {
593
594 }
595 } else {
596 String name = atts.getValue("name");
597 String value = atts.getValue("value");
598 if (value == null) {
599 value = getFile(atts.getValue("path")).getPath();
600 }
601 System.setProperty(name, value);
602 }
603 }
604 }
605
606
607
608
609
610
611
612 private void parseClasspath(String localName, Attributes atts) throws SAXException {
613 if ("include".equals(localName)) {
614 String flag = atts.getValue("recursive");
615 boolean recursive = "true".equals(flag);
616
617 for (int i = 0; i < atts.getLength(); i++) {
618 String attrName = atts.getLocalName(i);
619 if ("".equals(attrName)) {
620 attrName = atts.getQName(i);
621 }
622
623 String attrValue = atts.getValue(i);
624 if ("dir".equals(attrName)) {
625 addDir(attrValue);
626 } else if ("file".equals(attrName)) {
627 addFile(attrValue);
628 } else if ("jars".equals(attrName)) {
629 addJars(attrValue, recursive);
630 } else if ("url".equals(attrName)) {
631 addUrl(attrValue);
632 } else if ("war".equals(attrName)) {
633 addWar(attrValue);
634 } else if ("webapp".equals(attrName)) {
635 addWebApp(attrValue);
636 }
637 }
638 }
639 }
640
641
642
643
644
645
646
647
648 public void error(SAXParseException e) throws SAXException {
649 throw e;
650 }
651
652
653
654
655 public void fatalError(SAXParseException e) throws SAXException {
656 throw e;
657 }
658
659
660
661
662 public void warning(SAXParseException err) throws SAXException {
663 System.out.println("WARNING: line " + err.getLineNumber()
664 + ", uri " + err.getSystemId());
665 System.out.println(" - " + err.getMessage());
666 }
667 }