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