1 package org
.argeo
.slc
.autoui
.launcher
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
6 import java
.util
.ArrayList
;
7 import java
.util
.Enumeration
;
9 import java
.util
.Properties
;
10 import java
.util
.Vector
;
12 import org
.apache
.felix
.framework
.Felix
;
13 import org
.apache
.felix
.framework
.cache
.BundleCache
;
14 import org
.apache
.felix
.main
.AutoActivator
;
15 import org
.argeo
.slc
.autoui
.AutoUiActivator
;
16 import org
.argeo
.slc
.autoui
.AutoUiApplication
;
17 import org
.netbeans
.jemmy
.ClassReference
;
18 import org
.osgi
.framework
.BundleContext
;
19 import org
.osgi
.framework
.ServiceReference
;
23 public static void main(String
[] args
) {
26 Properties config
= prepareConfig();
27 Felix felix
= startSystem(config
);
29 // Start UI (in main class loader)
33 automateUi(felix
.getBundleContext());
36 } catch (Exception e
) {
42 protected static Properties
prepareConfig() throws Exception
{
43 final File cachedir
= createTemporaryCacheDir();
46 Properties config
= new Properties();
47 InputStream in
= null;
51 .getResourceAsStream("/org/argeo/slc/autoui/launcher/felix.properties");
58 // Perform variable substitution for system properties.
59 for (Enumeration e
= config
.propertyNames(); e
.hasMoreElements();) {
60 String name
= (String
) e
.nextElement();
61 config
.setProperty(name
, org
.apache
.felix
.main
.Main
.substVars(
62 config
.getProperty(name
), name
, null, config
));
65 config
.put(BundleCache
.CACHE_PROFILE_DIR_PROP
, cachedir
71 protected static File
createTemporaryCacheDir() throws IOException
{
72 // Create a temporary bundle cache directory and
73 // make sure to clean it up on exit.
74 final File cachedir
= File
.createTempFile("argeo.slc.autoui", null);
76 Runtime
.getRuntime().addShutdownHook(new Thread() {
78 deleteFileOrDir(cachedir
);
84 public static Felix
startSystem(Properties config
) throws Exception
{
85 // Create list to hold custom framework activators.
86 List list
= new ArrayList();
87 // Add activator to process auto-start/install properties.
88 list
.add(new AutoActivator(config
));
89 // Add our own activator.
90 list
.add(new AutoUiActivator());
92 // Now create an instance of the framework.
93 Felix felix
= new Felix(config
, list
);
99 public static void startUi(Properties config
) throws Exception
{
100 String className
= config
.getProperty("argeo.scl.autoui.uiclass");
101 String
[] uiArgs
= readArgumentsFromLine(config
.getProperty(
102 "argeo.slc.autoui.uiargs", ""));
103 ClassReference classReference
= new ClassReference(className
);
104 classReference
.startApplication(uiArgs
);
107 protected static void automateUi(BundleContext context
) throws Exception
{
108 // Retrieve service and execute it
109 ServiceReference ref
= context
110 .getServiceReference("org.argeo.slc.autoui.AutoUiApplication");
111 Object service
= context
.getService(ref
);
112 AutoUiActivator
.stdOut("service.class=" + service
.getClass());
113 AutoUiApplication app
= (AutoUiApplication
) service
;
120 * Transform a line into an array of arguments, taking "" as single
121 * arguments. (nested \" are not supported)
123 private static String
[] readArgumentsFromLine(String lineOrig
) {
125 String line
= lineOrig
.trim();// remove trailing spaces
126 // System.out.println("line=" + line);
127 List args
= new Vector();
128 StringBuffer curr
= new StringBuffer("");
129 boolean inQuote
= false;
130 char[] arr
= line
.toCharArray();
131 for (int i
= 0; i
< arr
.length
; i
++) {
138 if (!inQuote
) {// otherwise, no break: goes to default
139 if (curr
.length() > 0) {
140 args
.add(curr
.toString());
141 curr
= new StringBuffer("");
152 if (curr
.length() > 0) {
153 args
.add(curr
.toString());
157 String
[] res
= new String
[args
.size()];
158 for (int i
= 0; i
< args
.size(); i
++) {
159 res
[i
] = args
.get(i
).toString();
160 // System.out.println("res[i]=" + res[i]);
165 private static void deleteFileOrDir(File file
) {
166 if (file
.isDirectory()) {
167 File
[] childs
= file
.listFiles();
168 for (int i
= 0; i
< childs
.length
; i
++) {
169 deleteFileOrDir(childs
[i
]);