Groovy Documentation

groovy.util
[Java] Class FactoryBuilderSupport

java.lang.Object
  groovy.lang.GroovyObjectSupport
      groovy.lang.Binding
          groovy.util.FactoryBuilderSupport

public abstract class FactoryBuilderSupport
extends Binding

Mix of BuilderSupport and SwingBuilder's factory support. Warning: this implementation is not thread safe and should not be used across threads in a multi-threaded environment. A locking mechanism should be implemented by the subclass if use is expected across multiple threads.

Authors:
James Strachan
Andres Almiray
Danno Ferrin


Field Summary
static java.lang.String CHILD_BUILDER

static java.lang.String CURRENT_BUILDER

static java.lang.String CURRENT_FACTORY

static java.lang.String CURRENT_NAME

static java.lang.String CURRENT_NODE

static java.lang.String OWNER

static java.lang.String PARENT_BUILDER

static java.lang.String PARENT_CONTEXT

static java.lang.String PARENT_FACTORY

static java.lang.String PARENT_NAME

static java.lang.String PARENT_NODE

static java.lang.String SCRIPT_CLASS_NAME

protected java.util.LinkedList attributeDelegates

protected boolean autoRegistrationComplete

protected boolean autoRegistrationRunning

protected java.util.Map explicitMethods

protected java.util.Map explicitProperties

protected Closure methodMissingDelegate

protected java.util.LinkedList postInstantiateDelegates

protected java.util.LinkedList postNodeCompletionDelegates

protected java.util.LinkedList preInstantiateDelegates

protected Closure propertyMissingDelegate

protected java.util.Map registrationGroup

protected java.lang.String registrationGroupName

 
Constructor Summary
FactoryBuilderSupport()

FactoryBuilderSupport(boolean init)

 
Method Summary
Closure addAttributeDelegate(Closure attrDelegate)

Add an attribute delegate so it can intercept attributes being set.

void addDisposalClosure(Closure closure)

Closure addPostInstantiateDelegate(Closure delegate)

Closure addPostNodeCompletionDelegate(Closure delegate)

Closure addPreInstantiateDelegate(Closure delegate)

void autoRegisterNodes()

Ask the nodes to be registered

java.lang.Object build(java.lang.Class viewClass)

java.lang.Object build(Script script)

java.lang.Object build(java.lang.String script, GroovyClassLoader loader)

protected boolean checkExplicitMethod(java.lang.String methodName, java.lang.Object args, Reference result)

static void checkValueIsNull(java.lang.Object value, java.lang.Object name)

Throws an exception if value is null.

static boolean checkValueIsType(java.lang.Object value, java.lang.Object name, java.lang.Class type)

Checks type of value against builder type

static boolean checkValueIsTypeNotString(java.lang.Object value, java.lang.Object name, java.lang.Class type)

Checks values against factory's type

protected java.lang.Object createNode(java.lang.Object name, java.util.Map attributes, java.lang.Object value)

protected java.lang.Object dispathNodeCall(java.lang.Object name, java.lang.Object args)

void dispose()

java.util.List getAttributeDelegates()

FactoryBuilderSupport getChildBuilder()

java.util.Map getContext()

java.lang.Object getContextAttribute(java.lang.String key)

protected java.util.LinkedList getContexts()

Restores the state of the current builder to the same state as an older build.

protected java.util.Map getContinuationData()

java.lang.Object getCurrent()

@return the current node being built.

FactoryBuilderSupport getCurrentBuilder()

@return the node of the parent of the current node.

Factory getCurrentFactory()

@return the factory of the parent of the current node.

java.lang.String getCurrentName()

@return the builder that built the current node.

java.util.Map getExplicitMethods()

@return the explicit properties map (Unmodifiable Map).

java.util.Map getExplicitProperties()

@return the factory map (Unmodifiable Map).

java.util.Map getFactories()

@return the factory map (Unmodifiable Map).

java.util.Map getLocalExplicitMethods()

@return the explicit properties map (Unmodifiable Map).

java.util.Map getLocalExplicitProperties()

java.util.Map getLocalFactories()

@return the explicit methods map (Unmodifiable Map).

Closure getMethodMissingDelegate()

java.lang.Object getName(java.lang.String methodName)

A hook to allow names to be converted into some other object such as a QName in XML or ObjectName in JMX.

Closure getNameMappingClosure()

java.util.Map getParentContext()

