1 package org
.argeo
.slc
.detached
.launcher
;
4 import java
.io
.FileInputStream
;
5 import java
.io
.InputStream
;
6 import java
.lang
.reflect
.Method
;
7 import java
.util
.ArrayList
;
8 import java
.util
.Hashtable
;
9 import java
.util
.Iterator
;
10 import java
.util
.List
;
12 import java
.util
.Properties
;
13 import java
.util
.StringTokenizer
;
14 import java
.util
.Vector
;
16 import org
.eclipse
.core
.runtime
.adaptor
.EclipseStarter
;
17 import org
.osgi
.framework
.Bundle
;
18 import org
.osgi
.framework
.BundleContext
;
19 import org
.osgi
.framework
.BundleException
;
22 public final static String PROP_SLC_HOME
= "slc.home";
23 public final static String PROP_SLC_OSGI_START
= "slc.osgi.start";
24 public final static String PROP_SLC_OSGI_SCAN_CLASSPATH
= "slc.osgi.scanClasspath";
25 public final static String PROP_SLC_OSGI_EQUINOX_ARGS
= "slc.osgi.equinox.args";
27 private final static String DEV_BUNDLE_PREFIX
= "slc.osgi.devbundle.";
29 public static void main(String
[] args
) {
30 info("Argeo SLC Detached launcher starting...");
33 String propertyPath
= "slc-detached.properties";
34 Properties config
= prepareConfig(propertyPath
);
36 // Start app (in main class loader)
39 // Start OSGi framework
42 } catch (Exception e
) {
43 // TODO Auto-generated catch block
46 info("Argeo SLC Detached launcher started.");
47 } catch (Exception e
) {
53 protected static Properties
prepareConfig(String propertyFilePath
)
56 String slcHome
= System
.getProperty(PROP_SLC_HOME
);
57 if (slcHome
!= null) {
58 slcHome
= new File(slcHome
).getCanonicalPath();
59 System
.setProperty(PROP_SLC_HOME
, slcHome
);
63 Properties config
= new Properties();
64 InputStream in
= null;
66 File file
= new File(propertyFilePath
);
68 in
= new FileInputStream(propertyFilePath
);
76 // System properties have priority.
77 config
.putAll(System
.getProperties());
81 public static void startEquinox(Properties config
) throws Exception
{
82 info("java.home=" + System
.getProperty("java.home"));
83 info("java.class.path=" + System
.getProperty("java.class.path"));
85 File baseDir
= new File(System
.getProperty("user.dir"))
87 String equinoxConfigurationPath
= baseDir
.getPath() + File
.separator
88 + "slc-detached" + File
.separator
+ "equinoxConfiguration";
90 String equinoxArgsLineDefault
= "-console -noExit -clean -debug -configuration "
91 + equinoxConfigurationPath
;
92 String equinoxArgsLine
= config
.getProperty(PROP_SLC_OSGI_EQUINOX_ARGS
,
93 equinoxArgsLineDefault
);
94 // String[] equinoxArgs = { "-console", "-noExit", "-clean", "-debug",
95 // "-configuration", equinoxConfigurationPath };
96 String
[] equinoxArgs
= equinoxArgsLine
.split(" ");
98 BundleContext context
= EclipseStarter
.startup(equinoxArgs
, null);
100 List installBundleNames
= new ArrayList();
102 // Load from class path (dev environment, maven)
103 if (config
.getProperty(PROP_SLC_OSGI_SCAN_CLASSPATH
, "false").equals(
105 StringTokenizer st
= new StringTokenizer(System
106 .getProperty("java.class.path"), File
.pathSeparator
);
107 while (st
.hasMoreTokens()) {
109 String path
= st
.nextToken();
110 String url
= "reference:file:"
111 + new File(path
).getCanonicalPath();
112 Bundle bundle
= context
.installBundle(url
);
113 if (bundle
.getSymbolicName() != null)
114 installBundleNames
.add(bundle
.getSymbolicName());
115 info("Installed from classpath " + url
);
116 } catch (Exception e
) {
117 bundleInstallWarn(e
.getMessage());
122 // Load from dev bundles
123 Map devBundleUrls
= getDevBundleUrls(config
);
124 Iterator devBundles
= devBundleUrls
.keySet().iterator();
125 while (devBundles
.hasNext()) {
127 String bundleName
= (String
) devBundles
.next();
128 String url
= (String
) devBundleUrls
.get(bundleName
);
129 Bundle bundle
= context
.installBundle(url
);
130 installBundleNames
.add(bundle
.getSymbolicName());
131 info("Installed as dev bundle " + url
);
132 } catch (Exception e
) {
133 bundleInstallWarn(e
.getMessage());
137 // Load from distribution
138 String slcHome
= config
.getProperty(PROP_SLC_HOME
);
139 if (slcHome
!= null) {
140 File libDir
= new File(slcHome
+ File
.separator
+ "lib");
141 File
[] bundleFiles
= libDir
.listFiles();
142 for (int i
= 0; i
< bundleFiles
.length
; i
++) {
144 String url
= "reference:file:"
145 + bundleFiles
[i
].getCanonicalPath();
146 Bundle bundle
= context
.installBundle(url
);
147 installBundleNames
.add(bundle
.getSymbolicName());
148 info("INFO: Installed from SLC home " + url
);
149 } catch (Exception e
) {
150 bundleInstallWarn(e
.getMessage());
157 String bundleStart
= config
.getProperty(PROP_SLC_OSGI_START
,
158 "org.springframework.osgi.extender,org.argeo.slc.detached");
160 if (bundleStart
.trim().equals("*")) {
161 for (int i
= 0; i
< installBundleNames
.size(); i
++) {
162 Object obj
= installBundleNames
.get(i
);
164 String bundleSymbolicName
= obj
.toString();
166 startBundle(context
, bundleSymbolicName
);
167 } catch (Exception e
) {
168 bundleInstallWarn("Cannot start " + bundleSymbolicName
169 + ": " + e
.getMessage());
174 StringTokenizer stBundleStart
= new StringTokenizer(bundleStart
,
176 while (stBundleStart
.hasMoreTokens()) {
177 String bundleSymbolicName
= stBundleStart
.nextToken();
178 startBundle(context
, bundleSymbolicName
);
183 private static Map
getDevBundleUrls(Properties config
) {
184 Map bundles
= new Hashtable();
185 Iterator keys
= config
.keySet().iterator();
186 while (keys
.hasNext()) {
187 String key
= (String
) keys
.next();
188 if (key
.startsWith(DEV_BUNDLE_PREFIX
)) {
189 String bundle
= key
.substring(DEV_BUNDLE_PREFIX
.length());
190 String path
= config
.getProperty(key
);
191 bundles
.put(bundle
, path
);
197 private static void startBundle(BundleContext bundleContext
,
198 String symbolicName
) throws BundleException
{
199 //info("Starting bundle " + symbolicName + "...");
200 Bundle bundle
= findBundleBySymbolicName(bundleContext
, symbolicName
);
204 throw new RuntimeException("Bundle " + symbolicName
+ " not found");
205 info("Started " + symbolicName
);
208 /** WARNING: return the first one found! */
209 private static Bundle
findBundleBySymbolicName(BundleContext bundleContext
,
210 String symbolicName
) {
211 Bundle
[] bundles
= bundleContext
.getBundles();
212 for (int i
= 0; i
< bundles
.length
; i
++) {
213 Bundle bundle
= bundles
[i
];
214 String bundleSymbolicName
= bundle
.getSymbolicName();
215 if (bundleSymbolicName
!= null) {
216 // throw new RuntimeException("Bundle " + bundle.getBundleId()
217 // + " (" + bundle.getLocation()
218 // + ") has no symbolic name.");
220 if (bundleSymbolicName
.equals(symbolicName
)) {
228 public static void startApp(Properties config
) throws Exception
{
229 String className
= config
.getProperty("slc.detached.appclass");
230 String
[] uiArgs
= readArgumentsFromLine(config
.getProperty(
231 "slc.detached.appargs", ""));
233 if (className
== null) {
234 info("No slc.detached.appclass property define: does not try to launch an app from the standard classpath.");
236 // Launch main method using reflection
237 Class clss
= Class
.forName(className
);
238 Class
[] mainArgsClasses
= new Class
[] { uiArgs
.getClass() };
239 Object
[] mainArgs
= { uiArgs
};
240 Method mainMethod
= clss
.getMethod("main", mainArgsClasses
);
241 mainMethod
.invoke(null, mainArgs
);
248 * Transform a line into an array of arguments, taking "" as single
249 * arguments. (nested \" are not supported)
251 private static String
[] readArgumentsFromLine(String lineOrig
) {
253 String line
= lineOrig
.trim();// remove trailing spaces
254 // System.out.println("line=" + line);
255 List args
= new Vector();
256 StringBuffer curr
= new StringBuffer("");
257 boolean inQuote
= false;
258 char[] arr
= line
.toCharArray();
259 for (int i
= 0; i
< arr
.length
; i
++) {
266 if (!inQuote
) {// otherwise, no break: goes to default
267 if (curr
.length() > 0) {
268 args
.add(curr
.toString());
269 curr
= new StringBuffer("");
280 if (curr
.length() > 0) {
281 args
.add(curr
.toString());
285 String
[] res
= new String
[args
.size()];
286 for (int i
= 0; i
< args
.size(); i
++) {
287 res
[i
] = args
.get(i
).toString();
288 // System.out.println("res[i]=" + res[i]);
293 private static void info(Object obj
) {
294 System
.out
.println("[INFO] " + obj
);
297 private static void bundleInstallWarn(Object obj
) {
298 System
.err
.println("[WARN] " + obj
);
299 //Thread.dumpStack();