public class DelegateMethodAdapter<T> extends Object implements ComponentAdapter<T>
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.
ComponentAdapter.NOTHING| Constructor and Description |
|---|
DelegateMethodAdapter(Object componentKey,
ComponentMonitor monitor,
Object targetInstance,
DelegateMethod factoryMethod) |
DelegateMethodAdapter(Object componentKey,
Object targetInstance,
DelegateMethod factoryMethod) |
| Modifier and Type | Method and Description |
|---|---|
void |
accept(PicoVisitor visitor) |
ComponentAdapter<T> |
findAdapterOfType(Class adapterType) |
Class<? extends T> |
getComponentImplementation() |
T |
getComponentInstance(PicoContainer container)
Deprecated.
|
T |
getComponentInstance(PicoContainer container,
Type into)
Returns the
|
Object |
getComponentKey() |
ComponentAdapter<T> |
getDelegate()
No delegates.
|
String |
getDescriptor() |
void |
verify(PicoContainer container) |
public DelegateMethodAdapter(Object componentKey, Object targetInstance, DelegateMethod factoryMethod)
componentKey - Component - Implementation will be the expected return type of the factory
method.public DelegateMethodAdapter(Object componentKey, ComponentMonitor monitor, Object targetInstance, DelegateMethod factoryMethod)
componentKey - componentImplementation - monitor - public T getComponentInstance(PicoContainer container, Type into) throws PicoCompositionException
getComponentInstance in interface ComponentAdapter<T>PicoCompositionExceptionpublic String getDescriptor()
getDescriptor in interface ComponentAdapter<T>ComponentAdapter.getDescriptor()public void verify(PicoContainer container) throws PicoCompositionException
verify in interface ComponentAdapter<T>PicoCompositionExceptionpublic void accept(PicoVisitor visitor)
accept in interface ComponentAdapter<T>public ComponentAdapter<T> findAdapterOfType(Class adapterType)
findAdapterOfType in interface ComponentAdapter<T>public Class<? extends T> getComponentImplementation()
getComponentImplementation in interface ComponentAdapter<T>@Deprecated public T getComponentInstance(PicoContainer container) throws PicoCompositionException
getComponentInstance in interface ComponentAdapter<T>PicoCompositionExceptionpublic Object getComponentKey()
getComponentKey in interface ComponentAdapter<T>public ComponentAdapter<T> getDelegate()
getDelegate in interface ComponentAdapter<T>Copyright © 2003-2014 Codehaus. All Rights Reserved.