Package net.sf.jguiraffe.di.impl
Class BeanContextWrapper
- java.lang.Object
-
- net.sf.jguiraffe.di.impl.BeanContextWrapper
-
- All Implemented Interfaces:
BeanContext
public class BeanContextWrapper extends Object implements BeanContext
A simple wrapper implementation of the
BeanContext
interface.The main purpose of this class is to override the default
BeanStore
of a bean context. It is initialized with theBeanContext
object to be wrapped and the defaultBeanStore
to be used. All calls to methods defined by theBeanContext
interface are delegated to the wrapped bean context object. If they address the default bean store (i.e. no bean store is explicitly passed to the method), the bean store set for this instance is used. That way an already configured bean context can be used in a different context (which means, using another bean store as default entry in the look up mechanism) without manipulating its state.- Version:
- $Id: BeanContextWrapper.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
-
-
Constructor Summary
Constructors Constructor Description BeanContextWrapper(BeanContext wrappedContext)
Creates a new instance ofBeanContextWrapper
and sets the wrapped context.BeanContextWrapper(BeanContext wrappedContext, BeanStore defaultStore)
Creates a new instance ofBeanContextWrapper
and sets the wrapped context and 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()
Returns a set of the classes of the beans available in this bean context's default bean store.Set<Class<?>>
beanClasses(BeanStore store)
Returns a set of the classes of the beans available in the specified bean store.String
beanNameFor(BeanProvider beanProvider)
Returns the name of the givenBeanProvider
starting the search with the default bean store.String
beanNameFor(BeanProvider beanProvider, BeanStore store)
Returns the name of the givenBeanProvider
starting the search with the specified bean store.Set<String>
beanNames()
Returns a set with the names of the beans defined in this context's default bean store.Set<String>
beanNames(BeanStore store)
Returns a set with the names of the beans defined in the given bean store.void
close()
Closes thisBeanContext
.boolean
containsBean(Class<?> beanClass)
Checks whether a bean with the given class can be found in this bean context's default bean store.boolean
containsBean(Class<?> beanClass, BeanStore store)
Checks whether a bean with the given class can be found in the specified bean store.boolean
containsBean(String name)
Checks whether a bean with the given name can be found in the default bean store of this bean context.boolean
containsBean(String name, BeanStore store)
Checks 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 given class starting the search with the default bean store.<T> T
getBean(Class<T> beanCls, BeanStore store)
Returns the bean with the given class starting the search with the specified bean store.Object
getBean(String name)
Returns the bean with the given name starting the search with the default bean store.Object
getBean(String name, BeanStore store)
Returns the bean with the given name starting the search with the specified bean store.ClassLoaderProvider
getClassLoaderProvider()
Returns theClassLoaderProvider
used by this context.BeanStore
getDefaultBeanStore()
Returns the default bean store.BeanContext
getWrappedContext()
Returns the wrapped bean context.void
removeBeanCreationListener(BeanCreationListener l)
Removes the specifiedBeanCreationListener
from this context.void
setClassLoaderProvider(ClassLoaderProvider clp)
Sets theClassLoaderProvider
to be used by this context.void
setDefaultBeanStore(BeanStore store)
Sets the default bean store.
-
-
-
Constructor Detail
-
BeanContextWrapper
public BeanContextWrapper(BeanContext wrappedContext)
Creates a new instance ofBeanContextWrapper
and sets the wrapped context.- Parameters:
wrappedContext
- the bean context to be wrapped (must not be null)- Throws:
IllegalArgumentException
- if the bean context is null
-
BeanContextWrapper
public BeanContextWrapper(BeanContext wrappedContext, BeanStore defaultStore)
Creates a new instance ofBeanContextWrapper
and sets the wrapped context and the default bean store.- Parameters:
wrappedContext
- the bean context to be wrapped (must not be null)defaultStore
- the default bean store ton be used- Throws:
IllegalArgumentException
- if the bean context is null
-
-
Method Detail
-
getWrappedContext
public BeanContext getWrappedContext()
Returns the wrapped bean context.- Returns:
- a reference to the original bean context
-
beanClasses
public Set<Class<?>> beanClasses()
Returns a set of the classes of the beans available in this bean context's default bean store. This implementation delegates to the wrapped context.- Specified by:
beanClasses
in interfaceBeanContext
- Returns:
- a set with the classes of the defined beans
-
beanClasses
public Set<Class<?>> beanClasses(BeanStore store)
Returns a set of the classes of the beans available in the specified bean store. This implementation delegates to the wrapped context.- Specified by:
beanClasses
in interfaceBeanContext
- Parameters:
store
- the bean store- Returns:
- a set with the classes of the defined beans
-
beanNames
public Set<String> beanNames()
Returns a set with the names of the beans defined in this context's default bean store. This implementation delegates to the wrapped context.- Specified by:
beanNames
in interfaceBeanContext
- Returns:
- a set with 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. This implementation delegates to the wrapped context.- 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)
Checks whether a bean with the given name can be found in the default bean store of this bean context. This implementation delegates to the wrapped context.- Specified by:
containsBean
in interfaceBeanContext
- Parameters:
name
- the name of the searched bean- Returns:
- a flag whether this bean can be found
-
containsBean
public boolean containsBean(String name, BeanStore store)
Checks whether a bean with the given name can be found in the specified bean store. This implementation delegates to the wrapped context.- Specified by:
containsBean
in interfaceBeanContext
- Parameters:
name
- the name of the beanstore
- the bean store- Returns:
- a flag whether this bean can be found
-
containsBean
public boolean containsBean(Class<?> beanClass)
Checks whether a bean with the given class can be found in this bean context's default bean store. This implementation delegates to the wrapped context.- 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)
Checks whether a bean with the given class can be found in the specified bean store. This implementation delegates to the wrapped context.- Specified by:
containsBean
in interfaceBeanContext
- Parameters:
beanClass
- the class of the beanstore
- the bean store- Returns:
- a flag whether this bean can be found
-
getBean
public Object getBean(String name)
Returns the bean with the given name starting the search with the default bean store. This implementation delegates to the wrapped context.- Specified by:
getBean
in interfaceBeanContext
- Parameters:
name
- the name of the desired bean- Returns:
- the bean
-
getBean
public Object getBean(String name, BeanStore store)
Returns the bean with the given name starting the search with the specified bean store. This implementation delegates to the wrapped context.- Specified by:
getBean
in interfaceBeanContext
- Parameters:
name
- the name of the desired beanstore
- the bean store to start with- Returns:
- the bean
-
getBean
public <T> T getBean(Class<T> beanCls)
Returns the bean with the given class starting the search with the default bean store. This implementation delegates to the wrapped context.- Specified by:
getBean
in interfaceBeanContext
- Type Parameters:
T
- the type of the bean- Parameters:
beanCls
- the class of the desired bean- Returns:
- the bean
-
getBean
public <T> T getBean(Class<T> beanCls, BeanStore store)
Returns the bean with the given class starting the search with the specified bean store. This implementation delegates to the wrapped context.- Specified by:
getBean
in interfaceBeanContext
- Type Parameters:
T
- the type of the bean- Parameters:
beanCls
- the class of the desired beanstore
- the store to start with- Returns:
- the bean
-
beanNameFor
public String beanNameFor(BeanProvider beanProvider)
Returns the name of the givenBeanProvider
starting the search with the default bean store. This implementation delegates to the wrapped context.- Specified by:
beanNameFor
in interfaceBeanContext
- Parameters:
beanProvider
- theBeanProvider
- Returns:
- the name of this
BeanProvider
or null - See Also:
BeanContext.beanNameFor(BeanProvider, BeanStore)
-
beanNameFor
public String beanNameFor(BeanProvider beanProvider, BeanStore store)
Returns the name of the givenBeanProvider
starting the search with the specified bean store. This implementation delegates to the wrapped context.- Specified by:
beanNameFor
in interfaceBeanContext
- Parameters:
beanProvider
- theBeanProvider
store
- theBeanStore
- Returns:
- the name of this
BeanProvider
or null
-
getDefaultBeanStore
public BeanStore getDefaultBeanStore()
Returns the default bean store. This implementation returns the default store set for this wrapped context and not the one of the underlying context.- Specified by:
getDefaultBeanStore
in interfaceBeanContext
- Returns:
- the default bean store
-
setDefaultBeanStore
public void setDefaultBeanStore(BeanStore store)
Sets the default bean store. This implementation only changes the default bean store of this wrapped context. The default store of the underlying context is not touched.- Specified by:
setDefaultBeanStore
in interfaceBeanContext
- Parameters:
store
- the new default bean store
-
addBeanCreationListener
public void addBeanCreationListener(BeanCreationListener l)
Adds the specifiedBeanCreationListener
to this context. This implementation ensures that events from the wrapped context are received. However, the events are correctly transformed so that this context is set as the source context of the event.- Specified by:
addBeanCreationListener
in interfaceBeanContext
- Parameters:
l
- the listener to be added
-
removeBeanCreationListener
public void removeBeanCreationListener(BeanCreationListener l)
Removes the specifiedBeanCreationListener
from this context.- Specified by:
removeBeanCreationListener
in interfaceBeanContext
- Parameters:
l
- the listener to be removed
-
close
public void close()
Closes thisBeanContext
. This implementation removes theBeanCreationListener
registered at the wrapped context. Note that the wrapped context will not be closed! This is because the wrapped context is typically shared.- Specified by:
close
in interfaceBeanContext
-
getClassLoaderProvider
public ClassLoaderProvider getClassLoaderProvider()
Returns theClassLoaderProvider
used by this context. This implementation delegates to the wrapped context.- Specified by:
getClassLoaderProvider
in interfaceBeanContext
- Returns:
- the
ClassLoaderProvider
-
setClassLoaderProvider
public void setClassLoaderProvider(ClassLoaderProvider clp)
Sets theClassLoaderProvider
to be used by this context. This implementation delegates to the wrapped context.- Specified by:
setClassLoaderProvider
in interfaceBeanContext
- Parameters:
clp
- the newClassLoaderProvider
-
-