]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.detached.launcher/src/main/java/org/argeo/slc/autoui/launcher/Main.java
Rename packages
[gpl/argeo-slc.git] / org.argeo.slc.detached.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.commons.logging.Log;
14 import org.apache.commons.logging.LogFactory;
15 import org.apache.felix.framework.Felix;
16 import org.apache.felix.framework.cache.BundleCache;
17 import org.apache.felix.main.AutoActivator;
18 import org.argeo.slc.autoui.AutoUiActivator;
19 import org.argeo.slc.autoui.DetachedExecutionServer;
20 import org.argeo.slc.autoui.DetachedStep;
21 import org.argeo.slc.autoui.DetachedStepRequest;
22 import org.osgi.framework.BundleContext;
23 import org.osgi.framework.ServiceReference;
24
25 public class Main {
26 private final static Log log = LogFactory.getLog(Main.class);
27
28 public static void main(String[] args) {
29 try {
30 // Load properties
31 Properties config = prepareConfig();
32
33 // Start UI (in main class loader)
34 startUi(config);
35 //Thread.sleep(10000);
36
37 // Start OSGi system
38 Felix felix = startSystem(config);
39
40 // Automate
41 //automateUi(felix.getBundleContext());
42
43 // felix.stop();
44 } catch (Exception e) {
45 e.printStackTrace();
46 System.exit(-1);
47 }
48 }
49
50 protected static Properties prepareConfig() throws Exception {
51 final File cachedir = createTemporaryCacheDir();
52
53 // Load config
54 Properties config = new Properties();
55 InputStream in = null;
56 ;
57 try {
58 in = Main.class
59 .getResourceAsStream("/org/argeo/slc/autoui/launcher/felix.properties");
60 config.load(in);
61 } finally {
62 if (in != null)
63 in.close();
64 }
65
66 // Perform variable substitution for system properties.
67 for (Enumeration e = config.propertyNames(); e.hasMoreElements();) {
68 String name = (String) e.nextElement();
69 config.setProperty(name, org.apache.felix.main.Main.substVars(
70 config.getProperty(name), name, null, config));
71 }
72
73 config.put(BundleCache.CACHE_PROFILE_DIR_PROP, cachedir
74 .getAbsolutePath());
75
76 return config;
77 }
78
79 protected static File createTemporaryCacheDir() throws IOException {
80 // Create a temporary bundle cache directory and
81 // make sure to clean it up on exit.
82 final File cachedir = File.createTempFile("argeo.slc.autoui", null);
83 cachedir.delete();
84 Runtime.getRuntime().addShutdownHook(new Thread() {
85 public void run() {
86 deleteFileOrDir(cachedir);
87 }
88 });
89 return cachedir;
90 }
91
92 public static Felix startSystem(Properties config) throws Exception {
93 // Create list to hold custom framework activators.
94 List list = new ArrayList();
95 // Add activator to process auto-start/install properties.
96 list.add(new AutoActivator(config));
97 // Add our own activator.
98 //list.add(new AutoUiActivator());
99
100 // Now create an instance of the framework.
101 Felix felix = new Felix(config, list);
102 felix.start();
103
104 return felix;
105 }
106
107 public static void startUi(Properties config) throws Exception {
108 String className = config.getProperty("argeo.scl.autoui.uiclass");
109 String[] uiArgs = readArgumentsFromLine(config.getProperty(
110 "argeo.slc.autoui.uiargs", ""));
111
112 // Launch main method using reflection
113 Class clss = Class.forName(className);
114 Class[] mainArgsClasses = new Class[] { uiArgs.getClass() };
115 Object[] mainArgs = { uiArgs };
116 Method mainMethod = clss.getMethod("main", mainArgsClasses);
117 mainMethod.invoke(null, mainArgs);
118 }
119
120 protected static void automateUi(BundleContext bundleContext)
121 throws Exception {
122 // Retrieve service and execute it
123 ServiceReference ref = bundleContext
124 .getServiceReference(DetachedExecutionServer.class.getName());
125 Object service = bundleContext.getService(ref);
126
127 log.debug("service.class=" + service.getClass());
128 DetachedExecutionServer app = (DetachedExecutionServer) service;
129 DetachedStepRequest request = new DetachedStepRequest();
130 request.setStepRef("jemmyTest");
131 app.executeStep(request);
132 }
133
134 /* UTILITIES */
135
136 /**
137 * Transform a line into an array of arguments, taking "" as single
138 * arguments. (nested \" are not supported)
139 */
140 private static String[] readArgumentsFromLine(String lineOrig) {
141
142 String line = lineOrig.trim();// remove trailing spaces
143 // System.out.println("line=" + line);
144 List args = new Vector();
145 StringBuffer curr = new StringBuffer("");
146 boolean inQuote = false;
147 char[] arr = line.toCharArray();
148 for (int i = 0; i < arr.length; i++) {
149 char c = arr[i];
150 switch (c) {
151 case '\"':
152 inQuote = !inQuote;
153 break;
154 case ' ':
155 if (!inQuote) {// otherwise, no break: goes to default
156 if (curr.length() > 0) {
157 args.add(curr.toString());
158 curr = new StringBuffer("");
159 }
160 break;
161 }
162 default:
163 curr.append(c);
164 break;
165 }
166 }
167
168 // Add last arg
169 if (curr.length() > 0) {
170 args.add(curr.toString());
171 curr = null;
172 }
173
174 String[] res = new String[args.size()];
175 for (int i = 0; i < args.size(); i++) {
176 res[i] = args.get(i).toString();
177 // System.out.println("res[i]=" + res[i]);
178 }
179 return res;
180 }
181
182 private static void deleteFileOrDir(File file) {
183 if (file.isDirectory()) {
184 File[] childs = file.listFiles();
185 for (int i = 0; i < childs.length; i++) {
186 deleteFileOrDir(childs[i]);
187 }
188 }
189 file.delete();
190 }
191
192 }