X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=eclipse%2Fplugins%2Forg.argeo.slc.ide.ui%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fide%2Fui%2Flaunch%2Fosgi%2FOsgiLaunchHelper.java;h=31aaf136f68dd9f82fc4f3aaf5fb0617e7c7092b;hb=9b15274f8e1425882bee220a1dd7f9ab9e5aba6f;hp=425352beb42f870c67ddbc312d508784505be086;hpb=1ec88bb547b163d84d4e3eecc87cf78ecbd28f6c;p=gpl%2Fargeo-slc.git diff --git a/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiLaunchHelper.java b/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiLaunchHelper.java index 425352beb..31aaf136f 100644 --- a/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiLaunchHelper.java +++ b/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiLaunchHelper.java @@ -1,8 +1,10 @@ package org.argeo.slc.ide.ui.launch.osgi; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Properties; @@ -10,24 +12,60 @@ import java.util.StringTokenizer; import org.argeo.slc.ide.ui.SlcIdeUiPlugin; import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; +import org.eclipse.core.variables.IStringVariableManager; +import org.eclipse.core.variables.VariablesPlugin; +import org.eclipse.debug.core.ILaunchConfiguration; import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy; import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants; import org.eclipse.jdt.launching.IVMInstall; import org.eclipse.jdt.launching.IVMInstall2; import org.eclipse.jdt.launching.IVMInstallType; import org.eclipse.jdt.launching.JavaRuntime; +import org.eclipse.jface.dialogs.ErrorDialog; import org.eclipse.pde.ui.launcher.IPDELauncherConstants; +import org.eclipse.swt.widgets.Display; -public class OsgiLaunchHelper { +public class OsgiLaunchHelper implements OsgiLauncherConstants { private static Boolean debug = false; + /** Expect properties file to be set as mapped resources */ public static void updateLaunchConfiguration( + ILaunchConfigurationWorkingCopy configuration) { + try { + IFile propertiesFile = (IFile) configuration.getMappedResources()[0]; + propertiesFile.refreshLocal(IResource.DEPTH_ONE, null); + + Properties properties = OsgiLaunchHelper + .readProperties(propertiesFile); + + List bundlesToStart = new ArrayList(); + Map systemPropertiesToAppend = new HashMap(); + OsgiLaunchHelper.interpretProperties(properties, bundlesToStart, + systemPropertiesToAppend); + + File workingDir = getWorkingDirectory(configuration); + File dataDir = new File(workingDir, "data"); + + OsgiLaunchHelper.updateLaunchConfiguration(configuration, + bundlesToStart, systemPropertiesToAppend, dataDir + .getAbsolutePath()); + } catch (Exception e) { + ErrorDialog.openError(Display.getCurrent().getActiveShell(), + "Error", "Cannot read properties", + new Status(IStatus.ERROR, SlcIdeUiPlugin.ID, + e.getMessage(), e)); + return; + } + } + + static void updateLaunchConfiguration( ILaunchConfigurationWorkingCopy configuration, List bundlesToStart, - Map systemPropertiesToAppend, String dir) + Map systemPropertiesToAppend, String dataDir) throws CoreException { // Convert bundle lists String targetBundles = configuration.getAttribute( @@ -43,14 +81,46 @@ public class OsgiLaunchHelper { // Update other default information configuration.setAttribute(IPDELauncherConstants.DEFAULT_AUTO_START, false); - configuration.setAttribute(IPDELauncherConstants.CONFIG_CLEAR_AREA, - true); + + // VM arguments (system properties) String defaultVmArgs = configuration.getAttribute( OsgiLauncherConstants.ATTR_DEFAULT_VM_ARGS, ""); StringBuffer vmArgs = new StringBuffer(defaultVmArgs); - vmArgs.append(" -Xmx256m"); + // Data dir system property + if (dataDir != null) + addSysProperty(vmArgs, OsgiLauncherConstants.ARGEO_OSGI_DATA_DIR, + dataDir); // Add locations of JVMs + if (configuration.getAttribute(ATTR_ADD_JVM_PATHS, false)) + addVms(vmArgs); + + // Add other system properties + for (String key : systemPropertiesToAppend.keySet()) + addSysProperty(vmArgs, key, systemPropertiesToAppend.get(key)); + + vmArgs.append(" ").append( + configuration.getAttribute(ATTR_ADDITIONAL_VM_ARGS, "")); + + configuration.setAttribute( + IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, vmArgs + .toString()); + + // Program arguments + StringBuffer progArgs = new StringBuffer(""); + if (dataDir != null) { + progArgs.append("-data "); + progArgs.append(surroundSpaces(dataDir)); + } + String additionalProgramArgs = configuration.getAttribute( + OsgiLauncherConstants.ATTR_ADDITIONAL_PROGRAM_ARGS, ""); + progArgs.append(' ').append(additionalProgramArgs); + configuration.setAttribute( + IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, + progArgs.toString()); + } + + protected static void addVms(StringBuffer vmArgs) { addVmSysProperty(vmArgs, "default", JavaRuntime.getDefaultVMInstall()); IVMInstallType[] vmTypes = JavaRuntime.getVMInstallTypes(); for (IVMInstallType vmType : vmTypes) { @@ -75,20 +145,6 @@ public class OsgiLaunchHelper { } } - // Add other system properties - for (String key : systemPropertiesToAppend.keySet()) - addSysProperty(vmArgs, key, systemPropertiesToAppend.get(key)); - - configuration.setAttribute( - IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, vmArgs - .toString()); - - // String dir = findWorkingDirectory(); - if (dir != null) - configuration.setAttribute( - IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, - dir); - } protected static void addVmSysProperty(StringBuffer vmArgs, String suffix, @@ -99,10 +155,17 @@ public class OsgiLaunchHelper { protected static void addSysProperty(StringBuffer vmArgs, String key, String value) { + surroundSpaces(value); String str = "-D" + key + "=" + value; - if (str.contains(" ")) - str = "\"" + str + "\""; - vmArgs.append(" " + str); + // surroundSpaces(str); + vmArgs.append(' ').append(str); + } + + protected static String surroundSpaces(String str) { + if (str.indexOf(' ') >= 0) + return '\"' + str + '\"'; + else + return str; } protected static String convertBundleList(List bundlesToStart, @@ -110,29 +173,34 @@ public class OsgiLaunchHelper { StringBuffer bufBundles = new StringBuffer(1024); StringTokenizer stComa = new StringTokenizer(original, ","); boolean first = true; - while (stComa.hasMoreTokens()) { + bundles: while (stComa.hasMoreTokens()) { if (first) first = false; else bufBundles.append(','); - String tkComa = stComa.nextToken(); - int indexAt = tkComa.indexOf('@'); + String bundleId = stComa.nextToken(); + int indexAt = bundleId.indexOf('@'); boolean modified = false; if (indexAt >= 0) { - String bundelId = tkComa.substring(0, indexAt); - - if (bundlesToStart.contains(bundelId)) { - bufBundles.append(bundelId).append('@').append( - "default:true"); - modified = true; - if (debug) - System.out.println("Will start " + bundelId); - } + bundleId = bundleId.substring(0, indexAt); + } + + if (bundleId.endsWith(".source")) { + if (debug) + System.out.println("Skip source bundle " + bundleId); + continue bundles; + } + + if (bundlesToStart.contains(bundleId)) { + bufBundles.append(bundleId).append('@').append("default:true"); + modified = true; + if (debug) + System.out.println("Will start " + bundleId); } if (!modified) - bufBundles.append(tkComa); + bufBundles.append(bundleId); } String output = bufBundles.toString(); return output; @@ -164,9 +232,11 @@ public class OsgiLaunchHelper { Map systemPropertiesToAppend) { String argeoOsgiStart = properties .getProperty(OsgiLauncherConstants.ARGEO_OSGI_START); - StringTokenizer st = new StringTokenizer(argeoOsgiStart, ","); - while (st.hasMoreTokens()) - bundlesToStart.add(st.nextToken()); + if (argeoOsgiStart != null) { + StringTokenizer st = new StringTokenizer(argeoOsgiStart, ","); + while (st.hasMoreTokens()) + bundlesToStart.add(st.nextToken()); + } propKeys: for (Object keyObj : properties.keySet()) { String key = keyObj.toString(); @@ -183,4 +253,39 @@ public class OsgiLaunchHelper { } } + + // Hacked from + // org.eclipse.pde.internal.ui.launcher.LaunchArgumentsHelper.getWorkingDirectory(ILaunchConfiguration) + public static File getWorkingDirectory(ILaunchConfiguration configuration) + throws CoreException { + String working; + try { + working = configuration.getAttribute( + IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY, + new File(".").getCanonicalPath()); //$NON-NLS-1$ + } catch (IOException e) { + working = "${workspace_loc}/../"; //$NON-NLS-1$ + } + File dir = new File(getSubstitutedString(working)); + if (!dir.exists()) + dir.mkdirs(); + return dir; + } + + // Hacked from + // org.eclipse.pde.internal.ui.launcher.LaunchArgumentsHelper.getSubstitutedString(String) + private static String getSubstitutedString(String text) + throws CoreException { + if (text == null) + return ""; //$NON-NLS-1$ + IStringVariableManager mgr = VariablesPlugin.getDefault() + .getStringVariableManager(); + return mgr.performStringSubstitution(text); + } + + // static void initializeConfiguration( + // ILaunchConfigurationWorkingCopy configuration) { + // new OSGiLaunchConfigurationInitializer().initialize(configuration); + // } + }