com.sun.grid.jgrid.server
Class JCEPHandler

java.lang.Object
  extended bycom.sun.grid.jgrid.server.JCEPHandler
All Implemented Interfaces:
JCEPListener, LogListener

public class JCEPHandler
extends java.lang.Object
implements JCEPListener, LogListener

The JCEPHandler handles all JCEP communications. The main method listens for incoming socket requests and spins off a new thread to handle each. The new thread runs an instance of JCEPHandler which them gets a JCEPProtocolModule to handle the socket communications. When a client makes a request, the JCEPProtocolModule forwards the request to the JCEPHandler, which them performs the requested action.

Since:
0.2

Nested Class Summary
private static class JCEPHandler.LogFlusher
          A helper class used as a shutdown hook.
 
Field Summary
private  JCEPProtocolModule connection
          The JCEPProtocolModule which is handling the client communications
private static boolean debug
          Whether debug messages should be printed
private static long GRACE_PERIOD
          The default amount of number of miliseconds to wait for jobs to complete after a shutdown before explicitly exiting the VM.
private static long gracePeriod
          The amount of time to wait for jobs to complete after a shutdown before explicitly exiting the VM.
private static java.util.Map jobMap
          A Map of running and completed Jobs
private static java.util.logging.Logger log
          The logging mechanism
private static java.lang.Thread mainThread
          The thread running the main() method.
private static int numberOfRunningJobs
          The number of running jobs in this engine
private static int port
          The port number on which to listen for incoming requests
private static java.lang.String REGISTRY_HOST
          The default registry host to use if none is specified on the command line
private static int REGISTRY_PORT
          The default registry port to use if none is specified on the command line
private static java.lang.String registryHost
          The hostname for the RMI lookup server
private static int registryPort
          The port number for the RMI lookup server
private static ResultChannel resultChannel
          The ResultChannel retreived from the registry server to be used for returning results
private static java.lang.String USAGE
          The usage information String
 
Constructor Summary
private JCEPHandler(java.net.Socket socket)
          Creates a new instance of JCEPHandler
 
Method Summary
 void cancelJob(java.lang.String jobId)
          Cancel the executing Job.
 void checkpoint(java.lang.String jobId)
          Checkpoint the executing Job.
private static void connectToResultChannel()
          This method retrieves the ResultChannel object from the RMI registry server.
private static void decrementNumberOfJobs()
          Decrement the number of running jobs by 1
private static void flushLogs()
          Flushes the logs.
private static int getNumberOfJobs()
          Get the number of running jobs.
private static void incrementNumberOfJobs()
          Increment the number of running jobs by 1
 void logError(java.lang.String jobId, java.lang.String error)
          An error has occured in an executing job.
 void logMessage(java.lang.String jobId, java.lang.String message)
          An executing Job has sent a message.
static void main(java.lang.String[] args)
          Starts up the server, which listens for incoming socket connections.
 void notifyJobCheckpointed(java.lang.String jobId)
          An executing Job has been checkpointed.
 void notifyJobCompleted(java.lang.String jobId)
          An executing Job has completed normally.
 void notifyJobFailed(java.lang.String jobId)
          An executing Job has exited abnormally by throwing an Exception.
 void notifyJobResumed(java.lang.String jobId)
          An executing Job has been resumed.
 void notifyJobStarted(java.lang.String jobId)
          A Job has begun execution.
 void notifyJobStopped(java.lang.String jobId)
          An executing Job has exited abnormally by being canceled .
 void notifyJobSuspended(java.lang.String jobId)
          An executing Job has been suspended.
private static void prepareLogging()
          This method sets up the Logger object
private static void processArguments(java.lang.String[] args)
          Parses the command line arguments and acts accordingly
 void register(java.lang.String jobId)
          Register this JCEPHandler to receive messages from an executing Job.
 void resume(java.lang.String jobId)
          Resume a suspended Job If the Job cannot be resume, either because it is not suspended or it is uninterruptable this method will notify the client by throwing a CommandFailedException.
 void shutdown()
          Shutdown the JCEP server.
 void submitJob(Job job)
          Submit a Job for execution.
 void suspend(java.lang.String jobId)
          Suspend an executing Job If the Job cannot be suspended, either because it is no longer executing or it is uninterruptable this method will notify the client by throwing a CommandFailedException.
 void unregister(java.lang.String jobId)
          Unregister for messages from a Job.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

REGISTRY_HOST

private static final java.lang.String REGISTRY_HOST
The default registry host to use if none is specified on the command line

See Also:
Constant Field Values

REGISTRY_PORT

private static final int REGISTRY_PORT
The default registry port to use if none is specified on the command line

See Also:
Constant Field Values

GRACE_PERIOD

private static final long GRACE_PERIOD
The default amount of number of miliseconds to wait for jobs to complete after a shutdown before explicitly exiting the VM.

