com.iplanet.jato
Class ModelManager

java.lang.Object
  |
  +--com.iplanet.jato.ModelManager
All Implemented Interfaces:
RequestCompletionListener

public class ModelManager
extends java.lang.Object
implements RequestCompletionListener

A factory for the instantiation and retrieval of Model objects within a request. This class guarantees that only a single instance of a requested model is created during each request. Models obtained via this class are shared between all objects in the current request. This class lazily instantiates objects as needed, but never creates more than one instance of a given type with the same instance name.

Developers need not create any instances of this class. A single instance is automatically created at the beginning of the request and stored in the request context.

Some user-defined models may wish to remain stateful between user requests (none of the models provided with JATO are stateful). Therefore, this class is capable of automatically storing and retreiving models from the HTTP session. However, because storing objects in the session is performance overhead, we anticipate that most models will be instantiated on a per-request, as-needed basis. Regardless, the capability to store and retrieve model instances from the session is supported here.

This class anticipates that most of its callers will not supply an instance name for a model, since in most requests a single instance of a given model is sufficient. Therefore, when a client only requests a model by type and does not supply a specific instance name, the manager returns an instance reserved for instance nameless requests. The name used to store these models in the ModelManager can be obtained by calling the getDefaultModelInstanceName() method.

Version:
JATO/1.2.2 $Id: ModelManager.java,v 1.26 2002/03/16 03:26:27 todd Exp $
See Also:
RequestContext, ModelTypeMap, Model

Constructor Summary
ModelManager(RequestContext requestContext, ModelTypeMap typeMap)
           
 
Method Summary
 void addToSession(Model model)
          This method allows the caller to schedule the specified model for addition to the session.
 java.lang.String getDefaultModelInstanceName(java.lang.Class modelClass)
          Returns the default model instance name for the given model interface class
 java.lang.String getDefaultModelInstanceName(java.lang.String modelClassName)
          Returns the default model instance name for the given model interface class name
 Model getModel(java.lang.Class modelClass)
          Returns a reference to the unnamed instance that implements the requested Model interface.
 Model getModel(java.lang.Class modelClass, java.lang.String instanceName)
          Returns a reference to the named instance that implements the requested Model interface.
 Model getModel(java.lang.Class modelClass, java.lang.String instanceName, boolean lookInSession)
          Returns a reference to the named instance that implements the requested Model interface.
 Model getModel(java.lang.Class modelClass, java.lang.String instanceName, boolean lookInSession, boolean storeInSession)
          Returns a reference to the named instance that implements the requested Model interface.
 Model getModel(java.lang.String className)
          Returns a reference to the unnamed instance that implements the requested Model interface
 Model getModel(java.lang.String className, java.lang.String instanceName)
          Returns a reference to the named instance that implements the requested Model interface.
 Model getModel(java.lang.String className, java.lang.String instanceName, boolean lookInSession)
          Returns a reference to the named instance that implements the requested Model interface.
 Model getModel(java.lang.String className, java.lang.String instanceName, boolean lookInSession, boolean storeInSession)
          Returns a reference to the named instance that implements the requested Model interface.
protected  RequestContext getRequestContext()
          Returns the request context
 void notifyRequestCompletionListeners()
          Deprecated. Code that calls this method is no longer required and should be removed
 void removeFromSession(Model model)
          This method allows the caller to schedule the specified model for immediate removal from the session.
 void requestComplete()
          Called when the request is at an end.
 void setTypeMap(ModelTypeMap value)
          Sets the current ModelTypeMap instance
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ModelManager

public ModelManager(RequestContext requestContext,
                    ModelTypeMap typeMap)
Parameters:
requestContext - The request context of the current request
typeMap - An application-specific map that associates Model interfaces with corresponding Model implementation classes.
Method Detail

getRequestContext

protected RequestContext getRequestContext()
Returns the request context

getModel

