eu.beesoft.gaia.util
Class ObjectBuilder<T>

java.lang.Object
  extended by eu.beesoft.gaia.util.ObjectBuilder<T>
Type Parameters:
T - class of object created by this builder

public abstract class ObjectBuilder<T>
extends java.lang.Object

An abstract superclass of all builders.

A builder serves as a factory for the creation and initialization of the created object. The building process is managed by ObjectBuilderFactory. This implementation does not dictate relationships between builder, created object and XML element - it is your responsibility.

Each builder is managed by ObjectBuilderFactory instance that created it. ObjectBuilderFactory loads a XML description and for each element creates a new appropriate builder. This is done in these steps:

  1. this builder's instance is created
  2. references to instance of ObjectBuilderFactory and builder's parent are stored to this builder
  3. attributes from XML element are stored to this builder as properties
  4. builder invokes method createObject() or createObject(String), if the class property was found

When ObjectBuilderFactory processed all XML elements and for each executed steps described above, it walks each builder again and invokes method initObjectProperties() on it. Builder for each property invokes method initObjectProperty(String, String). This method in current implementation searches for initialization method for given property and invokes it.

When all builders were requested to initiaze their properties, the ObjectBuilderFactory notifies each builder about its hierarchy position. On each builder with children is invoked method addChild(ObjectBuilder). This is done from bottom to top in the builder hierarchy, so parent is notified about its children when each its child was notified about its children.

After that is builder and its object fully initialized and object can be used in application.


Constructor Summary
ObjectBuilder()
           
 
Method Summary
protected  void addChild(ObjectBuilder<?> builder)
          Invoked from ObjectBuilderFactory when adding child to this builder.
protected abstract  T createObject()
          Creates object for this builder.
protected  T createObject(java.lang.String className)
          Creates object for this builder.
 ObjectBuilderFactory getFactory()
          Returns factory which created this builder.
 java.util.List<ObjectBuilder<?>> getChildren()
          Returns list of builder children.
 java.lang.String getId()
          Returns id of this builder.
 T getObject()
          Returns object created by this builder.
 ObjectBuilder<?> getParent()
          Returns parent of this builder.
 java.util.Map<java.lang.String,java.lang.String> getProperties()
          Returns properties for this this builder as map {name : value}.
 java.lang.String getProperty(java.lang.String name)
          Returns a value of property with given name.
protected  void initClass(java.lang.String value)
           
protected  void initId(java.lang.String value)
          Initializes property ID for this value.
 void initObjectProperties()
          Initializes object from builder properties.
protected  void initObjectProperty(java.lang.String name, java.lang.String value)
          Initializes this builder's object property with given name and value.
protected  boolean parseBoolean(java.lang.String value)
          Helper method to convert given String value to boolean.
protected  java.lang.Class<?> parseClass(java.lang.String value)
          Helper method to get class with given name.
protected  java.lang.Object parseConstant(java.lang.String name, java.lang.Class<?> owner)
          Helper method to get value of a constant (static field) with given name from given class.
protected  java.lang.Object parseConstructor(java.lang.String value, java.lang.String defaultPackage)
          Helper method to get object for given value.
protected  double parseDouble(java.lang.String value)
          Helper method to convert given String value to double.
protected  java.lang.Object parseInstance(java.lang.String value)
          Helper method to get object for given name.
protected  java.lang.Object parseInstance(java.lang.String value, java.lang.Class<?> instanceOf)
          Helper method to get object for given name.
protected  int parseInt(java.lang.String value)
          Helper method to convert given String value to int.
protected  int parseInt(java.util.StringTokenizer tokenizer)
          Helper method to convert given String value to int.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectBuilder

public ObjectBuilder()
Method Detail

getFactory

public ObjectBuilderFactory getFactory()
Returns factory which created this builder.

Returns:
factory of this builder

getObject

public T getObject()
Returns object created by this builder.

Returns:
created object

createObject

protected abstract T createObject()
Creates object for this builder. This method is invoked only once, when properties for builder and the builder's parent (but not children) are set.

Returns:
created object

createObject

protected T createObject(java.lang.String className)
Creates object for this builder. This method is invoked only once, when properties for builder and the builder's parent (but not children) are set.

Parameters:
className - - name of the class to instantiate object
Returns:
created object

getProperties

public java.util.Map<java.lang.String,java.lang.String> getProperties()
Returns properties for this this builder as map {name : value}.

Returns:
properties of this builder

getProperty

