protoj.core
Class Command

java.lang.Object
  extended by protoj.core.Command

public final class Command
extends java.lang.Object

Represents a unit of work to be carried out by the project. A command consists of two parts:

  1. bootstrap: the first part is actually the information used to create a new vm and is called the bootstrap. This is so that various vm parameters can be configured such as the maximum amount of memory, because it isn't possible to reconfigure an already running vm.
  2. body: the second part is the actual code that should be executed, called the body.
To determine whether the command will bootstrap or whether its body will be executed, the execute(Instruction) method checks for the existence of the BODY command line options. If it isn't present the command will bootstrap to a new vm, but with the BODY option added. If the BODY option does exist then the body will be executed.

Author:
Ashley Williams

Constructor Summary
Command(CommandStore store, java.lang.String name, java.lang.String description, java.lang.String memory, java.lang.Runnable body)
          Creates a command with an associated body that will execute in another instance of the current vm.
 
Method Summary
 boolean containsAlias(java.lang.String alias)
          Convenience method that checks whether or not the given alias is known to this command.
 void execute(Instruction instruction)
          Executes this command using the given specified instruction that usually originates from the command line.
 java.util.List<java.lang.String> getAliases()
          A read-only list of all the names this command is known by.
 java.lang.String getAliasText()
          Reports the aliases available, useful for help text.
 java.lang.String getDescription()
          A description useful for help text.
 java.lang.String getHelpText()
          Builds a message suitable for displaying in response to a help command.
 java.lang.String getInstructionText()
          Includes both the command name and arguments, if any, that make up the full definition of the command.
 java.lang.String getMemory()
          The maximum amount of memory that the forked vm will be able to claim.
 java.lang.String getName()
          The unique name of this command.
 joptsimple.OptionSet getOptions()
          The helper responsible for parsing the command line arguments.
 joptsimple.OptionParser getParser()
          Use in order to configure the command line options for this command.
 ArgRunnable<StartVmCommand> getStartVmConfig()
          The instance used to provide additional configuration before the vm is launched inside the startVm() method.
 CommandStore getStore()
          A reference to the owning store.
 void initAliases(java.lang.String... aliases)
          Optional method, specify alternate names for this command.
 void initBootstrapCurrentVm()
          There is almost no reason to use this method since by default most commands should bootstrap to the project main.
 void setMemory(java.lang.String memory)
          See getMemory().
 void setStartVmConfig(ArgRunnable<StartVmCommand> startVmConfig)
          See setStartVmConfig(ArgRunnable).
 void throwBadOptionsException(java.lang.String hint)
          Throws an InformationException with standardized error information.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Command

public Command(CommandStore store,
               java.lang.String name,
               java.lang.String description,
               java.lang.String memory,
               java.lang.Runnable body)
Creates a command with an associated body that will execute in another instance of the current vm.

Parameters:
store -
name -
description -
memory -
body -
Method Detail

setStartVmConfig

public void setStartVmConfig(ArgRunnable<StartVmCommand> startVmConfig)
See setStartVmConfig(ArgRunnable).

Parameters:
startVmConfig -

getStartVmConfig

public ArgRunnable<StartVmCommand> getStartVmConfig()
The instance used to provide additional configuration before the vm is launched inside the startVm() method.

Returns:

initAliases

public void initAliases(java.lang.String... aliases)
Optional method, specify alternate names for this command.

Parameters:
aliases -

initBootstrapCurrentVm

public void initBootstrapCurrentVm()
There is almost no reason to use this method since by default most commands should bootstrap to the project main. This was created for the compile task that must bootstrap to the currently running vm since the project main classes may not have been compiled.


execute

public void execute(Instruction instruction)
Executes this command using the given specified instruction that usually originates from the command line. The --body option is used to determine what should happen when this method is called:

Parameters:
instruction -

getMemory

public java.lang.String getMemory()
The maximum amount of memory that the forked vm will be able to claim.

Returns:

setMemory

public void setMemory(java.lang.String memory)
See getMemory().

Parameters:
memory -

getName

public java.lang.String getName()
The unique name of this command. Also appears in the list of aliases as the very first element.

Returns:

getAliases

public java.util.List<java.lang.String> getAliases()
A read-only list of all the names this command is known by.

Returns:

getDescription

public java.lang.String getDescription()
A description useful for help text.

Returns:

getParser

public joptsimple.OptionParser getParser()
Use in order to configure the command line options for this command. The returned parser can be used as in the following example that expects a mandatory file path to be passed via the -f option:
 OptionSpec<File> fileOpt = parser.accepts("f").withRequiredArg().ofType(
                File.class);
 

The returned parser is already configured to look for the "--body" option that when specified indicates the command body is to be executed. When not specified, a new vm is to be created.

Returns:

getAliasText

public java.lang.String getAliasText()
Reports the aliases available, useful for help text. An example return value would be: "foo (f, -foo, --foo)"

Returns:

getOptions

public joptsimple.OptionSet getOptions()
The helper responsible for parsing the command line arguments.

Returns:

containsAlias

public boolean containsAlias(java.lang.String alias)
Convenience method that checks whether or not the given alias is known to this command.

Parameters:
alias -
Returns:

getInstructionText

public java.lang.String getInstructionText()
Includes both the command name and arguments, if any, that make up the full definition of the command.

Returns:

getStore

public CommandStore getStore()
A reference to the owning store.

Returns:

getHelpText

public java.lang.String getHelpText()
Builds a message suitable for displaying in response to a help command.

Returns:

throwBadOptionsException

public void throwBadOptionsException(java.lang.String hint)
Throws an InformationException with standardized error information. Useful for calling from a command that has badly specified options. The help description is included in the detailed message along with an additional hint that gives more information as to what the problem is.

Parameters:
hint - may be null or empty if no hint is available