public Model getModel(java.lang.String className)
               throws java.lang.ClassNotFoundException
Returns a reference to the unnamed instance that implements the requested Model interface
Parameters:
className - The Model's interface or implementation class name. The ModelManager initially assumes this class is a model interface class and will attempt to map it to a class that implements the specified interface using its ModelTypeMap object. However, if no mapping is found, it assumes the provided class is the model implementation class and attempts to return an instance of that class.
Returns:
A Model instance that corresponds to the supplied model class name

getModel

public Model getModel(java.lang.String className,
                      java.lang.String instanceName)
               throws java.lang.ClassNotFoundException
Returns a reference to the named instance that implements the requested Model interface.
Parameters:
className - The Model's interface or implementation class name. The ModelManager initially assumes this class is a model interface class and will attempt to map it to a class that implements the specified interface using its ModelTypeMap object. However, if no mapping is found, it assumes the provided class is the model implementation class and attempts to return an instance of that class.
instanceName - The name of the requested instance
Returns:
A Model instance that corresponds to the supplied model class name

getModel

public Model getModel(java.lang.String className,
                      java.lang.String instanceName,
                      boolean lookInSession)
               throws java.lang.ClassNotFoundException
Returns a reference to the named instance that implements the requested Model interface. If the caller requests that the model be fetched from session but does not currrently exist in session, the ModelManager will create the instance but will not store it in the session before returning the reference to the caller.
Parameters:
className - The Model's interface or implementation class name. The ModelManager initially assumes this class is a model interface class and will attempt to map it to a class that implements the specified interface using its ModelTypeMap object. However, if no mapping is found, it assumes the provided class is the model implementation class and attempts to return an instance of that class.
instanceName - The name of the requested instance
lookInSession - Directs the ModelManager to look or not look into the current user's HttpSession for the named model instance.
Returns:
A Model instance that corresponds to the supplied model class name

getModel

public Model getModel(java.lang.String className,
                      java.lang.String instanceName,
                      boolean lookInSession,
                      boolean storeInSession)
               throws java.lang.ClassNotFoundException
Returns a reference to the named instance that implements the requested Model interface. If the caller requests that the model be fetched from session but does not currrently exist in session, the ModelManager will create the instance, schedule it for storage in the session if the value of the storeInSession parameter is true, and return the reference to the caller. The model is stored in the session under its instance name.
Parameters:
className - The Model's interface or implementation class name. The ModelManager initially assumes this class is a model interface class and will attempt to map it to a class that implements the specified interface using its ModelTypeMap object. However, if no mapping is found, it assumes the provided class is the model implementation class and attempts to return an instance of that class.
instanceName - The name of the requested instance
lookInSession - Directs the ModelManager to look or not look into the current user's HttpSession for the named model instance.
storeInSession - Directs the ModelManager to schedule the model for addition to the current user's HttpSession under the provided instance name
Returns:
A Model instance that corresponds to the supplied model class name

getModel

public Model getModel(java.lang.Class modelClass)
Returns a reference to the unnamed instance that implements the requested Model interface.
Parameters:
modelClass - The Model's interface or implementation class name. The ModelManager initially assumes this class is a model interface class and will attempt to map it to a class that implements the specified interface using its ModelTypeMap object. However, if no mapping is found, it assumes the provided class is the model implementation class and attempts to return an instance of that class.
Returns:
A Model instance that corresponds to the supplied model class

getModel

public Model getModel(java.lang.Class modelClass,
                      java.lang.String instanceName)
Returns a reference to the named instance that implements the requested Model interface.
Parameters:
modelClass - The Model's interface or implementation class. The ModelManager initially assumes this class is a model interface class and will attempt to map it to a class that implements the specified interface using its ModelTypeMap object. However, if no mapping is found, it assumes the provided class is the model implementation class and attempts to return an instance of that class.
instanceName - The name of the requested instance
Returns:
A Model instance that corresponds to the supplied model class