@return the name of the parent of the current node.

Factory getParentFactory()

@return the context of the parent of the current node.

java.lang.String getParentName()

java.lang.Object getParentNode()

@return the factory of the parent of the current node.

java.util.List getPostInstantiateDelegates()

java.util.List getPostNodeCompletionDelegates()

java.util.List getPreInstantiateDelegates()

java.lang.Object getProperty(java.lang.String property)

Closure getPropertyMissingDelegate()

protected FactoryBuilderSupport getProxyBuilder()

java.util.Set getRegistrationGroupItems(java.lang.String group)

java.util.Set getRegistrationGroups()

java.lang.Object getVariable(java.lang.String name)

@param name the name of the variable to lookup

java.util.Map getVariables()

protected void handleNodeAttributes(java.lang.Object node, java.util.Map attributes)

java.lang.Object invokeMethod(java.lang.String methodName)

Convenience method when no arguments are required

java.lang.Object invokeMethod(java.lang.String methodName, java.lang.Object args)

protected void newContext()

A hook to allow nodes to be processed once they have had all of their children applied.

protected void nodeCompleted(java.lang.Object parent, java.lang.Object node)

protected java.util.Map popContext()

protected void postInstantiate(java.lang.Object name, java.util.Map attributes, java.lang.Object node)

protected java.lang.Object postNodeCompletion(java.lang.Object parent, java.lang.Object node)

A hook before the factory creates the node.

protected void preInstantiate(java.lang.Object name, java.util.Map attributes, java.lang.Object value)

A strategy method to allow derived builders to use builder-trees and switch in different kinds of builders.

void registerBeanFactory(java.lang.String theName, java.lang.Class beanClass)

void registerBeanFactory(java.lang.String theName, java.lang.String groupName, java.lang.Class beanClass)

void registerExplicitMethod(java.lang.String name, Closure closure)

void registerExplicitMethod(java.lang.String name, java.lang.String groupName, Closure closure)

void registerExplicitProperty(java.lang.String name, Closure getter, Closure setter)

void registerExplicitProperty(java.lang.String name, java.lang.String groupName, Closure getter, Closure setter)

void registerFactory(java.lang.String name, Factory factory)

Registers a factory for a node name.

void registerFactory(java.lang.String name, java.lang.String groupName, Factory factory)

void removeAttributeDelegate(Closure attrDelegate)

void removePostInstantiateDelegate(Closure delegate)

void removePostNodeCompletionDelegate(Closure delegate)

void removePreInstantiateDelegate(Closure delegate)

protected void reset()

protected Closure resolveExplicitMethod(java.lang.String methodName, java.lang.Object args)

protected Closure[] resolveExplicitProperty(java.lang.String propertyName)

protected Factory resolveFactory(java.lang.Object name, java.util.Map attributes, java.lang.Object value)

This is a hook for subclasses to plugin a custom strategy for mapping names to factories.

protected void restoreFromContinuationData(java.util.Map data)

protected void setClosureDelegate(Closure closure, java.lang.Object node)

void setMethodMissingDelegate(Closure delegate)

void setNameMappingClosure(Closure nameMappingClosure)

protected void setNodeAttributes(java.lang.Object node, java.util.Map attributes)

protected void setParent(java.lang.Object parent, java.lang.Object child)

@return the stack of available contexts.

void setProperty(java.lang.String property, java.lang.Object newValue)

Overloaded to make variables appear as bean properties or via the subscript operator

void setPropertyMissingDelegate(Closure delegate)

@return the context of the current node.

protected void setProxyBuilder(FactoryBuilderSupport proxyBuilder)

Assigns any existing properties to the node.

void setVariable(java.lang.String name, java.lang.Object value)

java.lang.Object withBuilder(FactoryBuilderSupport builder, Closure closure)

java.lang.Object withBuilder(FactoryBuilderSupport builder, java.lang.String name, Closure closure)

Switches the builder's proxyBuilder during the execution of a closure.

java.lang.Object withBuilder(java.util.Map attributes, FactoryBuilderSupport builder, java.lang.String name, Closure closure)

 
Methods inherited from class Binding
getProperty, getVariable, getVariables, hasVariable, setProperty, setVariable
 
Methods inherited from class GroovyObjectSupport
getMetaClass, getProperty, invokeMethod, setMetaClass, setProperty
 
