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
.ArrayList
;
22 import java
.util
.List
;
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
= getProperty(OsgiBoot
.PROP_ARGEO_OSGI_BOOT_SYSTEM_PROPERTIES_FILE
);
33 if (systemPropertiesFilePath
!= null) {
36 in
= new FileInputStream(systemPropertiesFilePath
);
37 System
.getProperties().load(in
);
38 } catch (IOException e1
) {
39 throw new RuntimeException("Cannot load system properties from " + systemPropertiesFilePath
, e1
);
44 } catch (Exception e
) {
54 BundleContext bundleContext
= null;
56 bundleContext
= EclipseStarter
.startup(args
, null);
57 } catch (Exception e
) {
58 throw new RuntimeException("Cannot start Equinox.", e
);
62 OsgiBoot osgiBoot
= new OsgiBoot(bundleContext
);
66 protected static void startMainClass() {
67 String className
= getProperty(OsgiBoot
.PROP_ARGEO_OSGI_BOOT_APPCLASS
);
68 if (className
== null)
71 String line
= System
.getProperty(OsgiBoot
.PROP_ARGEO_OSGI_BOOT_APPARGS
, "");
73 String
[] uiArgs
= readArgumentsFromLine(line
);
76 // Launch main method using reflection
77 Class
<?
> clss
= Class
.forName(className
);
78 Class
<?
>[] mainArgsClasses
= new Class
[] { uiArgs
.getClass() };
79 Object
[] mainArgs
= { uiArgs
};
80 Method mainMethod
= clss
.getMethod("main", mainArgsClasses
);
81 mainMethod
.invoke(null, mainArgs
);
82 } catch (Exception e
) {
83 throw new RuntimeException("Cannot start main class.", e
);
89 * Transform a line into an array of arguments, taking "" as single
90 * arguments. (nested \" are not supported)
92 private static String
[] readArgumentsFromLine(String lineOrig
) {
93 String line
= lineOrig
.trim();// remove trailing spaces
94 List
<String
> args
= new ArrayList
<String
>();
95 StringBuffer curr
= new StringBuffer("");
96 boolean inQuote
= false;
97 char[] arr
= line
.toCharArray();
98 for (int i
= 0; i
< arr
.length
; i
++) {
105 if (!inQuote
) {// otherwise, no break: goes to default
106 if (curr
.length() > 0) {
107 args
.add(curr
.toString());
108 curr
= new StringBuffer("");
119 if (curr
.length() > 0) {
120 args
.add(curr
.toString());
124 String
[] res
= new String
[args
.size()];
125 for (int i
= 0; i
< args
.size(); i
++) {
126 res
[i
] = args
.get(i
).toString();
131 public static String
getProperty(String name
, String defaultValue
) {
133 if (defaultValue
!= null)
134 value
= System
.getProperty(name
, defaultValue
);
136 value
= System
.getProperty(name
);
138 if (value
== null || value
.equals(""))
144 public static String
getProperty(String name
) {
145 return getProperty(name
, null);