Introduce specific tab to OSGi boot
authorMathieu Baudier <mbaudier@argeo.org>
Tue, 20 Apr 2010 14:50:52 +0000 (14:50 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Tue, 20 Apr 2010 14:50:52 +0000 (14:50 +0000)
Clean up code

git-svn-id: https://svn.argeo.org/slc/trunk@3480 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

eclipse/plugins/org.argeo.slc.ide.ui/META-INF/MANIFEST.MF
eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/SlcIdeUiPlugin.java
eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/AbstractOsgiLaunchShortcut.java
eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiBootEquinoxLaunchConfiguration.java
eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiBootLaunchShortcut.java
eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiBootLauncherTabGroup.java
eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiBootMainTab.java [new file with mode: 0644]
eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiLaunchHelper.java
eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiLauncherConstants.java

index 0503ead52409515b1c86c47abefb66a8bd9547b7..f89640e16ba8fffb49e6a9caecb597f56090befb 100644 (file)
@@ -19,3 +19,4 @@ Require-Bundle: org.eclipse.ui,
  org.eclipse.jdt.ui;bundle-version="3.4.2"
 Bundle-RequiredExecutionEnvironment: J2SE-1.5
 Bundle-Vendor: Argeo
+Bundle-ActivationPolicy: lazy
index c9ddd4b8d1992a8345304d921b1539fab725e14b..2c288cff598f2a85148c2ca3e305f5df1894912d 100644 (file)
@@ -1,5 +1,10 @@
 package org.argeo.slc.ide.ui;\r
 \r
+import java.net.URL;\r
+\r
+import org.eclipse.jface.resource.ImageDescriptor;\r
+import org.eclipse.jface.resource.ImageRegistry;\r
+import org.eclipse.swt.graphics.Image;\r
 import org.eclipse.ui.plugin.AbstractUIPlugin;\r
 import org.osgi.framework.BundleContext;\r
 \r
@@ -41,4 +46,16 @@ public class SlcIdeUiPlugin extends AbstractUIPlugin {
                return plugin;\r
        }\r
 \r
+       public Image getImage(String relativeURL) {\r
+               ImageRegistry imageRegistry = getImageRegistry();\r
+               Image image = imageRegistry.get(relativeURL);\r
+               if (image == null) {\r
+                       URL imageURL = getBundle().getEntry(relativeURL);\r
+                       ImageDescriptor descriptor = ImageDescriptor\r
+                                       .createFromURL(imageURL);\r
+                       image = descriptor.createImage();\r
+                       imageRegistry.put(relativeURL, image);\r
+               }\r
+               return image;\r
+       }\r
 }\r
