com.sas.framework.expojo
Class ModelExposer

java.lang.Object
  extended by com.sas.framework.expojo.ModelExposer

public class ModelExposer
extends java.lang.Object

Central class for implementing the "Exposed Model" pattern. Supports the 'availability' of the ModelExposer to all objects in a thread via attachment to the thread ie., dependency injection. In most scenarios threads are shared resources (eg., servlet environment) and so should not be permanently attached to a ModelExposer. attachThread and detachThread are provided and work with the ThreadLocal attribute to provide almost 'singleton' like (without the bad bits) access to the model exposer from within a thread that has been attached. No automatic attachment/detachment takes place in the constructor, init or destroy methods because no assumptions are made that these methods are running in the thread that is the one that should be attached to the ModelExposer. Typically in a web app environment the developer will configure the ExpojoServletFilter in web.xml and this will perform the dependency injection automatically. See web/web.xml for an example web.xml file that configures the ExpojoServletContextListener and ExpojoServletFilter for a typical web application.

See Also:
ExpojoServletContextListener, ExpojoServletFilter

Field Summary
 PersistenceProvider persistenceProvider
           
 PersistenceProviderFactory persistenceProviderFactory
           
 java.util.Hashtable repositories
           
 java.util.Hashtable services
           
protected  boolean traceTxDepth
          If true dumps information about the transaction depth each time begin, commit or rollback are called.
 
Constructor Summary
ModelExposer(PersistenceProviderFactory iPersistenceProviderFactory)
          Constructs the object, passing in the persistence provider factory shared by all model exposers accessing a particular datastore.
 
Method Summary
 void addRepository(RepositoryComponent repositoryComponent)
          Adds a service component to the model exposer.
 void addService(ServiceComponent serviceComponent)
          Adds a service component to the model exposer.
 void attachThread()
          Implements Dependency Injection: Attaches this ModelExposer to the current thread.
 void destroy()
          Destroys this thread's model exposer instance by setting it's ThreadLocal value instance to null.
 void detachThread()
          Detaches a ModelExposer from the current thread - enabling it to be garbage collected if necessary (assuming ThreadLocal had the last remaining reference to the ModelExposer).
protected  void finalize()
          Calls to destroy the ModelExposer.
static ModelExposer get()
          Returns the ModelExposer for the current thread.
 PersistenceProvider getPersistenceProvider()
          Returns a ThreadLocal version of the appropriate PersistenceProvider.
 RepositoryComponent getRepository(java.lang.String repositoryName)
          Returns a repository of the given name or null if not found.
 ServiceComponent getService(java.lang.String serviceName)
          Returns the service of the given name or null if not found.
 boolean getTraceTxDepth()
          Returns traceTxDepth
 void init()
          This should be called after construction to allow initialization of the exposer components that have been added.
 boolean isTestDb()
          Returns testDb
 void setTraceTxDepth(boolean traceTxDepth)
          Sets traceTxDepth
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

traceTxDepth

protected transient boolean traceTxDepth
If true dumps information about the transaction depth each time begin, commit or rollback are called.


persistenceProvider

public PersistenceProvider persistenceProvider

services

public transient java.util.Hashtable services

repositories

public transient java.util.Hashtable repositories

persistenceProviderFactory

public transient PersistenceProviderFactory persistenceProviderFactory
Constructor Detail

ModelExposer

public ModelExposer(PersistenceProviderFactory iPersistenceProviderFactory)
Constructs the object, passing in the persistence provider factory shared by all model exposers accessing a particular datastore.

Method Detail

setTraceTxDepth

public void setTraceTxDepth(boolean traceTxDepth)
Sets traceTxDepth


getTraceTxDepth

public boolean getTraceTxDepth()
Returns traceTxDepth


attachThread

public void attachThread()
Implements Dependency Injection: Attaches this ModelExposer to the current thread. This is called automatically by the ExpojoServletFilter prior to servicing a HTTP request so that model objects can call ModelExposer.get() and get this model exposer during the servicing of that HTTP request. This eliminates the need to manually inject a ModelExposer dependency into various objects that might need one because the ModelExposer is available to all objects in the current thread via ModelExposer.get().


detachThread

public void detachThread()
Detaches a ModelExposer from the current thread - enabling it to be garbage collected if necessary (assuming ThreadLocal had the last remaining reference to the ModelExposer). This is called automatically by the ExpojoServletFilter after the current HTTP request has been serviced.


finalize

protected void finalize()
Calls to destroy the ModelExposer.

Overrides:
finalize in class java.lang.Object

isTestDb

public boolean isTestDb()
Returns testDb


init

public void init()
This should be called after construction to allow initialization of the exposer components that have been added. Calls init on each ModelExposerComponent that has been added. They may override ModelExposerComponent.init to set up references to other services or repositories as required - in this case the super class init() must be called at the end of the overwritten init() method.


getPersistenceProvider

public PersistenceProvider getPersistenceProvider()
Returns a ThreadLocal version of the appropriate PersistenceProvider. Each thread that calls this will then end up with a unique connection to the persistence service with which to access the data store.


getRepository

public RepositoryComponent getRepository(java.lang.String repositoryName)
Returns a repository of the given name or null if not found.


getService

public ServiceComponent getService(java.lang.String serviceName)
Returns the service of the given name or null if not found.


get

public static ModelExposer get()
Returns the ModelExposer for the current thread. Each thread has its own seperate set of instances of the ModelExposer and its components - thus avoiding issues with the Singleton pattern.


destroy

public void destroy()
Destroys this thread's model exposer instance by setting it's ThreadLocal value instance to null.


addService

public void addService(ServiceComponent serviceComponent)
Adds a service component to the model exposer.


addRepository

public void addRepository(RepositoryComponent repositoryComponent)
Adds a service component to the model exposer.