]> 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 introducing the interfaces
[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.lang.reflect.Method;
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.osgi.framework.BundleContext;
19 import org.osgi.framework.ServiceReference;
20
21 public class Main {
22
23 public static void main(String[] args) {
24 try {
25 // Load properties
26 Properties config = prepareConfig();
27
28 // Start UI (in main class loader)
29 startUi(config);
30
31 // Start OSGi system
32 Felix felix = startSystem(config);
33
34 // Automate
35 automateUi(felix.getBundleContext());
36
37 felix.stop();
38 } catch (Exception e) {
39 e.printStackTrace();
40 System.exit(-1);
41 }
42 }
43
44 protected static Properties prepareConfig() throws Exception {
45 final File cachedir = createTemporaryCacheDir();
46
47 // Load config
48 Properties config = new Properties();
49 InputStream in = null;
50 ;
51 try {
52 in = Main.class
53 .getResourceAsStream("/org/argeo/slc/autoui/launcher/felix.properties");
54 config.load(in);
55 } finally {
56 if (in != null)
57 in.close();
58 }
59
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));
65 }
66
67 config.put(BundleCache.CACHE_PROFILE_DIR_PROP, cachedir
68 .getAbsolutePath());
69
70 return config;
71 }
72
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);
77 cachedir.delete();
78 Runtime.getRuntime().addShutdownHook(new Thread() {
79 public void run() {
80 deleteFileOrDir(cachedir);
81 }
82 });
83 return cachedir;
84 }
85
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());
93
94 // Now create an instance of the framework.
95 Felix felix = new Felix(config, list);
96 felix.start();
97
98 return felix;
99 }
100
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", ""));
105
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);
113
114 // ClassReference classReference = new ClassReference(className);
115 // classReference.startApplication(uiArgs);
116 }
117
118 protected static void automateUi(BundleContext bundleContext)
119 throws Exception {
120 // Retrieve service and execute it
121 ServiceReference ref = bundleContext
122 .getServiceReference("org.argeo.slc.autoui.AutoUiApplication");
123 Object service = bundleContext.getService(ref);
124
125 // Object service = applicationContext.getBean("jemmyTest");
126 AutoUiActivator.stdOut("service.class=" + service.getClass());
127 AutoUiApplication app = (AutoUiApplication) service;
128 app.execute(null);
129 }
130
131 /* UTILITIES */
132
133 /**
134 * Transform a line into an array of arguments, taking "" as single
135 * arguments. (nested \" are not supported)
136 */
137 private static String[] readArgumentsFromLine(String lineOrig) {
138
139 String line = lineOrig.trim();// remove trailing spaces
140 // System.out.println("line=" + line);
141 List args = new Vector();
142 StringBuffer curr = new StringBuffer("");
143 boolean inQuote = false;
144 char[] arr = line.toCharArray();
145 for (int i = 0; i < arr.length; i++) {
146 char c = arr[i];
147 switch (c) {
148 case '\"':
149 inQuote = !inQuote;
150 break;
151 case ' ':
152 if (!inQuote) {// otherwise, no break: goes to default
153 if (curr.length() > 0) {
154 args.add(curr.toString());
155 curr = new StringBuffer("");
156 }
157 break;
158 }
159 default:
160 curr.append(c);
161 break;
162 }
163 }
164
165 // Add last arg
166 if (curr.length() > 0) {
167 args.add(curr.toString());
168 curr = null;
169 }
170
171 String[] res = new String[args.size()];
172 for (int i = 0; i < args.size(); i++) {
173 res[i] = args.get(i).toString();
174 // System.out.println("res[i]=" + res[i]);
175 }
176 return res;
177 }
178
179 private static void deleteFileOrDir(File file) {
180 if (file.isDirectory()) {
181 File[] childs = file.listFiles();
182 for (int i = 0; i < childs.length; i++) {
183 deleteFileOrDir(childs[i]);
184 }
185 }
186 file.delete();
187 }
188
189 }