]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.osgiboot/src/main/java/org/argeo/slc/osgiboot/Launcher.java
remove empty packages
[gpl/argeo-slc.git] / runtime / org.argeo.slc.osgiboot / src / main / java / org / argeo / slc / osgiboot / 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.slc.osgiboot;
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.Properties;
24 import java.util.Vector;
25
26 import org.eclipse.core.runtime.adaptor.EclipseStarter;
27 import org.osgi.framework.BundleContext;
28
29 public class Launcher {
30
31 public static void main(String[] args) {
32 // Try to load system properties
33 String systemPropertiesFilePath = System
34 .getProperty(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 if (className == null)
74 return;
75
76 String[] uiArgs = readArgumentsFromLine(config.getProperty(
77 "slc.osgiboot.appargs", ""));
78 try {
79 // Launch main method using reflection
80 Class clss = Class.forName(className);
81 Class[] mainArgsClasses = new Class[] { uiArgs.getClass() };
82 Object[] mainArgs = { uiArgs };
83 Method mainMethod = clss.getMethod("main", mainArgsClasses);
84 mainMethod.invoke(null, mainArgs);
85 } catch (Exception e) {
86 throw new RuntimeException("Cannot start main class.", e);
87 }
88
89 }
90
91 /**
92 * Transform a line into an array of arguments, taking "" as single
93 * arguments. (nested \" are not supported)
94 */
95 private static String[] readArgumentsFromLine(String lineOrig) {
96
97 String line = lineOrig.trim();// remove trailing spaces
98 // System.out.println("line=" + line);
99 List args = new Vector();
100 StringBuffer curr = new StringBuffer("");
101 boolean inQuote = false;
102 char[] arr = line.toCharArray();
103 for (int i = 0; i < arr.length; i++) {
104 char c = arr[i];
105 switch (c) {
106 case '\"':
107 inQuote = !inQuote;
108 break;
109 case ' ':
110 if (!inQuote) {// otherwise, no break: goes to default
111 if (curr.length() > 0) {
112 args.add(curr.toString());
113 curr = new StringBuffer("");
114 }
115 break;
116 }
117 default:
118 curr.append(c);
119 break;
120 }
121 }
122
123 // Add last arg
124 if (curr.length() > 0) {
125 args.add(curr.toString());
126 curr = null;
127 }
128
129 String[] res = new String[args.size()];
130 for (int i = 0; i < args.size(); i++) {
131 res[i] = args.get(i).toString();
132 // System.out.println("res[i]=" + res[i]);
133 }
134 return res;
135 }
136
137 }