See Also:
Constant Field Values

USAGE

private static final java.lang.String USAGE
The usage information String

See Also:
Constant Field Values

jobMap

private static java.util.Map jobMap
A Map of running and completed Jobs


mainThread

private static java.lang.Thread mainThread
The thread running the main() method. Used to interrupt the server on a shutdown command.


resultChannel

private static ResultChannel resultChannel
The ResultChannel retreived from the registry server to be used for returning results


debug

private static boolean debug
Whether debug messages should be printed


registryHost

private static java.lang.String registryHost
The hostname for the RMI lookup server


registryPort

private static int registryPort
The port number for the RMI lookup server


port

private static int port
The port number on which to listen for incoming requests


log

private static java.util.logging.Logger log
The logging mechanism


gracePeriod

private static long gracePeriod
The amount of time to wait for jobs to complete after a shutdown before explicitly exiting the VM.


numberOfRunningJobs

private static int numberOfRunningJobs
The number of running jobs in this engine


connection

private JCEPProtocolModule connection
The JCEPProtocolModule which is handling the client communications

Constructor Detail

JCEPHandler

private JCEPHandler(java.net.Socket socket)
             throws java.io.IOException
Creates a new instance of JCEPHandler

Parameters:
socket - The socket connection to be used for communicating with the client
Throws:
java.io.IOException - Thrown when an error occurs while establishing a client connection
Method Detail

main

public static void main(java.lang.String[] args)
                 throws java.lang.Exception
Starts up the server, which listens for incoming socket connections.

Parameters:
args - The command line arguments
Throws:
java.lang.Exception - Thrown when something goes wrong. This is a bad thing and should be fixed before turning this loose on the public.

processArguments

private static void processArguments(java.lang.String[] args)
Parses the command line arguments and acts accordingly

Parameters:
args - The command line arguments

prepareLogging

private static void prepareLogging()
This method sets up the Logger object


register

public void register(java.lang.String jobId)
              throws CommandFailedException
Register this JCEPHandler to receive messages from an executing Job. Called by the JCEPProtocolModule.

Specified by:
register in interface JCEPListener
Parameters:
jobId - The id of the Job to which to attach
Throws:
CommandFailedException - thrown if this method cannot complete its task due to errors or failures
See Also:
Logger, Job

unregister

public void unregister(java.lang.String jobId)
                throws CommandFailedException
Unregister for messages from a Job. This method may be used to detach from Jobs that are no longer running. Called by the JCEPProtocolModule.

Specified by:
unregister in interface JCEPListener
Parameters:
jobId - The id of the Job from which to detach
Throws:
CommandFailedException - thrown if this method cannot complete its task due to errors or failures
See Also:
register(String)

cancelJob

public void cancelJob(java.lang.String jobId)
               throws CommandFailedException
Cancel the executing Job. This is done by calling the Job.cancel() method. If the Job cannot be canceled, either because it is no longer executing or it is uninterruptable this method will notify the client by throwing a CommandFailedException.

Specified by:
cancelJob in interface JCEPListener
Parameters:
jobId - The id of the Job to cancel
Throws:
CommandFailedException - thrown if this method cannot complete its task due to errors or failures
See Also:
Job.cancel()

checkpoint

public void checkpoint(java.lang.String jobId)
                throws CommandFailedException
Checkpoint the executing Job. This is done by calling the Job.checkpoint() method. If the Job cannot be checkpointed, either because it is no longer executing or it is uninterruptable this method will notify the client by throwing a CommandFailedException. Called by the JCEPProtocolModule.

Specified by:
checkpoint in interface JCEPListener
Parameters:
jobId - The id of the job to checkpoint
Throws:
CommandFailedException - thrown if this method cannot complete its task due to errors or failures
See Also:
Job.checkpoint()

shutdown

public void shutdown()
Shutdown the JCEP server. All Jobs are first checkpointed and then canceled. Called by JCEPProtocolModule.

Specified by:
shutdown in interface JCEPListener
See Also:
Job.cancel(), Job.checkpoint()

submitJob

public void submitJob(Job job)
Submit a Job for execution. A new thread will be created for the Job, and the Job object will be stored in the Job Map. Called by JCEPProtocolModule.

Specified by:
submitJob in interface JCEPListener
Parameters:
job - A Job object representing the job to be executed

logError

public void logError(java.lang.String jobId,
                     java.lang.String error)
An error has occured in an executing job. The error is communicated to the client via JCEPProtocolModule.logError(). Called by Logger.

Specified by:
logError in interface LogListener
Parameters:
jobId - The id of the Job from which the error originated
error - A String describing the error
See Also:
JCEPProtocolModule.logError(String, String), Logger

logMessage

public void logMessage(java.lang.String jobId,
                       java.lang.String message)
