com.sun.faban.harness.agent
Class CmdAgentImpl

java.lang.Object
  extended by java.rmi.server.RemoteObject
      extended by java.rmi.server.RemoteServer
          extended by java.rmi.server.UnicastRemoteObject
              extended by com.sun.faban.harness.agent.CmdAgentImpl
All Implemented Interfaces:
CommandChecker, CmdAgent, Serializable, Remote, Unreferenced

public class CmdAgentImpl
extends UnicastRemoteObject
implements CmdAgent, CommandChecker, Unreferenced

CmdAgentImpl is the class that runs remote commands for the CmdService This implementation provides a robust means of running remote commands. All error messages from the remote commands are logged to the error log, which should help in debugging. The user is encouraged not to run huge shell scripts using this interface as the debugging advantages will be lost. Rather, try and break up the task to running Java/native apps as far as possible and use shell scripts sparingly. If the shell scripts spit out periodic status messages indicating the position in its execution cycle, this will aid in debugging.

Author:
Ramesh Ramachandran
See Also:
CmdAgent, CmdService, Serialized Form

Field Summary
 
Fields inherited from class java.rmi.server.RemoteObject
ref
 
Method Summary
 List<String> checkCommand(List<String> cmd, Map<String,List<String>> extMap)
          Checks and completes the command list, if possible.
 List<String> checkJavaCommand(List<String> cmd, List<String> extClassPath)
          Checks and completes the java command, if possible.
 void downloadServices(List<String> pathList)
          Downloads the files used by services and tools to the remote agent system.
<V extends Serializable>
V
exec(RemoteCallable<V> callable, String contextLocation)
          Executes the RemoteCallable on the target instance.
 CommandHandle execute(Command c, String svcPath)
          Executes a command from the remote command agent.
static CmdAgentImpl getHandle()
          Only Other Agents should access the command agent using this method.
static String getHost()
          Obtains the hostname.
 String getHostName()
          Return the hostname of this machine as known to this machine itself.
static String getMaster()
          Obtains the master hostname.
static Registry getRegistry()
          Obtains the registry.
 long getTime()
          Gets the time on the agent host, in millis.
 Timer getTimer()
          Obtains the timer associated with this command agent.
 String getTmpDir()
          Obtains the tmp directory of a remote host.
 CommandHandle java(Command c, String svcPath)
          Executes a java command from the remote command agent.
 void kill()
          Kill off all processes started.
 void setLogLevel(String name, Level level)
          Set the logging level of the specified logger.
 void setTime(String gmtTimeString)
          Sets the time on the agent host, in GMT.
 boolean startAgent(Class agentClass, String identifier)
          Registers and starts agent.
 void unreferenced()
          When this instance is unreferenced the application must exit.
 void updatePaths(List<String> pathList)
          Updates the paths, usually in the local command agent.
 
Methods inherited from class java.rmi.server.UnicastRemoteObject
clone, exportObject, exportObject, exportObject, unexportObject
 
Methods inherited from class java.rmi.server.RemoteServer
getClientHost, getLog, setLog
 
Methods inherited from class java.rmi.server.RemoteObject
equals, getRef, hashCode, toString, toStub
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

getHostName

public String getHostName()
Return the hostname of this machine as known to this machine itself. This method is included in order to solve a Naming problem related to the names of the tpcw result files to be transferred to the the master machine.

Specified by:
getHostName in interface CmdAgent
Returns:
The hostname

getHandle

public static CmdAgentImpl getHandle()
Only Other Agents should access the command agent using this method.

Returns:
this Command Agent

getTmpDir

public String getTmpDir()
Obtains the tmp directory of a remote host.

Specified by:
getTmpDir in interface CmdAgent
Returns:
The tmp directory.

setLogLevel

public void setLogLevel(String name,
                        Level level)
Set the logging level of the specified logger.

Specified by:
setLogLevel in interface CmdAgent
Parameters:
name - Name of the logger. If "" is passed the root logger level will be set.
level - The Log level to set

updatePaths

public void updatePaths(List<String> pathList)
Updates the paths, usually in the local command agent.

Specified by:
updatePaths in interface CmdAgent
Parameters:
pathList - The list of paths to download

downloadServices

public void downloadServices(List<String> pathList)
Downloads the files used by services and tools to the remote agent system.

Specified by:
downloadServices in interface CmdAgent
Parameters:
pathList - The list of service bundle paths

execute

public CommandHandle execute(Command c,
                             String svcPath)
                      throws IOException,
                             InterruptedException
Executes a command from the remote command agent.

Specified by:
execute in interface CmdAgent
Parameters:
c - The command to be executed
svcPath - The service location, if any
Returns:
A handle to the command
Throws:
IOException - Error communicating with resulting process
InterruptedException - Thread got interrupted waiting

java

public CommandHandle java(Command c,
                          String svcPath)
                   throws IOException,
                          InterruptedException
Executes a java command from the remote command agent.

Specified by:
java in interface CmdAgent
Parameters:
c - The command containing the main class
svcPath -
Returns:
A handle to the command
Throws:
IOException - Error communicating with resulting process
InterruptedException - Thread got interrupted waiting

exec

public <V extends Serializable> V exec(RemoteCallable<V> callable,
                                       String contextLocation)
                            throws Exception
Executes the RemoteCallable on the target instance.

Specified by:
exec in interface CmdAgent
Parameters:
callable - The callable to execute
contextLocation - The context location of the invoker
Returns:
The type specified at creation of the callable.
Throws:
Exception - Any exception from the callable

startAgent

public boolean startAgent(Class agentClass,
                          String identifier)
                   throws Exception
Registers and starts agent.

Specified by:
startAgent in interface CmdAgent
Parameters:
agentClass - The agent class
identifier - The agent id
Returns:
always returns true
Throws:
Exception - If an error occurs in registering the class

kill

public void kill()
Kill off all processes started.

Specified by:
kill in interface CmdAgent

unreferenced

public void unreferenced()
When this instance is unreferenced the application must exit.

Specified by:
unreferenced in interface Unreferenced
See Also:
Unreferenced

getRegistry

public static Registry getRegistry()
Obtains the registry.

Returns:
Registry

getHost

public static String getHost()
Obtains the hostname.

Returns:
hostname

getMaster

public static String getMaster()
Obtains the master hostname.

Returns:
master hostname

checkCommand

public List<String> checkCommand(List<String> cmd,
                                 Map<String,List<String>> extMap)
Checks and completes the command list, if possible.

Specified by:
checkCommand in interface CommandChecker
Parameters:
cmd - The command and arg list
extMap - The external map, if any
Returns:
The checked command

checkJavaCommand

public List<String> checkJavaCommand(List<String> cmd,
                                     List<String> extClassPath)
Checks and completes the java command, if possible.

Specified by:
checkJavaCommand in interface CommandChecker
Parameters:
cmd - The original command
extClassPath - The extended classpath, if any
Returns:
The completed java command

setTime

public void setTime(String gmtTimeString)
             throws IOException
Sets the time on the agent host, in GMT. The time string must be in the format MMddHHmmyyyy.ss according to Unix date specs and must be in GMT time.

Specified by:
setTime in interface CmdAgent
Parameters:
gmtTimeString - Time string in format
Throws:
IOException - An I/O error occurred

getTime

public long getTime()
Gets the time on the agent host, in millis.

Specified by:
getTime in interface CmdAgent
Returns:
The time on the remote system.

getTimer

public Timer getTimer()
Obtains the timer associated with this command agent.

Returns:
the timer for this command agent