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