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