]> git.argeo.org Git - lgpl/argeo-commons.git/blob - Launcher.java
3dad955778a0ecbccdc0a49a1f9192167f1498e5
[lgpl/argeo-commons.git] / 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.Vector;
8
9 import org.eclipse.core.runtime.adaptor.EclipseStarter;
10 import org.osgi.framework.BundleContext;
11
12 public class Launcher {
13
14 public static void main(String[] args) {
15 // Try to load system properties
16 String systemPropertiesFilePath = OsgiBootUtils.getPropertyCompat(
17 OsgiBoot.PROP_ARGEO_OSGI_BOOT_SYSTEM_PROPERTIES_FILE,
18 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 String className = OsgiBootUtils.getPropertyCompat(
58 OsgiBoot.PROP_ARGEO_OSGI_BOOT_APPCLASS,
59 OsgiBoot.PROP_SLC_OSGIBOOT_APPCLASS);
60 if (className == null)
61 return;
62
63 // should use OsgiBootUtils.getPropertyCompat(), but it does not
64 // work for "" as default value
65 // so no warning displayed if PROP_SLC_OSGIBOOT_APPARGS is used
66 // FIXME: change OsgiBootUtils.getPropertyCompat()
67 String line = System.getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_APPARGS,
68 System.getProperty(OsgiBoot.PROP_SLC_OSGIBOOT_APPARGS, ""));
69
70 String[] uiArgs = readArgumentsFromLine(line);
71
72 try {
73 // Launch main method using reflection
74 Class clss = Class.forName(className);
75 Class[] mainArgsClasses = new Class[] { uiArgs.getClass() };
76 Object[] mainArgs = { uiArgs };
77 Method mainMethod = clss.getMethod("main", mainArgsClasses);
78 mainMethod.invoke(null, mainArgs);
79 } catch (Exception e) {
80 throw new RuntimeException("Cannot start main class.", e);
81 }
82
83 }
84
85 /**
86 * Transform a line into an array of arguments, taking "" as single
87 * arguments. (nested \" are not supported)
88 */
89 private static String[] readArgumentsFromLine(String lineOrig) {
90
91 String line = lineOrig.trim();// remove trailing spaces
92 // System.out.println("line=" + line);
93 List args = new Vector();
94 StringBuffer curr = new StringBuffer("");
95 boolean inQuote = false;
96 char[] arr = line.toCharArray();
97 for (int i = 0; i < arr.length; i++) {
98 char c = arr[i];
99 switch (c) {
100 case '\"':
101 inQuote = !inQuote;
102 break;
103 case ' ':
104 if (!inQuote) {// otherwise, no break: goes to default
105 if (curr.length() > 0) {
106 args.add(curr.toString());
107 curr = new StringBuffer("");
108 }
109 break;
110 }
111 default:
112 curr.append(c);
113 break;
114 }
115 }
116
117 // Add last arg
118 if (curr.length() > 0) {
119 args.add(curr.toString());
120 curr = null;
121 }
122
123 String[] res = new String[args.size()];
124 for (int i = 0; i < args.size(); i++) {
125 res[i] = args.get(i).toString();
126 // System.out.println("res[i]=" + res[i]);
127 }
128 return res;
129 }
130
131 }