index 757e528bd203775f4a1659bbd227e5025f724534..c9337d85f56a3b114f39db1268666f3ac5e5e9f8 100644 (file)
@@ -48,13 +48,15 @@ public abstract class AbstractOsgiLaunchShortcut extends OSGiLaunchShortcut {
                        String defaultProgArgs = configuration.getAttribute(
                                        IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS,
                                        "");
-                       configuration.setAttribute(
-                                       OsgiLauncherConstants.ATTR_DEFAULT_PROGRAM_ARGS,
-                                       defaultProgArgs);
+                       // configuration.setAttribute(
+                       // OsgiLauncherConstants.ATTR_DEFAULT_PROGRAM_ARGS,
+                       // defaultProgArgs);
 
+                       configuration.setAttribute(
+                                       IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY,
+                                       findWorkingDirectory());
                        OsgiLaunchHelper.updateLaunchConfiguration(configuration,
-                                       bundlesToStart, systemPropertiesToAppend,
-                                       findWorkingDirectory(), null);
+                                       bundlesToStart, systemPropertiesToAppend, null);
                } catch (CoreException e) {
                        Shell shell = Display.getCurrent().getActiveShell();
                        ErrorDialog.openError(shell, "Error",
index cbd84cbe75bf3ee3b3b48654c6e56b03ba2b40f7..c00b3075224ebb86d5e1f5509de12090faa185d4 100644 (file)
@@ -1,12 +1,5 @@
 package org.argeo.slc.ide.ui.launch.osgi;
 
-import java.io.File;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Properties;
-
 import org.argeo.slc.ide.ui.SlcIdeUiPlugin;
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IResource;
@@ -15,10 +8,7 @@ import org.eclipse.core.runtime.IProgressMonitor;
 import org.eclipse.debug.core.ILaunch;
 import org.eclipse.debug.core.ILaunchConfiguration;
 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
-import org.eclipse.jface.dialogs.ErrorDialog;
 import org.eclipse.pde.ui.launcher.EquinoxLaunchConfiguration;
-import org.eclipse.pde.ui.launcher.OSGiLaunchShortcut;
-import org.eclipse.swt.widgets.Display;
 
 public class OsgiBootEquinoxLaunchConfiguration extends
                EquinoxLaunchConfiguration {
@@ -44,44 +34,9 @@ public class OsgiBootEquinoxLaunchConfiguration extends
        protected void preLaunchCheck(ILaunchConfiguration configuration,
                        ILaunch launch, IProgressMonitor monitor) throws CoreException {
                // System.out.println("Launching... " + launch);
-               IFile propertiesFile = (IFile) configuration.getMappedResources()[0];
-
-               Properties properties = null;
-               try {
-                       properties = OsgiLaunchHelper.readProperties(propertiesFile);
-               } catch (CoreException e) {
-                       ErrorDialog.openError(Display.getCurrent().getActiveShell(),
-                                       "Error", "Cannot execute launch shortcut", e.getStatus());
-                       return;
-               }
-
-               List<String> bundlesToStart = new ArrayList<String>();
-               Map<String, String> systemPropertiesToAppend = new HashMap<String, String>();
-               OsgiLaunchHelper.interpretProperties(properties, bundlesToStart,
-                               systemPropertiesToAppend);
-
-               File workingDir = getWorkingDirectory(configuration);
-
                ILaunchConfigurationWorkingCopy wc = configuration.getWorkingCopy();
-
-               // Reinitialize using standard OSGi launch shortcut
-               // Kind of a hack but it lacks extension capabilities and it is still
-               // cleaner than forking the code (which would imply a lot of fork indeed
-               // because of all the internal classes)
-               new OSGiLaunchShortcut() {
-                       @Override
-                       public void initializeConfiguration(
-                                       ILaunchConfigurationWorkingCopy configuration) {
-                               // TODO Auto-generated method stub
-                               super.initializeConfiguration(configuration);
-                       }
-               }.initializeConfiguration(wc);
-
-               OsgiLaunchHelper.updateLaunchConfiguration(wc, bundlesToStart,
-                               systemPropertiesToAppend, null, new File(workingDir, "data")
-                                               .getAbsolutePath());
+               OsgiLaunchHelper.updateLaunchConfiguration(wc);
                wc.doSave();
-
                super.preLaunchCheck(configuration, launch, monitor);
 
                // Note that if a Java project contains a build.properties it has to
index 513ba50a6c600d32d0b1a730898595ea55891e04..3dbdbb19da7611d3b33f5419d4f05bf516c4eed2 100644 (file)
@@ -1,7 +1,10 @@
 package org.argeo.slc.ide.ui.launch.osgi;
 
+import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.Iterator;
-import java.util.Properties;
+import java.util.List;
+import java.util.Map;
 
 import org.eclipse.core.resources.IFile;
 import org.eclipse.core.resources.IFolder;
@@ -9,14 +12,24 @@ import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IPath;
+import org.eclipse.debug.core.ILaunchConfiguration;
+import org.eclipse.debug.core.ILaunchConfigurationType;
 import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
+import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
 import org.eclipse.jface.dialogs.ErrorDialog;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.pde.ui.launcher.IPDELauncherConstants;
+import org.eclipse.pde.ui.launcher.OSGiLaunchShortcut;
 import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
 
-public class OsgiBootLaunchShortcut extends AbstractOsgiLaunchShortcut {
+public class OsgiBootLaunchShortcut extends OSGiLaunchShortcut implements
+               OsgiLauncherConstants {
+       protected StringBuffer name = null;
        protected IFile propertiesFile = null;
+       protected List<String> bundlesToStart = new ArrayList<String>();
+       protected Map<String, String> systemPropertiesToAppend = new HashMap<String, String>();
 
        @Override
        protected String getLaunchConfigurationTypeName() {
@@ -37,20 +50,23 @@ public class OsgiBootLaunchShortcut extends AbstractOsgiLaunchShortcut {
 
                name = new StringBuffer(extractName(propertiesFile));
 
-               Properties properties = null;
-               try {
-                       properties = OsgiLaunchHelper.readProperties(propertiesFile);
-               } catch (CoreException e) {
-                       ErrorDialog.openError(Display.getCurrent().getActiveShell(),
-                                       "Error", "Cannot execute launch shortcut", e.getStatus());
-                       return;
-               }
-
-               OsgiLaunchHelper.interpretProperties(properties, bundlesToStart,
-                               systemPropertiesToAppend);
+               // Properties properties = null;
+               // try {
+               // properties = OsgiLaunchHelper.readProperties(propertiesFile);
+               // } catch (CoreException e) {
+               // ErrorDialog.openError(Display.getCurrent().getActiveShell(),
+               // "Error", "Cannot execute launch shortcut", e.getStatus());
+               // return;
+               // }
+               //
+               // OsgiLaunchHelper.interpretProperties(properties, bundlesToStart,
+               // systemPropertiesToAppend);
                super.launch(selection, mode);
-               
-               propertiesFile = null;
+               // name = null;
+               // bundlesToStart.clear();
+               // systemPropertiesToAppend.clear();
+               //
+               // propertiesFile = null;
        }
 
        @Override
@@ -59,13 +75,34 @@ public class OsgiBootLaunchShortcut extends AbstractOsgiLaunchShortcut {
                IResource[] resources = { propertiesFile };
                configuration.setMappedResources(resources);
                super.initializeConfiguration(configuration);
+
+               try {
+                       configuration.setAttribute(ATTR_ADD_JVM_PATHS, false);
+                       configuration.setAttribute(ATTR_ADDITIONAL_VM_ARGS, "-Xmx128m");
+                       configuration
+                                       .setAttribute(ATTR_ADDITIONAL_PROGRAM_ARGS, "-console");
+
+                       // Defaults
+                       String originalVmArgs = configuration.getAttribute(
+                                       IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS, "");
+                       configuration.setAttribute(ATTR_DEFAULT_VM_ARGS, originalVmArgs);
+                       configuration.setAttribute(IPDELauncherConstants.CONFIG_CLEAR_AREA,
+                                       true);
+
+                       configuration.setAttribute(
+                                       IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY,
+                                       findWorkingDirectory());
+
+                       OsgiLaunchHelper.updateLaunchConfiguration(configuration);
+               } catch (CoreException e) {
+                       Shell shell = Display.getCurrent().getActiveShell();
+                       ErrorDialog.openError(shell, "Error",
+                                       "Cannot execute initalize configuration", e.getStatus());
+               }
        }
 
-       @Override
        protected String findWorkingDirectory() {
                try {
-                       // String relPath = "exec/" + extractName(propertiesFile);
-
                        IProject project = propertiesFile.getProject();
                        IPath parent = propertiesFile.getProjectRelativePath()
                                        .removeLastSegments(1);
@@ -77,18 +114,6 @@ public class OsgiBootLaunchShortcut extends AbstractOsgiLaunchShortcut {
                                                        extractName(propertiesFile)));
                        if (!launchFolder.exists())
                                launchFolder.create(true, true, null);
-
-                       // IPath execDirPath = propertiesFile.getFullPath()
-                       // .removeLastSegments(1).append(relPath);
-                       // File baseDir = propertiesFile.getRawLocation().toFile()
-                       // .getParentFile();
-                       // File execDir = new File(baseDir.getCanonicalPath()
-                       // + File.separatorChar
-                       // + relPath.replace('/', File.separatorChar));
-                       // File execDir = execDirPath.toFile();
-                       // execDir.mkdirs();
-                       // return "${workspace_loc:" + execDirPath.toString().substring(1)
-                       // + "}";
                        return "${workspace_loc:"
                                        + launchFolder.getFullPath().toString().substring(1) + "}";
                } catch (Exception e) {
@@ -103,4 +128,20 @@ public class OsgiBootLaunchShortcut extends AbstractOsgiLaunchShortcut {
                return pathNoExt.segment(pathNoExt.segmentCount() - 1);
 
        }
+
+       protected String getName(ILaunchConfigurationType type) {
+               if (name != null && !name.toString().trim().equals(""))
+                       return name.toString();
+               else
+                       return "SLC";
+       }
+
+       @Override
+       protected boolean isGoodMatch(ILaunchConfiguration configuration) {
+               if (name != null) {
+                       return name.toString().equals(configuration.getName());
+               }
+               return super.isGoodMatch(configuration);
+       }
+
 }
index a184135867240951b83dcb6ebc8948cc4f4a3685..d7fa841a542eec1b110e76a0f3900d220a8697c7 100644 (file)
@@ -1,10 +1,12 @@
 package org.argeo.slc.ide.ui.launch.osgi;
 
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.debug.core.ILaunchConfiguration;
+import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
 import org.eclipse.debug.ui.CommonTab;
 import org.eclipse.debug.ui.EnvironmentTab;
 import org.eclipse.debug.ui.ILaunchConfigurationDialog;
 import org.eclipse.debug.ui.ILaunchConfigurationTab;
-import org.eclipse.jdt.debug.ui.launchConfigurations.JavaArgumentsTab;
 import org.eclipse.pde.ui.launcher.BundlesTab;
 import org.eclipse.pde.ui.launcher.OSGiLauncherTabGroup;
 import org.eclipse.pde.ui.launcher.OSGiSettingsTab;
@@ -15,8 +17,30 @@ public class OsgiBootLauncherTabGroup extends OSGiLauncherTabGroup {
        @Override
        public void createTabs(ILaunchConfigurationDialog dialog, String mode) {
                ILaunchConfigurationTab[] tabs = new ILaunchConfigurationTab[] {
-                               new JavaArgumentsTab(), new EnvironmentTab(), new BundlesTab(),
-                               new OSGiSettingsTab(), new TracingTab(), new CommonTab() };
+                               new OsgiBootMainTab(), new EnvironmentTab(), new BundlesTab() {
+                                       private boolean activating = false;
+
+                                       @Override
+                                       public void performApply(
+                                                       ILaunchConfigurationWorkingCopy config) {
+                                               System.out.println("performApply");
+                                               super.performApply(config);
+                                               if (activating) {
+                                                       try {
+                                                               config.doSave();
+                                                       } catch (CoreException e) {
+                                                               e.printStackTrace();
+                                                       }
+                                                       activating = false;
+                                               }
+                                       }
+
+                                       @Override
+                                       public void activated(
+                                                       ILaunchConfigurationWorkingCopy workingCopy) {
+                                               activating = true;
+                                       }
+                               }, new OSGiSettingsTab(), new TracingTab(), new CommonTab() };
                setTabs(tabs);
        }
 
diff --git a/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiBootMainTab.java b/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiBootMainTab.java
new file mode 100644 (file)
index 0000000..95d32eb
--- /dev/null
@@ -0,0 +1,220 @@
+package org.argeo.slc.ide.ui.launch.osgi;
+
+import org.argeo.slc.ide.ui.SlcIdeUiPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.debug.core.ILaunchConfiguration;
+import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
+import org.eclipse.debug.ui.AbstractLaunchConfigurationTab;
+import org.eclipse.jface.dialogs.Dialog;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.ModifyEvent;
+import org.eclipse.swt.events.ModifyListener;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.graphics.Image;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.layout.GridLayout;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Group;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.swt.widgets.Text;
+
+public class OsgiBootMainTab extends AbstractLaunchConfigurationTab implements
+               OsgiLauncherConstants {
+       private Listener listener = new Listener();
+       private Button addJvmPaths;
+       private Text additionalVmArgs;
+
+       private Text additionalProgramArgs;
+
+       public void createControl(Composite parent) {
+               Composite container = new Composite(parent, SWT.NONE);
+               container.setLayout(new GridLayout());
+               container.setLayoutData(new GridData(GridData.FILL_BOTH));
+               createAdditionalProgramArgs(container);
+               createAdditionalVmArgumentBlock(container);
+               Dialog.applyDialogFont(container);
+               setControl(container);
+       }
+
+       protected void createAdditionalProgramArgs(Composite parent) {
+               Group container = new Group(parent, SWT.NONE);
+               container.setText("Additional Program Arguments");
+               GridLayout layout = new GridLayout();
+               layout.numColumns = 2;
+               container.setLayout(layout);
+               container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
+
+               additionalProgramArgs = new Text(container, SWT.MULTI | SWT.WRAP
+                               | SWT.BORDER | SWT.V_SCROLL);
+               GridData gd = new GridData(GridData.FILL_BOTH);
+               gd.heightHint = 60;
+               gd.widthHint = 100;
+               gd.horizontalSpan = 2;
+               additionalProgramArgs.setLayoutData(gd);
+               additionalProgramArgs.addModifyListener(listener);
+       }
+
+       protected void createAdditionalVmArgumentBlock(Composite parent) {
+               Group container = new Group(parent, SWT.NONE);
+               container.setText("Additional VM Arguments");
+               GridLayout layout = new GridLayout();
+               layout.numColumns = 2;
+               container.setLayout(layout);
+               container.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
+
+               additionalVmArgs = new Text(container, SWT.MULTI | SWT.WRAP
+                               | SWT.BORDER | SWT.V_SCROLL);
+               GridData gd = new GridData(GridData.FILL_BOTH);
+               gd.heightHint = 60;
+               gd.widthHint = 100;
+               gd.horizontalSpan = 2;
+               additionalVmArgs.setLayoutData(gd);
+               additionalVmArgs.addModifyListener(listener);
+
+               addJvmPaths = new Button(container, SWT.CHECK);
+               addJvmPaths.addSelectionListener(listener);
+               new Label(container, SWT.NONE)
+                               .setText("Add workspace JVM paths as non-standard system properties");
+
+       }
+
+       public String getName() {
+               return "OSGi Boot";
+       }
+
+       @Override
+       public Image getImage() {
+               return SlcIdeUiPlugin.getDefault().getImage("icons/slc-launch.gif");
+       }
+
+       public void initializeFrom(ILaunchConfiguration configuration) {
+               // System.out.println("initializeFrom");
+               try {
+                       additionalProgramArgs.setText(configuration.getAttribute(
+                                       ATTR_ADDITIONAL_PROGRAM_ARGS, ""));
+                       addJvmPaths.setSelection(configuration.getAttribute(
+                                       ATTR_ADD_JVM_PATHS, false));
+                       additionalVmArgs.setText(configuration.getAttribute(
+                                       ATTR_ADDITIONAL_VM_ARGS, ""));
+                       // readProperties(configuration);
+               } catch (CoreException e) {
+                       e.printStackTrace();
+               }
+       }
+
+       public void performApply(ILaunchConfigurationWorkingCopy configuration) {
+               // System.out.println("performApply");
+               configuration.setAttribute(ATTR_ADDITIONAL_PROGRAM_ARGS,
+                               additionalProgramArgs.getText());
+               configuration.setAttribute(ATTR_ADDITIONAL_VM_ARGS, additionalVmArgs
+                               .getText());
+               configuration.setAttribute(ATTR_ADD_JVM_PATHS, addJvmPaths
+                               .getSelection());
+               // writeProperties(configuration);
+
+               OsgiLaunchHelper.updateLaunchConfiguration(configuration);
+       }
+
+       public void setDefaults(ILaunchConfigurationWorkingCopy configuration) {
+               // System.out.println("setDefaults");
+               configuration.setAttribute(ATTR_ADD_JVM_PATHS, false);
+               configuration.setAttribute(ATTR_ADDITIONAL_VM_ARGS, "-Xmx128m");
+               configuration.setAttribute(ATTR_ADDITIONAL_PROGRAM_ARGS, "-console");
+       }
+
+       @Override
+       public void activated(ILaunchConfigurationWorkingCopy workingCopy) {
+               initializeFrom(workingCopy);
+               try {
+                       workingCopy.doSave();
+               } catch (CoreException e) {
+                       e.printStackTrace();
+               }
+       }
+
+       @Override
+       public void deactivated(ILaunchConfigurationWorkingCopy workingCopy) {
+               // do nothing
+       }
+
+       class Listener extends SelectionAdapter implements ModifyListener {
+               public void widgetSelected(SelectionEvent e) {
+                       // Object source = e.getSource();
+                       // setDirty(true);
+                       updateLaunchConfigurationDialog();
+               }
+
+               public void modifyText(ModifyEvent e) {
+                       // System.out.println("modifyText : " + e);
+                       // setDirty(true);
+                       updateLaunchConfigurationDialog();
+               }
+       }
+
+       // private void readProperties(ILaunchConfiguration configuration) {
+       // BufferedReader reader = null;
+       // try {
+       // IFile propertiesFile = (IFile) configuration.getMappedResources()[0];
+       // propertiesFile.refreshLocal(IResource.DEPTH_ONE, null);
+       // reader = new BufferedReader(new InputStreamReader(propertiesFile
+       // .getContents()));
+       // String line = null;
+       // StringBuffer buf = new StringBuffer("");
+       // while ((line = reader.readLine()) != null) {
+       // buf.append(line);
+       // buf.append("\n");
+       // }
+       // propertiesText.setText(buf.toString());
+       // } catch (CoreException e) {
+       // ErrorDialog.openError(Display.getCurrent().getActiveShell(),
+       // "Error", "Cannot read properties", e.getStatus());
+       // return;
+       // } catch (Exception e) {
+       // ErrorDialog.openError(Display.getCurrent().getActiveShell(),
+       // "Error", "Cannot read properties",
+       // new Status(IStatus.ERROR, SlcIdeUiPlugin.ID,
+       // e.getMessage(), e));
+       // return;
+       // } finally {
+       // if (reader != null)
+       // try {
+       // reader.close();
+       // } catch (IOException e) {
+       // // silent
+       // }
+       // }
+       //
+       // }
+       //
+       // private void writeProperties(ILaunchConfiguration configuration) {
+       // InputStream in = null;
+       // IFile propertiesFile = null;
+       // try {
+       // propertiesFile = (IFile) configuration.getMappedResources()[0];
+       // in = new ByteArrayInputStream(propertiesText.getText().getBytes());
+       // propertiesFile.setContents(in, true, true, null);
+       // propertiesFile.refreshLocal(IResource.DEPTH_ONE, null);
+       // } catch (CoreException e) {
+       // ErrorDialog.openError(Display.getCurrent().getActiveShell(),
+       // "Error", "Cannot write properties", e.getStatus());
+       // return;
+       // } catch (Exception e) {
+       // ErrorDialog.openError(Display.getCurrent().getActiveShell(),
+       // "Error", "Cannot write properties",
+       // new Status(IStatus.ERROR, SlcIdeUiPlugin.ID,
+       // e.getMessage(), e));
+       // return;
+       // } finally {
+       // if (in != null)
+       // try {
+       // in.close();
+       // } catch (IOException e) {
+       // // silent
+       // }
+       // }
+       //
+       // }
+
+}
index ebf41c0a4387e52cb537f92b7f3084a980e4ac47..253a82afff007924286368ea02da6fe9aa596228 100644 (file)
@@ -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;
@@ -13,22 +15,74 @@ import org.eclipse.core.resources.IFile;
 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.pde.ui.launcher.OSGiLaunchConfigurationInitializer;
+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];
+
+                       Properties properties = OsgiLaunchHelper
+                                       .readProperties(propertiesFile);
+
+                       List<String> bundlesToStart = new ArrayList<String>();
+                       Map<String, String> systemPropertiesToAppend = new HashMap<String, String>();
+                       OsgiLaunchHelper.interpretProperties(properties, bundlesToStart,
+                                       systemPropertiesToAppend);
+
+                       // Reinitialize using standard OSGi launch shortcut
+                       // Kind of a hack but it lacks extension capabilities and it is
+                       // still
+                       // cleaner than forking the code (which would imply a lot of fork
+                       // indeed
+                       // because of all the internal classes)
+                       // new OSGiLaunchShortcut() {
+                       // @Override
+                       // public void initializeConfiguration(
+                       // ILaunchConfigurationWorkingCopy configuration) {
+                       // // TODO Auto-generated method stub
+                       // super.initializeConfiguration(configuration);
+                       // }
+                       // }.initializeConfiguration(configuration);
+
+                       // initializeConfiguration(configuration);
+
+                       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;
+               }
+       }
+
        public static void updateLaunchConfiguration(
                        ILaunchConfigurationWorkingCopy configuration,
                        List<String> bundlesToStart,
-                       Map<String, String> systemPropertiesToAppend, String workingDir,
-                       String dataDir) throws CoreException {
+                       Map<String, String> systemPropertiesToAppend, String dataDir)
+                       throws CoreException {
                // Convert bundle lists
                String targetBundles = configuration.getAttribute(
                                IPDELauncherConstants.TARGET_BUNDLES, "");
@@ -43,37 +97,15 @@ public class OsgiLaunchHelper {
                // Update other default information
                configuration.setAttribute(IPDELauncherConstants.DEFAULT_AUTO_START,
                                false);
-               configuration.setAttribute(IPDELauncherConstants.CONFIG_CLEAR_AREA,
-                               true);
                String defaultVmArgs = configuration.getAttribute(
                                OsgiLauncherConstants.ATTR_DEFAULT_VM_ARGS, "");
                StringBuffer vmArgs = new StringBuffer(defaultVmArgs);
-               vmArgs.append(" -Xmx256m");
+               vmArgs.append(" "
+                               + configuration.getAttribute(ATTR_ADDITIONAL_VM_ARGS, ""));
 
                // Add locations of JVMs
-               addVmSysProperty(vmArgs, "default", JavaRuntime.getDefaultVMInstall());
-               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) {
-                                       // 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);
-                               }
-                       }
-               }
+               if (configuration.getAttribute(ATTR_ADD_JVM_PATHS, false))
+                       addVms(vmArgs);
 
                // Add other system properties
                for (String key : systemPropertiesToAppend.keySet())
@@ -88,9 +120,8 @@ public class OsgiLaunchHelper {
                                                .toString());
 
                // Program arguments
-               String defaultProgArgs = configuration.getAttribute(
-                               OsgiLauncherConstants.ATTR_DEFAULT_PROGRAM_ARGS, "");
-               StringBuffer progArgs = new StringBuffer(defaultProgArgs);
+               StringBuffer progArgs = new StringBuffer(configuration.getAttribute(
+                               OsgiLauncherConstants.ATTR_ADDITIONAL_PROGRAM_ARGS, ""));
                if (dataDir != null) {
                        progArgs.append(" -data ");
                        progArgs.append(surroundSpaces(dataDir));
@@ -100,10 +131,33 @@ public class OsgiLaunchHelper {
                                progArgs.toString());
 
                // String dir = findWorkingDirectory();
-               if (workingDir != null)
-                       configuration.setAttribute(
-                                       IJavaLaunchConfigurationConstants.ATTR_WORKING_DIRECTORY,
-                                       workingDir);
+
+       }
+
+       protected static void addVms(StringBuffer vmArgs) {
+               addVmSysProperty(vmArgs, "default", JavaRuntime.getDefaultVMInstall());
+               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) {
+                                       // 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);
+                               }
+                       }
+               }
 
        }
 
