1 package org
.argeo
.slc
.autoui
.launcher
;
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
;
10 import java
.util
.Properties
;
11 import java
.util
.Vector
;
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
;
26 private final static Log log
= LogFactory
.getLog(Main
.class);
28 public static void main(String
[] args
) {
31 Properties config
= prepareConfig();
33 // Start UI (in main class loader)
35 //Thread.sleep(10000);
38 Felix felix
= startSystem(config
);
41 //automateUi(felix.getBundleContext());
44 } catch (Exception e
) {
50 protected static Properties
prepareConfig() throws Exception
{
51 final File cachedir
= createTemporaryCacheDir();
54 Properties config
= new Properties();
55 InputStream in
= null;
59 .getResourceAsStream("/org/argeo/slc/autoui/launcher/felix.properties");
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
));
73 config
.put(BundleCache
.CACHE_PROFILE_DIR_PROP
, cachedir
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);
84 Runtime
.getRuntime().addShutdownHook(new Thread() {
86 deleteFileOrDir(cachedir
);
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());
100 // Now create an instance of the framework.
101 Felix felix
= new Felix(config
, list
);
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", ""));
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
);
120 protected static void automateUi(BundleContext bundleContext
)
122 // Retrieve service and execute it
123 ServiceReference ref
= bundleContext
124 .getServiceReference(DetachedExecutionServer
.class.getName());
125 Object service
= bundleContext
.getService(ref
);
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
);
137 * Transform a line into an array of arguments, taking "" as single
138 * arguments. (nested \" are not supported)
140 private static String
[] readArgumentsFromLine(String lineOrig
) {
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
++) {
155 if (!inQuote
) {// otherwise, no break: goes to default
156 if (curr
.length() > 0) {
157 args
.add(curr
.toString());
158 curr
= new StringBuffer("");
169 if (curr
.length() > 0) {
170 args
.add(curr
.toString());
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]);
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
]);