org.rhq.enterprise.communications.command.server
Class MultipleCommandService

java.lang.Object
  extended by org.rhq.enterprise.communications.command.server.CommandMBean
      extended by org.rhq.enterprise.communications.command.server.CommandService
          extended by org.rhq.enterprise.communications.command.server.MultipleCommandService
All Implemented Interfaces:
MBeanRegistration, CommandExecutor, CommandServiceMBean

public abstract class MultipleCommandService
extends CommandService

Provides some infrastructure to more easily facilitate the ability to execute different commands. This class provides another level of indirection to locate the executors of commands. Where CommandService is the object that explicitly executes the requested command, this class will actually hand off the command to the actual CommandExecutor object that will execute the command.

This is similar to what the CommandProcessor does; however, this class allows a single, deployed MBean to handle multiple, distinctly different, command types (whereas the command processor looks for command service MBeans which do not easily support distinctly different commands (short of implementing a series of if-else statements to perform instanceof checks on the incoming command). Therefore, this class allows a single, deployed MBean to handle any number of commands, allowing for a smaller deployment configuration (rather than being forced to deploy X MBeans to handle X commands; this allows you to deploy 1 MBean to handle X commands).

Usually, this class is helpful when a command service needs to execute distinctly different commands, but where those commands are logically related to one another.

Subclasses of this class should not override CommandServiceMBean.getSupportedCommandTypes(), instead they must implement getSupportedCommandTypeExecutors().

Subclasses also should not override execute(Command, InputStream, OutputStream).

The executors this class uses can be instantiated on a per-request basis, or they may be reused (in which case, the implementor of the CommandExecutor class must ensure its thread safety).

Author:
John Mazzitelli

Nested Class Summary
protected  class MultipleCommandService.CommandTypeExecutor
          An inner class used only by the MultipleCommandService that encasulates a supported command type and the executor that should be used to execute commands of that type.
 
Constructor Summary
MultipleCommandService()
           
 
Method Summary
 CommandResponse execute(Command command, InputStream in, OutputStream out)
          Given a command to execute, this method will lookup that command's type to determine what executor should be used.
protected  Map<CommandType,MultipleCommandService.CommandTypeExecutor> getExecutors()
          Gets the set of executors whose map keys are the command types and whose map values are MultipleCommandService.CommandTypeExecutor objects.
protected abstract  MultipleCommandService.CommandTypeExecutor[] getSupportedCommandTypeExecutors()
          Returns a set of MultipleCommandService.CommandTypeExecutor objects that define what command types this service supports and the executors that will execute commands of those types.
 CommandType[] getSupportedCommandTypes()
          Subclasses to this class do not override this method; instead, they need to implement getSupportedCommandTypeExecutors().
 
Methods inherited from class org.rhq.enterprise.communications.command.server.CommandService
getCommandServiceId, getConnector, getServiceContainer, getSubsystem, prepareRemoteInputStream, prepareRemoteOutputStream, preRegister, setServiceContainer
 
Methods inherited from class org.rhq.enterprise.communications.command.server.CommandMBean
getLog, getMBeanServer, getObjectName, postDeregister, postRegister, preDeregister, startService, stopService
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MultipleCommandService

public MultipleCommandService()
Method Detail

execute

public CommandResponse execute(Command command,
                               InputStream in,
                               OutputStream out)
Given a command to execute, this method will lookup that command's type to determine what executor should be used. Once the executor is determined, this method delegates to that object to execute the command.

Parameters:
command - the command to execute
in - input stream should the executor want to get streamed data from the client
out - output stream should the executor want to stream data to the client
Returns:
the results of the command execution
Throws:
IllegalArgumentException - if the command is of an invalid or unknown type
See Also:
CommandExecutor.execute(Command, InputStream, OutputStream)

getSupportedCommandTypes

public CommandType[] getSupportedCommandTypes()
Subclasses to this class do not override this method; instead, they need to implement getSupportedCommandTypeExecutors().

Returns:
array of supported command types
See Also:
CommandServiceMBean.getSupportedCommandTypes()

getExecutors

protected Map<CommandType,MultipleCommandService.CommandTypeExecutor> getExecutors()
Gets the set of executors whose map keys are the command types and whose map values are MultipleCommandService.CommandTypeExecutor objects.

This is the method that actually calls the subclass' getSupportedCommandTypeExecutors() and builds the map from that. We create a Map as opposed to just using the array to make lookups by command type faster.

Returns:
map of executors

getSupportedCommandTypeExecutors

protected abstract MultipleCommandService.CommandTypeExecutor[] getSupportedCommandTypeExecutors()
Returns a set of MultipleCommandService.CommandTypeExecutor objects that define what command types this service supports and the executors that will execute commands of those types.

The returned array should be fixed during the lifetime of this service (or at least during its registration in an MBeanServer). Changes to the supported command types during runtime will not be detected once the CommandServiceDirectory has discovered this service. As a corollary to this rule, this method must be ready to provide the array of support command types at the time it is registered on an MBeanServer (in other words, this method will be called, specifically by the directory, as soon as this service is registered).

Unlike direct subclasses to CommandService, subclasses of this class do not need to override CommandServiceMBean.getSupportedCommandTypes(); instead, they override this method to inform the framework not only what command types the subclass supports but also what executors should handle the execution of commands.

Returns:
array of supported command types/executors


Copyright © 2008-2012 Red Hat, Inc.. All Rights Reserved.