1 package org
.argeo
.slc
.autoui
.launcher
;
4 import java
.io
.IOException
;
5 import java
.io
.InputStream
;
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
.AutoUiApplication
;
18 import org
.netbeans
.jemmy
.ClassReference
;
19 import org
.osgi
.framework
.Bundle
;
20 import org
.osgi
.framework
.BundleContext
;
21 import org
.osgi
.framework
.ServiceReference
;
22 import org
.springframework
.beans
.factory
.xml
.XmlBeanDefinitionReader
;
23 import org
.springframework
.context
.ApplicationContext
;
24 import org
.springframework
.context
.support
.GenericApplicationContext
;
25 import org
.springframework
.core
.io
.UrlResource
;
29 public static void main(String
[] args
) {
32 Properties config
= prepareConfig();
33 Felix felix
= startSystem(config
);
35 // GenericApplicationContext context = new
36 // GenericApplicationContext();
37 // XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(
39 // Bundle[] bundles = felix.getBundleContext().getBundles();
40 // for (int i = 0; i < bundles.length; i++) {
41 // Bundle bundle = bundles[i];
43 // .getResource("META-INF/slc/conf/applicationContext.xml");
45 // System.out.println("Loads application context from bundle "
46 // + bundle.getSymbolicName());
47 // xmlReader.loadBeanDefinitions(new UrlResource(url));
51 // Start UI (in main class loader)
55 automateUi(felix
.getBundleContext());
58 } catch (Exception e
) {
64 protected static Properties
prepareConfig() throws Exception
{
65 final File cachedir
= createTemporaryCacheDir();
68 Properties config
= new Properties();
69 InputStream in
= null;
73 .getResourceAsStream("/org/argeo/slc/autoui/launcher/felix.properties");
80 // Perform variable substitution for system properties.
81 for (Enumeration e
= config
.propertyNames(); e
.hasMoreElements();) {
82 String name
= (String
) e
.nextElement();
83 config
.setProperty(name
, org
.apache
.felix
.main
.Main
.substVars(
84 config
.getProperty(name
), name
, null, config
));
87 config
.put(BundleCache
.CACHE_PROFILE_DIR_PROP
, cachedir
93 protected static File
createTemporaryCacheDir() throws IOException
{
94 // Create a temporary bundle cache directory and
95 // make sure to clean it up on exit.
96 final File cachedir
= File
.createTempFile("argeo.slc.autoui", null);
98 Runtime
.getRuntime().addShutdownHook(new Thread() {
100 deleteFileOrDir(cachedir
);
106 public static Felix
startSystem(Properties config
) throws Exception
{
107 // Create list to hold custom framework activators.
108 List list
= new ArrayList();
109 // Add activator to process auto-start/install properties.
110 list
.add(new AutoActivator(config
));
111 // Add our own activator.
112 list
.add(new AutoUiActivator());
114 // Now create an instance of the framework.
115 Felix felix
= new Felix(config
, list
);
121 public static void startUi(Properties config
) throws Exception
{
122 String className
= config
.getProperty("argeo.scl.autoui.uiclass");
123 String
[] uiArgs
= readArgumentsFromLine(config
.getProperty(
124 "argeo.slc.autoui.uiargs", ""));
125 ClassReference classReference
= new ClassReference(className
);
126 classReference
.startApplication(uiArgs
);
129 protected static void automateUi(BundleContext bundleContext
)
131 // Retrieve service and execute it
132 ServiceReference ref
= bundleContext
133 .getServiceReference("org.argeo.slc.autoui.AutoUiApplication");
134 Object service
= bundleContext
.getService(ref
);
136 // Object service = applicationContext.getBean("jemmyTest");
137 AutoUiActivator
.stdOut("service.class=" + service
.getClass());
138 AutoUiApplication app
= (AutoUiApplication
) service
;
145 * Transform a line into an array of arguments, taking "" as single
146 * arguments. (nested \" are not supported)
148 private static String
[] readArgumentsFromLine(String lineOrig
) {
150 String line
= lineOrig
.trim();// remove trailing spaces
151 // System.out.println("line=" + line);
152 List args
= new Vector();
153 StringBuffer curr
= new StringBuffer("");
154 boolean inQuote
= false;
155 char[] arr
= line
.toCharArray();
156 for (int i
= 0; i
< arr
.length
; i
++) {
163 if (!inQuote
) {// otherwise, no break: goes to default
164 if (curr
.length() > 0) {
165 args
.add(curr
.toString());
166 curr
= new StringBuffer("");
177 if (curr
.length() > 0) {
178 args
.add(curr
.toString());
182 String
[] res
= new String
[args
.size()];
183 for (int i
= 0; i
< args
.size(); i
++) {
184 res
[i
] = args
.get(i
).toString();
185 // System.out.println("res[i]=" + res[i]);
190 private static void deleteFileOrDir(File file
) {
191 if (file
.isDirectory()) {
192 File
[] childs
= file
.listFiles();
193 for (int i
= 0; i
< childs
.length
; i
++) {
194 deleteFileOrDir(childs
[i
]);