2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.osgi
.boot
;
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
;
24 import org
.eclipse
.core
.runtime
.adaptor
.EclipseStarter
;
25 import org
.osgi
.framework
.BundleContext
;
27 /** Command line interface. */
28 public class Launcher
{
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) {
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
);
47 } catch (Exception e
) {
57 BundleContext bundleContext
= null;
59 bundleContext
= EclipseStarter
.startup(args
, null);
60 } catch (Exception e
) {
61 throw new RuntimeException("Cannot start Equinox.", e
);
65 OsgiBoot osgiBoot
= new OsgiBoot(bundleContext
);
69 protected static void startMainClass() {
70 String className
= OsgiBootUtils
71 .getProperty(OsgiBoot
.PROP_ARGEO_OSGI_BOOT_APPCLASS
);
72 if (className
== null)
75 String line
= System
.getProperty(OsgiBoot
.PROP_ARGEO_OSGI_BOOT_APPARGS
,
78 String
[] uiArgs
= readArgumentsFromLine(line
);
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
);
94 * Transform a line into an array of arguments, taking "" as single
95 * arguments. (nested \" are not supported)
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
++) {
110 if (!inQuote
) {// otherwise, no break: goes to default
111 if (curr
.length() > 0) {
112 args
.add(curr
.toString());
113 curr
= new StringBuffer("");
124 if (curr
.length() > 0) {
125 args
.add(curr
.toString());
129 String
[] res
= new String
[args
.size()];
130 for (int i
= 0; i
< args
.size(); i
++) {
131 res
[i
] = args
.get(i
).toString();