]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.autoui.launcher/src/main/java/org/argeo/slc/autoui/launcher/Main.java
Start using Spring
[gpl/argeo-slc.git] / org.argeo.slc.autoui.launcher / src / main / java / org / argeo / slc / autoui / launcher / Main.java
1 package org.argeo.slc.autoui.launcher;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.URL;
7 import java.util.ArrayList;
8 import java.util.Enumeration;
9 import java.util.List;
10 import java.util.Properties;
11 import java.util.Vector;
12
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;
26
27 public class Main {
28
29 public static void main(String[] args) {
30 try {
31 // Start OSGi system
32 Properties config = prepareConfig();
33 Felix felix = startSystem(config);
34
35 // GenericApplicationContext context = new
36 // GenericApplicationContext();
37 // XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(
38 // context);
39 // Bundle[] bundles = felix.getBundleContext().getBundles();
40 // for (int i = 0; i < bundles.length; i++) {
41 // Bundle bundle = bundles[i];
42 // URL url = bundle
43 // .getResource("META-INF/slc/conf/applicationContext.xml");
44 // if (url != null) {
45 // System.out.println("Loads application context from bundle "
46 // + bundle.getSymbolicName());
47 // xmlReader.loadBeanDefinitions(new UrlResource(url));
48 // }
49 // }
50
51 // Start UI (in main class loader)
52 startUi(config);
53
54 // Automate
55 automateUi(felix.getBundleContext());
56
57 felix.stop();
58 } catch (Exception e) {
59 e.printStackTrace();
60 System.exit(-1);
61 }
62 }
63
64 protected static Properties prepareConfig() throws Exception {
65 final File cachedir = createTemporaryCacheDir();
66
67 // Load config
68 Properties config = new Properties();
69 InputStream in = null;
70 ;
71 try {
72 in = Main.class
73 .getResourceAsStream("/org/argeo/slc/autoui/launcher/felix.properties");
74 config.load(in);
75 } finally {
76 if (in != null)
77 in.close();
78 }
79
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));
85 }
86
87 config.put(BundleCache.CACHE_PROFILE_DIR_PROP, cachedir
88 .getAbsolutePath());
89
90 return config;
91 }
92
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);
97 cachedir.delete();
98 Runtime.getRuntime().addShutdownHook(new Thread() {
99 public void run() {
100 deleteFileOrDir(cachedir);
101 }
102 });
103 return cachedir;
104 }
105
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());
113
114 // Now create an instance of the framework.
115 Felix felix = new Felix(config, list);
116 felix.start();
117
118 return felix;
119 }
120
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);
127 }
128
129 protected static void automateUi(BundleContext bundleContext)
130 throws Exception {
131 // Retrieve service and execute it
132 ServiceReference ref = bundleContext
133 .getServiceReference("org.argeo.slc.autoui.AutoUiApplication");
134 Object service = bundleContext.getService(ref);
135
136 // Object service = applicationContext.getBean("jemmyTest");
137 AutoUiActivator.stdOut("service.class=" + service.getClass());
138 AutoUiApplication app = (AutoUiApplication) service;
139 app.execute(null);
140 }
141
142 /* UTILITIES */
143
144 /**
145 * Transform a line into an array of arguments, taking "" as single
146 * arguments. (nested \" are not supported)
147 */
148 private static String[] readArgumentsFromLine(String lineOrig) {
149
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++) {
157 char c = arr[i];
158 switch (c) {
159 case '\"':
160 inQuote = !inQuote;
161 break;
162 case ' ':
163 if (!inQuote) {// otherwise, no break: goes to default
164 if (curr.length() > 0) {
165 args.add(curr.toString());
166 curr = new StringBuffer("");
167 }
168 break;
169 }
170 default:
171 curr.append(c);
172 break;
173 }
174 }
175
176 // Add last arg
177 if (curr.length() > 0) {
178 args.add(curr.toString());
179 curr = null;
180 }
181
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]);
186 }
187 return res;
188 }
189
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]);
195 }
196 }
197 file.delete();
198 }
199
200 }