org.picocontainer.gems.adapters
Class DelegateMethodAdapter<T>

java.lang.Object
  extended by org.picocontainer.gems.adapters.DelegateMethodAdapter<T>
All Implemented Interfaces:
ComponentAdapter<T>

public class DelegateMethodAdapter<T>
extends Object
implements ComponentAdapter<T>

Object construction is sometimes expensive, especially when it is seldom used object. The goal of this adapter is to use the DelegateMethod type to allow delayed construction of objects.

For example, in a web application, to be able to have classes that depend on the HttpSession object, you would have to call HttpServletRequest.getSession(true). This is fine unless you don't want to create a session until you absolutely have to.

Enter DelegateMethodAdapter:

 //Assumed Variables: request == HttpServletRequest.
 //Typical PicoContainer
 MutablePicoContainer pico = new PicoBuilder().withLifecycle().withCaching()
                .build();
 
 //Create a delegate method that will invoke HttpServletRequest.getSession(true) when invoke() is called.
 DelegateMethod delegateMethod = new DelegateMethod(HttpServletRequest.class,
                "getSession", true);
 
 //Create the Adapter wrapping the delegate method.  
 DelegateMethodAdapter methodAdapter = new DelegateMethodAdapter(
                HttpSession.class, request, delegateMethod);
 pico.addAdapter(methodAdapter);
 
 //If only executing this code, the HttpSession should not be created yet.
 assertNull(request.getSession(false));
 
 //Will get the session object by having the delegate method call HttpServletRequest.getSession(true)
 HttpSession session = pico.getComponent(HttpSession.class);
 assertNotNull(session);
 
 //Should demonstrate that the session has now been created.
 assertNotNull(request.getSession(false));
 

With an adapter like this, you can write classes like:

 public class SessionUser {
        public SessionUser(HttpSession session) {
                //.....
        }
 }
 

With impunity, and are guaranteed that the session would not be created unless the SessionUser object was constructed.

Author:
Michael Rimov

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.picocontainer.ComponentAdapter
ComponentAdapter.NOTHING
 
Constructor Summary
DelegateMethodAdapter(Object componentKey, ComponentMonitor monitor, Object targetInstance, DelegateMethod factoryMethod)
           
DelegateMethodAdapter(Object componentKey, Object targetInstance, DelegateMethod factoryMethod)
           
 
Method Summary
 void accept(PicoVisitor visitor)
          Accepts a visitor for this ComponentAdapter.
 ComponentAdapter<T> findAdapterOfType(Class componentAdapterType)
          Locates a component adapter of type componentAdapterType in the ComponentAdapter chain.
 Class<T> getComponentImplementation()
          Retrieve the class of the component.
 T getComponentInstance(PicoContainer container)
          Deprecated. 
 T getComponentInstance(PicoContainer container, Type into)
          Returns the
 Object getComponentKey()
          Retrieve the key associated with the component.
 ComponentAdapter<T> getDelegate()
          No delegates.
 String getDescriptor()
          Get a string key descriptor of the component adapter.
 void verify(PicoContainer container)
          Verify that all dependencies for this adapter can be satisfied.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DelegateMethodAdapter

public DelegateMethodAdapter(Object componentKey,
                             Object targetInstance,
                             DelegateMethod factoryMethod)
Parameters:
componentKey -
Component - Implementation will be the expected return type of the factory method.

DelegateMethodAdapter

public DelegateMethodAdapter(Object componentKey,
                             ComponentMonitor monitor,
                             Object targetInstance,
                             DelegateMethod factoryMethod)
Parameters:
componentKey -
componentImplementation -
monitor -
Method Detail

getComponentInstance

public T getComponentInstance(PicoContainer container,
                              Type into)
                       throws PicoCompositionException
Returns the

Specified by:
getComponentInstance in interface ComponentAdapter<T>
Parameters:
container - the PicoContainer, that is used to resolve any possible dependencies of the instance.
into - the class that is about to be injected into. Use ComponentAdapter.NOTHING.class if this is not important to you.
Returns:
the component instance.
Throws:
PicoCompositionException - if the component has dependencies which could not be resolved, or instantiation of the component lead to an ambiguous situation within the container.

getDescriptor

public String getDescriptor()
Get a string key descriptor of the component adapter.

Specified by:
getDescriptor in interface ComponentAdapter<T>
Returns:
See Also:
ComponentAdapter.getDescriptor()

verify

public void verify(PicoContainer container)
            throws PicoCompositionException
Verify that all dependencies for this adapter can be satisfied. Normally, the adapter should verify this by checking that the associated PicoContainer contains all the needed dependencies.

Specified by:
verify in interface ComponentAdapter<T>
Parameters:
container - the PicoContainer, that is used to resolve any possible dependencies of the instance.
Throws:
PicoCompositionException - if one or more dependencies cannot be resolved.

accept

public void accept(PicoVisitor visitor)
Accepts a visitor for this ComponentAdapter. The method is normally called by visiting a PicoContainer, that cascades the visitor also down to all its ComponentAdapter instances.

Specified by:
accept in interface ComponentAdapter<T>
Parameters:
visitor - the visitor.

findAdapterOfType

public ComponentAdapter<T> findAdapterOfType(Class componentAdapterType)
Locates a component adapter of type componentAdapterType in the ComponentAdapter chain. Will return null if there is no adapter of the given type.

Specified by:
findAdapterOfType in interface ComponentAdapter<T>
Parameters:
componentAdapterType - the class of the adapter type being located. Never null.
Returns:
the appropriate component adapter of type U. May return null if the component adapter type is not returned.

getComponentImplementation

public Class<T> getComponentImplementation()
Retrieve the class of the component.

Specified by:
getComponentImplementation in interface ComponentAdapter<T>
Returns:
the component's implementation class. Should normally be a concrete class (ie, a class that can be instantiated).

getComponentInstance

@Deprecated
public T getComponentInstance(PicoContainer container)
                       throws PicoCompositionException
Deprecated. 

Retrieve the component instance. This method will usually create a new instance each time it is called, but that is not required. For example, Cached will always return the same instance.

Specified by:
getComponentInstance in interface ComponentAdapter<T>
Parameters:
container - the PicoContainer, that is used to resolve any possible dependencies of the instance.
Returns:
the component instance.
Throws:
PicoCompositionException - if the component has dependencies which could not be resolved, or instantiation of the component lead to an ambigous situation within the container.

getComponentKey

public Object getComponentKey()
Retrieve the key associated with the component.

Specified by:
getComponentKey in interface ComponentAdapter<T>
Returns:
the component's key. Should either be a class type (normally an interface) or an identifier that is unique (within the scope of the current PicoContainer).

getDelegate

public ComponentAdapter<T> getDelegate()
No delegates.

Specified by:
getDelegate in interface ComponentAdapter<T>
Returns:
the next component adapter in line or null if there is no delegate ComponentAdapter.


Copyright © 2003-2010 Codehaus. All Rights Reserved.