org.picocontainer.defaults
Class BeanPropertyComponentAdapter

java.lang.Object
  extended by org.picocontainer.defaults.DecoratingComponentAdapter
      extended by org.picocontainer.defaults.BeanPropertyComponentAdapter
All Implemented Interfaces:
java.io.Serializable, ComponentAdapter, ComponentMonitorStrategy, LifecycleStrategy, LifecycleManager

public class BeanPropertyComponentAdapter
extends DecoratingComponentAdapter

Decorating component adapter that can be used to set additional properties on a component in a bean style. These properties must be managed manually by the user of the API, and will not be managed by PicoContainer. This class is therefore not the same as SetterInjectionComponentAdapter, which is a true Setter Injection adapter.

This adapter is mostly handy for setting various primitive properties via setters; it is also able to set javabean properties by discovering an appropriate PropertyEditor and using its setAsText method.

Note that this class doesn't cache instances. If you want caching, use a CachingComponentAdapter around this one.

Since:
1.0
Version:
$Revision: 2793 $
Author:
Aslak Hellesøy
See Also:
Serialized Form

Field Summary
private  java.util.Map properties
           
private  java.util.Map setters
           
 
Constructor Summary
BeanPropertyComponentAdapter(ComponentAdapter delegate)
          Construct a BeanPropertyComponentAdapter.
 
Method Summary
static java.lang.Object convert(java.lang.String typeName, java.lang.String value, java.lang.ClassLoader classLoader)
          Converts a String value of a named type to an object.
private  java.lang.Object convertType(PicoContainer container, java.lang.reflect.Method setter, java.lang.String propertyValue)
           
 java.lang.Object getComponentInstance(PicoContainer container)
          Get a component instance and set given property values.
private  java.lang.reflect.Method[] getMethods(java.lang.Class clazz)
           
private  java.lang.String getPropertyName(java.lang.reflect.Method method)
           
private  java.lang.Object getSetterParameter(java.lang.String propertyName, java.lang.Object propertyValue, java.lang.Object componentInstance, PicoContainer container)
          Converts and validates the given property value to an appropriate object for calling the bean's setter.
private  java.util.Map getSetters(java.lang.Class clazz)
           
private  boolean isSetter(java.lang.reflect.Method method)
           
 void setProperties(java.util.Map properties)
          Sets the bean property values that should be set upon creation.
 
Methods inherited from class org.picocontainer.defaults.DecoratingComponentAdapter
accept, changeMonitor, currentMonitor, dispose, dispose, getComponentImplementation, getComponentKey, getDelegate, hasLifecycle, hasLifecycle, start, start, stop, stop, toString, verify
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

properties

private java.util.Map properties

setters

private transient java.util.Map setters
Constructor Detail

BeanPropertyComponentAdapter

public BeanPropertyComponentAdapter(ComponentAdapter delegate)
                             throws PicoInitializationException
Construct a BeanPropertyComponentAdapter.

Parameters:
delegate - the wrapped ComponentAdapter
Throws:
PicoInitializationException
Method Detail

getComponentInstance

public java.lang.Object getComponentInstance(PicoContainer container)
                                      throws PicoInitializationException,
                                             PicoIntrospectionException,
                                             AssignabilityRegistrationException,
                                             NotConcreteRegistrationException
Get a component instance and set given property values.

Specified by:
getComponentInstance in interface ComponentAdapter
Overrides:
getComponentInstance in class DecoratingComponentAdapter
Parameters:
container - the PicoContainer, that is used to resolve any possible dependencies of the instance.
Returns:
the component instance with any properties of the properties map set.
Throws:
PicoInitializationException - if the component could not be instantiated.
PicoIntrospectionException - if the component has dependencies which could not be resolved, or instantiation of the component lead to an ambigous situation within the container.
AssignabilityRegistrationException
NotConcreteRegistrationException
See Also:
setProperties(Map)

getSetters

private java.util.Map getSetters(java.lang.Class clazz)

getMethods

private java.lang.reflect.Method[] getMethods(java.lang.Class clazz)

getPropertyName

private java.lang.String getPropertyName(java.lang.reflect.Method method)

isSetter

private boolean isSetter(java.lang.reflect.Method method)

convertType

private java.lang.Object convertType(PicoContainer container,
                                     java.lang.reflect.Method setter,
                                     java.lang.String propertyValue)
                              throws java.lang.ClassNotFoundException
Throws:
java.lang.ClassNotFoundException

convert

public static java.lang.Object convert(java.lang.String typeName,
                                       java.lang.String value,
                                       java.lang.ClassLoader classLoader)
                                throws java.lang.ClassNotFoundException
Converts a String value of a named type to an object. Works with primitive wrappers, String, File, URL types, or any type that has an appropriate PropertyEditor.

Parameters:
typeName - name of the type
value - its value
classLoader - used to load a class if typeName is "class" or "java.lang.Class" (ignored otherwise)
Returns:
instantiated object or null if the type was unknown/unsupported
Throws:
java.lang.ClassNotFoundException - if typeName is "class" or "java.lang.Class" and class couldn't be loaded.

setProperties

public void setProperties(java.util.Map properties)
Sets the bean property values that should be set upon creation.

Parameters:
properties - bean properties

getSetterParameter

private java.lang.Object getSetterParameter(java.lang.String propertyName,
                                            java.lang.Object propertyValue,
                                            java.lang.Object componentInstance,
                                            PicoContainer container)
                                     throws PicoInitializationException,
                                            java.lang.ClassCastException
Converts and validates the given property value to an appropriate object for calling the bean's setter.

Parameters:
propertyName - String the property name on the component that we will be setting the value to.
propertyValue - Object the property value that we've been given. It may need conversion to be formed into the value we need for the component instance setter.
componentInstance - the component that we're looking to provide the setter to.
Returns:
Object: the final converted object that can be used in the setter.
Throws:
PicoInitializationException
java.lang.ClassCastException