public java.lang.String getProperty(java.lang.String name)
Returns a value of property with given name. The property name is case insensitive.

Parameters:
name - - name of the property
Returns:
value of the named property

initObjectProperties

public void initObjectProperties()
Initializes object from builder properties. This method is executed only once, the next calls are ignored. The initialization is performed in these steps:
  1. property with name "as" is searched. If found, the referenced builder is initialized and properties from it are copied to this builder
  2. property with name "id" is searched. If found, the method initObjectProperty(String, String) is invoked
  3. the properties of this builder are walked. For each property (except "as" and "id") is invoked method initObjectProperty(String, String)


initObjectProperty

protected void initObjectProperty(java.lang.String name,
                                  java.lang.String value)
Initializes this builder's object property with given name and value. Method is invoked from initObjectProperties() method. This implementation searches for initialization method for the property and then invokes it. If the property name is "abc", it searches for the method "initAbc (String)". Override this method if you want to change this behavior.

Parameters:
name - - name of the initialized property
value - - value of the property
Throws:
java.lang.RuntimeException - if there is no initialization method for requested property found

initId

protected void initId(java.lang.String value)
Initializes property ID for this value. Sets property to instance variable.

Parameters:
value - - id to set

getId

public java.lang.String getId()
Returns id of this builder.

Returns:
identifier of this builder

initClass

protected void initClass(java.lang.String value)

getParent

public ObjectBuilder<?> getParent()
Returns parent of this builder.

Returns:
parent of this builder

addChild

protected void addChild(ObjectBuilder<?> builder)
Invoked from ObjectBuilderFactory when adding child to this builder. In this implementation does nothing.

Parameters:
builder - - child to add

getChildren

public java.util.List<ObjectBuilder<?>> getChildren()
Returns list of builder children.

Returns:
builder children

parseBoolean

protected boolean parseBoolean(java.lang.String value)
Helper method to convert given String value to boolean.

Parameters:
value - - String value to be converted; if it is null, returns false
Returns:
boolean value of given String

parseInt

protected int parseInt(java.lang.String value)
Helper method to convert given String value to int.

Parameters:
value - - String value to be converted
Returns:
int value of given String
Throws:
java.lang.RuntimeException - if given value cannot be converted to int

parseInt

protected int parseInt(java.util.StringTokenizer tokenizer)
Helper method to convert given String value to int.

Parameters:
tokenizer - - supplier of the converted String
Returns:
int value of the next token
Throws:
java.lang.RuntimeException - if next token cannot be converted to int

parseDouble

protected double parseDouble(java.lang.String value)
Helper method to convert given String value to double.

Parameters:
value - - String value to be converted
Returns:
double value of given String
Throws:
java.lang.RuntimeException - if given value cannot be converted to double

parseConstant

protected java.lang.Object parseConstant(java.lang.String name,
                                         java.lang.Class<?> owner)
Helper method to get value of a constant (static field) with given name from given class.

Parameters:
name - - name of the constant
owner - - class in which is the constant with given name
Returns:
value of constant with given name
Throws:
java.lang.RuntimeException - if cannot find constant with given name

parseClass

protected java.lang.Class<?> parseClass(java.lang.String value)
Helper method to get class with given name.

Parameters:
value - - name of the class
Returns:
class with given name
Throws:
java.lang.RuntimeException - if cannot find class with given name

parseInstance

protected java.lang.Object parseInstance(java.lang.String value)
Helper method to get object for given name. This method works this way:

Parameters:
value - - name for object lookup
Returns:
a found object

parseInstance

protected java.lang.Object parseInstance(java.lang.String value,
                                         java.lang.Class<?> instanceOf)
Helper method to get object for given name. This method calls parseInstance(String) to get requested object and then checks if returned object is instance of given class.

Parameters:
value - - name for object lookup
instanceOf - - class that is accessible from returned object
Returns:
a found object
Throws:
java.lang.RuntimeException - if found object is not an instance of given class

parseConstructor

protected java.lang.Object parseConstructor(java.lang.String value,
                                            java.lang.String defaultPackage)
Helper method to get object for given value. The value should be string as "new java.util.ArrayList (10)". If the value does not contain package (just the simple class name), the method uses given defaultPackage to get qualified class name. Arguments must be enclosed by parentheses. Possible values are:

Parameters:
value - - string to create object
defaultPackage - - name of a package if value does not contain qualified class name
Returns:
a created object
Throws:
java.lang.RuntimeException - if cannot create new object