1 package org
.argeo
.osgi
.boot
;
3 import java
.io
.FileInputStream
;
4 import java
.io
.IOException
;
5 import java
.lang
.reflect
.Method
;
6 import java
.util
.ArrayList
;
9 import org
.eclipse
.core
.runtime
.adaptor
.EclipseStarter
;
10 import org
.osgi
.framework
.BundleContext
;
12 /** Command line interface. */
13 public class Launcher
{
15 public static void main(String
[] args
) {
16 // Try to load system properties
17 String systemPropertiesFilePath
= getProperty(OsgiBoot
.PROP_ARGEO_OSGI_BOOT_SYSTEM_PROPERTIES_FILE
);
18 if (systemPropertiesFilePath
!= null) {
21 in
= new FileInputStream(systemPropertiesFilePath
);
22 System
.getProperties().load(in
);
23 } catch (IOException e1
) {
24 throw new RuntimeException("Cannot load system properties from " + systemPropertiesFilePath
, e1
);
29 } catch (Exception e
) {
39 BundleContext bundleContext
= null;
41 bundleContext
= EclipseStarter
.startup(args
, null);
42 } catch (Exception e
) {
43 throw new RuntimeException("Cannot start Equinox.", e
);
47 OsgiBoot osgiBoot
= new OsgiBoot(bundleContext
);
51 protected static void startMainClass() {
52 String className
= getProperty(OsgiBoot
.PROP_ARGEO_OSGI_BOOT_APPCLASS
);
53 if (className
== null)
56 String line
= System
.getProperty(OsgiBoot
.PROP_ARGEO_OSGI_BOOT_APPARGS
, "");
58 String
[] uiArgs
= readArgumentsFromLine(line
);
61 // Launch main method using reflection
62 Class
<?
> clss
= Class
.forName(className
);
63 Class
<?
>[] mainArgsClasses
= new Class
[] { uiArgs
.getClass() };
64 Object
[] mainArgs
= { uiArgs
};
65 Method mainMethod
= clss
.getMethod("main", mainArgsClasses
);
66 mainMethod
.invoke(null, mainArgs
);
67 } catch (Exception e
) {
68 throw new RuntimeException("Cannot start main class.", e
);
74 * Transform a line into an array of arguments, taking "" as single
75 * arguments. (nested \" are not supported)
77 private static String
[] readArgumentsFromLine(String lineOrig
) {
78 String line
= lineOrig
.trim();// remove trailing spaces
79 List
<String
> args
= new ArrayList
<String
>();
80 StringBuffer curr
= new StringBuffer("");
81 boolean inQuote
= false;
82 char[] arr
= line
.toCharArray();
83 for (int i
= 0; i
< arr
.length
; i
++) {
90 if (!inQuote
) {// otherwise, no break: goes to default
91 if (curr
.length() > 0) {
92 args
.add(curr
.toString());
93 curr
= new StringBuffer("");
104 if (curr
.length() > 0) {
105 args
.add(curr
.toString());
109 String
[] res
= new String
[args
.size()];
110 for (int i
= 0; i
< args
.size(); i
++) {
111 res
[i
] = args
.get(i
).toString();
116 public static String
getProperty(String name
, String defaultValue
) {
118 if (defaultValue
!= null)
119 value
= System
.getProperty(name
, defaultValue
);
121 value
= System
.getProperty(name
);
123 if (value
== null || value
.equals(""))
129 public static String
getProperty(String name
) {
130 return getProperty(name
, null);