Class DIBuilderData
- java.lang.Object
-
- net.sf.jguiraffe.gui.builder.di.DIBuilderData
-
public class DIBuilderData extends Object
A data class for maintaining all information required for a DI builder operation.
An instance of this class will be present in the Jelly context when a Jelly script for populating a
BeanContext
is running. The specific tag handler classes can access the information they need for adding newly created beans to the correctBeanStore
or for accessing required helper objects.One important task of this class is dealing with
BeanStore
objects: One root bean store must be provided, to which bean definitions are added per default. In a builder script, it is possible to create an arbitrary number of further bean stores below this root store. In a bean definition the target bean store (where the bean should be stored) can be specified.This class also maintains the current
InvocationHelper
instance which is responsible for reflection operations and (indirectly) for data type conversions required by the current builder script. Either an instance ofInvocationHelper
can be set explicitly using thesetInvocationHelper(InvocationHelper)
method or a default instance will be created. If the rootBeanStore
is created by this object, theConversionHelper
will be set automatically. If the rootBeanStore
is set manually using theinitRootBeanStore(MutableBeanStore)
method, the caller is responsible for initializing the root store with the correctConversionHelper
instance.Implementation note: This class is not thread-safe. It is intended to be used inside a Jelly script, which runs in a single thread.
- Version:
- $Id: DIBuilderData.java 205 2012-01-29 18:29:57Z oheger $
- Author:
- Oliver Heger
-
-
Constructor Summary
Constructors Constructor Description DIBuilderData()
Creates a new instance ofDIBuilderData
.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description String
addAnonymousBeanProvider(String storeName, BeanProvider bean)
Adds an "anonymous"BeanProvider
to aBeanStore
.void
addBeanProvider(String storeName, String beanName, BeanProvider bean)
Adds aBeanProvider
to aBeanStore
.void
addBeanStore(String name, String parentName)
Creates aBeanStore
with the specified name and adds it to the internal list of existing bean stores.protected MutableBeanStore
createRootStore()
Creates the root bean store.static DIBuilderData
get(org.apache.commons.jelly.JellyContext context)
Obtains an instance of this class from the specified Jelly context.BeanStore
getBeanStore(String name)
Returns theBeanStore
with the specified name.Set<String>
getBeanStoreNames()
Returns a set with the names of the existing bean stores.ClassLoaderProvider
getClassLoaderProvider()
Returns theClassLoaderProvider
to be used.InvocationHelper
getInvocationHelper()
Returns the currentInvocationHelper
.BeanStore
getRootBeanStore()
Returns the root bean store.boolean
hasBeanStore(String name)
Returns a flag whether the bean store with the given name is present.void
initRootBeanStore(MutableBeanStore rootBeanStore)
Sets the root bean store.protected MutableBeanStore
internalGetBeanStore(String name)
Returns the internal bean store with the given name.void
put(org.apache.commons.jelly.JellyContext context)
Stores this instance in the specified context.void
setClassLoaderProvider(ClassLoaderProvider classLoaderProvider)
Sets theClassLoaderProvider
to be used.void
setInvocationHelper(InvocationHelper invocationHelper)
Sets theInvocationHelper
to be used by this object.
-
-
-
Method Detail
-
getClassLoaderProvider
public ClassLoaderProvider getClassLoaderProvider()
Returns theClassLoaderProvider
to be used. This method never returns null. If no specificClassLoaderProvider
has been set so far (by using thesetClassLoaderProvider(ClassLoaderProvider)
method), a new default provider is created and returned, which does not contain any specific registered class loaders.- Returns:
- the
ClassLoaderProvider
to be used
-
setClassLoaderProvider
public void setClassLoaderProvider(ClassLoaderProvider classLoaderProvider)
Sets theClassLoaderProvider
to be used. This object is especially required for resolvingClassDescription
objects.- Parameters:
classLoaderProvider
- theClassLoaderProvider
to be used
-
getInvocationHelper
public InvocationHelper getInvocationHelper()
Returns the currentInvocationHelper
. This object can be used by all components involved in the builder process if reflection operations or data type conversions are needed. If custom type converters are needed, they have to be registered at conversion helper managed by this instance.- Returns:
- the
InvocationHelper
- See Also:
setInvocationHelper(InvocationHelper)
-
setInvocationHelper
public void setInvocationHelper(InvocationHelper invocationHelper)
Sets theInvocationHelper
to be used by this object. Using this method a specificInvocationHelper
instance can be set which will then be returned bygetInvocationHelper()
. IfgetInvocationHelper()
is called and no helper object has been set, a new default instance is created automatically.- Parameters:
invocationHelper
- theInvocationHelper
to be used
-
getRootBeanStore
public BeanStore getRootBeanStore()
Returns the root bean store. This method returns never null; if no root bean store has been set so far, a default one is created now.- Returns:
- the root bean store
-
initRootBeanStore
public void initRootBeanStore(MutableBeanStore rootBeanStore)
Sets the root bean store. This is the default bean store, to which all beans are added unless a specific bean store is specified. Using theaddBeanStore()
method it is possible to create further bean stores below this root store.- Parameters:
rootBeanStore
- the root bean store
-
getBeanStore
public BeanStore getBeanStore(String name)
Returns theBeanStore
with the specified name. The root store can be obtained by passing in null for the name. If a name cannot be resolved, an exception is thrown.- Parameters:
name
- the name of the desired bean store- Returns:
- the bean store with this name
- Throws:
NoSuchElementException
- if the name cannot be resolved
-
hasBeanStore
public boolean hasBeanStore(String name)
Returns a flag whether the bean store with the given name is present.- Parameters:
name
- the name of the bean store in question- Returns:
- a flag whether this bean store is present
-
getBeanStoreNames
public Set<String> getBeanStoreNames()
Returns a set with the names of the existing bean stores. These are the bean stores that have been added using theaddBeanStore()
method. Note that the root bean store (which is identified by the name null) is not contained in this set.- Returns:
- a set with the names of the existing bean stores
-
addBeanStore
public void addBeanStore(String name, String parentName)
Creates aBeanStore
with the specified name and adds it to the internal list of existing bean stores. The new store is added as a child of the specified parent bean store. If null is passed in for the parent, the new store will become a child of the root bean store. Note that the names of bean stores must be unique, even if they are at different levels of the hierarchy.- Parameters:
name
- the name of the bean store to be added (must not be null)parentName
- the name of the parent bean store- Throws:
NoSuchElementException
- if the parent name cannot be resolvedIllegalArgumentException
- if the name is null or is already used by an existing bean store
-
addBeanProvider
public void addBeanProvider(String storeName, String beanName, BeanProvider bean)
Adds aBeanProvider
to aBeanStore
. The bean store with the specified name is resolved (null selects the root bean store), and the providedBeanProvider
is added to it under the given name.- Parameters:
storeName
- the name of the bean storebeanName
- the name of the bean provider (must not be null)bean
- the bean provider to be added (must not be null)- Throws:
NoSuchElementException
- if the bean store cannot be resolvedIllegalArgumentException
- if the bean name or the bean provider is null
-
addAnonymousBeanProvider
public String addAnonymousBeanProvider(String storeName, BeanProvider bean)
Adds an "anonymous"BeanProvider
to aBeanStore
. This method can be used for beans that are visible in a local context only. It works similar toaddBeanProvider()
, but the bean store'saddAnonymousBeanProvider()
method will be called. This class will keep a counter for generating indices for anonymous bean providers. This ensures that the generated names used internally for these beans are unique.- Parameters:
storeName
- the name of the bean storebean
- the bean provider to be added (must not be null)- Returns:
- the name generated for the bean provider added
- Throws:
IllegalArgumentException
- if no provider is specified
-
put
public void put(org.apache.commons.jelly.JellyContext context)
Stores this instance in the specified context. It is stored there under a default key.- Parameters:
context
- the Jelly context
-
get
public static DIBuilderData get(org.apache.commons.jelly.JellyContext context)
Obtains an instance of this class from the specified Jelly context. The instance is looked up under a default key, the same that is also used by theput()
method. If no instance can be found in this context, null is returned.- Parameters:
context
- the Jelly context- Returns:
- the instance found in this context
-
internalGetBeanStore
protected MutableBeanStore internalGetBeanStore(String name)
Returns the internal bean store with the given name. Internally this class operates on mutableDefaultBeanStore
objects. To its clients only the immutableBeanStore
interface is exposed.- Parameters:
name
- the name of the bean store to be retrieved (null means the root bean store)- Returns:
- the bean store with this name
- Throws:
NoSuchElementException
- if the bean store cannot be resolved
-
createRootStore
protected MutableBeanStore createRootStore()
Creates the root bean store. This method is called byinternalGetBeanStore()
when the root bean store is accessed for the first time and has not been explicitly initialized.- Returns:
- the new root bean store
-
-