From 3ceff6ab55ed91637ddcb4233edb28c79a3a7ae4 Mon Sep 17 00:00:00 2001 From: Wainer S. Moschetta Date: Thu, 4 Jul 2013 23:08:37 -0300 Subject: Remote proxy: add getEnv() method Added remote implementations of System.getenv() into the proxies. Change-Id: Ib62b072b351a99c5d0079d742f786ff38d2faa45 Signed-off-by: Wainer S. Moschetta Signed-off-by: Rodrigo Fraxino Araujo Reviewed-on: https://git.eclipse.org/r/14295 Tested-by: Hudson CI Reviewed-by: Roland Grunberg IP-Clean: Roland Grunberg Tested-by: Roland Grunberg --- .../META-INF/MANIFEST.MF | 2 +- .../pom.xml | 2 +- .../profiling/launch/IRemoteEnvProxyManager.java | 44 ++++++++++++++ .../profiling/launch/RemoteEnvProxyManager.java | 55 +++++++++++++++++ .../profiling/launch/RemoteProxyManager.java | 23 ++++++-- .../META-INF/MANIFEST.MF | 2 +- profiling/org.eclipse.linuxtools.rdt.proxy/pom.xml | 2 +- .../linuxtools/rdt/proxy/RDTProxyManager.java | 29 ++++++++- .../META-INF/MANIFEST.MF | 2 +- profiling/org.eclipse.linuxtools.ssh.proxy/pom.xml | 2 +- .../linuxtools/ssh/proxy/SSHProxyManager.java | 68 +++++++++++++++++++++- .../core/factory/LinuxtoolsProcessFactory.java | 21 ++++++- 12 files changed, 235 insertions(+), 17 deletions(-) create mode 100644 profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/profiling/launch/IRemoteEnvProxyManager.java create mode 100644 profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/profiling/launch/RemoteEnvProxyManager.java diff --git a/profiling/org.eclipse.linuxtools.profiling.launch/META-INF/MANIFEST.MF b/profiling/org.eclipse.linuxtools.profiling.launch/META-INF/MANIFEST.MF index 14e5e26585..685b8063f4 100644 --- a/profiling/org.eclipse.linuxtools.profiling.launch/META-INF/MANIFEST.MF +++ b/profiling/org.eclipse.linuxtools.profiling.launch/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %Bundle-Name.0 Bundle-SymbolicName: org.eclipse.linuxtools.profiling.launch;singleton:=true -Bundle-Version: 2.0.0.qualifier +Bundle-Version: 2.1.0.qualifier Bundle-Activator: org.eclipse.linuxtools.internal.profiling.launch.ProfileLaunchPlugin Require-Bundle: org.eclipse.ui, org.eclipse.core.runtime, diff --git a/profiling/org.eclipse.linuxtools.profiling.launch/pom.xml b/profiling/org.eclipse.linuxtools.profiling.launch/pom.xml index d2c9dbff73..d7d2455cef 100644 --- a/profiling/org.eclipse.linuxtools.profiling.launch/pom.xml +++ b/profiling/org.eclipse.linuxtools.profiling.launch/pom.xml @@ -18,7 +18,7 @@ org.eclipse.linuxtools.profiling.launch - 2.0.0-SNAPSHOT + 2.1.0-SNAPSHOT eclipse-plugin Linux Tools Profiling Launch Plug-in diff --git a/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/profiling/launch/IRemoteEnvProxyManager.java b/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/profiling/launch/IRemoteEnvProxyManager.java new file mode 100644 index 0000000000..99c29ea1d6 --- /dev/null +++ b/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/profiling/launch/IRemoteEnvProxyManager.java @@ -0,0 +1,44 @@ +/******************************************************************************* + * Copyright (c) 2013 IBM Corporation. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - Rodrigo Fraxino De Araujo + *******************************************************************************/ + +package org.eclipse.linuxtools.profiling.launch; + +import java.net.URI; +import java.util.Map; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; + +/** + * Interface to handle system's environment variables. + * + * @since 2.1 + */ +public interface IRemoteEnvProxyManager extends IRemoteProxyManager { + /** + * Method to get system's environment variables. + * + * @param Project + * IProject + * @return Mapping of environment variables + * @since 2.1 + */ + public Map getEnv(IProject project) throws CoreException; + /** + * Method to get system's environment variables. + * + * @param Resource URI + * URI + * @return Mapping of environment variables + * @since 2.1 + */ + public Map getEnv(URI uri) throws CoreException; +} diff --git a/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/profiling/launch/RemoteEnvProxyManager.java b/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/profiling/launch/RemoteEnvProxyManager.java new file mode 100644 index 0000000000..ce5c2f878f --- /dev/null +++ b/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/profiling/launch/RemoteEnvProxyManager.java @@ -0,0 +1,55 @@ +/******************************************************************************* + * Copyright (c) 2013 IBM Corporation. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Corporation - Rodrigo Fraxino De Araujo + * IBM Corporation - Wainer Santos Moschetta + *******************************************************************************/ + +package org.eclipse.linuxtools.profiling.launch; + +import java.net.URI; +import java.util.Map; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; + +/** + * Class to handle system's environment variables. + * + * @since 2.1 + */ +public class RemoteEnvProxyManager extends RemoteProxyManager implements IRemoteEnvProxyManager { + + public Map getEnv(IProject project) throws CoreException { + String scheme = mapping.getSchemeFromNature(project); + if (scheme!=null) { + IRemoteProxyManager manager = getRemoteManager(scheme); + IRemoteEnvProxyManager envManager; + if (manager instanceof IRemoteEnvProxyManager) { + envManager = (IRemoteEnvProxyManager) manager; + return envManager.getEnv(project); + } + } + URI projectURI = project.getLocationURI(); + return getEnv(projectURI); + } + + public Map getEnv(URI uri) throws CoreException { + String scheme = uri.getScheme(); + if (scheme != null && !scheme.equals(LOCALSCHEME)){ + IRemoteProxyManager manager = getRemoteManager(scheme); + IRemoteEnvProxyManager envManager; + if (manager instanceof IRemoteEnvProxyManager) { + envManager = (IRemoteEnvProxyManager) manager; + return envManager.getEnv(uri); + } + } + return System.getenv(); + } + +} diff --git a/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/profiling/launch/RemoteProxyManager.java b/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/profiling/launch/RemoteProxyManager.java index 84c74c73e0..387338b45b 100644 --- a/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/profiling/launch/RemoteProxyManager.java +++ b/profiling/org.eclipse.linuxtools.profiling.launch/src/org/eclipse/linuxtools/profiling/launch/RemoteProxyManager.java @@ -29,14 +29,23 @@ import org.eclipse.linuxtools.internal.profiling.launch.ProfileLaunchPlugin; public class RemoteProxyManager implements IRemoteProxyManager { private static final String EXT_ATTR_CLASS = "class"; //$NON-NLS-1$ - private static final String LOCALSCHEME = "file"; //$NON-NLS-1$ + /** + * @since 2.1 + */ + protected static final String LOCALSCHEME = "file"; //$NON-NLS-1$ private static RemoteProxyManager manager; private LocalFileProxy lfp; - private RemoteProxyNatureMapping mapping = new RemoteProxyNatureMapping(); + /** + * @since 2.1 + */ + protected RemoteProxyNatureMapping mapping = new RemoteProxyNatureMapping(); private Map remoteManagers = new HashMap(); - private RemoteProxyManager() { + /** + * @since 2.1 + */ + protected RemoteProxyManager() { // do nothing } @@ -51,8 +60,10 @@ public class RemoteProxyManager implements IRemoteProxyManager { lfp = new LocalFileProxy(uri); return lfp; } - - private IRemoteProxyManager getRemoteManager(String schemeId) throws CoreException { + /** + * @since 2.1 + */ + protected IRemoteProxyManager getRemoteManager(String schemeId) throws CoreException { IRemoteProxyManager remoteManager = remoteManagers.get(schemeId); if (remoteManager == null) { IExtensionPoint extensionPoint = Platform.getExtensionRegistry().getExtensionPoint(ProfileLaunchPlugin.PLUGIN_ID, IRemoteProxyManager.EXTENSION_POINT_ID); @@ -143,4 +154,6 @@ public class RemoteProxyManager implements IRemoteProxyManager { URI projectURI = project.getLocationURI(); return getOS(projectURI); } + + } diff --git a/profiling/org.eclipse.linuxtools.rdt.proxy/META-INF/MANIFEST.MF b/profiling/org.eclipse.linuxtools.rdt.proxy/META-INF/MANIFEST.MF index 06253e3148..908bf7fe29 100644 --- a/profiling/org.eclipse.linuxtools.rdt.proxy/META-INF/MANIFEST.MF +++ b/profiling/org.eclipse.linuxtools.rdt.proxy/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %bundleName Bundle-SymbolicName: org.eclipse.linuxtools.rdt.proxy;singleton:=true -Bundle-Version: 1.0.0.qualifier +Bundle-Version: 1.1.0.qualifier Bundle-Activator: org.eclipse.linuxtools.rdt.proxy.Activator Require-Bundle: org.eclipse.ui, org.eclipse.core.runtime, diff --git a/profiling/org.eclipse.linuxtools.rdt.proxy/pom.xml b/profiling/org.eclipse.linuxtools.rdt.proxy/pom.xml index 50580748b4..67f98c05d2 100644 --- a/profiling/org.eclipse.linuxtools.rdt.proxy/pom.xml +++ b/profiling/org.eclipse.linuxtools.rdt.proxy/pom.xml @@ -18,7 +18,7 @@ org.eclipse.linuxtools.rdt.proxy - 1.0.0-SNAPSHOT + 1.1.0-SNAPSHOT eclipse-plugin Linux Tools RDT Proxy Plug-in diff --git a/profiling/org.eclipse.linuxtools.rdt.proxy/src/org/eclipse/linuxtools/rdt/proxy/RDTProxyManager.java b/profiling/org.eclipse.linuxtools.rdt.proxy/src/org/eclipse/linuxtools/rdt/proxy/RDTProxyManager.java index 236db166a1..bd361eb12a 100644 --- a/profiling/org.eclipse.linuxtools.rdt.proxy/src/org/eclipse/linuxtools/rdt/proxy/RDTProxyManager.java +++ b/profiling/org.eclipse.linuxtools.rdt.proxy/src/org/eclipse/linuxtools/rdt/proxy/RDTProxyManager.java @@ -11,19 +11,24 @@ package org.eclipse.linuxtools.rdt.proxy; import java.net.URI; +import java.util.Collections; +import java.util.Map; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; import org.eclipse.linuxtools.internal.rdt.proxy.RDTCommandLauncher; import org.eclipse.linuxtools.internal.rdt.proxy.RDTFileProxy; import org.eclipse.linuxtools.profiling.launch.IRemoteCommandLauncher; +import org.eclipse.linuxtools.profiling.launch.IRemoteEnvProxyManager; import org.eclipse.linuxtools.profiling.launch.IRemoteFileProxy; -import org.eclipse.linuxtools.profiling.launch.IRemoteProxyManager; import org.eclipse.ptp.remote.core.IRemoteConnection; import org.eclipse.ptp.remote.core.IRemoteServices; import org.eclipse.ptp.remote.core.RemoteServices; +import org.eclipse.ptp.remote.core.exception.RemoteConnectionException; -public class RDTProxyManager implements IRemoteProxyManager { +public class RDTProxyManager implements IRemoteEnvProxyManager { public final static String SYNC_NATURE = "org.eclipse.ptp.rdt.sync.core.remoteSyncNature"; //$NON-NLS-1$ @@ -64,4 +69,24 @@ public class RDTProxyManager implements IRemoteProxyManager { return getOS(uri); } + public Map getEnv(URI uri) throws CoreException { + IRemoteServices services = RemoteServices.getRemoteServices(uri); + IRemoteConnection connection = services.getConnectionManager().getConnection(uri); + if(!connection.isOpen()) { + try { + connection.open(null); + } catch (RemoteConnectionException e) { + Status status = new Status(IStatus.ERROR, e.getMessage(), Activator.PLUGIN_ID); + Activator.getDefault().getLog().log(status); + return Collections.emptyMap(); + } + } + return connection.getEnv(); + } + + public Map getEnv(IProject project) throws CoreException { + URI uri = project.getLocationURI(); + return getEnv(uri); + } + } diff --git a/profiling/org.eclipse.linuxtools.ssh.proxy/META-INF/MANIFEST.MF b/profiling/org.eclipse.linuxtools.ssh.proxy/META-INF/MANIFEST.MF index ae33b47bcd..2a92ba7338 100644 --- a/profiling/org.eclipse.linuxtools.ssh.proxy/META-INF/MANIFEST.MF +++ b/profiling/org.eclipse.linuxtools.ssh.proxy/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %bundleName Bundle-SymbolicName: org.eclipse.linuxtools.ssh.proxy;singleton:=true -Bundle-Version: 1.0.0.qualifier +Bundle-Version: 1.1.0.qualifier Bundle-Activator: org.eclipse.linuxtools.ssh.proxy.Activator Require-Bundle: org.eclipse.ui, org.eclipse.core.runtime, diff --git a/profiling/org.eclipse.linuxtools.ssh.proxy/pom.xml b/profiling/org.eclipse.linuxtools.ssh.proxy/pom.xml index de6901b474..b93e5f352c 100644 --- a/profiling/org.eclipse.linuxtools.ssh.proxy/pom.xml +++ b/profiling/org.eclipse.linuxtools.ssh.proxy/pom.xml @@ -18,7 +18,7 @@ org.eclipse.linuxtools.ssh.proxy - 1.0.0-SNAPSHOT + 1.1.0-SNAPSHOT eclipse-plugin Linux Tools SSH Proxy Plug-in diff --git a/profiling/org.eclipse.linuxtools.ssh.proxy/src/org/eclipse/linuxtools/ssh/proxy/SSHProxyManager.java b/profiling/org.eclipse.linuxtools.ssh.proxy/src/org/eclipse/linuxtools/ssh/proxy/SSHProxyManager.java index 0786398b80..26a3ddc49b 100644 --- a/profiling/org.eclipse.linuxtools.ssh.proxy/src/org/eclipse/linuxtools/ssh/proxy/SSHProxyManager.java +++ b/profiling/org.eclipse.linuxtools.ssh.proxy/src/org/eclipse/linuxtools/ssh/proxy/SSHProxyManager.java @@ -11,20 +11,28 @@ *******************************************************************************/ package org.eclipse.linuxtools.ssh.proxy; +import java.io.BufferedReader; import java.io.InputStream; import java.io.IOException; +import java.io.InputStreamReader; import java.net.URI; +import java.util.Collections; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.eclipse.core.resources.IProject; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Status; import org.eclipse.linuxtools.internal.ssh.proxy.SSHCommandLauncher; import org.eclipse.linuxtools.internal.ssh.proxy.SSHFileProxy; import org.eclipse.linuxtools.profiling.launch.IRemoteCommandLauncher; +import org.eclipse.linuxtools.profiling.launch.IRemoteEnvProxyManager; import org.eclipse.linuxtools.profiling.launch.IRemoteFileProxy; -import org.eclipse.linuxtools.profiling.launch.IRemoteProxyManager; -public class SSHProxyManager implements IRemoteProxyManager { +public class SSHProxyManager implements IRemoteEnvProxyManager { @Override public IRemoteFileProxy getFileProxy(URI uri) { @@ -72,4 +80,60 @@ public class SSHProxyManager implements IRemoteProxyManager { URI uri = project.getLocationURI(); return getOS(uri); } + + @Override + public Map getEnv(URI uri) throws CoreException { + Map env = Collections.emptyMap(); + SSHCommandLauncher cmdLauncher = new SSHCommandLauncher(uri); + Process p = cmdLauncher.execute(new Path("/bin/env"), new String[] {}, new String[] {}, null, null); + BufferedReader error = new BufferedReader(new InputStreamReader(p.getErrorStream())); + BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); + String errorLine; + try { + if((errorLine = error.readLine()) != null){ + throw new IOException(errorLine); + } + error.close(); + } catch (IOException e) { + Status status = new Status(IStatus.ERROR, e.getMessage(), Activator.PLUGIN_ID); + Activator.getDefault().getLog().log(status); + return Collections.emptyMap(); + } + /* + * It is common to export functions declaration in the environment so + * this pattern filters out them because they get truncated + * and might end up on failure. + * + * Patterns added in the env list: + * var=value + * var=value + * + * Patterns not added in the env list: + * var=() { something + * + * TODO: implement a parser for function declarations so that they do not need to be excluded + */ + Pattern variablePattern = Pattern.compile("^(.+)=([^\\(\\)\\s{].*|)$"); + Matcher m; + try { + String readLine = reader.readLine(); + while (readLine != null) { + m = variablePattern.matcher(readLine); + if(m.matches()) + env.put(m.group(1), m.group(2)); + readLine = reader.readLine(); + } + } catch (IOException e) { + Status status = new Status(IStatus.ERROR, e.getMessage(), Activator.PLUGIN_ID); + Activator.getDefault().getLog().log(status); + return Collections.emptyMap(); + } + return env; + } + + @Override + public Map getEnv(IProject project) throws CoreException { + URI uri = project.getLocationURI(); + return getEnv(uri); + } } diff --git a/profiling/org.eclipse.linuxtools.tools.launch.core/src/org/eclipse/linuxtools/tools/launch/core/factory/LinuxtoolsProcessFactory.java b/profiling/org.eclipse.linuxtools.tools.launch.core/src/org/eclipse/linuxtools/tools/launch/core/factory/LinuxtoolsProcessFactory.java index f1bcfb8e33..306832d593 100644 --- a/profiling/org.eclipse.linuxtools.tools.launch.core/src/org/eclipse/linuxtools/tools/launch/core/factory/LinuxtoolsProcessFactory.java +++ b/profiling/org.eclipse.linuxtools.tools.launch.core/src/org/eclipse/linuxtools/tools/launch/core/factory/LinuxtoolsProcessFactory.java @@ -15,6 +15,8 @@ import java.util.Map; import java.util.Set; import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.linuxtools.profiling.launch.RemoteEnvProxyManager; import org.eclipse.linuxtools.tools.launch.core.properties.LinuxtoolsPathProperty; /* @@ -45,7 +47,20 @@ public abstract class LinuxtoolsProcessFactory { envp = new String[0]; String ltPath = LinuxtoolsPathProperty.getInstance().getLinuxtoolsPath(project); String envpPath = getEnvpPath(envp); - String systemPath = System.getenv(PATH); + String systemPath = null; + Map systemEnvMap = null; + try { + systemEnvMap = RemoteEnvProxyManager.class.newInstance().getEnv(project); + systemPath = systemEnvMap.get(PATH); + if (systemPath==null) + systemPath = System.getenv(PATH); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (CoreException e) { + e.printStackTrace(); + } StringBuffer newPath = new StringBuffer(); newPath.append(PATH_EQUAL); @@ -74,7 +89,7 @@ public abstract class LinuxtoolsProcessFactory { newEnvp[i] = newPath.toString(); else newEnvp[i] = envp[i]; - } else { + } else if (systemEnvMap != null) { Map envVars = System.getenv(); Set keySet = envVars.keySet(); newEnvp = new String[envVars.size()]; @@ -84,6 +99,8 @@ public abstract class LinuxtoolsProcessFactory { newEnvp[i] = key + "=" + envVars.get(key); i++; } + } else { + newEnvp = new String[] {}; } return newEnvp; } -- cgit v1.2.3