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