net.sf.jguiraffe.di.impl
Class CombinedBeanContext

java.lang.Object
  extended by net.sf.jguiraffe.di.impl.CombinedBeanContext
All Implemented Interfaces:
BeanContext

public class CombinedBeanContext
extends java.lang.Object
implements BeanContext

A specific BeanContext implementation that allows to combine multiple bean contexts.

Combining bean context objects makes sense when a hierarchy of bean stores is not sufficient for expressing complex scopes of beans. One use case is a bean store with bean definitions that are only locally visible and with the restriction that beans defined globally take precedence over the local beans. (To achieve this only with bean stores, the store with local beans would have to be put on top of the hierarchy; but then its beans would be globally visible.)

An instance of this class maintains an arbitrary number (> 0) of child bean contexts. The methods demanded by the BeanContext interface are implemented in a way that calls are delegated to the child contexts in the following manner:

Note: This implementation is immutable and thus can be shared between multiple parties and threads.

Version:
$Id: CombinedBeanContext.java 156 2009-03-03 21:04:47Z oheger $
Author:
Oliver Heger

Constructor Summary
CombinedBeanContext(BeanContext... childContexts)
          Creates a new instance of CombinedBeanContext and sets the child contexts.
 
Method Summary
 void addBeanCreationListener(BeanCreationListener l)
          Adds the specified BeanCreationListener to this context.
 java.util.Set<java.lang.Class<?>> beanClasses()
          Returns a set with the classes of all beans that can be reached from the default bean store.
 java.util.Set<java.lang.Class<?>> beanClasses(BeanStore store)
          Returns a set with the classes of the existing beans starting with the given bean store.
 java.lang.String beanNameFor(BeanProvider beanProvider)
          Returns the name for the specified BeanProvider starting search at the default bean store.
 java.lang.String beanNameFor(BeanProvider beanProvider, BeanStore store)
          Returns the name for the specified BeanProvider starting search at the specified bean store.
 java.util.Set<java.lang.String> beanNames()
          Returns a set with the names of all beans that can be reached from the default bean store.
 java.util.Set<java.lang.String> beanNames(BeanStore store)
          Returns a set with the names of the existing beans starting with the given bean store.
 void close()
          Closes this BeanContext.
 boolean containsBean(java.lang.Class<?> beanClass)
          Tests whether a bean with the given class can be found starting from the default bean store.
 boolean containsBean(java.lang.Class<?> beanClass, BeanStore store)
          Tests whether a bean with the given class can be found starting from the specified bean store.
 boolean containsBean(java.lang.String name)
          Tests whether a bean with the given name can be found starting from the default bean store.
 boolean containsBean(java.lang.String name, BeanStore store)
          Tests whether a bean with the given name can be found starting from the specified bean store.
<T> T
getBean(java.lang.Class<T> beanCls)
          Returns the bean with the given class starting search at the default bean store.
<T> T
getBean(java.lang.Class<T> beanCls, BeanStore store)
          Returns the bean with the given class starting search at the provided bean store.
 java.lang.Object getBean(java.lang.String name)
          Returns the bean with the given name starting search at the default bean store.
 java.lang.Object getBean(java.lang.String name, BeanStore store)
          Returns the bean with the given name starting search at the provided bean store.
 BeanContext getChildContext(int idx)
          Returns the child context with the given index.
 BeanStore getDefaultBeanStore()
          Returns the default bean store.
 void removeBeanCreationListener(BeanCreationListener l)
          Removes the specified BeanCreationListener from this context.
 void setDefaultBeanStore(BeanStore store)
          Sets the default bean store.
 int size()
          Returns the number of child contexts.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

CombinedBeanContext

public CombinedBeanContext(BeanContext... childContexts)
Creates a new instance of CombinedBeanContext and sets the child contexts. The passed in array must contain at least a single element. null references are not permitted.

Parameters:
childContexts - an array with the child contexts
Throws:
java.lang.IllegalArgumentException - if the array with the child contexts is empty or contains null elements
Method Detail

size

public int size()
Returns the number of child contexts.

Returns:
the number of child contexts managed by this object

getChildContext

public BeanContext getChildContext(int idx)
Returns the child context with the given index. Indices are 0-based and must be in the range 0 <= idx < size().

Parameters:
idx - the index
Returns:
the child context with this index
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index is invalid

beanClasses

public java.util.Set<java.lang.Class<?>> beanClasses()
Returns a set with the classes of all beans that can be reached from the default bean store. This implementation constructs a union over all child contexts.

Specified by:
beanClasses in interface BeanContext
Returns:
a set with the classes of the defined beans

beanClasses

public java.util.Set<java.lang.Class<?>> beanClasses(BeanStore store)
Returns a set with the classes of the existing beans starting with the given bean store. This implementation delegates to the first child context.

Specified by:
beanClasses in interface BeanContext
Parameters:
store - the store
Returns:
a set with the classes of the defined beans

beanNames

public java.util.Set<java.lang.String> beanNames()
Returns a set with the names of all beans that can be reached from the default bean store. This implementation constructs a union over all child contexts.

Specified by:
beanNames in interface BeanContext
Returns:
a set with the names of the defined beans

beanNames

public java.util.Set<java.lang.String> beanNames(BeanStore store)
Returns a set with the names of the existing beans starting with the given bean store. This implementation delegates to the first child context.

