From 01e4319b49fcc2a6cdf6f0f9271ae27343c7430f Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Tue, 20 Apr 2010 14:50:52 +0000 Subject: [PATCH] Introduce specific tab to OSGi boot Clean up code git-svn-id: https://svn.argeo.org/slc/trunk@3480 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- .../org.argeo.slc.ide.ui/META-INF/MANIFEST.MF | 1 + .../org/argeo/slc/ide/ui/SlcIdeUiPlugin.java | 17 ++ .../osgi/AbstractOsgiLaunchShortcut.java | 12 +- .../OsgiBootEquinoxLaunchConfiguration.java | 47 +--- .../launch/osgi/OsgiBootLaunchShortcut.java | 101 +++++--- .../launch/osgi/OsgiBootLauncherTabGroup.java | 30 ++- .../ide/ui/launch/osgi/OsgiBootMainTab.java | 220 ++++++++++++++++++ .../ide/ui/launch/osgi/OsgiLaunchHelper.java | 170 ++++++++++---- .../ui/launch/osgi/OsgiLauncherConstants.java | 14 +- 9 files changed, 485 insertions(+), 127 deletions(-) create mode 100644 eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiBootMainTab.java diff --git a/eclipse/plugins/org.argeo.slc.ide.ui/META-INF/MANIFEST.MF b/eclipse/plugins/org.argeo.slc.ide.ui/META-INF/MANIFEST.MF index 0503ead52..f89640e16 100644 --- a/eclipse/plugins/org.argeo.slc.ide.ui/META-INF/MANIFEST.MF +++ b/eclipse/plugins/org.argeo.slc.ide.ui/META-INF/MANIFEST.MF @@ -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 diff --git a/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/SlcIdeUiPlugin.java b/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/SlcIdeUiPlugin.java index c9ddd4b8d..2c288cff5 100644 --- a/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/SlcIdeUiPlugin.java +++ b/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/SlcIdeUiPlugin.java @@ -1,5 +1,10 @@ package org.argeo.slc.ide.ui; +import java.net.URL; + +import org.eclipse.jface.resource.ImageDescriptor; +import org.eclipse.jface.resource.ImageRegistry; +import org.eclipse.swt.graphics.Image; import org.eclipse.ui.plugin.AbstractUIPlugin; import org.osgi.framework.BundleContext; @@ -41,4 +46,16 @@ public class SlcIdeUiPlugin extends AbstractUIPlugin { return plugin; } + public Image getImage(String relativeURL) { + ImageRegistry imageRegistry = getImageRegistry(); + Image image = imageRegistry.get(relativeURL); + if (image == null) { + URL imageURL = getBundle().getEntry(relativeURL); + ImageDescriptor descriptor = ImageDescriptor + .createFromURL(imageURL); + image = descriptor.createImage(); + imageRegistry.put(relativeURL, image); + } + return image; + } } diff --git a/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/AbstractOsgiLaunchShortcut.java b/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/AbstractOsgiLaunchShortcut.java index 757e528bd..c9337d85f 100644 --- a/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/AbstractOsgiLaunchShortcut.java +++ b/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/AbstractOsgiLaunchShortcut.java @@ -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", diff --git a/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiBootEquinoxLaunchConfiguration.java b/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiBootEquinoxLaunchConfiguration.java index cbd84cbe7..c00b30752 100644 --- a/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiBootEquinoxLaunchConfiguration.java +++ b/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiBootEquinoxLaunchConfiguration.java @@ -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 bundlesToStart = new ArrayList(); - Map systemPropertiesToAppend = new HashMap(); - 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 diff --git a/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiBootLaunchShortcut.java b/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiBootLaunchShortcut.java index 513ba50a6..3dbdbb19d 100644 --- a/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiBootLaunchShortcut.java +++ b/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiBootLaunchShortcut.java @@ -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 bundlesToStart = new ArrayList(); + protected Map systemPropertiesToAppend = new HashMap(); @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); + } + } diff --git a/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiBootLauncherTabGroup.java b/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiBootLauncherTabGroup.java index a18413586..d7fa841a5 100644 --- a/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiBootLauncherTabGroup.java +++ b/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiBootLauncherTabGroup.java @@ -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 index 000000000..95d32eb95 --- /dev/null +++ b/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiBootMainTab.java @@ -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 + // } + // } + // + // } + +} 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 ebf41c0a4..253a82aff 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; @@ -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 bundlesToStart = new ArrayList(); + Map systemPropertiesToAppend = new HashMap(); + 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 bundlesToStart, - Map systemPropertiesToAppend, String workingDir, - String dataDir) throws CoreException { + Map 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 tokens = new ArrayList(); - 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 tokens = new ArrayList(); + 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); + } + } diff --git a/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiLauncherConstants.java b/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiLauncherConstants.java index d1f33319b..208f384ae 100644 --- a/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiLauncherConstants.java +++ b/eclipse/plugins/org.argeo.slc.ide.ui/src/main/java/org/argeo/slc/ide/ui/launch/osgi/OsgiLauncherConstants.java @@ -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"; } -- 2.39.2