/************************************************* * * = PACKAGE * JACE.Connection * * = FILENAME * StrategyAcceptor.java * *@author Prashant Jain * *************************************************/ package JACE.Connection; import java.io.*; import java.net.*; import JACE.OS.*; import JACE.SOCK_SAP.*; /** * Abstract factory for creating, accepting into, and activating * a service handler. *
* Uses instances of CreationStrategy, AcceptStrategy, and
* ActivateStrategy.
*
*@see SvcHandler
*@see CreationStrategy
*@see AcceptStrategy
*@see ActivateStrategy
*/
public class StrategyAcceptor extends Acceptor
{
/**
* Create an instance of StrategyAcceptor. The caller must
* call setHandlerFactory before open!
*/
public StrategyAcceptor ()
{
this (null, null, null, null);
}
/**
* Create an instance of StrategyAcceptor.
*@param handlerFactory Svc Handler factory that is used to create
* an instance of a Svc Handler when a connection is accepted.
*/
public StrategyAcceptor (Class handlerFactory)
{
this (handlerFactory, null, null, null);
}
/**
* Create an instance of StrategyAcceptor. Use the creation
* strategy and the handlerFactory passed in to creae a new instance
* of the Svc Handler.
*@param handlerFactory Svc Handler factory that is used to create
* an instance of a Svc Handler when a connection is accepted.
*@param creStrategy Creation strategy to use to create a new
* instance of the Svc Handler.
*@param acceptStrategy Accept strategy to use to accept a new
* connection into the Svc Handler.
*@param activateStrategy Activate strategy to use to activate the
* instance of the Svc Handler.
*/
public StrategyAcceptor (Class handlerFactory,
CreationStrategy creStrategy,
AcceptStrategy acceptStrategy,
ActivateStrategy activateStrategy)
{
// Cache everything
this.handlerFactory_ = handlerFactory;
this.creStrategy_ = creStrategy;
this.acceptStrategy_ = acceptStrategy;
this.activateStrategy_ = activateStrategy;
}
/**
* Initialize the Strategy Acceptor. The method creates the
* appropriate strategies as needed.
*@param port port number where the server will listen for connections
*@exception IOException Socket level error
*/
public void open (int port) throws IOException
{
if (this.creStrategy_ == null)
this.creStrategy_ = new CreationStrategy (this.handlerFactory_);
if (this.acceptStrategy_ == null)
this.acceptStrategy_ = new AcceptStrategy (port);
else
this.acceptStrategy_.open (port);
if (this.activateStrategy_ == null)
this.activateStrategy_ = new ActivateStrategy ();
}
/**
* Accept a connection using the appropriate strategies.
*
*@exception SocketException Socket level error
*@exception InstantiationException Problem creating a handler
*@exception IllegalAccessException No strategy available
*@exception IOException Socket level error
*/
public void accept () throws SocketException,
InstantiationException,
IllegalAccessException,
IOException
{
// Create a Svc_Handler using the appropriate Creation_Strategy
SvcHandler sh = this.makeSvcHandler ();
// Accept a connection into the Svc_Handler
this.acceptSvcHandler (sh);
// Activate the Svc_Handler
this.activateSvcHandler (sh);
}
/**
* Bridge method for creating a SvcHandler. The strategy for
* creating a SvcHandler is configured into the Acceptor via it's
* creStrategy_. If no strategy is passed in, the default behavior
* of this method is to use the default CreationStrategy.
*@return a new instance of the Svc Handler
*@exception InstantiationException Couldn't create SvcHandler
*@exception IllegalAccessException No strategy available
*/
protected SvcHandler makeSvcHandler ()
throws InstantiationException, IllegalAccessException
{
// Create a new handler for the connection
return this.creStrategy_.makeSvcHandler ();
}
/**
* Bridge method for accepting the new connection into the
*