An executing Job has sent a message. The message is communicated to the client via JCEPProtocolModule.logMessage(). Called by Logger.

Specified by:
logMessage in interface LogListener
Parameters:
jobId - The id of the Job from which the message originated
message - The text of the message
See Also:
JCEPProtocolModule.logMessage(String, String), Logger

notifyJobStopped

public void notifyJobStopped(java.lang.String jobId)
An executing Job has exited abnormally by being canceled . The client is notified of the state change via JCEPProtocolModule.notifyJobStopped(). Called by Logger.

Specified by:
notifyJobStopped in interface LogListener
Parameters:
jobId - The id of the Job which exited
See Also:
JCEPProtocolModule.notifyJobStopped(String), Logger

notifyJobFailed

public void notifyJobFailed(java.lang.String jobId)
An executing Job has exited abnormally by throwing an Exception. The client is notified of the state change via JCEPProtocolModule.notifyJobFailed(). Called by Logger.

Specified by:
notifyJobFailed in interface LogListener
Parameters:
jobId - The id of the Job which exited
See Also:
JCEPProtocolModule.notifyJobFailed(String), Logger

notifyJobCheckpointed

public void notifyJobCheckpointed(java.lang.String jobId)
An executing Job has been checkpointed. The information is communicated to the client via JCEPProtocolModule.notifyJobCheckpointed(). Called by Logger.

Specified by:
notifyJobCheckpointed in interface LogListener
Parameters:
jobId - The id of the Job which has been checkpointed
See Also:
JCEPProtocolModule.notifyJobCheckpointed(String), Logger

notifyJobSuspended

public void notifyJobSuspended(java.lang.String jobId)
An executing Job has been suspended. The information is communicated to the client via JCEPProtocolModule.notifyJobSuspended(). Called by Logger.

Specified by:
notifyJobSuspended in interface LogListener
Parameters:
jobId - The id of the Job which has been suspended
See Also:
JCEPProtocolModule.notifyJobSuspended(String), Logger

notifyJobResumed

public void notifyJobResumed(java.lang.String jobId)
An executing Job has been resumed. The information is communicated to the client via JCEPProtocolModule.notifyJobResumed(). Called by Logger.

Specified by:
notifyJobResumed in interface LogListener
Parameters:
jobId - The id of the Job which has been resumed
See Also:
JCEPProtocolModule.notifyJobResumed(String), Logger

notifyJobCompleted

public void notifyJobCompleted(java.lang.String jobId)
An executing Job has completed normally. The client is notified of the state change via JCEPProtocolModule.notifyJobCompleted(). Called by Logger.

Specified by:
notifyJobCompleted in interface LogListener
Parameters:
jobId - The id of the Job which has completed
See Also:
JCEPProtocolModule.notifyJobCompleted(String), Logger

notifyJobStarted

public void notifyJobStarted(java.lang.String jobId)
A Job has begun execution. The client is notified of the state change via JCEPProtocolModule.notifyJobStarted(). Called by Logger.

Specified by:
notifyJobStarted in interface LogListener
Parameters:
jobId - The id of the Job which has started execution
See Also:
JCEPProtocolModule.notifyJobStarted(String), Logger

connectToResultChannel

private static void connectToResultChannel()
                                    throws java.rmi.RemoteException
This method retrieves the ResultChannel object from the RMI registry server.

Throws:
java.rmi.RemoteException - if an error occurs while trying to retrieve the ResultChannel object

incrementNumberOfJobs

private static void incrementNumberOfJobs()
Increment the number of running jobs by 1


decrementNumberOfJobs

private static void decrementNumberOfJobs()
Decrement the number of running jobs by 1


getNumberOfJobs

private static int getNumberOfJobs()
Get the number of running jobs.

Returns:
the number of running jobs

flushLogs

private static void flushLogs()
Flushes the logs. Used in a shutdown hook to make sure all information gets displayed in the event the server is stopped.


resume

public void resume(java.lang.String jobId)
            throws CommandFailedException
Resume a suspended Job If the Job cannot be resume, either because it is not suspended or it is uninterruptable this method will notify the client by throwing a CommandFailedException. Called by the JCEPProtocolModule.

Specified by:
resume in interface JCEPListener
Parameters:
jobId - The id of the Job to resume
Throws:
CommandFailedException - if the command was unable to complete sucessfully
See Also:
Job.resume()

suspend

public void suspend(java.lang.String jobId)
             throws CommandFailedException
Suspend an executing Job If the Job cannot be suspended, either because it is no longer executing or it is uninterruptable this method will notify the client by throwing a CommandFailedException. Called by the JCEPProtocolModule.

Specified by:
suspend in interface JCEPListener
Parameters:
jobId - The id of the Job to suspend
Throws:
CommandFailedException - if the command was unable to complete sucessfully
See Also:
Job.suspend()