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