1 package org
.argeo
.init
.osgi
;
3 import java
.io
.FileInputStream
;
4 import java
.io
.IOException
;
5 import java
.lang
.reflect
.Method
;
6 import java
.util
.ArrayList
;
9 import org
.osgi
.framework
.BundleContext
;
11 /** An OSGi launcher executing first another class in the system class path. */
12 public class Launcher
{
14 public static void main(String
[] args
) {
15 // Try to load system properties
16 String systemPropertiesFilePath
= getProperty(OsgiBoot
.PROP_ARGEO_OSGI_BOOT_SYSTEM_PROPERTIES_FILE
);
17 if (systemPropertiesFilePath
!= null) {
20 in
= new FileInputStream(systemPropertiesFilePath
);
21 System
.getProperties().load(in
);
22 } catch (IOException e1
) {
23 throw new RuntimeException("Cannot load system properties from " + systemPropertiesFilePath
, e1
);
28 } catch (Exception e
) {
38 BundleContext bundleContext
= null;
40 bundleContext
= OsgiBootUtils
.launch(OsgiBootUtils
.equinoxArgsToConfiguration(args
)).getBundleContext();
41 } catch (Exception e
) {
42 throw new RuntimeException("Cannot start Equinox.", e
);
46 OsgiBoot osgiBoot
= new OsgiBoot(bundleContext
);
50 protected static void startMainClass() {
51 String className
= getProperty(OsgiBoot
.PROP_ARGEO_OSGI_BOOT_APPCLASS
);
52 if (className
== null)
55 String line
= System
.getProperty(OsgiBoot
.PROP_ARGEO_OSGI_BOOT_APPARGS
, "");
57 String
[] uiArgs
= readArgumentsFromLine(line
);
60 // Launch main method using reflection
61 Class
<?
> clss
= Class
.forName(className
);
62 Class
<?
>[] mainArgsClasses
= new Class
[] { uiArgs
.getClass() };
63 Object
[] mainArgs
= { uiArgs
};
64 Method mainMethod
= clss
.getMethod("main", mainArgsClasses
);
65 mainMethod
.invoke(null, mainArgs
);
66 } catch (Exception e
) {
67 throw new RuntimeException("Cannot start main class.", e
);
73 * Transform a line into an array of arguments, taking "" as single arguments.
74 * (nested \" are not supported)
76 private static String
[] readArgumentsFromLine(String lineOrig
) {
77 String line
= lineOrig
.trim();// remove trailing spaces
78 List
<String
> args
= new ArrayList
<String
>();
79 StringBuffer curr
= new StringBuffer("");
80 boolean inQuote
= false;
81 char[] arr
= line
.toCharArray();
82 for (int i
= 0; i
< arr
.length
; i
++) {
89 if (!inQuote
) {// otherwise, no break: goes to default
90 if (curr
.length() > 0) {
91 args
.add(curr
.toString());
92 curr
= new StringBuffer("");
103 if (curr
.length() > 0) {
104 args
.add(curr
.toString());
108 String
[] res
= new String
[args
.size()];
109 for (int i
= 0; i
< args
.size(); i
++) {
110 res
[i
] = args
.get(i
).toString();
115 public static String
getProperty(String name
, String defaultValue
) {
117 if (defaultValue
!= null)
118 value
= System
.getProperty(name
, defaultValue
);
120 value
= System
.getProperty(name
);
122 if (value
== null || value
.equals(""))
128 public static String
getProperty(String name
) {
129 return getProperty(name
, null);