Package net.sf.jguiraffe.di.impl
Class DefaultBeanContext
- java.lang.Object
-
- net.sf.jguiraffe.di.impl.DefaultBeanContext
-
- All Implemented Interfaces:
BeanContext
public class DefaultBeanContext extends Object implements BeanContext
A default implementation of the
BeanContext
interface.This class allows full access to all beans defined in a hierarchy of
BeanStore
s. Dependencies are dynamically resolved and injected.The class is thread-safe. When operating on a bean store hierarchy it implements transactional behavior as described in the documentation to the
BeanStore
interface, i.e. if two threads try to access aBeanProvider
concurrently, one of the will be suspended until the other one has resolved all of its dependencies. This enables concurrent, synchronized access to bean stores in a read-only manner. However the bean stores should not be written at the same time.- Version:
- $Id: DefaultBeanContext.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
-
-
Constructor Summary
Constructors Constructor Description DefaultBeanContext()
Creates a new instance ofDefaultBeanContext
.DefaultBeanContext(BeanStore defStore)
Creates a new instance ofDefaultBeanContext
and sets the default bean store.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addBeanCreationListener(BeanCreationListener l)
Adds the specifiedBeanCreationListener
to this context.Set<Class<?>>
beanClasses()
Obtains a list of the classes of the beans defined in the default bean store.Set<Class<?>>
beanClasses(BeanStore store)
Obtains a list of the classes of the beans defined in the given store and its parents.String
beanNameFor(BeanProvider beanProvider)
Searches for the specifiedBeanProvider
in the accessible bean stores (starting with the default bean store) and the returns the name, under which it is registered.String
beanNameFor(BeanProvider beanProvider, BeanStore store)
Searches for the specifiedBeanProvider
in the accessible bean stores (starting with the specified bean store) and the returns the name, under which it is registered.Set<String>
beanNames()
Returns a set with the names of the beans defined in the default bean store.Set<String>
beanNames(BeanStore store)
Returns a set with the names of the beans defined in the given bean store (or its parent).void
close()
Closes thisBeanContext
.boolean
containsBean(Class<?> beanClass)
Tests whether a bean with the given bean class can be found in the default bean store.boolean
containsBean(Class<?> beanClass, BeanStore store)
Tests whether a bean with the given class can be found in the specified bean store.boolean
containsBean(String name)
Tests whether a bean with the given name can be found in the default store.boolean
containsBean(String name, BeanStore store)
Tests whether a bean with the given name can be found in the specified bean store.<T> T
getBean(Class<T> beanCls)
Returns the bean with the specified class from the default bean store.<T> T
getBean(Class<T> beanCls, BeanStore store)
Returns the bean with the specified class from the given bean store.Object
getBean(String name)
Returns the bean with the specified name from the default store.Object
getBean(String name, BeanStore store)
Returns the bean with the specified name from the given bean store.protected Object
getBean(Dependency dependency, BeanStore store)
Obtains the bean from theBeanProvider
specified by the givenDependency
.ClassLoaderProvider
getClassLoaderProvider()
Returns theClassLoaderProvider
used by this bean context.BeanStore
getDefaultBeanStore()
Returns the default bean store.void
removeBeanCreationListener(BeanCreationListener l)
Removes the specifiedBeanCreationListener
from this context.void
setClassLoaderProvider(ClassLoaderProvider classLoaderProvider)
Sets theClassLoaderProvider
to be used by this bean context.void
setDefaultBeanStore(BeanStore store)
Sets the default bean store.protected void
waitForTx(BeanStore root)
Waits at the specified bean store until the current transaction finishes.
-
-
-
Constructor Detail
-
DefaultBeanContext
public DefaultBeanContext()
Creates a new instance ofDefaultBeanContext
.
-
DefaultBeanContext
public DefaultBeanContext(BeanStore defStore)
Creates a new instance ofDefaultBeanContext
and sets the default bean store. This parameter is optional. If noBeanStore
is provided, a default store must be set later or only methods can be used that expect a store as argument.- Parameters:
defStore
- the default bean store
-
-
Method Detail
-
getClassLoaderProvider
public ClassLoaderProvider getClassLoaderProvider()
Returns theClassLoaderProvider
used by this bean context.- Specified by:
getClassLoaderProvider
in interfaceBeanContext
- Returns:
- the
ClassLoaderProvider
- See Also:
setClassLoaderProvider(ClassLoaderProvider)
-
setClassLoaderProvider
public void setClassLoaderProvider(ClassLoaderProvider classLoaderProvider)
Sets theClassLoaderProvider
to be used by this bean context. TheClassLoaderProvider
is needed when dependencies to beans are to be resolved that specified by class names. When a newDefaultBeanContext
instance is created, a defaultClassLoaderProvider
is set. It is then possible to change this object using this method.- Specified by:
setClassLoaderProvider
in interfaceBeanContext
- Parameters:
classLoaderProvider
- the newClassLoaderProvider
(must not be null)- Throws:
IllegalArgumentException
- if the parameter is null
-
beanClasses
public Set<Class<?>> beanClasses()
Obtains a list of the classes of the beans defined in the default bean store.- Specified by:
beanClasses
in interfaceBeanContext
- Returns:
- the classes of the defined beans
-
beanClasses
public Set<Class<?>> beanClasses(BeanStore store)
Obtains a list of the classes of the beans defined in the given store and its parents.- Specified by:
beanClasses
in interfaceBeanContext
- Parameters:
store
- the store to start from- Returns:
- the classes of the defined beans
-
beanNames
public Set<String> beanNames()
Returns a set with the names of the beans defined in the default bean store.- Specified by:
beanNames
in interfaceBeanContext
- Returns:
- the names of the defined beans
-
beanNames
public Set<String> beanNames(BeanStore store)
Returns a set with the names of the beans defined in the given bean store (or its parent).- Specified by:
beanNames
in interfaceBeanContext
- Parameters:
store
- the bean store- Returns:
- a set with the names of the defined beans
-
containsBean
public boolean containsBean(String name)
Tests whether a bean with the given name can be found in the default store.- Specified by:
containsBean
in interfaceBeanContext
- Parameters:
name
- the name of the bean- Returns:
- a flag whether this bean can be found
-
containsBean
public boolean containsBean(String name, BeanStore store)
Tests whether a bean with the given name can be found in the specified bean store. null can be specified for both the name and the bean store; result will then be false.- Specified by:
containsBean
in interfaceBeanContext
- Parameters:
name
- the name of the beanstore
- the bean store- Returns:
- a flag whether this bean can be found in this store
-
containsBean
public boolean containsBean(Class<?> beanClass)
Tests whether a bean with the given bean class can be found in the default bean store.- Specified by:
containsBean
in interfaceBeanContext
- Parameters:
beanClass
- the class of the bean- Returns:
- a flag whether this bean can be found
-
containsBean
public boolean containsBean(Class<?> beanClass, BeanStore store)
Tests whether a bean with the given class can be found in the specified bean store. null can be specified for both the name and the bean store; result will then be false.- Specified by:
containsBean
in interfaceBeanContext
- Parameters:
beanClass
- the class of the beanstore
- the bean store- Returns:
- a flag whether this bean can be found in this store
-
getBean
public Object getBean(String name)
Returns the bean with the specified name from the default store.- Specified by:
getBean
in interfaceBeanContext
- Parameters:
name
- the name of the bean to retrieve- Returns:
- the bean with this name
- Throws:
InjectionException
- if an error occurs
-
getBean
public Object getBean(String name, BeanStore store)
Returns the bean with the specified name from the given bean store.- Specified by:
getBean
in interfaceBeanContext
- Parameters:
name
- the name of the bean to retrievestore
- the bean store- Returns:
- the bean with this name
- Throws:
InjectionException
- if an error occurs
-
getBean
public <T> T getBean(Class<T> beanCls)
Returns the bean with the specified class from the default bean store.- Specified by:
getBean
in interfaceBeanContext
- Type Parameters:
T
- the type of the bean to be retrieved- Parameters:
beanCls
- the class of the bean to be retrieved- Returns:
- the bean with this class
- Throws:
InjectionException
- if an error occurs
-
getBean
public <T> T getBean(Class<T> beanCls, BeanStore store)
Returns the bean with the specified class from the given bean store.- Specified by:
getBean
in interfaceBeanContext
- Type Parameters:
T
- the type of the bean to be retrieved- Parameters:
beanCls
- the class of the bean to be retrievedstore
- the bean store- Returns:
- the bean with this class
- Throws:
InjectionException
- if an error occurs
-
getDefaultBeanStore
public BeanStore getDefaultBeanStore()
Returns the default bean store.- Specified by:
getDefaultBeanStore
in interfaceBeanContext
- Returns:
- the default bean store
-
beanNameFor
public String beanNameFor(BeanProvider beanProvider)
Searches for the specifiedBeanProvider
in the accessible bean stores (starting with the default bean store) and the returns the name, under which it is registered.- Specified by:
beanNameFor
in interfaceBeanContext
- Parameters:
beanProvider
- theBeanProvider
in question- Returns:
- the corresponding bean name or null if it cannot be resolved
- See Also:
BeanContext.beanNameFor(BeanProvider, BeanStore)
-
beanNameFor
public String beanNameFor(BeanProvider beanProvider, BeanStore store)
Searches for the specifiedBeanProvider
in the accessible bean stores (starting with the specified bean store) and the returns the name, under which it is registered.- Specified by:
beanNameFor
in interfaceBeanContext
- Parameters:
beanProvider
- theBeanProvider
in questionstore
- theBeanStore
- Returns:
- the corresponding bean name or null if it cannot be resolved
-
close
public void close()
Closes thisBeanContext
. This is just an empty dummy implementation. There are no resources to be freed.- Specified by:
close
in interfaceBeanContext
-
setDefaultBeanStore
public void setDefaultBeanStore(BeanStore store)
Sets the default bean store.- Specified by:
setDefaultBeanStore
in interfaceBeanContext
- Parameters:
store
- the new default bean store
-
addBeanCreationListener
public void addBeanCreationListener(BeanCreationListener l)
Adds the specifiedBeanCreationListener
to this context.- Specified by:
addBeanCreationListener
in interfaceBeanContext
- Parameters:
l
- the listener to add (must not be null)- Throws:
IllegalArgumentException
- if the listener is null
-
removeBeanCreationListener
public void removeBeanCreationListener(BeanCreationListener l)
Removes the specifiedBeanCreationListener
from this context.- Specified by:
removeBeanCreationListener
in interfaceBeanContext
- Parameters:
l
- the listener to be removed
-
getBean
protected Object getBean(Dependency dependency, BeanStore store)
Obtains the bean from theBeanProvider
specified by the givenDependency
. This method is called by the othergetBean()
methods. It does the real work.- Parameters:
dependency
- the dependency to be resolvedstore
- the current store- Returns:
- the bean managed by the specified provider
- Throws:
InjectionException
- if an error occurs while resolving the dependency
-
waitForTx
protected void waitForTx(BeanStore root) throws InterruptedException
Waits at the specified bean store until the current transaction finishes. This method is called when a transaction affects a bean provider that is already locked by another transaction. In this case this other transaction has to finish first. This implementation callswait()
on the given bean store.- Parameters:
root
- the root bean store- Throws:
InterruptedException
- if the thread is interrupted
-
-