]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - eclipse/plugins/org.argeo.slc.ui.launch/src/main/java/org/argeo/slc/ui/launch/osgi/SlcLaunchShortcut.java
Working version
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.ui.launch / src / main / java / org / argeo / slc / ui / launch / osgi / SlcLaunchShortcut.java
index e7764a731beae57e64056e577543305059ba36cf..a2e1cff3a58f4df4449fff23ffe54fb2d984ca13 100644 (file)
@@ -7,12 +7,16 @@ import java.util.List;
 import java.util.Map;
 import java.util.StringTokenizer;
 
-import org.argeo.slc.ui.launch.SlcUiLaunchPlugin;
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.runtime.Assert;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.debug.core.ILaunchConfigurationType;
 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.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.IStructuredSelection;
@@ -21,12 +25,17 @@ import org.eclipse.pde.core.plugin.IPluginModelBase;
 import org.eclipse.pde.core.plugin.PluginRegistry;
 import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
 import org.eclipse.pde.ui.launcher.OSGiLaunchShortcut;
+import org.eclipse.swt.widgets.DirectoryDialog;
+import org.eclipse.swt.widgets.Display;
 import org.eclipse.swt.widgets.Shell;
 
 public class SlcLaunchShortcut extends OSGiLaunchShortcut {
+       public final static String VMS_PROPERTY_PREFIX = "slc.launch.vm";
+
        private Boolean debug = false;
 
        private String springOsgiExtenderId = "org.springframework.osgi.extender";
+       private String slcSupportEquinoxId = "org.argeo.slc.support.equinox";
        // private String slcAgentId = "org.argeo.slc.agent";
        // private String osgiBootId = "org.argeo.slc.osgiboot";
 
@@ -39,10 +48,10 @@ public class SlcLaunchShortcut extends OSGiLaunchShortcut {
        public SlcLaunchShortcut() {
                super();
                defaultBundlesToStart.add(springOsgiExtenderId);
+               defaultBundlesToStart.add(slcSupportEquinoxId);
                // defaultBundlesToStart.add(slcAgentId);
        }
 
-       @Override
        public void launch(ISelection selection, String mode) {
                this.selection = selection;
                this.name = new StringBuffer();
@@ -62,7 +71,6 @@ public class SlcLaunchShortcut extends OSGiLaunchShortcut {
                bundlesToStart = null;
        }
 
-       @Override
        protected void initializeConfiguration(
                        ILaunchConfigurationWorkingCopy configuration) {
                try {
@@ -82,16 +90,102 @@ public class SlcLaunchShortcut extends OSGiLaunchShortcut {
                        // Update other default information
                        configuration.setAttribute(
                                        IPDELauncherConstants.DEFAULT_AUTO_START, false);
+                       configuration.setAttribute(IPDELauncherConstants.CONFIG_CLEAR_AREA,
+                                       true);
+                       String defaultVmArgs = configuration.getAttribute(
+                                       IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, "");
+                       StringBuffer vmArgs = new StringBuffer(defaultVmArgs);
+                       vmArgs.append(" -Xmx256m");
+
+                       // Add locations of JVMs
+                       addVmSysProperty(vmArgs, "default", JavaRuntime
+                                       .getDefaultVMInstall());
+                       IVMInstallType[] vmTypes = JavaRuntime.getVMInstallTypes();
+                       for (IVMInstallType vmType : vmTypes) {
+                               // System.out.println("vmType: id=" + vmType.getId() + ", name="
+                               // + vmType.getName() + ", toString=" + vmType);
+                               for (IVMInstall vmInstall : vmType.getVMInstalls()) {
+                                       // printVm("", vmInstall);
+                                       // properties based on name
+                                       addVmSysProperty(vmArgs, vmInstall.getName(), vmInstall);
+                                       if (vmInstall instanceof IVMInstall2) {
+                                               // properties based on version
+                                               IVMInstall2 vmInstall2 = (IVMInstall2) vmInstall;
+                                               String version = vmInstall2.getJavaVersion();
+                                               addVmSysProperty(vmArgs, version, vmInstall);
+
+                                               List<String> tokens = new ArrayList<String>();
+                                               StringTokenizer st = new StringTokenizer(version, ".");
+                                               while (st.hasMoreTokens())
+                                                       tokens.add(st.nextToken());
+                                               if (tokens.size() >= 2)
+                                                       addVmSysProperty(vmArgs, tokens.get(0) + "."
+                                                                       + tokens.get(1), vmInstall);
+                                       }
+                               }
+                       }
+                       configuration.setAttribute(
+                                       IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, vmArgs
+                                                       .toString());
+
+                       // Choose working directory
+                       Shell shell = Display.getCurrent().getActiveShell();
+                       DirectoryDialog dirDialog = new DirectoryDialog(shell);
+                       dirDialog.setText("Working Directory");
+                       dirDialog.setMessage("Choose the working directory");
+                       String dir = dirDialog.open();
+                       if (dir != null)
+                               configuration
+                                               .setAttribute(
+                                                               IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY,
+                                                               dir);
 
                } catch (CoreException e) {
-                       Shell shell = SlcUiLaunchPlugin.getDefault().getWorkbench()
-                                       .getActiveWorkbenchWindow().getShell();
+                       Shell shell = Display.getCurrent().getActiveShell();
                        ErrorDialog.openError(shell, "Error",
                                        "Cannot execute SLC launch shortcut", e.getStatus());
                }
 
        }
 
+       protected void addVmSysProperty(StringBuffer vmArgs, String suffix,
+                       IVMInstall vmInstall) {
+               addSysProperty(vmArgs, VMS_PROPERTY_PREFIX + "." + suffix, vmInstall
+                               .getInstallLocation().getPath());
+       }
+
+       protected void addSysProperty(StringBuffer vmArgs, String key, String value) {
+               String str = "-D" + key + "=" + value;
+               if (str.contains(" "))
+                       str = "\"" + str + "\"";
+               vmArgs.append(" " + str);
+       }
+
+       protected void printVm(String prefix, IVMInstall vmInstall) {
+               System.out.println(prefix + " vmInstall: id=" + vmInstall.getId()
+                               + ", name=" + vmInstall.getName() + ", installLocation="
+                               + vmInstall.getInstallLocation() + ", toString=" + vmInstall);
+               if (vmInstall instanceof IVMInstall2) {
+                       IVMInstall2 vmInstall2 = (IVMInstall2) vmInstall;
+                       System.out.println("  vmInstall: javaVersion="
+                                       + vmInstall2.getJavaVersion());
+               }
+               // printVm("Default", JavaRuntime.getDefaultVMInstall());
+               // IExecutionEnvironment[] execEnvs = JavaRuntime
+               // .getExecutionEnvironmentsManager()
+               // .getExecutionEnvironments();
+               // for (IExecutionEnvironment execEnv : execEnvs) {
+               // System.out.println("execEnv: id=" + execEnv.getId() + ", desc="
+               // + execEnv.getDescription());
+               // if (execEnv.getId().startsWith("J2SE")
+               // || execEnv.getId().startsWith("Java")) {
+               // IVMInstall vmInstall = execEnv.getDefaultVM();
+               // printVm("", vmInstall);
+               // }
+               // }
+
+       }
+
        protected String convertBundleList(List<String> bundlesToStart,
                        String original) {
                StringBuffer bufBundles = new StringBuffer(1024);
@@ -179,9 +273,8 @@ public class SlcLaunchShortcut extends OSGiLaunchShortcut {
                }
        }
 
-       @Override
        protected String getName(ILaunchConfigurationType type) {
-               if (name != null)
+               if (name != null && !name.toString().trim().equals(""))
                        return name.toString();
                else
                        return "SLC";