Methods inherited from class java.lang.Object
java.lang.Object#wait(long, int), java.lang.Object#wait(long), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll()
 

Field Detail

CHILD_BUILDER

public static final java.lang.String CHILD_BUILDER


CURRENT_BUILDER

public static final java.lang.String CURRENT_BUILDER


CURRENT_FACTORY

public static final java.lang.String CURRENT_FACTORY


CURRENT_NAME

public static final java.lang.String CURRENT_NAME


CURRENT_NODE

public static final java.lang.String CURRENT_NODE


OWNER

public static final java.lang.String OWNER


PARENT_BUILDER

public static final java.lang.String PARENT_BUILDER


PARENT_CONTEXT

public static final java.lang.String PARENT_CONTEXT


PARENT_FACTORY

public static final java.lang.String PARENT_FACTORY


PARENT_NAME

public static final java.lang.String PARENT_NAME


PARENT_NODE

public static final java.lang.String PARENT_NODE


SCRIPT_CLASS_NAME

public static final java.lang.String SCRIPT_CLASS_NAME


attributeDelegates

protected java.util.LinkedList attributeDelegates


autoRegistrationComplete

protected boolean autoRegistrationComplete


autoRegistrationRunning

protected boolean autoRegistrationRunning


explicitMethods

protected java.util.Map explicitMethods


explicitProperties

protected java.util.Map explicitProperties


methodMissingDelegate

protected Closure methodMissingDelegate


postInstantiateDelegates

protected java.util.LinkedList postInstantiateDelegates


postNodeCompletionDelegates

protected java.util.LinkedList postNodeCompletionDelegates


preInstantiateDelegates

protected java.util.LinkedList preInstantiateDelegates


propertyMissingDelegate

protected Closure propertyMissingDelegate


registrationGroup

protected java.util.Map registrationGroup


registrationGroupName

protected java.lang.String registrationGroupName


 
Constructor Detail

FactoryBuilderSupport

public FactoryBuilderSupport()


FactoryBuilderSupport

public FactoryBuilderSupport(boolean init)


 
Method Detail

addAttributeDelegate

public Closure addAttributeDelegate(Closure attrDelegate)
Add an attribute delegate so it can intercept attributes being set. Attribute delegates are fired in a FILO pattern, so that nested delegates get first crack.
Parameters:
attrDelegate - the closure to be called
Returns:
attrDelegate


addDisposalClosure

public void addDisposalClosure(Closure closure)


addPostInstantiateDelegate

public Closure addPostInstantiateDelegate(Closure delegate)


addPostNodeCompletionDelegate

public Closure addPostNodeCompletionDelegate(Closure delegate)


addPreInstantiateDelegate

public Closure addPreInstantiateDelegate(Closure delegate)


autoRegisterNodes

public void autoRegisterNodes()
Ask the nodes to be registered


build

public java.lang.Object build(java.lang.Class viewClass)


build

public java.lang.Object build(Script script)


build

public java.lang.Object build(java.lang.String script, GroovyClassLoader loader)


checkExplicitMethod

protected boolean checkExplicitMethod(java.lang.String methodName, java.lang.Object args, Reference result)


checkValueIsNull

public static void checkValueIsNull(java.lang.Object value, java.lang.Object name)
Throws an exception if value is null.
Parameters:
value - the node's value
name - the node's name


checkValueIsType

public static boolean checkValueIsType(java.lang.Object value, java.lang.Object name, java.lang.Class type)
Checks type of value against builder type
Parameters:
value - the node's value
name - the node's name
type - a Class that may be assignable to the value's class
Returns:
true if type is assignable to the value's class, false if value is null.


checkValueIsTypeNotString

public static boolean checkValueIsTypeNotString(java.lang.Object value, java.lang.Object name, java.lang.Class type)
Checks values against factory's type
Parameters:
value - the node's value
name - the node's name
type - a Class that may be assignable to the value's class
Returns:
Returns true if type is assignable to the value's class, false if value is null or a String.


createNode

protected java.lang.Object createNode(java.lang.Object name, java.util.Map attributes, java.lang.Object value)


dispathNodeCall

protected java.lang.Object dispathNodeCall(java.lang.Object name, java.lang.Object args)


dispose

public void dispose()


getAttributeDelegates

public java.util.List getAttributeDelegates()


getChildBuilder

public FactoryBuilderSupport getChildBuilder()


getContext

public java.util.Map getContext()


