1 package org
.argeo
.slc
.autoui
.launcher
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.lang
.reflect
.Method
;
7 import java
.util
.ArrayList
;
8 import java
.util
.Enumeration
;
10 import java
.util
.Properties
;
11 import java
.util
.Vector
;
13 import org
.apache
.felix
.framework
.Felix
;
14 import org
.apache
.felix
.framework
.cache
.BundleCache
;
15 import org
.apache
.felix
.main
.AutoActivator
;
16 import org
.argeo
.slc
.autoui
.AutoUiActivator
;
17 import org
.argeo
.slc
.autoui
.DetachedStep
;
18 import org
.osgi
.framework
.BundleContext
;
19 import org
.osgi
.framework
.ServiceReference
;
23 public static void main(String
[] args
) {
26 Properties config
= prepareConfig();
28 // Start UI (in main class loader)
32 Felix felix
= startSystem(config
);
35 automateUi(felix
.getBundleContext());
38 } catch (Exception e
) {
44 protected static Properties
prepareConfig() throws Exception
{
45 final File cachedir
= createTemporaryCacheDir();
48 Properties config
= new Properties();
49 InputStream in
= null;
53 .getResourceAsStream("/org/argeo/slc/autoui/launcher/felix.properties");
60 // Perform variable substitution for system properties.
61 for (Enumeration e
= config
.propertyNames(); e
.hasMoreElements();) {
62 String name
= (String
) e
.nextElement();
63 config
.setProperty(name
, org
.apache
.felix
.main
.Main
.substVars(
64 config
.getProperty(name
), name
, null, config
));
67 config
.put(BundleCache
.CACHE_PROFILE_DIR_PROP
, cachedir
73 protected static File
createTemporaryCacheDir() throws IOException
{
74 // Create a temporary bundle cache directory and
75 // make sure to clean it up on exit.
76 final File cachedir
= File
.createTempFile("argeo.slc.autoui", null);
78 Runtime
.getRuntime().addShutdownHook(new Thread() {
80 deleteFileOrDir(cachedir
);
86 public static Felix
startSystem(Properties config
) throws Exception
{
87 // Create list to hold custom framework activators.
88 List list
= new ArrayList();
89 // Add activator to process auto-start/install properties.
90 list
.add(new AutoActivator(config
));
91 // Add our own activator.
92 list
.add(new AutoUiActivator());
94 // Now create an instance of the framework.
95 Felix felix
= new Felix(config
, list
);
101 public static void startUi(Properties config
) throws Exception
{
102 String className
= config
.getProperty("argeo.scl.autoui.uiclass");
103 String
[] uiArgs
= readArgumentsFromLine(config
.getProperty(
104 "argeo.slc.autoui.uiargs", ""));
106 // Launch main method using reflection
107 Class clss
= Class
.forName(className
);
108 Class
[] mainArgsClasses
= new Class
[] { uiArgs
.getClass() };
109 Object
[] mainArgs
= { uiArgs
};
110 // mainArgs[0] = Class.forName("[Ljava.lang.String;");
111 Method mainMethod
= clss
.getMethod("main", mainArgsClasses
);
112 mainMethod
.invoke(null, mainArgs
);
115 protected static void automateUi(BundleContext bundleContext
)
117 // Retrieve service and execute it
118 ServiceReference ref
= bundleContext
119 .getServiceReference("org.argeo.slc.autoui.DetachedStep");
120 Object service
= bundleContext
.getService(ref
);
122 AutoUiActivator
.stdOut("service.class=" + service
.getClass());
123 DetachedStep app
= (DetachedStep
) service
;
124 app
.execute(null, null);
130 * Transform a line into an array of arguments, taking "" as single
131 * arguments. (nested \" are not supported)
133 private static String
[] readArgumentsFromLine(String lineOrig
) {
135 String line
= lineOrig
.trim();// remove trailing spaces
136 // System.out.println("line=" + line);
137 List args
= new Vector();
138 StringBuffer curr
= new StringBuffer("");
139 boolean inQuote
= false;
140 char[] arr
= line
.toCharArray();
141 for (int i
= 0; i
< arr
.length
; i
++) {
148 if (!inQuote
) {// otherwise, no break: goes to default
149 if (curr
.length() > 0) {
150 args
.add(curr
.toString());
151 curr
= new StringBuffer("");
162 if (curr
.length() > 0) {
163 args
.add(curr
.toString());
167 String
[] res
= new String
[args
.size()];
168 for (int i
= 0; i
< args
.size(); i
++) {
169 res
[i
] = args
.get(i
).toString();
170 // System.out.println("res[i]=" + res[i]);
175 private static void deleteFileOrDir(File file
) {
176 if (file
.isDirectory()) {
177 File
[] childs
= file
.listFiles();
178 for (int i
= 0; i
< childs
.length
; i
++) {
179 deleteFileOrDir(childs
[i
]);