]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.detached.launcher/src/main/java/org/argeo/slc/detached/launcher/Main.java
Facilitate OSGi deployment
[gpl/argeo-slc.git] / org.argeo.slc.detached.launcher / src / main / java / org / argeo / slc / detached / launcher / Main.java
index 6ccdcdff40fec7a211d7c6ef6ad0070fa171996c..d7da1e673910b880aca9da684317a3d50111f180 100644 (file)
@@ -4,6 +4,7 @@ import java.io.File;
 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;
@@ -18,6 +19,11 @@ import org.osgi.framework.BundleContext;
 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) {
@@ -31,7 +37,12 @@ public class Main {
                        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();
@@ -42,10 +53,10 @@ public class Main {
        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
@@ -68,30 +79,41 @@ public class Main {
        }
 
        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());
+                               }
                        }
                }
 
@@ -102,7 +124,8 @@ public class Main {
                        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());
@@ -110,7 +133,7 @@ public class Main {
                }
 
                // 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();
@@ -118,7 +141,8 @@ public class Main {
                                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());
@@ -128,12 +152,26 @@ public class Main {
                }
 
                // 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);
+                       }
                }
        }
 
@@ -153,6 +191,7 @@ public class Main {
 
        private static void startBundle(BundleContext bundleContext,
                        String symbolicName) throws BundleException {
+               info("Starting bundle " + symbolicName + "...");
                Bundle bundle = findBundleBySymbolicName(bundleContext, symbolicName);
                if (bundle != null)
                        bundle.start();
@@ -167,7 +206,13 @@ public class Main {
                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;
                        }
                }
@@ -179,16 +224,16 @@ public class Main {
                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 */