]> 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.DetachedStep;
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
115 protected static void automateUi(BundleContext bundleContext)
116 throws Exception {
117 // Retrieve service and execute it
118 ServiceReference ref = bundleContext
119 .getServiceReference("org.argeo.slc.autoui.DetachedStep");
120 Object service = bundleContext.getService(ref);
121
122 AutoUiActivator.stdOut("service.class=" + service.getClass());
123 DetachedStep app = (DetachedStep) service;
124 app.execute(null, null);
125 }
126
127 /* UTILITIES */
128
129 /**
130 * Transform a line into an array of arguments, taking "" as single
131 * arguments. (nested \" are not supported)
132 */
133 private static String[] readArgumentsFromLine(String lineOrig) {
134
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++) {
142 char c = arr[i];
143 switch (c) {
144 case '\"':
145 inQuote = !inQuote;
146 break;
147 case ' ':
148 if (!inQuote) {// otherwise, no break: goes to default
149 if (curr.length() > 0) {
150 args.add(curr.toString());
151 curr = new StringBuffer("");
152 }
153 break;
154 }
155 default:
156 curr.append(c);
157 break;
158 }
159 }
160
161 // Add last arg
162 if (curr.length() > 0) {
163 args.add(curr.toString());
164 curr = null;
165 }
166
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]);
171 }
172 return res;
173 }
174
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]);
180 }
181 }
182 file.delete();
183 }
184
185 }