com.gridsystems.utils
Class NativeShell

java.lang.Object
  extended by com.gridsystems.utils.NativeShell

public final class NativeShell
extends java.lang.Object

Cross-Platform command execution manager.

This class provides a configurable mechanism to invoke native commands in a cross-platform way.

Author:
Rodrigo Ruiz

Constructor Summary
NativeShell()
          Creates an instance.
 
Method Summary
static java.lang.String alias(java.lang.String cmd, java.lang.Object... args)
          Gets a platform dependent alias for the specified command.
protected static java.lang.String doAlias(java.lang.String osName, java.lang.String cmd, java.lang.Object... args)
          Gets a platform dependent alias for the specified command.
protected  java.lang.Integer doExec(java.lang.String osName, java.lang.String cmd, java.lang.Object... args)
          Executes a native application.
protected static java.lang.String[] doShell(java.lang.String osName, java.lang.String cmd, java.lang.Object... args)
          Gets an array to be used for executing the given command in a platform dependent shell.
 java.lang.Integer exec(java.lang.String cmd, java.lang.Object... args)
          Executes a native application.
static java.lang.String[] getAllCommands()
          Gets a list of all commands this instance is aware of.
static java.lang.String[] getAvailableCommands()
          Gets a list of all commands available in this platform.
 java.io.File getDirectory()
          Gets the working directory.
 java.util.Map<java.lang.String,java.lang.String> getEnv()
          Gets the shell environment.
static void setConfigFile(java.io.File f)
          Sets the command alias configuration file.
 void setDirectory(java.io.File dir)
          Sets the working directory.
 void setStdErr(java.io.File f)
          Redirects the standard error to a file.
 void setStdErr(java.io.OutputStream os)
          Redirects the standard error to a stream.
 void setStdOut(java.io.File f)
          Redirects the standard output to a file.
 void setStdOut(java.io.OutputStream os)
          Redirects the standard output to a stream.
static java.lang.String[] shell(java.lang.String cmd, java.lang.Object... args)
          Gets an array to be used for executing the given command in a platform dependent shell.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

NativeShell

public NativeShell()
Creates an instance.

Method Detail

setConfigFile

public static void setConfigFile(java.io.File f)
Sets the command alias configuration file.

Parameters:
f - The file

alias

public static java.lang.String alias(java.lang.String cmd,
                                     java.lang.Object... args)
Gets a platform dependent alias for the specified command.

Parameters:
cmd - The command to search for
args - Command parameters
Returns:
The found alias

doAlias

protected static java.lang.String doAlias(java.lang.String osName,
                                          java.lang.String cmd,
                                          java.lang.Object... args)
Gets a platform dependent alias for the specified command.

Parameters:
cmd - The command to search for
osName - The platform for which the command is expected
args - Command parameters
Returns:
The found alias

getAllCommands

public static java.lang.String[] getAllCommands()
Gets a list of all commands this instance is aware of. The list includes commands that may be unsupported for the current platform. To get only those commands available in the local platform, use the method getAvailableCommands() instead.

Returns:
A list of known command names

getAvailableCommands

public static java.lang.String[] getAvailableCommands()
Gets a list of all commands available in this platform.

Returns:
A list of available command names

shell

public static java.lang.String[] shell(java.lang.String cmd,
                                       java.lang.Object... args)
Gets an array to be used for executing the given command in a platform dependent shell.

The command is converted in a platform dependent command by calling alias(String, Object...).

Parameters:
cmd - The command to execute
args - The command arguments
Returns:
An array that can be used in an exec() call

doShell

protected static java.lang.String[] doShell(java.lang.String osName,
                                            java.lang.String cmd,
                                            java.lang.Object... args)
Gets an array to be used for executing the given command in a platform dependent shell.

The command is converted in a platform dependent command by calling alias(String, Object...).

Parameters:
osName - The OS name
cmd - The command to execute
args - The command arguments
Returns:
An array that can be used in an exec() call

getEnv

public java.util.Map<java.lang.String,java.lang.String> getEnv()
Gets the shell environment.

Returns:
The environment

setDirectory

public void setDirectory(java.io.File dir)
Sets the working directory.

Parameters:
dir - The directory

getDirectory

public java.io.File getDirectory()
Gets the working directory.

Returns:
The directory

setStdOut

public void setStdOut(java.io.File f)
               throws java.io.IOException
Redirects the standard output to a file.

Parameters:
f - The file to redirect to
Throws:
java.io.IOException - If the file cannot be created

setStdOut

public void setStdOut(java.io.OutputStream os)
Redirects the standard output to a stream.

Parameters:
os - The stream to redirect to

setStdErr

public void setStdErr(java.io.File f)
               throws java.io.IOException
Redirects the standard error to a file.

Parameters:
f - The file to redirect to
Throws:
java.io.IOException - If the file cannot be created

setStdErr

public void setStdErr(java.io.OutputStream os)
Redirects the standard error to a stream.

Parameters:
os - The stream to redirect to

exec

public java.lang.Integer exec(java.lang.String cmd,
                              java.lang.Object... args)
                       throws java.io.IOException,
                              java.lang.InterruptedException
Executes a native application.

Parameters:
cmd - The command to execute
args - The command arguments
Returns:
null if nothing was executed, or the exit code
Throws:
java.io.IOException - If an error occurs during the execution
java.lang.InterruptedException - If the thread is interrupted

doExec

protected java.lang.Integer doExec(java.lang.String osName,
                                   java.lang.String cmd,
                                   java.lang.Object... args)
                            throws java.io.IOException,
                                   java.lang.InterruptedException
Executes a native application.

Parameters:
osName - The OS name
cmd - The command to execute
args - The command arguments
Returns:
null if nothing was executed, or the exit code
Throws:
java.io.IOException - If an error occurs during the execution
java.lang.InterruptedException - If the thread is interrupted


Copyright © 2007-2008 Grid Systems, S.A.. All Rights Reserved.