import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
+import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import org.osgi.framework.BundleException;
public class Main {
+ public final static String PROP_SLC_HOME = "slc.home";
+ public final static String PROP_SLC_OSGI_START = "slc.osgi.start";
+ public final static String PROP_SLC_OSGI_SCAN_CLASSPATH = "slc.osgi.scanClasspath";
+ public final static String PROP_SLC_OSGI_EQUINOX_ARGS = "slc.osgi.equinox.args";
+
private final static String DEV_BUNDLE_PREFIX = "slc.osgi.devbundle.";
public static void main(String[] args) {
startApp(config);
// Start OSGi framework
- startEquinox(config);
+ try {
+ startEquinox(config);
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
info("Argeo SLC Detached launcher started.");
} catch (Exception e) {
e.printStackTrace();
protected static Properties prepareConfig(String propertyFilePath)
throws Exception {
// Format slc.home
- String slcHome = System.getProperty("slc.home");
+ String slcHome = System.getProperty(PROP_SLC_HOME);
if (slcHome != null) {
slcHome = new File(slcHome).getCanonicalPath();
- System.setProperty("slc.home", slcHome);
+ System.setProperty(PROP_SLC_HOME, slcHome);
}
// Load config
}
public static void startEquinox(Properties config) throws Exception {
- System.out.println("java.class.path="
- + System.getProperty("java.class.path"));
+ info("java.class.path=" + System.getProperty("java.class.path"));
File baseDir = new File(System.getProperty("user.dir"))
.getCanonicalFile();
String equinoxConfigurationPath = baseDir.getPath() + File.separator
+ "slc-detached" + File.separator + "equinoxConfiguration";
- String[] equinoxArgs = { "-console", "-noExit", "-clean",
- "-configuration", equinoxConfigurationPath };
+
+ String equinoxArgsLineDefault = "-console -noExit -clean -debug -configuration "
+ + equinoxConfigurationPath;
+ String equinoxArgsLine = config.getProperty(PROP_SLC_OSGI_EQUINOX_ARGS,
+ equinoxArgsLineDefault);
+ // String[] equinoxArgs = { "-console", "-noExit", "-clean", "-debug",
+ // "-configuration", equinoxConfigurationPath };
+ String[] equinoxArgs = equinoxArgsLine.split(" ");
BundleContext context = EclipseStarter.startup(equinoxArgs, null);
+ List installBundleNames = new ArrayList();
+
// Load from class path (dev environment, maven)
- StringTokenizer st = new StringTokenizer(System
- .getProperty("java.class.path"), File.pathSeparator);
- while (st.hasMoreTokens()) {
- try {
- String path = st.nextToken();
- String url = "reference:file:"
- + new File(path).getCanonicalPath();
- context.installBundle(url);
- info("Installed from classpath " + url);
- } catch (Exception e) {
- bundleInstallWarn(e.getMessage());
+ if (config.getProperty(PROP_SLC_OSGI_SCAN_CLASSPATH, "false").equals(
+ "true")) {
+ StringTokenizer st = new StringTokenizer(System
+ .getProperty("java.class.path"), File.pathSeparator);
+ while (st.hasMoreTokens()) {
+ try {
+ String path = st.nextToken();
+ String url = "reference:file:"
+ + new File(path).getCanonicalPath();
+ Bundle bundle = context.installBundle(url);
+ installBundleNames.add(bundle.getSymbolicName());
+ info("Installed from classpath " + url);
+ } catch (Exception e) {
+ bundleInstallWarn(e.getMessage());
+ }
}
}
try {
String bundleName = (String) devBundles.next();
String url = (String) devBundleUrls.get(bundleName);
- context.installBundle(url);
+ Bundle bundle = context.installBundle(url);
+ installBundleNames.add(bundle.getSymbolicName());
info("Installed as dev bundle " + url);
} catch (Exception e) {
bundleInstallWarn(e.getMessage());
}
// Load from distribution
- String slcHome = config.getProperty("slc.home");
+ String slcHome = config.getProperty(PROP_SLC_HOME);
if (slcHome != null) {
File libDir = new File(slcHome + File.separator + "lib");
File[] bundleFiles = libDir.listFiles();
try {
String url = "reference:file:"
+ bundleFiles[i].getCanonicalPath();
- context.installBundle(url);
+ Bundle bundle = context.installBundle(url);
+ installBundleNames.add(bundle.getSymbolicName());
info("INFO: Installed from SLC home " + url);
} catch (Exception e) {
bundleInstallWarn(e.getMessage());
}
// Start bundles
- String bundleStart = config.getProperty("slc.osgi.start",
+ String bundleStart = config.getProperty(PROP_SLC_OSGI_START,
"org.springframework.osgi.extender,org.argeo.slc.detached");
- StringTokenizer stBundleStart = new StringTokenizer(bundleStart, ",");
- while (stBundleStart.hasMoreTokens()) {
- String bundleSymbolicName = stBundleStart.nextToken();
- startBundle(context, bundleSymbolicName);
+
+ if (bundleStart.trim().equals("*")) {
+ for (int i = 0; i < installBundleNames.size(); i++) {
+ String bundleSymbolicName = installBundleNames.get(i)
+ .toString();
+ try {
+ startBundle(context, bundleSymbolicName);
+ } catch (Exception e) {
+ bundleInstallWarn(e.getMessage());
+ }
+ }
+ } else {
+ StringTokenizer stBundleStart = new StringTokenizer(bundleStart,
+ ",");
+ while (stBundleStart.hasMoreTokens()) {
+ String bundleSymbolicName = stBundleStart.nextToken();
+ startBundle(context, bundleSymbolicName);
+ }
}
}
private static void startBundle(BundleContext bundleContext,
String symbolicName) throws BundleException {
+ info("Starting bundle " + symbolicName + "...");
Bundle bundle = findBundleBySymbolicName(bundleContext, symbolicName);
if (bundle != null)
bundle.start();
Bundle[] bundles = bundleContext.getBundles();
for (int i = 0; i < bundles.length; i++) {
Bundle bundle = bundles[i];
- if (bundle.getSymbolicName().equals(symbolicName)) {
+ String bundleSymbolicName = bundle.getSymbolicName();
+ if (bundleSymbolicName == null)
+ throw new RuntimeException("Bundle " + bundle.getBundleId()
+ + " (" + bundle.getLocation()
+ + ") has no symbolic name.");
+
+ if (bundleSymbolicName.equals(symbolicName)) {
return bundle;
}
}
String[] uiArgs = readArgumentsFromLine(config.getProperty(
"slc.detached.appargs", ""));
- if (className == null)
- throw new Exception(
- "A main class has to be defined with the system property slc.detached.appclass");
-
- // Launch main method using reflection
- Class clss = Class.forName(className);
- Class[] mainArgsClasses = new Class[] { uiArgs.getClass() };
- Object[] mainArgs = { uiArgs };
- Method mainMethod = clss.getMethod("main", mainArgsClasses);
- mainMethod.invoke(null, mainArgs);
+ if (className == null) {
+ info("No slc.detached.appclass property define: does not try to launch an app from the standard classpath.");
+ } else {
+ // Launch main method using reflection
+ Class clss = Class.forName(className);
+ Class[] mainArgsClasses = new Class[] { uiArgs.getClass() };
+ Object[] mainArgs = { uiArgs };
+ Method mainMethod = clss.getMethod("main", mainArgsClasses);
+ mainMethod.invoke(null, mainArgs);
+ }
}
/* UTILITIES */