getModel

public Model getModel(java.lang.Class modelClass,
                      java.lang.String instanceName,
                      boolean lookInSession)
Returns a reference to the named instance that implements the requested Model interface. If the caller requests that the model be fetched from session but does not currrently exist in session, the ModelManager will create the instance, schedule it for storage in the session if the value of the storeInSession parameter is true, and return the reference to the caller. The model is stored in the session under its instance name.
Parameters:
modelClass - The Model's interface or implementation class. The ModelManager initially assumes this class is a model interface class and will attempt to map it to a class that implements the specified interface using its ModelTypeMap object. However, if no mapping is found, it assumes the provided class is the model implementation class and attempts to return an instance of that class.
instanceName - The name of the requested instance
lookInSession - Directs the ModelManager to look or not look into the current user's HttpSession for the named model instance.
Returns:
A Model instance that corresponds to the supplied model class

getModel

public Model getModel(java.lang.Class modelClass,
                      java.lang.String instanceName,
                      boolean lookInSession,
                      boolean storeInSession)
Returns a reference to the named instance that implements the requested Model interface. If the caller requests that the model be fetched from session but does not currrently exist in session, the ModelManager will create the instance, schedule it for storage in the session if the value of the storeInSession parameter is true, and return the reference to the caller. The model is stored in the session under its instance name.
Parameters:
modelClass - The Model's interface or implementation class. The ModelManager initially assumes this class is a model interface class and will attempt to map it to a class that implements the specified interface using its ModelTypeMap object. However, if no mapping is found, it assumes the provided class is the model implementation class and attempts to return an instance of that class.
instanceName - The name of the requested instance
lookInSession - Directs the ModelManager to look or not look into the current user's HttpSession for the named model instance.
storeInSession - Directs the ModelManager to schedule the model for addition to the current user's HttpSession under the provided instance name
Returns:
A Model instance that corresponds to the supplied model class

addToSession

public void addToSession(Model model)
This method allows the caller to schedule the specified model for addition to the session. The model is not added to the session immediately; rather it is set in the session upon completion of the current request. Therefore, this method can be called at any time during the life of a request. This behavior is a defensive measure designed to make sure that all changes to the model during the request are incorporated in the sessioned copy (which may otherwise be an issue in certain containers). The model is stored in session under the name returned by Model.getName().
Parameters:
model - The model to store in session. The model must conform the container's requirements for being added to session. For example, it may need to be serializable.

removeFromSession

public void removeFromSession(Model model)
This method allows the caller to schedule the specified model for immediate removal from the session. The model is immediately removed from the HTTP session. However, the model instance which is currently in scope is still valid for the remainder of current request processing.
Parameters:
model - The model to remove from session. It is assumed that the model had been previously stored in session under the name returned by Model.getName().

setTypeMap

public void setTypeMap(ModelTypeMap value)
Sets the current ModelTypeMap instance
Parameters:
value - The map that will be used to map model interfaces to implementations

getDefaultModelInstanceName

public java.lang.String getDefaultModelInstanceName(java.lang.String modelClassName)
Returns the default model instance name for the given model interface class name

getDefaultModelInstanceName

public java.lang.String getDefaultModelInstanceName(java.lang.Class modelClass)
Returns the default model instance name for the given model interface class

notifyRequestCompletionListeners

public final void notifyRequestCompletionListeners()
Deprecated. Code that calls this method is no longer required and should be removed


requestComplete

public void requestComplete()
Called when the request is at an end. This method places all models scheduled for inclusion in the session into the HTTP session. A model is marked for inclusion in the session by calling the addToSession() method with the model as a parameter.

Each model is added to the session using the value returned by Model.getName() method as the attribute key. If a model returns null from this method, it is added under the name returned by the getDefaultModelInstanceName(Class) method.

Specified by:
requestComplete in interface RequestCompletionListener
Since:
JATO 1.1.2