]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiLaunchHelper.java
Fix issue with RCP launch
[gpl/argeo-slc.git] / plugins / org.argeo.slc.ide.ui / src / main / java / org / argeo / slc / ide / ui / launch / osgi / OsgiLaunchHelper.java
index 5d12fd8cb6f120219fb1eebc77a686bc720f3fd4..11135f208528fdb37eda7a336425c23e4f372665 100644 (file)
@@ -4,6 +4,7 @@ import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -11,6 +12,7 @@ import java.util.Map;
 import java.util.Properties;
 import java.util.Set;
 import java.util.StringTokenizer;
+import java.util.TreeMap;
 import java.util.TreeSet;
 
 import org.argeo.slc.ide.ui.SlcIdeUiPlugin;
@@ -116,16 +118,21 @@ public class OsgiLaunchHelper implements OsgiLauncherConstants {
        }
 
        /** Expects properties file to be set as mapped resources */
+       @SuppressWarnings("unchecked")
        public static void updateLaunchConfiguration(
                        ILaunchConfigurationWorkingCopy wc, Boolean isEclipse) {
                try {
+                       if (debug)
+                               debug("##\n## Launch " + wc.getName() + " - " + new Date()
+                                               + "\n##");
+
                        // Finds the properties file and load it
                        IFile propertiesFile = (IFile) wc.getMappedResources()[0];
                        propertiesFile.refreshLocal(IResource.DEPTH_ONE, null);
                        Properties properties = readProperties(propertiesFile);
 
                        // Extract information from the properties file
-                       List<String> bundlesToStart = new ArrayList<String>();
+                       Map<String, Integer> bundlesToStart = new TreeMap<String, Integer>();
                        Map<String, String> systemPropertiesToAppend = new HashMap<String, String>();
                        String applicationId = interpretProperties(properties,
                                        bundlesToStart, systemPropertiesToAppend);
@@ -148,6 +155,13 @@ public class OsgiLaunchHelper implements OsgiLauncherConstants {
                        updateLaunchConfiguration(wc, bundlesToStart,
                                        systemPropertiesToAppend, dataDir.getAbsolutePath(),
                                        isEclipse);
+
+                       if (debug) {
+                               Map<String, ?> attrs = new TreeMap<String, Object>(
+                                               (Map<String, String>) wc.getAttributes());
+                               for (String key : attrs.keySet())
+                                       OsgiLaunchHelper.debug(key + "=" + attrs.get(key));
+                       }
                } catch (Exception e) {
                        e.printStackTrace();
                        Shell shell = SlcIdeUiPlugin.getDefault().getWorkbench()
@@ -167,7 +181,8 @@ public class OsgiLaunchHelper implements OsgiLauncherConstants {
         * UI.
         */
        protected static void updateLaunchConfiguration(
-                       ILaunchConfigurationWorkingCopy wc, List<String> bundlesToStart,
+                       ILaunchConfigurationWorkingCopy wc,
+                       Map<String, Integer> bundlesToStart,
                        Map<String, String> systemPropertiesToAppend, String dataDir,
                        Boolean isEclipse) throws CoreException {
                // Convert bundle lists
@@ -205,6 +220,10 @@ public class OsgiLaunchHelper implements OsgiLauncherConstants {
                // Update other default information
                wc.setAttribute(IPDELauncherConstants.DEFAULT_AUTO_START, false);
 
+               wc.setAttribute(IPDELauncherConstants.USE_CUSTOM_FEATURES, false);
+               if (!isEclipse)
+                       wc.setAttribute(IPDELauncherConstants.USE_DEFAULT, true);
+
                // VM arguments (system properties)
                String defaultVmArgs = wc.getAttribute(
                                OsgiLauncherConstants.ATTR_DEFAULT_VM_ARGS, "");
@@ -273,21 +292,17 @@ public class OsgiLaunchHelper implements OsgiLauncherConstants {
         *         found
         */
        protected static String interpretProperties(Properties properties,
-                       List<String> bundlesToStart,
+                       Map<String, Integer> bundlesToStart,
                        Map<String, String> systemPropertiesToAppend) {
-               String argeoOsgiStart = properties
-                               .getProperty(OsgiLauncherConstants.ARGEO_OSGI_START);
-               if (argeoOsgiStart != null) {
-                       StringTokenizer st = new StringTokenizer(argeoOsgiStart, ",");
-                       while (st.hasMoreTokens())
-                               bundlesToStart.add(st.nextToken());
-               }
+               computeBundlesToStart(bundlesToStart, properties, null);
 
                String applicationId = null;
                propKeys: for (Object keyObj : properties.keySet()) {
                        String key = keyObj.toString();
                        if (OsgiLauncherConstants.ARGEO_OSGI_START.equals(key))
                                continue propKeys;
+                       if (key.startsWith(OsgiLauncherConstants.ARGEO_OSGI_START + "."))
+                               continue propKeys;
                        else if (OsgiLauncherConstants.ARGEO_OSGI_BUNDLES.equals(key))
                                continue propKeys;
                        else if (OsgiLauncherConstants.ARGEO_OSGI_LOCATIONS.equals(key))
@@ -316,7 +331,6 @@ public class OsgiLaunchHelper implements OsgiLauncherConstants {
                IVMInstallType[] vmTypes = JavaRuntime.getVMInstallTypes();
                for (IVMInstallType vmType : vmTypes) {
                        for (IVMInstall vmInstall : vmType.getVMInstalls()) {
-                               // printVm("", vmInstall);
                                // properties based on name
                                addVmSysProperty(vmArgs, vmInstall.getName(), vmInstall);
                                if (vmInstall instanceof IVMInstall2) {
@@ -357,10 +371,8 @@ public class OsgiLaunchHelper implements OsgiLauncherConstants {
         * Reformat the bundle list in order to reflect which bundles have to be
         * started.
         */
-       protected static String convertBundleList(List<String> bundlesToStart,
-                       String original) {
-               debug("Original bundle list: " + original);
-
+       protected static String convertBundleList(
+                       Map<String, Integer> bundlesToStart, String original) {
                StringTokenizer stComa = new StringTokenizer(original, ",");
                // sort by bundle symbolic name
                Set<String> bundleIds = new TreeSet<String>();
@@ -381,7 +393,7 @@ public class OsgiLaunchHelper implements OsgiLauncherConstants {
                        // We can now rely on bundleId value
 
                        if (bundleId.endsWith(".source")) {
-                               debug("Skip source bundle " + bundleId);
+                               // debug("Skip source bundle " + bundleId);
                                continue bundles;
                        } else if (bundleId
                                        .equals(IPDEBuildConstants.BUNDLE_SIMPLE_CONFIGURATOR)) {
@@ -399,10 +411,14 @@ public class OsgiLaunchHelper implements OsgiLauncherConstants {
                        else
                                bufBundles.append(',');
                        boolean modified = false;
-                       if (bundlesToStart.contains(bundleId)) {
-                               bufBundles.append(bundleId).append('@').append("default:true");
+                       if (bundlesToStart.containsKey(bundleId)) {
+                               Integer startLevel = bundlesToStart.get(bundleId);
+                               String startLevelStr = startLevel != null ? startLevel
+                                               .toString() : "default";
+                               bufBundles.append(bundleId).append('@').append(startLevelStr)
+                                               .append(":true");
                                modified = true;
-                               debug("Will start " + bundleId);
+                               debug("Will start " + bundleId + " at level " + startLevelStr);
                        }
 
                        if (!modified)
@@ -448,6 +464,58 @@ public class OsgiLaunchHelper implements OsgiLauncherConstants {
                return props;
        }
 
+       /** Determines the start levels for the bundles */
+       private static void computeBundlesToStart(
+                       Map<String, Integer> bundlesToStart, Properties properties,
+                       Integer defaultStartLevel) {
+
+               // default (and previously, only behaviour)
+               appendBundlesToStart(bundlesToStart, defaultStartLevel,
+                               properties.getProperty(OsgiLauncherConstants.ARGEO_OSGI_START,
+                                               ""));
+
+               // list argeo.osgi.start.* system properties
+               Iterator<Object> keys = properties.keySet().iterator();
+               final String prefix = OsgiLauncherConstants.ARGEO_OSGI_START + ".";
+               while (keys.hasNext()) {
+                       String key = (String) keys.next();
+                       if (key.startsWith(prefix)) {
+                               Integer startLevel;
+                               String suffix = key.substring(prefix.length());
+                               String[] tokens = suffix.split("\\.");
+                               if (tokens.length > 0 && !tokens[0].trim().equals(""))
+                                       try {
+                                               // first token is start level
+                                               startLevel = new Integer(tokens[0]);
+                                       } catch (NumberFormatException e) {
+                                               startLevel = defaultStartLevel;
+                                       }
+                               else
+                                       startLevel = defaultStartLevel;
+
+                               // append bundle names
+                               String bundleNames = properties.getProperty(key);
+                               appendBundlesToStart(bundlesToStart, startLevel, bundleNames);
+                       }
+               }
+       }
+
+       /** Append a comma-separated list of bundles to the start levels. */
+       private static void appendBundlesToStart(
+                       Map<String, Integer> bundlesToStart, Integer startLevel, String str) {
+               if (str == null || str.trim().equals(""))
+                       return;
+
+               String[] bundleNames = str.split(",");
+               for (int i = 0; i < bundleNames.length; i++) {
+                       if (bundleNames[i] != null && !bundleNames[i].trim().equals(""))
+                               bundlesToStart.put(bundleNames[i], startLevel);
+               }
+       }
+
+       /*
+        * HACKED UTILITIES
+        */
        // Hacked from
        // org.eclipse.pde.internal.ui.launcher.LaunchArgumentsHelper.getWorkingDirectory(ILaunchConfiguration)
        private static File getWorkingDirectory(ILaunchConfiguration configuration)