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