]> git.argeo.org Git - lgpl/argeo-commons.git/blob - base/runtime/org.argeo.osgi.boot/src/main/java/org/argeo/osgi/boot/Launcher.java
Do not force install of jars as file: instead of reference:file:
[lgpl/argeo-commons.git] / base / runtime / org.argeo.osgi.boot / src / main / java / org / argeo / osgi / boot / Launcher.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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 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.List;
22 import java.util.Vector;
23
24 import org.eclipse.core.runtime.adaptor.EclipseStarter;
25 import org.osgi.framework.BundleContext;
26
27 /** Command line interface. */
28 public class Launcher {
29
30 public static void main(String[] args) {
31 // Try to load system properties
32 String systemPropertiesFilePath = OsgiBootUtils
33 .getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_SYSTEM_PROPERTIES_FILE);
34 if (systemPropertiesFilePath != null) {
35 FileInputStream in;
36 try {
37 in = new FileInputStream(systemPropertiesFilePath);
38 System.getProperties().load(in);
39 } catch (IOException e1) {
40 throw new RuntimeException(
41 "Cannot load system properties from "
42 + systemPropertiesFilePath, e1);
43 }
44 if (in != null) {
45 try {
46 in.close();
47 } catch (Exception e) {
48 // silent
49 }
50 }
51 }
52
53 // Start main class
54 startMainClass();
55
56 // Start Equinox
57 BundleContext bundleContext = null;
58 try {
59 bundleContext = EclipseStarter.startup(args, null);
60 } catch (Exception e) {
61 throw new RuntimeException("Cannot start Equinox.", e);
62 }
63
64 // OSGi bootstrap
65 OsgiBoot osgiBoot = new OsgiBoot(bundleContext);
66 osgiBoot.bootstrap();
67 }
68
69 protected static void startMainClass() {
70 String className = OsgiBootUtils
71 .getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_APPCLASS);
72 if (className == null)
73 return;
74
75 String line = System.getProperty(OsgiBoot.PROP_ARGEO_OSGI_BOOT_APPARGS,
76 "");
77
78 String[] uiArgs = readArgumentsFromLine(line);
79
80 try {
81 // Launch main method using reflection
82 Class clss = Class.forName(className);
83 Class[] mainArgsClasses = new Class[] { uiArgs.getClass() };
84 Object[] mainArgs = { uiArgs };
85 Method mainMethod = clss.getMethod("main", mainArgsClasses);
86 mainMethod.invoke(null, mainArgs);
87 } catch (Exception e) {
88 throw new RuntimeException("Cannot start main class.", e);
89 }
90
91 }
92
93 /**
94 * Transform a line into an array of arguments, taking "" as single
95 * arguments. (nested \" are not supported)
96 */
97 private static String[] readArgumentsFromLine(String lineOrig) {
98 String line = lineOrig.trim();// remove trailing spaces
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 }
133 return res;
134 }
135
136 }