diff --git a/rse/plugins/org.eclipse.rse.subsystems.shells.core/src/org/eclipse/rse/subsystems/shells/core/model/SimpleCommandOperation.java b/rse/plugins/org.eclipse.rse.subsystems.shells.core/src/org/eclipse/rse/subsystems/shells/core/model/SimpleCommandOperation.java index 309e4f6cd87..0e333ddd0ea 100644 --- a/rse/plugins/org.eclipse.rse.subsystems.shells.core/src/org/eclipse/rse/subsystems/shells/core/model/SimpleCommandOperation.java +++ b/rse/plugins/org.eclipse.rse.subsystems.shells.core/src/org/eclipse/rse/subsystems/shells/core/model/SimpleCommandOperation.java @@ -1,46 +1,30 @@ -/******************************************************************************** - * Copyright (c) 2006 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 - * - * Initial Contributors: - * The following IBM employees contributed to the Remote System Explorer - * component that contains this file: David McKnight, Kushal Munir, - * Michael Berger, David Dykstal, Phil Coulthard, Don Yantzi, Eric Simpson, - * Emily Bruner, Mazen Faraj, Adrian Storisteanu, Li Ding, and Kent Hawley. - * - * Contributors: - * {Name} (company) - description of contribution. - ********************************************************************************/ - package org.eclipse.rse.subsystems.shells.core.model; +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.rse.subsystems.files.core.subsystems.IRemoteFile; import org.eclipse.rse.subsystems.shells.core.subsystems.IRemoteCmdSubSystem; import org.eclipse.rse.subsystems.shells.core.subsystems.IRemoteCommandShell; import org.eclipse.rse.subsystems.shells.core.subsystems.IRemoteOutput; -import org.eclipse.swt.widgets.Display; - public class SimpleCommandOperation { - private IRemoteCmdSubSystem _subsystem; - private IRemoteFile _workingDirectory; - private IRemoteCommandShell _cmdShell; - private int _outputLineIndex = 0; + protected IRemoteCmdSubSystem _subsystem; + protected IRemoteFile _workingDirectory; + protected IRemoteCommandShell _cmdShell; + protected List _envVars; + protected int _outputLineIndex = 0; + protected boolean _runAsShell = false; - public SimpleCommandOperation(IRemoteCmdSubSystem subsystem, IRemoteFile workingDirectory) + public SimpleCommandOperation(IRemoteCmdSubSystem subsystem, IRemoteFile workingDirectory, boolean runAsShell) { _subsystem = subsystem; _workingDirectory = workingDirectory; - } - - public void runCommand(String command) throws Exception - { - Object[] result =_subsystem.runCommand(command, _workingDirectory, false); - _cmdShell = (IRemoteCommandShell)result[0]; + _envVars = new ArrayList(); + _runAsShell = runAsShell; } public IRemoteCommandShell getCommandShell() @@ -48,11 +32,118 @@ public class SimpleCommandOperation return _cmdShell; } + public void setEnvironmentVariable(String name, String value) + { + _envVars.add(name + "=" + value); //$NON-NLS-1$ + + } + + public void setEnvironmentVariables(String[] names, String[] values) + { + for (int i = 0; i < names.length; i++) + { + setEnvironmentVariable(names[i], values[i]); + } + } + + public void setEnvironmentVariables(String[] vars) + { + for (int i = 0; i < vars.length; i++) + { + _envVars.add(vars[i]); + } + } + + public String[] getEnvironmentVariables() + { + String[] vars = new String[_envVars.size()]; + for (int i = 0; i < vars.length; i++) + { + vars[i] = (String)_envVars.get(i); + } + return vars; + } + + /** + * Run a command + * @param command the command to run + * @param exitShell indicates whether to exit the shell after running the command + * @throws Exception + */ + public void runCommand(String command, boolean exitShell) throws Exception + { + if (_runAsShell) + { + _cmdShell = _subsystem.runShell(null, _workingDirectory); + + + _subsystem.sendCommandToShell(new NullProgressMonitor(), command, _cmdShell); + if (exitShell) + { + _subsystem.sendCommandToShell(new NullProgressMonitor(), "exit", _cmdShell); //$NON-NLS-1$ + } + } + else + { + + Object[] result =_subsystem.runCommand(new NullProgressMonitor(), command, _workingDirectory, false); + _cmdShell= (IRemoteCommandShell)result[0]; + } + } + + /** + * Launch a shell with the specified exports and command + * @param exports the command to initialize the shell environment + * @param command the command to run + * @param exitShell indicates whether to exit the shell after running the command + * @throws Exception + */ + public void runCommandInShell(String exports, String command, boolean exitShell) throws Exception + { + _runAsShell = true; + _cmdShell = _subsystem.runShell(null, _workingDirectory); + + if (exports != null) + { + _subsystem.sendCommandToShell(new NullProgressMonitor(), exports, _cmdShell); + } + _subsystem.sendCommandToShell(new NullProgressMonitor(), command, _cmdShell); + if (exitShell) + { + exitShell(); + } + } + + public void removeShell() + { + try + { + _subsystem.removeShell(_cmdShell); + } + catch (Exception e) + { + } + } + + public void exitShell() + { + if (_runAsShell) + { + try + { + _subsystem.sendCommandToShell(new NullProgressMonitor(), "exit", _cmdShell); //$NON-NLS-1$ + } + catch (Exception e) + { + } + } + } + public void putInput(String input) throws Exception { if (isActive()) { - _subsystem.sendCommandToShell(input, _cmdShell); + _subsystem.sendCommandToShell(new NullProgressMonitor(), input, _cmdShell); } } @@ -69,8 +160,11 @@ public class SimpleCommandOperation { if (_cmdShell != null) { - - if (_cmdShell.listOutput().length > _outputLineIndex) + if (_cmdShell.getSize() > _outputLineIndex) + { + return true; + } + else if (_cmdShell.listOutput().length > _outputLineIndex) { return true; } @@ -82,7 +176,7 @@ public class SimpleCommandOperation { if (_cmdShell != null && _cmdShell.isActive()) { - _cmdShell.getCommandSubSystem().cancelShell(_cmdShell); + _cmdShell.getCommandSubSystem().cancelShell(null, _cmdShell); } } @@ -91,39 +185,44 @@ public class SimpleCommandOperation { if (_cmdShell != null) { + boolean isActive = true; if (!hasMoreOutput() && waitForOutput) - { - Display display = Display.getCurrent(); - try + { + while (!hasMoreOutput() && isActive) { - while (!hasMoreOutput() && isActive()) + + try { - while (display!=null && display.readAndDispatch()) { - //Process everything on event queue - } - if (!hasMoreOutput() && isActive()) Thread.sleep(100); + isActive = isActive(); + Thread.sleep(100); + } + catch (Exception e) + { + e.printStackTrace(); } - } - catch (InterruptedException e) - { - //Cancel waiting - } - if (!isActive()) - { - return null; } } { - Object output = _cmdShell.getOutputAt(_outputLineIndex); - _outputLineIndex++; - if (output instanceof IRemoteOutput) + Object[] out = _cmdShell.listOutput(); + if (out.length > _outputLineIndex) { - return ((IRemoteOutput)output).getText(); + Object output = out[_outputLineIndex]; + _outputLineIndex++; + if (output instanceof IRemoteOutput) + { + return ((IRemoteOutput)output).getText(); + } + else if (output instanceof IRemoteFile) + { + return ((IRemoteFile)output).getLabel(); + } + + } - else if (output instanceof IRemoteFile) + else if (!isActive) { - return ((IRemoteFile)output).getLabel(); + return null; } } @@ -131,4 +230,4 @@ public class SimpleCommandOperation return ""; //$NON-NLS-1$ } -} \ No newline at end of file +}