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

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

public class DelegateMethodAdapter<T>
extends Object
implements org.picocontainer.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
org.picocontainer.ComponentAdapter.NOTHING
 
Constructor Summary
DelegateMethodAdapter(Object componentKey, org.picocontainer.ComponentMonitor monitor, Object targetInstance, DelegateMethod factoryMethod)
           
DelegateMethodAdapter(Object componentKey, Object targetInstance, DelegateMethod factoryMethod)
           
 
Method Summary
 void accept(org.picocontainer.PicoVisitor visitor)
          
 org.picocontainer.ComponentAdapter<T> findAdapterOfType(Class adapterType)
          
 Class<T> getComponentImplementation()
          
 T getComponentInstance(org.picocontainer.PicoContainer container)
          Deprecated. 
 T getComponentInstance(org.picocontainer.PicoContainer container, Type into)
          Returns the
 Object getComponentKey()
          
 org.picocontainer.ComponentAdapter<T> getDelegate()
          No delegates.
 String getDescriptor()
          
 void verify(org.picocontainer.PicoContainer container)
          
 
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,
                             org.picocontainer.ComponentMonitor monitor,
                             Object targetInstance,
                             DelegateMethod factoryMethod)
Parameters:
componentKey -
componentImplementation -
monitor -
Method Detail

getComponentInstance

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

Specified by:
getComponentInstance in interface org.picocontainer.ComponentAdapter<T>
Throws:
org.picocontainer.PicoCompositionException

getDescriptor

public String getDescriptor()

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

verify

public void verify(org.picocontainer.PicoContainer container)
            throws org.picocontainer.PicoCompositionException

Specified by:
verify in interface org.picocontainer.ComponentAdapter<T>
Throws:
org.picocontainer.PicoCompositionException

accept

public void accept(org.picocontainer.PicoVisitor visitor)

Specified by:
accept in interface org.picocontainer.ComponentAdapter<T>

findAdapterOfType

public org.picocontainer.ComponentAdapter<T> findAdapterOfType(Class adapterType)

Specified by:
findAdapterOfType in interface org.picocontainer.ComponentAdapter<T>

getComponentImplementation

public Class<T> getComponentImplementation()

Specified by:
getComponentImplementation in interface org.picocontainer.ComponentAdapter<T>

getComponentInstance

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

Specified by:
getComponentInstance in interface org.picocontainer.ComponentAdapter<T>
Throws:
org.picocontainer.PicoCompositionException

getComponentKey

public Object getComponentKey()

Specified by:
getComponentKey in interface org.picocontainer.ComponentAdapter<T>

getDelegate

public org.picocontainer.ComponentAdapter<T> getDelegate()
No delegates.

Specified by:
getDelegate in interface org.picocontainer.ComponentAdapter<T>


Copyright © 2003-2009 Codehaus. All Rights Reserved.