getContextAttribute

public java.lang.Object getContextAttribute(java.lang.String key)


getContexts

protected java.util.LinkedList getContexts()
Restores the state of the current builder to the same state as an older build. Caution, this will destroy rather than merge the current build context if there is any,
Parameters:
data - the data retrieved from a compatible getContinuationData call


getContinuationData

protected java.util.Map getContinuationData()


getCurrent

public java.lang.Object getCurrent()
Returns:
the current node being built.


getCurrentBuilder

public FactoryBuilderSupport getCurrentBuilder()
Returns:
the node of the parent of the current node.


getCurrentFactory

public Factory getCurrentFactory()
Returns:
the factory of the parent of the current node.


getCurrentName

public java.lang.String getCurrentName()
Returns:
the builder that built the current node.


getExplicitMethods

public java.util.Map getExplicitMethods()
Returns:
the explicit properties map (Unmodifiable Map).


getExplicitProperties

public java.util.Map getExplicitProperties()
Returns:
the factory map (Unmodifiable Map).


getFactories

public java.util.Map getFactories()
Returns:
the factory map (Unmodifiable Map).


getLocalExplicitMethods

public java.util.Map getLocalExplicitMethods()
Returns:
the explicit properties map (Unmodifiable Map).


getLocalExplicitProperties

public java.util.Map getLocalExplicitProperties()


getLocalFactories

public java.util.Map getLocalFactories()
Returns:
the explicit methods map (Unmodifiable Map).


getMethodMissingDelegate

public Closure getMethodMissingDelegate()


getName

public java.lang.Object getName(java.lang.String methodName)
A hook to allow names to be converted into some other object such as a QName in XML or ObjectName in JMX.
Parameters:
methodName - the name of the desired method
Returns:
the object representing the name


getNameMappingClosure

public Closure getNameMappingClosure()


getParentContext

public java.util.Map getParentContext()
Returns:
the name of the parent of the current node.


getParentFactory

public Factory getParentFactory()
Returns:
the context of the parent of the current node.


getParentName

public java.lang.String getParentName()


getParentNode

public java.lang.Object getParentNode()
Returns:
the factory of the parent of the current node.


getPostInstantiateDelegates

public java.util.List getPostInstantiateDelegates()


getPostNodeCompletionDelegates

public java.util.List getPostNodeCompletionDelegates()


getPreInstantiateDelegates

public java.util.List getPreInstantiateDelegates()


getProperty

public java.lang.Object getProperty(java.lang.String property)


getPropertyMissingDelegate

public Closure getPropertyMissingDelegate()


getProxyBuilder

protected FactoryBuilderSupport getProxyBuilder()


getRegistrationGroupItems

public java.util.Set getRegistrationGroupItems(java.lang.String group)


getRegistrationGroups

public java.util.Set getRegistrationGroups()


getVariable

public java.lang.Object getVariable(java.lang.String name)
Parameters:
name - the name of the variable to lookup
Returns:
the variable value


getVariables

public java.util.Map getVariables()


handleNodeAttributes

protected void handleNodeAttributes(java.lang.Object node, java.util.Map attributes)


invokeMethod

public java.lang.Object invokeMethod(java.lang.String methodName)
Convenience method when no arguments are required
Parameters:
methodName - the name of the method to invoke
Returns:
the result of the call


invokeMethod

public java.lang.Object invokeMethod(java.lang.String methodName, java.lang.Object args)


newContext

protected void newContext()
A hook to allow nodes to be processed once they have had all of their children applied.
Parameters:
node - the current node being processed
parent - the parent of the node being processed


nodeCompleted

protected void nodeCompleted(java.lang.Object parent, java.lang.Object node)


popContext

protected java.util.Map popContext()


postInstantiate

protected void postInstantiate(java.lang.Object name, java.util.Map attributes, java.lang.Object node)


postNodeCompletion

protected java.lang.Object postNodeCompletion(java.lang.Object parent, java.lang.Object node)
A hook before the factory creates the node.
It will call any registered preInstantiateDelegates, if you override this method be sure to call this impl somewhere in your code.
Parameters:
name - the name of the node
attributes - the attributes of the node
value - the value argument(s) of the node


preInstantiate

