Package net.sf.jguiraffe.di.impl
Class SimpleBeanStoreImpl
- java.lang.Object
-
- net.sf.jguiraffe.di.impl.SimpleBeanStoreImpl
-
- All Implemented Interfaces:
BeanStore
public class SimpleBeanStoreImpl extends Object implements BeanStore
A helper class that simplifies implementations of the
BeanStoreinterface.The purpose of this class is to support
BeanStoreimplementations based on classes that are not aware ofBeanProviders, but provide a map-like interface for accessing their data. This is achieved in the following ways:- Static data (in form of arbitrary objects) can directly be added to this
class. Internally
ConstantBeanProviderobjects are created for the data objects to be managed. These providers are directly exposed through theBeanStoremethods. - If dynamic data is involved (i.e. objects that may change over time and
cannot be kept in a map), components can implement the internal
BeanContributorinterface and register themselves at aSimpleBeanStoreImplinstance. TheBeanConstributorinterface is much easier to implement than theBeanStoreinterface. The implementations of theBeanStoremethods provided by this class take the registeredBeanContributors into account. - Methods of the
BeanStoreinterface that do not directly deal with bean providers are already implemented by this class.
Note: The class per se is not thread-safe. When used inside the dependency injection framework, proper synchronization is automatically applied. But during initialization or for other use cases the developer has to ensure that there are no concurrent accesses.
- Version:
- $Id: SimpleBeanStoreImpl.java 213 2012-07-14 19:40:51Z oheger $
- Author:
- Oliver Heger
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceSimpleBeanStoreImpl.BeanContributorDefinition of an interface for objects that can contribute beans for aSimpleBeanStoreImplobject.
-
Constructor Summary
Constructors Constructor Description SimpleBeanStoreImpl()Creates a new instance ofSimpleBeanStoreImpl.SimpleBeanStoreImpl(String name, BeanStore parent)Creates a new instance ofSimpleBeanStoreImpland sets the name and the reference to the parent.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddBean(String name, Object bean)Adds the specified bean to this store.voidaddBeanContributor(SimpleBeanStoreImpl.BeanContributor contr)Adds a newBeanContributorto this object.BeanProvidergetBeanProvider(String name)Returns aBeanProviderfor the bean with the given name.ConversionHelpergetConversionHelper()Returns theConversionHelperassociated with this instance.StringgetName()Returns the name of this bean store.BeanStoregetParent()Returns the parent bean store.protected BeanProviderproviderFor(Object bean)Returns a bean provider for the specified bean.Set<String>providerNames()Returns a set with the names of the available bean providers.ObjectremoveBean(String name)Removes the bean with the given name.voidremoveBeanContributor(SimpleBeanStoreImpl.BeanContributor contr)Removes the specified bean contributor from this object.voidsetConversionHelper(ConversionHelper conversionHelper)Sets theConversionHelperassociated with this object.voidsetName(String name)Sets the name of this bean store.voidsetParent(BeanStore parent)Sets the parent bean store.
-
-
-
Method Detail
-
addBeanContributor
public void addBeanContributor(SimpleBeanStoreImpl.BeanContributor contr)
Adds a newBeanContributorto this object. This contributor will be triggered when this bean store is accessed.- Parameters:
contr- the contributor to be added (must not be null)- Throws:
IllegalArgumentException- if the contributor is null
-
removeBeanContributor
public void removeBeanContributor(SimpleBeanStoreImpl.BeanContributor contr)
Removes the specified bean contributor from this object.- Parameters:
contr- the contributor to remove
-
addBean
public void addBean(String name, Object bean)
Adds the specified bean to this store. For the passed in bean a constant bean provider is created. It can then be queried through thegetBeanProvider()method.- Parameters:
name- the name of the bean (must not be null)bean- the bean (must not be null)- Throws:
IllegalArgumentException- if the name or the bean is null
-
removeBean
public Object removeBean(String name)
Removes the bean with the given name.- Parameters:
name- the name of the bean to be removed- Returns:
- the removed bean (null if the bean was unknown)
-
getBeanProvider
public BeanProvider getBeanProvider(String name)
Returns aBeanProviderfor the bean with the given name. This implementation checks whether such a bean was directly added using theaddBean()method. If not, the registered bean contributors are consulted. If no such bean can be found, null is returned.- Specified by:
getBeanProviderin interfaceBeanStore- Parameters:
name- the name of the bean in question- Returns:
- a
BeanProviderfor this bean
-
getName
public String getName()
Returns the name of this bean store.
-
setName
public void setName(String name)
Sets the name of this bean store.- Parameters:
name- the new name
-
getParent
public BeanStore getParent()
Returns the parent bean store.
-
setParent
public void setParent(BeanStore parent)
Sets the parent bean store.- Parameters:
parent- the parent bean store
-
getConversionHelper
public ConversionHelper getConversionHelper()
Returns theConversionHelperassociated with this instance.- Specified by:
getConversionHelperin interfaceBeanStore- Returns:
- the
ConversionHelper
-
setConversionHelper
public void setConversionHelper(ConversionHelper conversionHelper)
Sets theConversionHelperassociated with this object. The object passed to this method will be returned bygetConversionHelper().- Parameters:
conversionHelper- theConversionHelper
-
providerNames
public Set<String> providerNames()
Returns a set with the names of the available bean providers. This implementation will first obtain the names of all directly added beans. Then the registered bean contributors are invoked to add their bean names to the resulting list.- Specified by:
providerNamesin interfaceBeanStore- Returns:
- a set with the names of the known bean providers
-
providerFor
protected BeanProvider providerFor(Object bean)
Returns a bean provider for the specified bean. This method is called whenever a bean has to be wrapped by a provider. This default implementation returns aConstantBeanProviderfor the passed in bean.- Parameters:
bean- the bean- Returns:
- a provider for this bean
-
-