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
BeanContextinterface.This class allows full access to all beans defined in a hierarchy of
BeanStores. 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
BeanStoreinterface, i.e. if two threads try to access aBeanProviderconcurrently, 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 ofDefaultBeanContextand sets the default bean store.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddBeanCreationListener(BeanCreationListener l)Adds the specifiedBeanCreationListenerto 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.StringbeanNameFor(BeanProvider beanProvider)Searches for the specifiedBeanProviderin the accessible bean stores (starting with the default bean store) and the returns the name, under which it is registered.StringbeanNameFor(BeanProvider beanProvider, BeanStore store)Searches for the specifiedBeanProviderin 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).voidclose()Closes thisBeanContext.booleancontainsBean(Class<?> beanClass)Tests whether a bean with the given bean class can be found in the default bean store.booleancontainsBean(Class<?> beanClass, BeanStore store)Tests whether a bean with the given class can be found in the specified bean store.booleancontainsBean(String name)Tests whether a bean with the given name can be found in the default store.booleancontainsBean(String name, BeanStore store)Tests whether a bean with the given name can be found in the specified bean store.<T> TgetBean(Class<T> beanCls)Returns the bean with the specified class from the default bean store.<T> TgetBean(Class<T> beanCls, BeanStore store)Returns the bean with the specified class from the given bean store.ObjectgetBean(String name)Returns the bean with the specified name from the default store.ObjectgetBean(String name, BeanStore store)Returns the bean with the specified name from the given bean store.protected ObjectgetBean(Dependency dependency, BeanStore store)Obtains the bean from theBeanProviderspecified by the givenDependency.ClassLoaderProvidergetClassLoaderProvider()Returns theClassLoaderProviderused by this bean context.BeanStoregetDefaultBeanStore()Returns the default bean store.voidremoveBeanCreationListener(BeanCreationListener l)Removes the specifiedBeanCreationListenerfrom this context.voidsetClassLoaderProvider(ClassLoaderProvider classLoaderProvider)Sets theClassLoaderProviderto be used by this bean context.voidsetDefaultBeanStore(BeanStore store)Sets the default bean store.protected voidwaitForTx(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 ofDefaultBeanContextand sets the default bean store. This parameter is optional. If noBeanStoreis 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 theClassLoaderProviderused by this bean context.- Specified by:
getClassLoaderProviderin interfaceBeanContext- Returns:
- the
ClassLoaderProvider - See Also:
setClassLoaderProvider(ClassLoaderProvider)
-
setClassLoaderProvider
public void setClassLoaderProvider(ClassLoaderProvider classLoaderProvider)
Sets theClassLoaderProviderto be used by this bean context. TheClassLoaderProvideris needed when dependencies to beans are to be resolved that specified by class names. When a newDefaultBeanContextinstance is created, a defaultClassLoaderProvideris set. It is then possible to change this object using this method.- Specified by:
setClassLoaderProviderin 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:
beanClassesin 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:
beanClassesin 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:
beanNamesin 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:
beanNamesin 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:
containsBeanin 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:
containsBeanin 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:
containsBeanin 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:
containsBeanin 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:
getBeanin 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:
getBeanin 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:
getBeanin 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:
getBeanin 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:
getDefaultBeanStorein interfaceBeanContext- Returns:
- the default bean store
-
beanNameFor
public String beanNameFor(BeanProvider beanProvider)
Searches for the specifiedBeanProviderin the accessible bean stores (starting with the default bean store) and the returns the name, under which it is registered.- Specified by:
beanNameForin interfaceBeanContext- Parameters:
beanProvider- theBeanProviderin 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 specifiedBeanProviderin the accessible bean stores (starting with the specified bean store) and the returns the name, under which it is registered.- Specified by:
beanNameForin interfaceBeanContext- Parameters:
beanProvider- theBeanProviderin 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:
closein interfaceBeanContext
-
setDefaultBeanStore
public void setDefaultBeanStore(BeanStore store)
Sets the default bean store.- Specified by:
setDefaultBeanStorein interfaceBeanContext- Parameters:
store- the new default bean store
-
addBeanCreationListener
public void addBeanCreationListener(BeanCreationListener l)
Adds the specifiedBeanCreationListenerto this context.- Specified by:
addBeanCreationListenerin 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 specifiedBeanCreationListenerfrom this context.- Specified by:
removeBeanCreationListenerin interfaceBeanContext- Parameters:
l- the listener to be removed
-
getBean
protected Object getBean(Dependency dependency, BeanStore store)
Obtains the bean from theBeanProviderspecified 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
-
-