]> git.argeo.org Git - lgpl/argeo-commons.git/blob - osgi/runtime/org.argeo.osgi.boot/src/main/java/org/argeo/osgi/boot/Launcher.java
Slightly modify the class to enable easy introduction of sorter on column headers...
[lgpl/argeo-commons.git] / osgi / runtime / org.argeo.osgi.boot / src / main / java / org / argeo / osgi / boot / Launcher.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.argeo.osgi.boot;
18
19 import java.io.FileInputStream;
20 import java.io.IOException;
21 import java.lang.reflect.Method;
22 import java.util.List;
23 import java.util.Vector;
24
25 import org.eclipse.core.runtime.adaptor.EclipseStarter;
26 import org.osgi.framework.BundleContext;
27
28 public class Launcher {
29
30 public static void main(String[] args) {
31 // Try to load system properties
32 String systemPropertiesFilePath = OsgiBootUtils.getPropertyCompat(
33 OsgiBoot.PROP_ARGEO_OSGI_BOOT_SYSTEM_PROPERTIES_FILE,
34 OsgiBoot.PROP_SLC_OSGIBOOT_SYSTEM_PROPERTIES_FILE);
35 if (systemPropertiesFilePath != null) {
36 FileInputStream in;
37 try {
38 in = new FileInputStream(systemPropertiesFilePath);
39 System.getProperties().load(in);
40 } catch (IOException e1) {
41 throw new RuntimeException(
42 "Cannot load system properties from "
43 + systemPropertiesFilePath, e1);
44 }
45 if (in != null) {
46 try {
47 in.close();
48 } catch (Exception e) {
49 // silent
50 }
51 }
52 }
53
54 // Start main class
55 startMainClass();
56
57 // Start Equinox
58 BundleContext bundleContext = null;
59 try {
60 bundleContext = EclipseStarter.startup(args, null);
61 } catch (Exception e) {
62 throw new RuntimeException("Cannot start Equinox.", e);
63 }
64
65 // OSGi bootstrap
66 OsgiBoot osgiBoot = new OsgiBoot(bundleContext);
67 osgiBoot.bootstrap();
68 }
69
70 protected static void startMainClass() {
71 // Properties config = System.getProperties();
72 // String className = config.getProperty("slc.osgiboot.appclass");
73 String className = OsgiBootUtils.getPropertyCompat(
74 OsgiBoot.PROP_ARGEO_OSGI_BOOT_APPCLASS,
75 OsgiBoot.PROP_SLC_OSGIBOOT_APPCLASS);
76 if (className == null)
77 return;
78
79 // should use OsgiBootUtils.getPropertyCompat(), but it does not
80 // work for "" as default value
81 // so no warning displayed if PROP_SLC_OSGIBOOT_APPARGS is used
82 // FIXME: change OsgiBootUtils.getPropertyCompat()
83 String line = System.getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_APPARGS,
84 System.getProperty(OsgiBoot.PROP_SLC_OSGIBOOT_APPARGS, ""));
85
86 String[] uiArgs = readArgumentsFromLine(line);
87
88 try {
89 // Launch main method using reflection
90 Class clss = Class.forName(className);
91 Class[] mainArgsClasses = new Class[] { uiArgs.getClass() };
92 Object[] mainArgs = { uiArgs };
93 Method mainMethod = clss.getMethod("main", mainArgsClasses);
94 mainMethod.invoke(null, mainArgs);
95 } catch (Exception e) {
96 throw new RuntimeException("Cannot start main class.", e);
97 }
98
99 }
100
101 /**
102 * Transform a line into an array of arguments, taking "" as single
103 * arguments. (nested \" are not supported)
104 */
105 private static String[] readArgumentsFromLine(String lineOrig) {
106
107 String line = lineOrig.trim();// remove trailing spaces
108 // System.out.println("line=" + line);
109 List args = new Vector();
110 StringBuffer curr = new StringBuffer("");
111 boolean inQuote = false;
112 char[] arr = line.toCharArray();
113 for (int i = 0; i < arr.length; i++) {
114 char c = arr[i];
115 switch (c) {
116 case '\"':
117 inQuote = !inQuote;
118 break;
119 case ' ':
120 if (!inQuote) {// otherwise, no break: goes to default
121 if (curr.length() > 0) {
122 args.add(curr.toString());
123 curr = new StringBuffer("");
124 }
125 break;
126 }
127 default:
128 curr.append(c);
129 break;
130 }
131 }
132
133 // Add last arg
134 if (curr.length() > 0) {
135 args.add(curr.toString());
136 curr = null;
137 }
138
139 String[] res = new String[args.size()];
140 for (int i = 0; i < args.size(); i++) {
141 res[i] = args.get(i).toString();
142 // System.out.println("res[i]=" + res[i]);
143 }
144 return res;
145 }
146
147 }