]>
git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.detached.launcher/src/main/java/org/argeo/slc/detached/launcher/Main.java
1 package org
.argeo
.slc
.detached
.launcher
;
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
;
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
;
22 private final static Log log
= LogFactory
.getLog(Main
.class);
24 public static void main(String
[] args
) {
25 log
.info("Argeo SLC Detached launcher starting...");
28 String propertyPath
= "slc-detached.properties";
29 Properties config
= prepareConfig(propertyPath
);
32 if (!config
.containsKey(BundleCache
.CACHE_PROFILE_DIR_PROP
)) {
33 final File cachedir
= createTemporaryCacheDir();
34 config
.put(BundleCache
.CACHE_PROFILE_DIR_PROP
, cachedir
38 // Start app (in main class loader)
40 // Thread.sleep(10000);
43 Felix felix
= startSystem(config
);
45 log
.info("Argeo SLC Detached system started (Felix " + felix
+ ")");
48 } catch (Exception e
) {
54 protected static Properties
prepareConfig(String propertyFilePath
)
57 String slcHome
= System
.getProperty("slc.home");
58 if (slcHome
!= null) {
59 slcHome
= new File(slcHome
).getCanonicalPath();
60 System
.setProperty("slc.home", slcHome
);
64 Properties config
= new Properties();
65 InputStream in
= null;
69 .getResourceAsStream("/org/argeo/slc/detached/launcher/felix.properties");
72 IOUtils
.closeQuietly(in
);
76 File file
= new File(propertyFilePath
);
78 in
= new FileInputStream(propertyFilePath
);
82 IOUtils
.closeQuietly(in
);
85 // System properties have priority.
86 config
.putAll(System
.getProperties());
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
));
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);
105 Runtime
.getRuntime().addShutdownHook(new Thread() {
107 deleteFileOrDir(cachedir
);
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());
121 // Now create an instance of the framework.
122 Felix felix
= new Felix(config
, list
);
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", ""));
133 if (className
== null)
135 "A main class has to be defined with teh system property slc.detached.appclass");
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
]);
147 mainMethod
.invoke(null, mainArgs
);
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);
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);
167 * Transform a line into an array of arguments, taking "" as single
168 * arguments. (nested \" are not supported)
170 private static String
[] readArgumentsFromLine(String lineOrig
) {
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
++) {
185 if (!inQuote
) {// otherwise, no break: goes to default
186 if (curr
.length() > 0) {
187 args
.add(curr
.toString());
188 curr
= new StringBuffer("");
199 if (curr
.length() > 0) {
200 args
.add(curr
.toString());
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]);
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
]);