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
.Hashtable
;
8 import java
.util
.Iterator
;
11 import java
.util
.Properties
;
12 import java
.util
.StringTokenizer
;
13 import java
.util
.Vector
;
15 import org
.eclipse
.core
.runtime
.adaptor
.EclipseStarter
;
16 import org
.osgi
.framework
.Bundle
;
17 import org
.osgi
.framework
.BundleContext
;
18 import org
.osgi
.framework
.BundleException
;
21 public final static String PROP_SLC_HOME
= "slc.home";
22 public final static String PROP_SLC_OSGI_START
= "slc.osgi.start";
23 public final static String PROP_SLC_OSGI_SCAN_CLASSPATH
= "slc.osgi.scanClasspath";
24 public final static String PROP_SLC_OSGI_EQUINOX_ARGS
= "slc.osgi.equinox.args";
26 private final static String DEV_BUNDLE_PREFIX
= "slc.osgi.devbundle.";
28 public static void main(String
[] args
) {
29 info("Argeo SLC Detached launcher starting...");
32 String propertyPath
= "slc-detached.properties";
33 Properties config
= prepareConfig(propertyPath
);
35 // Start app (in main class loader)
38 // Start OSGi framework
41 } catch (Exception e
) {
42 // TODO Auto-generated catch block
45 info("Argeo SLC Detached launcher started.");
46 } catch (Exception e
) {
52 protected static Properties
prepareConfig(String propertyFilePath
)
55 String slcHome
= System
.getProperty(PROP_SLC_HOME
);
56 if (slcHome
!= null) {
57 slcHome
= new File(slcHome
).getCanonicalPath();
58 System
.setProperty(PROP_SLC_HOME
, slcHome
);
62 Properties config
= new Properties();
63 InputStream in
= null;
65 File file
= new File(propertyFilePath
);
67 in
= new FileInputStream(propertyFilePath
);
75 // System properties have priority.
76 config
.putAll(System
.getProperties());
80 public static void startEquinox(Properties config
) throws Exception
{
81 info("java.class.path=" + System
.getProperty("java.class.path"));
83 File baseDir
= new File(System
.getProperty("user.dir"))
85 String equinoxConfigurationPath
= baseDir
.getPath() + File
.separator
86 + "slc-detached" + File
.separator
+ "equinoxConfiguration";
88 String equinoxArgsLineDefault
= "-console -noExit -clean -debug -configuration "
89 + equinoxConfigurationPath
;
90 String equinoxArgsLine
= config
.getProperty(PROP_SLC_OSGI_EQUINOX_ARGS
,
91 equinoxArgsLineDefault
);
92 // String[] equinoxArgs = { "-console", "-noExit", "-clean", "-debug",
93 // "-configuration", equinoxConfigurationPath };
94 String
[] equinoxArgs
= equinoxArgsLine
.split(" ");
96 BundleContext context
= EclipseStarter
.startup(equinoxArgs
, null);
98 // Load from class path (dev environment, maven)
99 if (config
.getProperty(PROP_SLC_OSGI_SCAN_CLASSPATH
, "false").equals(
101 StringTokenizer st
= new StringTokenizer(System
102 .getProperty("java.class.path"), File
.pathSeparator
);
103 while (st
.hasMoreTokens()) {
105 String path
= st
.nextToken();
106 String url
= "reference:file:"
107 + new File(path
).getCanonicalPath();
108 context
.installBundle(url
);
109 info("Installed from classpath " + url
);
110 } catch (Exception e
) {
111 bundleInstallWarn(e
.getMessage());
116 // Load from dev bundles
117 Map devBundleUrls
= getDevBundleUrls(config
);
118 Iterator devBundles
= devBundleUrls
.keySet().iterator();
119 while (devBundles
.hasNext()) {
121 String bundleName
= (String
) devBundles
.next();
122 String url
= (String
) devBundleUrls
.get(bundleName
);
123 context
.installBundle(url
);
124 info("Installed as dev bundle " + url
);
125 } catch (Exception e
) {
126 bundleInstallWarn(e
.getMessage());
130 // Load from distribution
131 String slcHome
= config
.getProperty(PROP_SLC_HOME
);
132 if (slcHome
!= null) {
133 File libDir
= new File(slcHome
+ File
.separator
+ "lib");
134 File
[] bundleFiles
= libDir
.listFiles();
135 for (int i
= 0; i
< bundleFiles
.length
; i
++) {
137 String url
= "reference:file:"
138 + bundleFiles
[i
].getCanonicalPath();
139 context
.installBundle(url
);
140 info("INFO: Installed from SLC home " + url
);
141 } catch (Exception e
) {
142 bundleInstallWarn(e
.getMessage());
149 String bundleStart
= config
.getProperty(PROP_SLC_OSGI_START
,
150 "org.springframework.osgi.extender,org.argeo.slc.detached");
151 StringTokenizer stBundleStart
= new StringTokenizer(bundleStart
, ",");
152 while (stBundleStart
.hasMoreTokens()) {
153 String bundleSymbolicName
= stBundleStart
.nextToken();
154 startBundle(context
, bundleSymbolicName
);
158 private static Map
getDevBundleUrls(Properties config
) {
159 Map bundles
= new Hashtable();
160 Iterator keys
= config
.keySet().iterator();
161 while (keys
.hasNext()) {
162 String key
= (String
) keys
.next();
163 if (key
.startsWith(DEV_BUNDLE_PREFIX
)) {
164 String bundle
= key
.substring(DEV_BUNDLE_PREFIX
.length());
165 String path
= config
.getProperty(key
);
166 bundles
.put(bundle
, path
);
172 private static void startBundle(BundleContext bundleContext
,
173 String symbolicName
) throws BundleException
{
174 info("Starting bundle " + symbolicName
+ "...");
175 Bundle bundle
= findBundleBySymbolicName(bundleContext
, symbolicName
);
179 throw new RuntimeException("Bundle " + symbolicName
+ " not found");
180 info("Started " + symbolicName
);
183 /** WARNING: return the first one found! */
184 private static Bundle
findBundleBySymbolicName(BundleContext bundleContext
,
185 String symbolicName
) {
186 Bundle
[] bundles
= bundleContext
.getBundles();
187 for (int i
= 0; i
< bundles
.length
; i
++) {
188 Bundle bundle
= bundles
[i
];
189 String bundleSymbolicName
= bundle
.getSymbolicName();
190 if (bundleSymbolicName
== null)
191 throw new RuntimeException("Bundle " + bundle
.getBundleId()
192 + " (" + bundle
.getLocation()
193 + ") has no symbolic name.");
195 if (bundleSymbolicName
.equals(symbolicName
)) {
202 public static void startApp(Properties config
) throws Exception
{
203 String className
= config
.getProperty("slc.detached.appclass");
204 String
[] uiArgs
= readArgumentsFromLine(config
.getProperty(
205 "slc.detached.appargs", ""));
207 if (className
== null) {
208 info("No slc.detached.appclass property define: does not try to launch an app from the standard classpath.");
210 // Launch main method using reflection
211 Class clss
= Class
.forName(className
);
212 Class
[] mainArgsClasses
= new Class
[] { uiArgs
.getClass() };
213 Object
[] mainArgs
= { uiArgs
};
214 Method mainMethod
= clss
.getMethod("main", mainArgsClasses
);
215 mainMethod
.invoke(null, mainArgs
);
222 * Transform a line into an array of arguments, taking "" as single
223 * arguments. (nested \" are not supported)
225 private static String
[] readArgumentsFromLine(String lineOrig
) {
227 String line
= lineOrig
.trim();// remove trailing spaces
228 // System.out.println("line=" + line);
229 List args
= new Vector();
230 StringBuffer curr
= new StringBuffer("");
231 boolean inQuote
= false;
232 char[] arr
= line
.toCharArray();
233 for (int i
= 0; i
< arr
.length
; i
++) {
240 if (!inQuote
) {// otherwise, no break: goes to default
241 if (curr
.length() > 0) {
242 args
.add(curr
.toString());
243 curr
= new StringBuffer("");
254 if (curr
.length() > 0) {
255 args
.add(curr
.toString());
259 String
[] res
= new String
[args
.size()];
260 for (int i
= 0; i
< args
.size(); i
++) {
261 res
[i
] = args
.get(i
).toString();
262 // System.out.println("res[i]=" + res[i]);
267 private static void info(Object obj
) {
268 System
.out
.println("[INFO] " + obj
);
271 private static void bundleInstallWarn(Object obj
) {
272 System
.err
.println("[WARN] " + obj
);