org.picocontainer.gems.adapters
Class ThreadLocalComponentAdapterFactory

java.lang.Object
  extended by org.picocontainer.defaults.MonitoringComponentAdapterFactory
      extended by org.picocontainer.defaults.DecoratingComponentAdapterFactory
          extended by org.picocontainer.gems.adapters.ThreadLocalComponentAdapterFactory
All Implemented Interfaces:
java.io.Serializable, org.picocontainer.defaults.ComponentAdapterFactory, org.picocontainer.defaults.ComponentMonitorStrategy

public class ThreadLocalComponentAdapterFactory
extends org.picocontainer.defaults.DecoratingComponentAdapterFactory

A ComponentAdapterFactory for components kept in ThreadLocal instances.

This factory has two operating modes. By default it ensures, that every thread uses its own component at any time. This mode (ENSURE_THREAD_LOCALITY) makes internal usage of a ThreadLocalComponentAdapter. If the application architecture ensures, that the thread that creates the component is always also the thread that is th only user, you can set the mode THREAD_ENSURES_LOCALITY. In this mode the factory uses a simple CachingComponentAdapter that uses a ThreadLocalReference to cache the component.

See the use cases for the subtile difference:

THREAD_ENSURES_LOCALITY is applicable, if the pico container is requested for a thread local component from the working thread e.g. in a web application for a request. In this environment it is ensured, that the request is processed from the same thread and the thread local component is reused, if a previous request was handled in the same thread. Note that thi scenario fails badly, if the thread local component is created because of another cached component indirectly by a dependecy. In this case the cached component already have an instance of the thread local component, that may have been created in another thread, since only the component adapter for the thread local component can ensure a unique component for each thread.

ENSURES_THREAD_LOCALITY solves this problem. In this case the returned component is just a proxy for the thread local component and this proxy ensures, that a new component is created for each thread. Even if another cached component has an indirect dependency on the thread local component, the proxy ensures unique instances. This is vital for a multithreaded application that uses EJBs.

Author:
Jörg Schaible
See Also:
Serialized Form

Field Summary
static boolean ENSURE_THREAD_LOCALITY
          ENSURE_THREAD_LOCALITY is the constant for created ComponentAdapter instances, that ensure unique instances of the component by delivering a proxy for the component.
private  boolean ensureThreadLocal
           
private  com.thoughtworks.proxy.ProxyFactory proxyFactory
           
static boolean THREAD_ENSURES_LOCALITY
          THREAD_ENSURES_LOCALITY is the constant for created ComponentAdapter instances, that create for the current thread a new component.
 
Constructor Summary
  ThreadLocalComponentAdapterFactory(org.picocontainer.defaults.ComponentAdapterFactory delegate)
          Constructs a wrapping ThreadLocalComponentAdapterFactory, that ensures the usage of the ThreadLocal.
  ThreadLocalComponentAdapterFactory(org.picocontainer.defaults.ComponentAdapterFactory delegate, boolean ensure)
          Constructs a wrapping ThreadLocalComponentAdapterFactory.
protected ThreadLocalComponentAdapterFactory(org.picocontainer.defaults.ComponentAdapterFactory delegate, boolean ensure, com.thoughtworks.proxy.ProxyFactory factory)
          Constructs a wrapping ThreadLocalComponentAdapterFactory.
  ThreadLocalComponentAdapterFactory(org.picocontainer.defaults.ComponentAdapterFactory delegate, com.thoughtworks.proxy.ProxyFactory proxyFactory)
          Constructs a wrapping ThreadLocalComponentAdapterFactory, that ensures the usage of the ThreadLocal.
 
Method Summary
 org.picocontainer.ComponentAdapter createComponentAdapter(java.lang.Object componentKey, java.lang.Class componentImplementation, org.picocontainer.Parameter[] parameters)
           
 
Methods inherited from class org.picocontainer.defaults.MonitoringComponentAdapterFactory
changeMonitor, currentMonitor
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ENSURE_THREAD_LOCALITY

public static final boolean ENSURE_THREAD_LOCALITY
ENSURE_THREAD_LOCALITY is the constant for created ComponentAdapter instances, that ensure unique instances of the component by delivering a proxy for the component.

See Also:
Constant Field Values

THREAD_ENSURES_LOCALITY

public static final boolean THREAD_ENSURES_LOCALITY
THREAD_ENSURES_LOCALITY is the constant for created ComponentAdapter instances, that create for the current thread a new component.

See Also:
Constant Field Values

ensureThreadLocal

private final boolean ensureThreadLocal

proxyFactory

private com.thoughtworks.proxy.ProxyFactory proxyFactory
Constructor Detail

ThreadLocalComponentAdapterFactory

public ThreadLocalComponentAdapterFactory(org.picocontainer.defaults.ComponentAdapterFactory delegate)
Constructs a wrapping ThreadLocalComponentAdapterFactory, that ensures the usage of the ThreadLocal. The Proxy instances are generated by the JDK.

Parameters:
delegate - The delegated ComponentAdapterFactory.

ThreadLocalComponentAdapterFactory

public ThreadLocalComponentAdapterFactory(org.picocontainer.defaults.ComponentAdapterFactory delegate,
                                          com.thoughtworks.proxy.ProxyFactory proxyFactory)
Constructs a wrapping ThreadLocalComponentAdapterFactory, that ensures the usage of the ThreadLocal.

Parameters:
delegate - The delegated ComponentAdapterFactory.
proxyFactory - The ProxyFactory to use.

ThreadLocalComponentAdapterFactory

public ThreadLocalComponentAdapterFactory(org.picocontainer.defaults.ComponentAdapterFactory delegate,
                                          boolean ensure)
Constructs a wrapping ThreadLocalComponentAdapterFactory.

Parameters:
delegate - The delegated ComponentAdapterFactory.
ensure - ENSURE_THREAD_LOCALITY or THREAD_ENSURES_LOCALITY.

ThreadLocalComponentAdapterFactory

protected ThreadLocalComponentAdapterFactory(org.picocontainer.defaults.ComponentAdapterFactory delegate,
                                             boolean ensure,
                                             com.thoughtworks.proxy.ProxyFactory factory)
Constructs a wrapping ThreadLocalComponentAdapterFactory.

Parameters:
delegate - The delegated ComponentAdapterFactory.
ensure - ENSURE_THREAD_LOCALITY or THREAD_ENSURES_LOCALITY.
factory - The ProxyFactory to use.
Method Detail

createComponentAdapter

public org.picocontainer.ComponentAdapter createComponentAdapter(java.lang.Object componentKey,
                                                                 java.lang.Class componentImplementation,
                                                                 org.picocontainer.Parameter[] parameters)
                                                          throws org.picocontainer.PicoIntrospectionException,
                                                                 org.picocontainer.defaults.AssignabilityRegistrationException,
                                                                 org.picocontainer.defaults.NotConcreteRegistrationException
Specified by:
createComponentAdapter in interface org.picocontainer.defaults.ComponentAdapterFactory
Overrides:
createComponentAdapter in class org.picocontainer.defaults.DecoratingComponentAdapterFactory
Throws:
org.picocontainer.PicoIntrospectionException
org.picocontainer.defaults.AssignabilityRegistrationException
org.picocontainer.defaults.NotConcreteRegistrationException