net.sf.jguiraffe.di.impl
Class SimpleBeanStoreImpl

java.lang.Object
  extended by net.sf.jguiraffe.di.impl.SimpleBeanStoreImpl
All Implemented Interfaces:
BeanStore

public class SimpleBeanStoreImpl
extends java.lang.Object
implements BeanStore

A helper class that simplifies implementations of the BeanStore interface.

The purpose of this class is to support BeanStore implementations based on classes that are not aware of BeanProviders, but provide a map-like interface for accessing their data. This is achieved in the following ways:

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 168 2009-09-11 20:12:19Z oheger $
Author:
Oliver Heger

Nested Class Summary
static interface SimpleBeanStoreImpl.BeanContributor
           Definition of an interface for objects that can contribute beans for a SimpleBeanStoreImpl object.
 
Constructor Summary
SimpleBeanStoreImpl()
          Creates a new instance of SimpleBeanStoreImpl.
SimpleBeanStoreImpl(java.lang.String name, BeanStore parent)
          Creates a new instance of SimpleBeanStoreImpl and sets the name and the reference to the parent.
 
Method Summary
 void addBean(java.lang.String name, java.lang.Object bean)
          Adds the specified bean to this store.
 void addBeanContributor(SimpleBeanStoreImpl.BeanContributor contr)
          Adds a new BeanContributor to this object.
 BeanProvider getBeanProvider(java.lang.String name)
          Returns a BeanProvider for the bean with the given name.
 java.lang.String getName()
          Returns the name of this bean store.
 BeanStore getParent()
          Returns the parent bean store.
protected  BeanProvider providerFor(java.lang.Object bean)
          Returns a bean provider for the specified bean.
 java.util.Set<java.lang.String> providerNames()
          Returns a set with the names of the available bean providers.
 java.lang.Object removeBean(java.lang.String name)
          Removes the bean with the given name.
 void removeBeanContributor(SimpleBeanStoreImpl.BeanContributor contr)
          Removes the specified bean contributor from this object.
 void setName(java.lang.String name)
          Sets the name of this bean store.
 void setParent(BeanStore parent)
          Sets the parent bean store.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

SimpleBeanStoreImpl

public SimpleBeanStoreImpl()
Creates a new instance of SimpleBeanStoreImpl.


SimpleBeanStoreImpl

public SimpleBeanStoreImpl(java.lang.String name,
                           BeanStore parent)
Creates a new instance of SimpleBeanStoreImpl and sets the name and the reference to the parent.

Parameters:
name - the name of this bean store
parent - the reference to the parent bean store
Method Detail

addBeanContributor

public void addBeanContributor(SimpleBeanStoreImpl.BeanContributor contr)
Adds a new BeanContributor to 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:
java.lang.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(java.lang.String name,
                    java.lang.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 the getBeanProvider() method.

Parameters:
name - the name of the bean (must not be null)
bean - the bean (must not be null)
Throws:
java.lang.IllegalArgumentException - if the name or the bean is null

removeBean

public java.lang.Object removeBean(java.lang.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(java.lang.String name)
Returns a BeanProvider for the bean with the given name. This implementation checks whether such a bean was directly added using the addBean() method. If not, the registered bean contributors are consulted. If no such bean can be found, null is returned.

Specified by:
getBeanProvider in interface BeanStore
Parameters:
name - the name of the bean in question
Returns:
a BeanProvider for this bean

getName

public java.lang.String getName()
Returns the name of this bean store.

Specified by:
getName in interface BeanStore
Returns:
the name of this bean store

setName

public void setName(java.lang.String name)
Sets the name of this bean store.

Parameters:
name - the new name

getParent

public BeanStore getParent()
Returns the parent bean store.

Specified by:
getParent in interface BeanStore
Returns:
the parent store

setParent

public void setParent(BeanStore parent)
Sets the parent bean store.

Parameters:
parent - the parent bean store

providerNames

public java.util.Set<java.lang.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:
providerNames in interface BeanStore
Returns:
a set with the names of the known bean providers

providerFor

protected BeanProvider providerFor(java.lang.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 a ConstantBeanProvider for the passed in bean.

Parameters:
bean - the bean
Returns:
a provider for this bean


Copyright © 2009 The JGUIraffe Team. All Rights Reserved.