protected void preInstantiate(java.lang.Object name, java.util.Map attributes, java.lang.Object value)
A strategy method to allow derived builders to use builder-trees and switch in different kinds of builders. This method should call the setDelegate() method on the closure which by default passes in this but if node is-a builder we could pass that in instead (or do something wacky too)
Parameters:
closure - the closure on which to call setDelegate()
node - the node value that we've just created, which could be a builder


registerBeanFactory

public void registerBeanFactory(java.lang.String theName, java.lang.Class beanClass)


registerBeanFactory

public void registerBeanFactory(java.lang.String theName, java.lang.String groupName, java.lang.Class beanClass)


registerExplicitMethod

public void registerExplicitMethod(java.lang.String name, Closure closure)


registerExplicitMethod

public void registerExplicitMethod(java.lang.String name, java.lang.String groupName, Closure closure)


registerExplicitProperty

public void registerExplicitProperty(java.lang.String name, Closure getter, Closure setter)


registerExplicitProperty

public void registerExplicitProperty(java.lang.String name, java.lang.String groupName, Closure getter, Closure setter)


registerFactory

public void registerFactory(java.lang.String name, Factory factory)
Registers a factory for a node name.
Parameters:
name - the name of the node
factory - the factory to return the values


registerFactory

public void registerFactory(java.lang.String name, java.lang.String groupName, Factory factory)


removeAttributeDelegate

public void removeAttributeDelegate(Closure attrDelegate)


removePostInstantiateDelegate

public void removePostInstantiateDelegate(Closure delegate)


removePostNodeCompletionDelegate

public void removePostNodeCompletionDelegate(Closure delegate)


removePreInstantiateDelegate

public void removePreInstantiateDelegate(Closure delegate)


reset

protected void reset()


resolveExplicitMethod

*/
protected Closure resolveExplicitMethod(java.lang.String methodName, java.lang.Object args)


resolveExplicitProperty

protected Closure[] resolveExplicitProperty(java.lang.String propertyName)


resolveFactory

protected Factory resolveFactory(java.lang.Object name, java.util.Map attributes, java.lang.Object value)
This is a hook for subclasses to plugin a custom strategy for mapping names to factories.
Parameters:
name - the name of the factory
attributes - the attributes from the node
value - value arguments from te node
Returns:
the Factory associated with name.


restoreFromContinuationData

protected void restoreFromContinuationData(java.util.Map data)


setClosureDelegate

* @param parent the object from the parent node
protected void setClosureDelegate(Closure closure, java.lang.Object node)


setMethodMissingDelegate

public void setMethodMissingDelegate(Closure delegate)


setNameMappingClosure

public void setNameMappingClosure(Closure nameMappingClosure)


setNodeAttributes

protected void setNodeAttributes(java.lang.Object node, java.util.Map attributes)


setParent

protected void setParent(java.lang.Object parent, java.lang.Object child)
Returns:
the stack of available contexts.


setProperty

public void setProperty(java.lang.String property, java.lang.Object newValue)
Overloaded to make variables appear as bean properties or via the subscript operator


setPropertyMissingDelegate

public void setPropertyMissingDelegate(Closure delegate)
Returns:
the context of the current node.


setProxyBuilder

protected void setProxyBuilder(FactoryBuilderSupport proxyBuilder)
Assigns any existing properties to the node.
It will call attributeDelegates before passing control to the factory that built the node.
Parameters:
node - the object returned by tne node factory
attributes - the attributes for the node


setVariable

public void setVariable(java.lang.String name, java.lang.Object value)


withBuilder

public java.lang.Object withBuilder(FactoryBuilderSupport builder, Closure closure)


withBuilder

public java.lang.Object withBuilder(FactoryBuilderSupport builder, java.lang.String name, Closure closure)
Switches the builder's proxyBuilder during the execution of a closure.
This is useful to temporary change the building context to another builder without the need for a contrived setup. It will also take care of restoring the previous proxyBuilder when the execution finishes, even if an exception was thrown from inside the closure. Additionally it will use the closure's result as the value for the node identified by 'name'.
throws:
RuntimeException - any exception the closure might have thrown during execution.
Parameters:
builder - the temporary builder to switch to as proxyBuilder.
name - the node to build on the 'parent' builder.
closure - the closure to be executed under the temporary builder.
Returns:
a node that responds to value of name with the closure's result as its value.


withBuilder

public java.lang.Object withBuilder(java.util.Map attributes, FactoryBuilderSupport builder, java.lang.String name, Closure closure)


 

Groovy Documentation