com.norconex.jef.exec
Class SystemCommand

java.lang.Object
  extended by com.norconex.jef.exec.SystemCommand

public class SystemCommand
extends Object

Represents a program to be executed by the underlying system (on the "command line"). This class attempts to be system-independent, which means given an executable path should be sufficient to run programs on any systems (e.g. it handles prefixing an executable with OS specific commands as well as preventing process hanging on some OS when there is nowhere to display the output).

Since:
1.1
Author:
Pascal Essiembre

Constructor Summary
SystemCommand(File workdir, String... command)
          Creates a command.
SystemCommand(String... command)
          Creates a command for which the execution will be in the working directory of the current process.
 
Method Summary
 void abort()
          Aborts the running command.
 void addErrorListener(IStreamListener listener)
          Adds an error (STDERR) listener to this system command.
 void addOutputListener(IStreamListener listener)
          Adds an output (STDOUT) listener to this system command.
 int execute()
          Executes the given command and returns only when the underlying process stopped running.
 int execute(boolean runInBackground)
          Executes the given system command.
 String[] getCommand()
          Gets the command to be run.
 String getDescription()
          Gets the description for this command.
 File getWorkdir()
          Gets the command working directory.
 boolean isRunning()
          Returns whether the command is currently running.
 void removeErrorListener(IStreamListener listener)
          Removes an error (STDERR) listener.
 void removeOutputListener(IStreamListener listener)
          Removes an output (STDOUT) listener.
 void setDescription(String description)
          Sets the description for this command.
 String toString()
          Returns the command to be executed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

SystemCommand

public SystemCommand(String... command)
Creates a command for which the execution will be in the working directory of the current process. If more than one command values are passed, the first element of the array is the command and subsequent elements are arguments.

Parameters:
command - the command to run

SystemCommand

public SystemCommand(File workdir,
                     String... command)
Creates a command. If more than one command values are passed, the first element of the array is the command and subsequent elements are arguments.

Parameters:
command - the command to run
workdir - specifies a working directory (default inherits the working directory of the current process.
Method Detail

getCommand

public String[] getCommand()
Gets the command to be run.

Returns:
the command

getDescription

public String getDescription()
Gets the description for this command.

Returns:
command description

setDescription

public void setDescription(String description)
Sets the description for this command.

Parameters:
description - command description

getWorkdir

public File getWorkdir()
Gets the command working directory.

Returns:
command working directory.

addErrorListener

public void addErrorListener(IStreamListener listener)
Adds an error (STDERR) listener to this system command.

Parameters:
listener - command error listener

removeErrorListener

public void removeErrorListener(IStreamListener listener)
Removes an error (STDERR) listener.

Parameters:
listener - command error listener

addOutputListener

public void addOutputListener(IStreamListener listener)
Adds an output (STDOUT) listener to this system command.

Parameters:
listener - command output listener

removeOutputListener

public void removeOutputListener(IStreamListener listener)
Removes an output (STDOUT) listener.

Parameters:
listener - command output listener

isRunning

public boolean isRunning()
Returns whether the command is currently running.

Returns:
true if running

abort

public void abort()
Aborts the running command. If the command is not currently running, aborting it will have no effect.


execute

public int execute()
            throws InterruptedException,
                   IOException
Executes the given command and returns only when the underlying process stopped running.

Returns:
process exit value
Throws:
InterruptedException - problem executing command
IOException - problem executing command

execute

public int execute(boolean runInBackground)
            throws InterruptedException,
                   IOException
Executes the given system command. When run in the background, this method will return quickly, with a status code of 0. The status will not reflect the sub-process termination status. When NOT run in the background, this method waits and returns only when the underlying process stopped running. Alternatively, to run a command asynchronously, you can wrap it in its own thread (e.g. wrapping it in a AsyncJobGroup).

Parameters:
runInBackground - true to runs the system command in background.
Returns:
process exit value
Throws:
InterruptedException - problem executing command
IOException - problem executing command
Since:
2.0

toString

public String toString()
Returns the command to be executed.

Overrides:
toString in class Object


Copyright © 2007-2013 Norconex Inc.. All Rights Reserved.