edu.nmu.os.shell
Class Command

java.lang.Object
  |
  +--edu.nmu.os.shell.Command
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
CommandImpl

public abstract class Command
extends java.lang.Object
implements java.lang.Runnable

Encapsulates the functionality of a single shell command

Since:
jdk1.4
Version:
0.1, Jan 24, 2003 4:23:08 PM
Author:
Matt Murphy

Constructor Summary
Command()
           
 
Method Summary
abstract  boolean forceCurrentThread()
          Determines whether the Command should be run in the same Thread as the executor
abstract  boolean fork()
          Determines whether this Command should be joined with immediately after its execution
abstract  java.lang.String[] getArgs()
          Gets the parameters that will be given to the program when this Command is executed
protected abstract  java.io.PrintStream getErr()
          Gets the PrintStream that should be used as NewSystem.err during execution
protected abstract  java.io.InputStream getIn()
          Gets the InputStream that should be used as NewSystem.in during execution
protected abstract  java.io.PrintStream getOut()
          Gets the PrintStream that should be used as NewSystem.out during execution
abstract  java.lang.String getProgram()
          Gets the name of the program that will be run when this Command is executed
abstract  boolean isExit()
          Determines whether or not this Command is a command to exit the Shell
 boolean isResolved()
          Determines whether or not this Command is able to resolve the program returned by getProgram()
protected  java.lang.Class resolveClass(java.lang.String program)
          Resolves the given program as a Class
protected  java.io.File resolveExecutable(java.lang.String program)
          Resolves the given program as a File
 void run()
          Executes the Command
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Command

public Command()
Method Detail

getProgram

public abstract java.lang.String getProgram()
Gets the name of the program that will be run when this Command is executed

Returns:
the name of the program

getArgs

public abstract java.lang.String[] getArgs()
Gets the parameters that will be given to the program when this Command is executed

Returns:
the parameters

fork

public abstract boolean fork()
Determines whether this Command should be joined with immediately after its execution

Returns:
false if the executor should wait for the Thread to exit before continuing, true otherwise

getIn

protected abstract java.io.InputStream getIn()
Gets the InputStream that should be used as NewSystem.in during execution

Returns:
the InputStream

getOut

protected abstract java.io.PrintStream getOut()
Gets the PrintStream that should be used as NewSystem.out during execution

Returns:
the PrintStream

getErr

protected abstract java.io.PrintStream getErr()
Gets the PrintStream that should be used as NewSystem.err during execution

Returns:
the PrintStream

isExit

public abstract boolean isExit()
Determines whether or not this Command is a command to exit the Shell

Returns:
true if the Shell should exit after execution, false othewise

forceCurrentThread

public abstract boolean forceCurrentThread()
Determines whether the Command should be run in the same Thread as the executor

Returns:
true if the Command should be run in the current Thread

isResolved

public boolean isResolved()
Determines whether or not this Command is able to resolve the program returned by getProgram()

Returns:
false if the Command will cause an error when executed, true if execution is forseeably safe

resolveClass

protected java.lang.Class resolveClass(java.lang.String program)
                                throws java.lang.ClassNotFoundException
Resolves the given program as a Class

The default behavior of this method is to return:

this.getClass().getClassLoader().loadClass(program)

Implementations of this class should resolve Classes as is appropriate.

Parameters:
program - the name of the program to be resolved
Returns:
the resolved Class
Throws:
java.lang.ClassNotFoundException - if resolution was unsuccessful

resolveExecutable

protected java.io.File resolveExecutable(java.lang.String program)
                                  throws java.io.FileNotFoundException
Resolves the given program as a File

The default behavior of this method is to return:

new File(program)

Implementations of this class should resolve Files as is appropriate.

Parameters:
program - the name of the program to be resolved
Returns:
the resolved File
Throws:
java.io.FileNotFoundException - if resolution was unsuccessful

run

public void run()
Executes the Command

This method first sets the NewSystem.in, NewSystem.out and NewSystem.err streams as specified by getIn(), getOut() and getErr() respectively.

Next, it will try to resolve the program as a Class. If successful, it will invoke the classes main method with the arguments given by getArgs().

If Class resolution was unsuccessful, an attempt to resolve a File will be made. If a file is found, the method will then call OldRuntime.exec(String[],String[],File) with a null environment and with the current ShellImpl's present working directory.

If all attempts at program resolution are unsuccessful, the method gives up and exits.

Specified by:
run in interface java.lang.Runnable
See Also:
OldRuntime.exec(String[],String[],File), ShellImpl