@@ -115,14 +169,15 @@ public class OsgiLaunchHelper {
 
        protected static void addSysProperty(StringBuffer vmArgs, String key,
                        String value) {
+               surroundSpaces(value);
                String str = "-D" + key + "=" + value;
-               surroundSpaces(str);
+               // surroundSpaces(str);
                vmArgs.append(" " + str);
        }
 
        protected static String surroundSpaces(String str) {
-               if (str.contains(" "))
-                       return "\"" + str + "\"";
+               if (str.indexOf(' ') >= 0)
+                       return '\"' + str + '\"';
                else
                        return str;
        }
@@ -196,7 +251,7 @@ public class OsgiLaunchHelper {
                        while (st.hasMoreTokens())
                                bundlesToStart.add(st.nextToken());
                }
-               
+
                propKeys: for (Object keyObj : properties.keySet()) {
                        String key = keyObj.toString();
                        if (OsgiLauncherConstants.ARGEO_OSGI_START.equals(key))
@@ -212,4 +267,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);
+       }
+
 }
index d1f33319b9007274fb3a38ec4adbe0108b0c6895..208f384ae0704ad04dda0dfe8bd2141a72a777e5 100644 (file)
@@ -10,9 +10,17 @@ public interface OsgiLauncherConstants {
        public final static String ARGEO_OSGI_DATA_DIR = "argeo.osgi.data.dir";
        public final static String OSGI_BUNDLES = "osgi.bundles";
        public final static String VMS_PROPERTY_PREFIX = "slc.launch.vm";
+       // public final static String ATTR_DEFAULT_PROGRAM_ARGS = SlcIdeUiPlugin.ID
+       // + ".defaultProgramArgs";
+
+       // Configuration
        public final static String ATTR_DEFAULT_VM_ARGS = SlcIdeUiPlugin.ID
                        + ".defaultVmArgs";
-       public final static String ATTR_DEFAULT_PROGRAM_ARGS = SlcIdeUiPlugin.ID
-                       + ".defaultProgramArgs";
-
+       public final static String ATTR_ADDITIONAL_PROGRAM_ARGS = SlcIdeUiPlugin.ID
+                       + ".additionalProgramArgs";
+       public final static String ATTR_ADDITIONAL_VM_ARGS = SlcIdeUiPlugin.ID
+                       + ".additionalVmArgs";
+       public final static String ATTR_ADD_JVM_PATHS = SlcIdeUiPlugin.ID
+                       + ".addJvmPaths";
+       public final static String ATTR_DATADIR = SlcIdeUiPlugin.ID + ".dataDir";
 }