Specified by:
beanNames in interface BeanContext
Parameters:
store - the store
Returns:
a set with the names of the defined beans

containsBean

public boolean containsBean(java.lang.String name)
Tests whether a bean with the given name can be found starting from the default bean store. This implementation asks each of the contained child contexts whether such a bean can be found.

Specified by:
containsBean in interface BeanContext
Parameters:
name - the name of the bean
Returns:
a flag whether this bean can be found

containsBean

public boolean containsBean(java.lang.String name,
                            BeanStore store)
Tests whether a bean with the given name can be found starting from the specified bean store. This implementation delegates to the first child context.

Specified by:
containsBean in interface BeanContext
Parameters:
name - the name of the bean in question
store - the bean store
Returns:
a flag whether this bean can be found

containsBean

public boolean containsBean(java.lang.Class<?> beanClass)
Tests whether a bean with the given class can be found starting from the default bean store. This implementation asks each of the contained child contexts whether such a bean can be found.

Specified by:
containsBean in interface BeanContext
Parameters:
beanClass - the class of the bean
Returns:
a flag whether this bean can be found

containsBean

public boolean containsBean(java.lang.Class<?> beanClass,
                            BeanStore store)
Tests whether a bean with the given class can be found starting from the specified bean store. This implementation delegates to the first child context.

Specified by:
containsBean in interface BeanContext
Parameters:
beanClass - the class of the bean in question
store - the bean store
Returns:
a flag whether this bean can be found

getBean

public java.lang.Object getBean(java.lang.String name)
Returns the bean with the given name starting search at the default bean store. This implementation asks the child contexts whether they contain this bean. On the first child context who does getBean() is invoked. If no child context contains the bean in question, an exception is thrown.

Specified by:
getBean in interface BeanContext
Parameters:
name - the name of the desired bean
Returns:
the bean with this name
Throws:
InjectionException - if the bean cannot be found or obtaining it causes an exception

getBean

public java.lang.Object getBean(java.lang.String name,
                                BeanStore store)
Returns the bean with the given name starting search at the provided bean store. This implementation delegates to the first child context.

Specified by:
getBean in interface BeanContext
Parameters:
name - the name of the desired bean
store - the bean store
Returns:
the bean with this name
Throws:
InjectionException - if the bean cannot be resolved

getBean

public <T> T getBean(java.lang.Class<T> beanCls)
Returns the bean with the given class starting search at the default bean store. This implementation asks the child contexts whether they contain this bean. On the first child context who does getBean() is invoked. If no child context contains the bean in question, an exception is thrown.

Specified by:
getBean in interface BeanContext
Parameters:
beanCls - the class of the desired bean
Returns:
the bean with this class
Throws:
InjectionException - if the bean cannot be found or obtaining it causes an exception

getBean

public <T> T getBean(java.lang.Class<T> beanCls,
                     BeanStore store)
Returns the bean with the given class starting search at the provided bean store. This implementation delegates to the first child context.

Specified by:
getBean in interface BeanContext
Parameters:
beanCls - the class of the desired bean
store - the bean store
Returns:
the bean with this class
Throws:
InjectionException - if the bean cannot be resolved

beanNameFor

public java.lang.String beanNameFor(BeanProvider beanProvider)
Returns the name for the specified BeanProvider starting search at the default bean store. This implementation searches the child contexts until a match is found.

Specified by:
beanNameFor in interface BeanContext
Parameters:
beanProvider - the BeanProvider
Returns:
the name of this BeanProvider or null
See Also:
BeanContext.beanNameFor(BeanProvider, BeanStore)

beanNameFor

public java.lang.String beanNameFor(BeanProvider beanProvider,
                                    BeanStore store)
Returns the name for the specified BeanProvider starting search at the specified bean store. This implementation delegates to the first child context.

Specified by:
beanNameFor in interface BeanContext
Parameters:
beanProvider - the BeanProvider
store - the BeanStore
Returns:
the name of this BeanProvider or null

getDefaultBeanStore

public BeanStore getDefaultBeanStore()
Returns the default bean store. For a combined bean context there is no default bean store, so this implementation always returns null.

Specified by:
getDefaultBeanStore in interface BeanContext
Returns:
the default bean store

setDefaultBeanStore

public void setDefaultBeanStore(BeanStore store)
Sets the default bean store. A combined bean context does not support a default bean store, so this implementation is just a dummy.

Specified by:
setDefaultBeanStore in interface BeanContext
Parameters:
store - the new default bean store

addBeanCreationListener

public void addBeanCreationListener(BeanCreationListener l)
Adds the specified BeanCreationListener to this context. This implementation ensures that bean creation events fired by all child contexts can be received.

Specified by:
addBeanCreationListener in interface BeanContext
Parameters:
l - the listener to be added
Throws:
java.lang.IllegalArgumentException - if one of the child stores throws an exception (which may be the case if the listener is null)

removeBeanCreationListener

public void removeBeanCreationListener(BeanCreationListener l)
Removes the specified BeanCreationListener from this context.

Specified by:
removeBeanCreationListener in interface BeanContext
Parameters:
l - the listener to be removed

close

public void close()
Closes this BeanContext. The creation listeners registered at the child contexts have to be removed. All child contexts are closed, too.

Specified by:
close in interface BeanContext


Copyright © 2009 The JGUIraffe Team. All Rights Reserved.