net.sourceforge.cruisecontrol.util
Class Commandline

java.lang.Object
  extended bynet.sourceforge.cruisecontrol.util.Commandline
All Implemented Interfaces:
java.lang.Cloneable
Direct Known Subclasses:
EnvCommandline

public class Commandline
extends java.lang.Object
implements java.lang.Cloneable

Commandline objects help handling command lines specifying processes to execute. The class can be used to define a command line as nested elements or as a helper to define a command line by an application.

<someelement>
  <acommandline executable="/executable/to/run">
    <argument value="argument 1" />
    <argument line="argument_1 argument_2 argument_3" />
    <argument value="argument 4" />
  </acommandline>
</someelement>
The element someelement must provide a method createAcommandline which returns an instance of this class.

Author:
thomas.haas@softwired-inc.com, Stefan Bodewig

Nested Class Summary
static class Commandline.Argument
          Used for nested xml command line definitions.
 class Commandline.Marker
          Class to keep track of the position of an Argument.
 
Constructor Summary
Commandline()
           
Commandline(java.lang.String toProcess)
           
Commandline(java.lang.String toProcess, CruiseRuntime cruiseRuntime)
           
 
Method Summary
 void addArguments(java.lang.String[] line)
           
 void clear()
          Clear out the whole command line.
 void clearArgs()
          Clear out the arguments but leave the executable in place for another operation.
 java.lang.Object clone()
           
 Commandline.Argument createArgument()
          Creates an argument object.
 Commandline.Argument createArgument(boolean insertAtStart)
          Creates an argument object and adds it to our list of args.
 Commandline.Argument createArgument(java.lang.String value)
          Same as calling createArgument().setValue(value), but much more convenient.
 void createArguments(java.lang.String first, java.lang.String second)
          Same as calling createArgument twice in a row, but can be used to make more obvious a relationship between to command line arguments, like "-folder c:\myfolder".
 Commandline.Marker createMarker()
          Return a marker.
 java.lang.Process execute()
          Executes the command.
 int executeAndWait(org.apache.log4j.Logger log)
          Executes the command and wait for it to finish.
 java.lang.String[] getArguments()
          Returns all arguments defined by addLine, addValue or the argument object.
 java.lang.String[] getCommandline()
          Returns the executable and all defined arguments.
 java.lang.String getExecutable()
           
protected  java.io.File getWorkingDir()
           
 java.io.File getWorkingDirectory()
           
static java.lang.String quoteArgument(java.lang.String argument)
          Put quotes around the given String if necessary.
 void setExecutable(java.lang.String executable)
          Sets the executable to run.
 void setWorkingDir(java.io.File workingDir)
          Sets exeuction directory
 void setWorkingDirectory(java.lang.String path)
          Sets execution directory.
 int size()
           
 java.lang.String toString()
           
static java.lang.String toString(java.lang.String[] line, boolean quote)
           
static java.lang.String toString(java.lang.String[] line, boolean quote, java.lang.String separator)
           
 java.lang.String toStringNoQuoting()
          Converts the command line to a string without adding quotes to any of the arguments.
static java.lang.String[] translateCommandline(java.lang.String toProcess)
           
 void useSafeQuoting(boolean safe)
          Enables and disables safe quoting when executing a command.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Commandline

public Commandline(java.lang.String toProcess,
                   CruiseRuntime cruiseRuntime)

Commandline

public Commandline(java.lang.String toProcess)

Commandline

public Commandline()
Method Detail

getWorkingDir

protected java.io.File getWorkingDir()

createArgument

public Commandline.Argument createArgument()
Creates an argument object.

Each commandline object has at most one instance of the argument class. This method calls this.createArgument(false).

Returns:
the argument object.
See Also:
createArgument(boolean)

createArgument

public Commandline.Argument createArgument(boolean insertAtStart)
Creates an argument object and adds it to our list of args.

Each commandline object has at most one instance of the argument class.

Parameters:
insertAtStart - if true, the argument is inserted at the beginning of the list of args, otherwise it is appended.

createArgument

public Commandline.Argument createArgument(java.lang.String value)
Same as calling createArgument().setValue(value), but much more convenient.


createArguments

public void createArguments(java.lang.String first,
                            java.lang.String second)
Same as calling createArgument twice in a row, but can be used to make more obvious a relationship between to command line arguments, like "-folder c:\myfolder".


setExecutable

public void setExecutable(java.lang.String executable)
Sets the executable to run.


getExecutable

public java.lang.String getExecutable()

addArguments

public void addArguments(java.lang.String[] line)

getCommandline

public java.lang.String[] getCommandline()
Returns the executable and all defined arguments.


getArguments

public java.lang.String[] getArguments()
Returns all arguments defined by addLine, addValue or the argument object.


toString

public java.lang.String toString()

toStringNoQuoting

public java.lang.String toStringNoQuoting()
Converts the command line to a string without adding quotes to any of the arguments.


quoteArgument

public static java.lang.String quoteArgument(java.lang.String argument)
                                      throws CruiseControlException
Put quotes around the given String if necessary.

If the argument doesn't include spaces or quotes, return it as is. If it contains double quotes, use single quotes - else surround the argument by double quotes.

Throws:
CruiseControlException - if the argument contains both, single and double quotes.

toString

public static java.lang.String toString(java.lang.String[] line,
                                        boolean quote)

toString

public static java.lang.String toString(java.lang.String[] line,
                                        boolean quote,
                                        java.lang.String separator)

translateCommandline

public static java.lang.String[] translateCommandline(java.lang.String toProcess)
                                               throws CruiseControlException
Throws:
CruiseControlException

size

public int size()

clone

public java.lang.Object clone()
                       throws java.lang.CloneNotSupportedException
Throws:
java.lang.CloneNotSupportedException

clear

public void clear()
Clear out the whole command line.


clearArgs

public void clearArgs()
Clear out the arguments but leave the executable in place for another operation.


createMarker

public Commandline.Marker createMarker()
Return a marker.

This marker can be used to locate a position on the commandline - to insert something for example - when all parameters have been set.


setWorkingDirectory

public void setWorkingDirectory(java.lang.String path)
                         throws CruiseControlException
Sets execution directory.

Throws:
CruiseControlException

useSafeQuoting

public void useSafeQuoting(boolean safe)
Enables and disables safe quoting when executing a command. When enabled: Quotes any arguments that need it when executing command. This should handle filenames with spaces, but may fall over if the arguments already have quoting within them. When disables: Arguments are passed as is.


setWorkingDir

public void setWorkingDir(java.io.File workingDir)
                   throws CruiseControlException
Sets exeuction directory

Throws:
CruiseControlException

getWorkingDirectory

public java.io.File getWorkingDirectory()

execute

public java.lang.Process execute()
                          throws java.io.IOException
Executes the command.

Throws:
java.io.IOException

executeAndWait

public int executeAndWait(org.apache.log4j.Logger log)
                   throws java.io.IOException,
                          java.lang.InterruptedException
Executes the command and wait for it to finish.

Parameters:
log - where the output and error streams are logged
Returns:
the process' exit value
Throws:
java.io.IOException
java.lang.InterruptedException