org.ourgrid.common.executor
Class SWANExecutor

java.lang.Object
  extended by org.ourgrid.common.executor.SWANExecutor
All Implemented Interfaces:
java.io.Serializable, Executor

public class SWANExecutor
extends java.lang.Object
implements Executor

This class is the concrete implementation of Executor interface that provides the platform dependent command execution. The reationale behind this implementation is to use SWAN / XEN 's mechanisms to involke Linux native commands. Unlike Regular Linux implementation of executor, SWANExecutor has a couple of limitations: i) dirName on execute() must be the playpen. ii) only one command can be executed per time. SWANExecutor may run a little slow. It's the trade-off for security.

See Also:
Serialized Form

Constructor Summary
protected SWANExecutor()
          A protected constructor to be accessible only by the ExecutorFactory.
protected SWANExecutor(LinuxExecutor testLinuxExecutor, java.io.File stdOutput, java.io.File errorOutput, java.io.File exitValue)
          A protected constructor to be used only by tests.
 
Method Summary
 void chmod(java.io.File file, java.lang.String modeStr)
          Changes the permissions of an especifc file
protected  java.io.File createExecutionScript(java.io.File script, java.lang.String dirName)
          The createExecutionScript will create a script to execute the script with the remote command.
protected  java.io.File createScript(java.lang.String command, java.lang.String dirName)
          Creates an auxiliary file (script) containing the command to be executed.
protected  java.io.File createScript(java.lang.String command, java.lang.String dirName, java.util.Map envVars)
          Creates an auxiliary file (script) containing the command to be executed.
 ExecutorHandle execute(java.lang.String dirName, java.lang.String command)
          This method implements the command execution feature for SWAN environment.
 ExecutorHandle execute(java.lang.String dirName, java.lang.String command, java.util.Map envVars)
          This method implements the command execution feature for SWAN environment.
 ExecutorResult getResult(ExecutorHandle handle)
          Returns the result of a execution especified by a handle.
 void kill(ExecutorHandle handle)
          Kills command that was issued via an execute method
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SWANExecutor

protected SWANExecutor()
A protected constructor to be accessible only by the ExecutorFactory.


SWANExecutor

protected SWANExecutor(LinuxExecutor testLinuxExecutor,
                       java.io.File stdOutput,
                       java.io.File errorOutput,
                       java.io.File exitValue)
A protected constructor to be used only by tests.

Method Detail

execute

public ExecutorHandle execute(java.lang.String dirName,
                              java.lang.String command)
                       throws ExecutorException
This method implements the command execution feature for SWAN environment.
One of SWAN's limitation is that it can run only one execution per time. Multiple executions are not allowed. A getResult() or a kill() must be done before starting a new execution.

Specified by:
execute in interface Executor
Parameters:
command - The command must be executed
dirName - The execution root directory following the Linux name convention. It must be the playpen.
Returns:
A handle that identifies this execution
Throws:
ExecutorException - If the command could not be executed.

execute

public ExecutorHandle execute(java.lang.String dirName,
                              java.lang.String command,
                              java.util.Map envVars)
                       throws ExecutorException
This method implements the command execution feature for SWAN environment.
One of SWAN's limitation is that it can run only one execution per time. Multiple executions are not allowed. A getResult() or a kill() must be done before starting a new execution.

Specified by:
execute in interface Executor
Parameters:
dirName - The execution root directory following the Linux name convention. It must be the playpen.
command - The command must be executed
envVars - A map (var name, value) with the environment variables used by the command
Returns:
A handle that identifies this execution
Throws:
ExecutorException - If the command could not be executed.

getResult

public ExecutorResult getResult(ExecutorHandle handle)
                         throws ExecutorException
Returns the result of a execution especified by a handle. Due to SWAN limitations, there is only one execution per time, and its result must be returned (or a kill has to be called) before a new one takes place.

Specified by:
getResult in interface Executor
Parameters:
handle - the command handle
Returns:
ExecutorResult StdOut, StdErr and exitValue from kill command.
Throws:
ExecutorException - when there is a problem while obtaining the result of an execution.

kill

public void kill(ExecutorHandle handle)
          throws ExecutorException
Kills command that was issued via an execute method

Specified by:
kill in interface Executor
Parameters:
handle - the command handle
Throws:
ExecutorException - when there is a problem while changing .the permissions of a file.

chmod

public void chmod(java.io.File file,
                  java.lang.String modeStr)
           throws ExecutorException
Changes the permissions of an especifc file

Specified by:
chmod in interface Executor
Parameters:
file - The file which permissions will be changed
modeStr - The new permitions.
Example: "123" = "--x-w--wx"
Throws:
ExecutorException - error changing the permissions

createScript

protected java.io.File createScript(java.lang.String command,
                                    java.lang.String dirName)
                             throws ExecutorException
Creates an auxiliary file (script) containing the command to be executed.

Parameters:
command - The command to be run
dirName - The path of the directory where the file will be created.
Returns:
a File representing the script created
Throws:
ExecutorException

createScript

protected java.io.File createScript(java.lang.String command,
                                    java.lang.String dirName,
                                    java.util.Map envVars)
                             throws ExecutorException
Creates an auxiliary file (script) containing the command to be executed.

Parameters:
command - The command to be run
dirName - The path of the directory where the file will be created.
envVars - A map (var name, value) with the environment variables used by the command
Returns:
a File representing the script created
Throws:
ExecutorException

createExecutionScript

protected java.io.File createExecutionScript(java.io.File script,
                                             java.lang.String dirName)
                                      throws ExecutorException
The createExecutionScript will create a script to execute the script with the remote command. This script will redirect output streams and also the exit value to some files to be used.

Parameters:
script - Script with remote execution commmand.
dirName - Directory of execution script.
Returns:
The execution script.
Throws:
ExecutorException - If its not possible to create the execution script.