From 3b9cfdb79dc141d414960115584ab6402f03530b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Galland?= Date: Sat, 10 Sep 2016 01:18:18 +0200 Subject: [PATCH] [eclipse] Add application runner for running inside the SARL project VM. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stéphane Galland --- eclipse-sarl/features/io.sarl.ide/feature.xml | 17 +-- .../io.sarl.lang.targetplatform.target | 19 +-- .../io.sarl.eclipse/META-INF/MANIFEST.MF | 3 +- .../config/ILaunchConfigurationAccessor.java | 7 ++ .../ILaunchConfigurationConfigurator.java | 7 ++ .../LaunchConfigurationConfigurator.java | 26 ++++ .../eclipse/launching/dialog/Messages.java | 1 + .../SARLMainLaunchConfigurationTab.java | 7 ++ .../launching/dialog/messages.properties | 1 + .../launching/runner/EmbeddedVMRunner.java | 117 ++++++++++++++++++ .../eclipse/launching/runner/Messages.java | 1 + .../SARLLaunchConfigurationDelegate.java | 8 ++ .../launching/runner/messages.properties | 1 + .../io.janusproject.feature/feature.xml | 8 ++ .../META-INF/MANIFEST.MF | 1 + 15 files changed, 207 insertions(+), 17 deletions(-) create mode 100644 eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/runner/EmbeddedVMRunner.java diff --git a/eclipse-sarl/features/io.sarl.ide/feature.xml b/eclipse-sarl/features/io.sarl.ide/feature.xml index 731686ac3b..d6cb469475 100644 --- a/eclipse-sarl/features/io.sarl.ide/feature.xml +++ b/eclipse-sarl/features/io.sarl.ide/feature.xml @@ -226,6 +226,7 @@ tools upon the Eclipse IDE. + @@ -234,18 +235,14 @@ tools upon the Eclipse IDE. + + + - - - - - - - @@ -259,6 +256,12 @@ tools upon the Eclipse IDE. + + + + + + - + @@ -26,14 +26,6 @@ - - - - - - - - @@ -52,6 +44,15 @@ + + + + + + + + + diff --git a/eclipse-sarl/plugins/io.sarl.eclipse/META-INF/MANIFEST.MF b/eclipse-sarl/plugins/io.sarl.eclipse/META-INF/MANIFEST.MF index 053a0cab4b..b1da0df8d5 100644 --- a/eclipse-sarl/plugins/io.sarl.eclipse/META-INF/MANIFEST.MF +++ b/eclipse-sarl/plugins/io.sarl.eclipse/META-INF/MANIFEST.MF @@ -26,7 +26,8 @@ Require-Bundle: io.sarl.lang.ui;bundle-version="0.4.0";visibility:=reexport, org.eclipse.ui, org.eclipse.ui.intro;bundle-version="3.5.0", org.eclipse.ui.intro.quicklinks;bundle-version="1.0.0", - org.eclipse.ui.intro.universal;bundle-version="3.3.0" + org.eclipse.ui.intro.universal;bundle-version="3.3.0", + org.arakhne.afc.core.vmutils;bundle-version="14.0.0" Bundle-Activator: io.sarl.eclipse.SARLEclipsePlugin Export-Package: io.sarl.eclipse, io.sarl.eclipse.buildpath, diff --git a/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/config/ILaunchConfigurationAccessor.java b/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/config/ILaunchConfigurationAccessor.java index 44ed047d4e..d409acb2f9 100644 --- a/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/config/ILaunchConfigurationAccessor.java +++ b/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/config/ILaunchConfigurationAccessor.java @@ -133,4 +133,11 @@ public interface ILaunchConfigurationAccessor { */ String getJRELaunchingArguments(ILaunchConfiguration configuration); + /** Replies the agent should be launched in the current Eclipse VM. + * + * @param configuration the configuration. + * @return true if the current Eclipse VM must be used for running the agent. + */ + boolean isEmbeddedSRE(ILaunchConfiguration configuration); + } diff --git a/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/config/ILaunchConfigurationConfigurator.java b/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/config/ILaunchConfigurationConfigurator.java index cd8371fd8f..edb89d8455 100644 --- a/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/config/ILaunchConfigurationConfigurator.java +++ b/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/config/ILaunchConfigurationConfigurator.java @@ -143,4 +143,11 @@ void setLaunchingFlags(ILaunchConfigurationWorkingCopy configuration, Boolean sh */ void setJRELaunchingArguments(ILaunchConfigurationWorkingCopy configuration, String arguments); + /** Set if the agent should be launched in the current Eclipse VM. + * + * @param configuration the configuration to change. + * @param embedded true if the current Eclipse VM must be used for running the agent. + */ + void setEmbeddedSRE(ILaunchConfigurationWorkingCopy configuration, boolean embedded); + } diff --git a/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/config/LaunchConfigurationConfigurator.java b/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/config/LaunchConfigurationConfigurator.java index 6dce1e71a2..022d9f9bf5 100644 --- a/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/config/LaunchConfigurationConfigurator.java +++ b/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/config/LaunchConfigurationConfigurator.java @@ -106,6 +106,12 @@ public class LaunchConfigurationConfigurator implements ILaunchConfigurationConf public static final String ATTR_SARL_RUNTIME_ENVIRONMENT_ARGUMENTS = SARLEclipsePlugin.PLUGIN_ID + ".SARL_RUNTIME_ENVIRONMENT_ARGUMENTS"; //$NON-NLS-1$ + /** + * Launch configuration attribute key. The value indicates if the agents are run in the same VM as + * the Eclipse product. + */ + public static final String ATTR_EMBEDDED_SRE = SARLEclipsePlugin.PLUGIN_ID + ".EMBEDDED_SRE"; //$NON-NLS-1$ + /** Identifier of the type of launch configuration dedicated to SARL * (value io.sarl.eclipse.debug.LaunchConfigType). */ @@ -121,6 +127,8 @@ public class LaunchConfigurationConfigurator implements ILaunchConfigurationConf private static final boolean DEFAULT_USE_PROJECT_SRE = false; + private static final boolean DEFAULT_EMBEDDED_SRE = false; + @Override public String getLaunchConfigurationType() { return SARL_LAUNCH_CONFIG_TYPE; @@ -390,4 +398,22 @@ public String getJRELaunchingArguments(ILaunchConfiguration configuration) { } } + /** {@inheritDoc} + */ + @Override + public boolean isEmbeddedSRE(ILaunchConfiguration configuration) { + try { + return configuration.getAttribute(ATTR_EMBEDDED_SRE, DEFAULT_EMBEDDED_SRE); + } catch (CoreException e) { + return DEFAULT_EMBEDDED_SRE; + } + } + + /** {@inheritDoc} + */ + @Override + public void setEmbeddedSRE(ILaunchConfigurationWorkingCopy configuration, boolean embedded) { + configuration.setAttribute(ATTR_EMBEDDED_SRE, embedded); + } + } diff --git a/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/dialog/Messages.java b/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/dialog/Messages.java index 800d2488a6..ddb82bd5ca 100644 --- a/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/dialog/Messages.java +++ b/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/dialog/Messages.java @@ -63,6 +63,7 @@ public class Messages extends NLS { public static String SARLArgumentsTab_2; public static String SARLArgumentsTab_3; public static String SARLArgumentsTab_4; + public static String SARLMainLaunchConfigurationTab_0; static { // initialize resource bundle NLS.initializeMessages(BUNDLE_NAME, Messages.class); diff --git a/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/dialog/SARLMainLaunchConfigurationTab.java b/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/dialog/SARLMainLaunchConfigurationTab.java index 0aa5850b42..da0be431ab 100644 --- a/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/dialog/SARLMainLaunchConfigurationTab.java +++ b/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/dialog/SARLMainLaunchConfigurationTab.java @@ -95,6 +95,8 @@ public class SARLMainLaunchConfigurationTab extends AbstractJavaMainTab { private Button offlineButton; + private Button runInEclipseButton; + private Button defaultContextIdentifierButton; private Button randomContextIdentifierButton; @@ -225,6 +227,8 @@ protected void createLaunchOptionEditor(Composite parent, String text) { this.showLogInfoButton.addSelectionListener(this.defaultListener); this.offlineButton = createCheckButton(group, Messages.MainLaunchConfigurationTab_16); this.offlineButton.addSelectionListener(this.defaultListener); + this.runInEclipseButton = createCheckButton(group, Messages.SARLMainLaunchConfigurationTab_0); + this.runInEclipseButton.addSelectionListener(this.defaultListener); } @Override @@ -266,9 +270,11 @@ protected void updateLaunchOptionsFromConfig(ILaunchConfiguration config) { final boolean showLogo = this.accessor.getShowLogoFlag(config); final boolean showLogInfo = this.accessor.getShowLogInfoFlag(config); final boolean offline = this.accessor.getOfflineFlag(config); + final boolean runInEclipse = this.accessor.isEmbeddedSRE(config); this.showLogoOptionButton.setSelection(showLogo); this.showLogInfoButton.setSelection(showLogInfo); this.offlineButton.setSelection(offline); + this.runInEclipseButton.setSelection(runInEclipse); } /** @@ -377,6 +383,7 @@ public void performApply(ILaunchConfigurationWorkingCopy config) { this.showLogoOptionButton.getSelection(), this.showLogInfoButton.getSelection(), this.offlineButton.getSelection()); + this.configurator.setEmbeddedSRE(config, this.runInEclipseButton.getSelection()); mapResources(config); } diff --git a/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/dialog/messages.properties b/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/dialog/messages.properties index 13f0dc8b91..8bf8258ed4 100644 --- a/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/dialog/messages.properties +++ b/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/dialog/messages.properties @@ -28,3 +28,4 @@ SARLArgumentsTab_1=Arguments for the SARL runtime environment (SRE): SARLArgumentsTab_2=Arguments for the Java virtual machine (JVM): SARLArgumentsTab_3=Arguments for the Java virtual machine (JVM): SARLArgumentsTab_4=Exception occurred reading configuration: {0} +SARLMainLaunchConfigurationTab_0=Run in the SARL product VM (experimental) diff --git a/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/runner/EmbeddedVMRunner.java b/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/runner/EmbeddedVMRunner.java new file mode 100644 index 0000000000..7fb41eb028 --- /dev/null +++ b/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/runner/EmbeddedVMRunner.java @@ -0,0 +1,117 @@ +/* + * $Id$ + * + * SARL is an general-purpose agent programming language. + * More details on http://www.sarl.io + * + * Copyright (C) 2014-2016 the original authors or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.sarl.eclipse.launching.runner; + +import java.lang.reflect.Method; +import java.net.MalformedURLException; +import java.net.URL; +import java.text.MessageFormat; +import java.util.Arrays; + +import org.arakhne.afc.vmutil.ClassLoaderFinder; +import org.arakhne.afc.vmutil.DynamicURLClassLoader; +import org.arakhne.afc.vmutil.FileSystem; +import org.arakhne.afc.vmutil.URISchemeType; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.jobs.Job; +import org.eclipse.debug.core.ILaunch; +import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; +import org.eclipse.jdt.launching.IVMRunner; +import org.eclipse.jdt.launching.VMRunnerConfiguration; +import org.eclipse.swt.widgets.Shell; + +import io.sarl.eclipse.SARLEclipsePlugin; + +/** + * Implementation of a VM runner that is running in the current Eclipse VM. + * This implementation is for debugging of SREs. + * + * @author $Author: sgalland$ + * @version $FullVersion$ + * @mavengroupid $GroupId$ + * @mavenartifactid $ArtifactId$ + */ +public class EmbeddedVMRunner implements IVMRunner { + + private static final String JAR_EXTENSION = ".jar"; //$NON-NLS-1$ + + private static final String SLASH = "/"; //$NON-NLS-1$ + + @Override + public void run(VMRunnerConfiguration configuration, ILaunch launch, IProgressMonitor monitor) + throws CoreException { + try { + final String[] classpath = configuration.getClassPath(); + final URL[] classPathURLs = new URL[classpath.length]; + Arrays.parallelSetAll(classPathURLs, (index) -> { + String path = classpath[index]; + final URL url = FileSystem.convertStringToURL(path, false); + path = url.getFile(); + if (!path.endsWith(JAR_EXTENSION) && !path.endsWith(SLASH)) { + path = path + SLASH; + } + try { + return new URL(URISchemeType.FILE.name(), null, path); + } catch (MalformedURLException e) { + throw new RuntimeException(e); + } + }); + monitor.done(); + final Job myJob = new Job(launch.getLaunchConfiguration().getName()) { + @Override + public IStatus run(IProgressMonitor monitor) { + monitor.subTask(MessageFormat.format(Messages.EmbeddedVMRunner_0, launch.getLaunchConfiguration().getName())); + final DynamicURLClassLoader classLoader = DynamicURLClassLoader.newInstance( + ClassLoader.getSystemClassLoader(), classPathURLs); + try { + ClassLoaderFinder.setPreferredClassLoader(classLoader); + final String mainClass = configuration.getClassToLaunch(); + final Class clazz = classLoader.loadClass(mainClass); + final Method mainMethod = clazz.getDeclaredMethod("main", String[].class); //$NON-NLS-1$ + mainMethod.invoke(null, (Object) configuration.getProgramArguments()); + return SARLEclipsePlugin.getDefault().createOkStatus(); + } catch (Throwable exception) { + return SARLEclipsePlugin.getDefault().createStatus(IStatus.ERROR, exception); + } finally { + ClassLoaderFinder.popPreferredClassLoader(); + monitor.done(); + } + } + }; + myJob.schedule(); + } catch (Exception exception) { + throw new CoreException(SARLEclipsePlugin.getDefault().createStatus(IStatus.ERROR, exception)); + } + } + + /** + * Convenience method to return the active workbench window shell. + * + * @return active workbench window shell + */ + protected static Shell getShell() { + return JDIDebugUIPlugin.getActiveWorkbenchShell(); + } + +} diff --git a/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/runner/Messages.java b/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/runner/Messages.java index d16e7f07af..8a8c7a5cc8 100644 --- a/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/runner/Messages.java +++ b/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/runner/Messages.java @@ -42,6 +42,7 @@ public class Messages extends NLS { public static String SARLLaunchConfigurationDelegate_6; public static String SARLLaunchConfigurationDelegate_7; public static String SARLLaunchConfigurationDelegate_8; + public static String EmbeddedVMRunner_0; static { // initialize resource bundle NLS.initializeMessages(BUNDLE_NAME, Messages.class); diff --git a/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/runner/SARLLaunchConfigurationDelegate.java b/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/runner/SARLLaunchConfigurationDelegate.java index 45a0d57391..c52642bff1 100644 --- a/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/runner/SARLLaunchConfigurationDelegate.java +++ b/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/runner/SARLLaunchConfigurationDelegate.java @@ -661,6 +661,14 @@ private static String join(String... values) { return buffer.toString(); } + @Override + public IVMRunner getVMRunner(ILaunchConfiguration configuration, String mode) throws CoreException { + if (this.accessor.isEmbeddedSRE(configuration)) { + return new EmbeddedVMRunner(); + } + return super.getVMRunner(configuration, mode); + } + /** Definition of the launching process splitted in separated steps for * making easier the cancellation. * diff --git a/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/runner/messages.properties b/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/runner/messages.properties index 1a96739ee9..dd194e785f 100644 --- a/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/runner/messages.properties +++ b/eclipse-sarl/plugins/io.sarl.eclipse/src/io/sarl/eclipse/launching/runner/messages.properties @@ -7,3 +7,4 @@ SARLLaunchConfigurationDelegate_5=Configuration of the stop-in-main configuratio SARLLaunchConfigurationDelegate_6=Launching {0}... SARLLaunchConfigurationDelegate_7=Post validation of the configuration SARLLaunchConfigurationDelegate_8= +EmbeddedVMRunner_0=Running {0}... diff --git a/sre/io.janusproject/io.janusproject.feature/feature.xml b/sre/io.janusproject/io.janusproject.feature/feature.xml index 3a78985fd5..e988efedf0 100644 --- a/sre/io.janusproject/io.janusproject.feature/feature.xml +++ b/sre/io.janusproject/io.janusproject.feature/feature.xml @@ -236,6 +236,7 @@ + + + diff --git a/sre/io.janusproject/io.janusproject.plugin/META-INF/MANIFEST.MF b/sre/io.janusproject/io.janusproject.plugin/META-INF/MANIFEST.MF index 501e15fa05..3c9d007710 100644 --- a/sre/io.janusproject/io.janusproject.plugin/META-INF/MANIFEST.MF +++ b/sre/io.janusproject/io.janusproject.plugin/META-INF/MANIFEST.MF @@ -8,6 +8,7 @@ Require-Bundle: io.sarl.core;bundle-version="0.4.0", io.sarl.util;bundle-version="0.4.0", io.sarl.eclipse;bundle-version="0.4.0", org.arakhne.afc.core.vmutils;bundle-version="14.0.0", + org.arakhne.afc.core.util;bundle-version="14.0.0", com.hazelcast;bundle-version="3.7.0", com.google.gson;bundle-version="2.4.0", org.zeromq.jeromq;bundle-version="0.3.5",