]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.osgiboot/src/main/java/org/argeo/slc/osgiboot/Launcher.java
Improve OSGI Boot logging
[gpl/argeo-slc.git] / runtime / org.argeo.slc.osgiboot / src / main / java / org / argeo / slc / osgiboot / Launcher.java
1 package org.argeo.slc.osgiboot;
2
3 import java.io.FileInputStream;
4 import java.io.IOException;
5 import java.lang.reflect.Method;
6 import java.util.List;
7 import java.util.Properties;
8 import java.util.Vector;
9
10 import org.eclipse.core.runtime.adaptor.EclipseStarter;
11 import org.osgi.framework.BundleContext;
12
13 public class Launcher {
14
15 public static void main(String[] args) {
16 // Try to load system properties
17 String systemPropertiesFilePath = System
18 .getProperty(OsgiBoot.PROP_SLC_OSGIBOOT_SYSTEM_PROPERTIES_FILE);
19 if (systemPropertiesFilePath != null) {
20 FileInputStream in;
21 try {
22 in = new FileInputStream(systemPropertiesFilePath);
23 System.getProperties().load(in);
24 } catch (IOException e1) {
25 throw new RuntimeException(
26 "Cannot load system properties from "
27 + systemPropertiesFilePath, e1);
28 }
29 if (in != null) {
30 try {
31 in.close();
32 } catch (Exception e) {
33 // silent
34 }
35 }
36 }
37
38 // Start main class
39 startMainClass();
40
41 // Start Equinox
42 BundleContext bundleContext = null;
43 try {
44 bundleContext = EclipseStarter.startup(args, null);
45 } catch (Exception e) {
46 throw new RuntimeException("Cannot start Equinox.", e);
47 }
48
49 // OSGi bootstrap
50 OsgiBoot osgiBoot = new OsgiBoot(bundleContext);
51 osgiBoot.bootstrap();
52 }
53
54 protected static void startMainClass() {
55 Properties config = System.getProperties();
56 String className = config.getProperty("slc.osgiboot.appclass");
57 if (className == null)
58 return;
59
60 String[] uiArgs = readArgumentsFromLine(config.getProperty(
61 "slc.osgiboot.appargs", ""));
62 try {
63 // Launch main method using reflection
64 Class clss = Class.forName(className);
65 Class[] mainArgsClasses = new Class[] { uiArgs.getClass() };
66 Object[] mainArgs = { uiArgs };
67 Method mainMethod = clss.getMethod("main", mainArgsClasses);
68 mainMethod.invoke(null, mainArgs);
69 } catch (Exception e) {
70 throw new RuntimeException("Cannot start main class.", e);
71 }
72
73 }
74
75 /**
76 * Transform a line into an array of arguments, taking "" as single
77 * arguments. (nested \" are not supported)
78 */
79 private static String[] readArgumentsFromLine(String lineOrig) {
80
81 String line = lineOrig.trim();// remove trailing spaces
82 // System.out.println("line=" + line);
83 List args = new Vector();
84 StringBuffer curr = new StringBuffer("");
85 boolean inQuote = false;
86 char[] arr = line.toCharArray();
87 for (int i = 0; i < arr.length; i++) {
88 char c = arr[i];
89 switch (c) {
90 case '\"':
91 inQuote = !inQuote;
92 break;
93 case ' ':
94 if (!inQuote) {// otherwise, no break: goes to default
95 if (curr.length() > 0) {
96 args.add(curr.toString());
97 curr = new StringBuffer("");
98 }
99 break;
100 }
101 default:
102 curr.append(c);
103 break;
104 }
105 }
106
107 // Add last arg
108 if (curr.length() > 0) {
109 args.add(curr.toString());
110 curr = null;
111 }
112
113 String[] res = new String[args.size()];
114 for (int i = 0; i < args.size(); i++) {
115 res[i] = args.get(i).toString();
116 // System.out.println("res[i]=" + res[i]);
117 }
118 return res;
119 }
120
121 }