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