org.jbpm.wire.descriptor
Class ObjectDescriptor

java.lang.Object
  extended by org.jbpm.util.DefaultObservable
      extended by org.jbpm.wire.descriptor.AbstractDescriptor
          extended by org.jbpm.wire.descriptor.ObjectDescriptor
All Implemented Interfaces:
java.io.Serializable, Observable, Descriptor

public class ObjectDescriptor
extends AbstractDescriptor
implements Descriptor

This Descriptor creates and initializes an object. Objects can be instantiated from a constructor or from a method invocation.

The way to create an object is specified one of these methods (see creating objects):

Only one of these methods can be used.

Creating objects

Creating object from a constructor

This method is used when getClassName()!=null && getMethodName()==null.

The construct(WireContext) method creates a new object from a constructor matching the given arguments (specified with setArgDescriptors(List)).

Creating an object from a method invocation

The name of the method to call is specified by the method attribute.

The object returned by construct(WireContext) is the object returned by the method invocation.

Initializing Objects

Auto Wiring

If the auto wiring is enabled for the object (isAutoWireEnabled()==true), the WireContext will try to look for objects with the same name as the fields in the class. If it finds an object with that name, and if it is assignable to the field's type, it is automatically injected, without the need for explicit FieldOperation that specifies the injection in the wiring xml.

If the auto wiring is enabled and the WireContext finds an object with the name of a field, but not assignable to this field, a warning message is generated.

Auto-wiring is disabled by default.

Operations

Field injection or property injection are done after the auto-wiring. For more information, see Operation.

If a field was injected by auto-wiring, its value can be overridden by specifying a FieldOperation or PropertyOperation operation.

Author:
Tom Baeyens, Guillaume Porcher (documentation)
See Also:
Serialized Form

Field Summary
 
Fields inherited from class org.jbpm.wire.descriptor.AbstractDescriptor
dbid, dbversion, init, INIT_EAGER, INIT_IMMEDIATE, INIT_LAZY, INIT_REQUIRED, name, version
 
Fields inherited from class org.jbpm.util.DefaultObservable
listeners
 
Fields inherited from interface org.jbpm.wire.Descriptor
EVENT_CONSTRUCTED, EVENT_CONSTRUCTING, EVENT_INITIALIZING, EVENT_REMOVE, EVENT_SET
 
Constructor Summary
ObjectDescriptor()
           
 
Method Summary
 void addArgDescriptor(ArgDescriptor argDescriptor)
          Adds a argument descriptor to the list of arguments descriptor to used when invoking the specified method.
 void addInjection(java.lang.String fieldName, Descriptor descriptor)
          add an injection based on a descriptor
 void addOperation(Operation operation)
          Adds an operation to perform during initialization.
 void addTypedInjection(java.lang.String fieldName, java.lang.Class<?> type)
          convenience method to add a type based field injection
protected  void autoWire(java.lang.Object object, WireContext wireContext)
          Auto wire object present in the context and the specified object's fields.
 java.lang.Object construct(WireContext wireContext)
          This method constructs a new Object from the ObjectDefinition.
 java.util.List<ArgDescriptor> getArgDescriptors()
          Gets the list of descriptors to use to create method arguments.
static java.lang.Object[] getArgs(WireContext wireContext, java.util.List<ArgDescriptor> argDescriptors)
          Creates a list of arguments (objects) from a list of argument descriptors.
 java.lang.String getClassName()
          Gets the class name of the object to create.
 Descriptor getFactoryDescriptor()
          Gets the Descriptor from which the object should be created.
 java.lang.String getFactoryObjectName()
          Gets the name of the object to get from the WireContext.
 java.lang.String getMethodName()
          Gets the name of the method to invoke.
 java.util.List<Operation> getOperations()
          Gets the list of operations to perform during initialization.
 java.lang.Class<?> getType(WireDefinition wireDefinition)
          the type of the produced object or null if that is not available
 void initialize(java.lang.Object object, WireContext wireContext)
          Initializes the specified object.
 boolean isAutoWireEnabled()
          Checks if auto-wiring is enabled
 void setArgDescriptors(java.util.List<ArgDescriptor> argDescriptors)
          Sets the list of descriptors to use to create method arguments.
 void setAutoWireEnabled(boolean isAutoWireEnabled)
          Enables/Disables auto wiring mode.
 void setClassName(java.lang.String className)
          Sets class name of the object to create.
 void setFactoryDescriptor(Descriptor factoryDescriptor)
          Sets the Descriptor from which the object should be created.
 void setFactoryObjectName(java.lang.String factoryObjectName)
          Sets name of the object to get from the WireContext.
 void setMethodName(java.lang.String methodName)
          Sets the name of the method to invoke.
 void setOperations(java.util.List<Operation> operations)
          Sets the list of operations to perform during initialization.
 
Methods inherited from class org.jbpm.wire.descriptor.AbstractDescriptor
getDbid, getName, isDelayable, isEagerInit, setInit, setName
 
Methods inherited from class org.jbpm.util.DefaultObservable
addListener, addListener, addListener, fire, fire, removeListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.jbpm.wire.Descriptor
getName, isDelayable, isEagerInit
 
Methods inherited from interface org.jbpm.util.Observable
addListener, addListener, addListener, fire, fire, removeListener
 

Constructor Detail

ObjectDescriptor

public ObjectDescriptor()
Method Detail

construct

public java.lang.Object construct(WireContext wireContext)
This method constructs a new Object from the ObjectDefinition. This object will be created from a class constructor or from a method invocation.

Specified by:
construct in interface Descriptor
Parameters:
wireContext - WireContext in which the object is created. This is also the WireContext where the object will search for other object that may be needed during the initialization phase.
Returns:
the constructed object.
Throws:
WireException - one of the following exception occurred:
  • if the object cannot be created (unable to load the specified class or no matching constructor found)
  • if the invocation of the specified method failed
See Also:
ObjectDescriptor

initialize

public void initialize(java.lang.Object object,
                       WireContext wireContext)
Initializes the specified object. If auto-wiring was set to true, auto-wiring is performed (see autoWire(Object, WireContext)). Fields and properties injections are then performed.

Specified by:
initialize in interface Descriptor
Overrides:
initialize in class AbstractDescriptor
Parameters:
object - object to initialize.
wireContext - the context in which the object will be initialized.

getType

public java.lang.Class<?> getType(WireDefinition wireDefinition)
Description copied from interface: Descriptor
the type of the produced object or null if that is not available

Specified by:
getType in interface Descriptor
Overrides:
getType in class AbstractDescriptor

autoWire

protected void autoWire(java.lang.Object object,
                        WireContext wireContext)
Auto wire object present in the context and the specified object's fields.

Parameters:
object - object on which auto-wiring is performed.
wireContext - context in which the wiring objects are searched.

getArgs

public static java.lang.Object[] getArgs(WireContext wireContext,
                                         java.util.List<ArgDescriptor> argDescriptors)
                                  throws java.lang.Exception
Creates a list of arguments (objects) from a list of argument descriptors.

Parameters:
wireContext - context used to create objects.
argDescriptors - list of argument descriptors.
Returns:
a list of object created from the descriptors.
Throws:
java.lang.Exception

addArgDescriptor

public void addArgDescriptor(ArgDescriptor argDescriptor)
Adds a argument descriptor to the list of arguments descriptor to used when invoking the specified method.

Parameters:
argDescriptor - argument descriptor to add.

addOperation

public void addOperation(Operation operation)
Adds an operation to perform during initialization.

Parameters:
operation - operation to add.

addTypedInjection

public void addTypedInjection(java.lang.String fieldName,
                              java.lang.Class<?> type)
convenience method to add a type based field injection


addInjection

public void addInjection(java.lang.String fieldName,
                         Descriptor descriptor)
add an injection based on a descriptor


getClassName

public java.lang.String getClassName()
Gets the class name of the object to create. This name is defined only when creating objects from a constructor or when invoking static methods.

Returns:
the name of the class of the object to create.

setClassName

public void setClassName(java.lang.String className)
Sets class name of the object to create. This name is defined only when creating objects from a constructor or when invoking static methods. If this name is set, the factoryObjectName and factoryDescriptor should not be set.

Parameters:
className - name of the class to use.
See Also:
setFactoryDescriptor(Descriptor), setFactoryObjectName(String)

getArgDescriptors

public java.util.List<ArgDescriptor> getArgDescriptors()
Gets the list of descriptors to use to create method arguments.

Returns:
list of descriptors to use to create method arguments.

setArgDescriptors

public void setArgDescriptors(java.util.List<ArgDescriptor> argDescriptors)
Sets the list of descriptors to use to create method arguments.

Parameters:
argDescriptors - list of descriptors to use to create method arguments.

getOperations

public java.util.List<Operation> getOperations()
Gets the list of operations to perform during initialization.

Returns:
list of operations to perform during initialization.

setOperations

public void setOperations(java.util.List<Operation> operations)
Sets the list of operations to perform during initialization.

Parameters:
operations - list of operations to perform during initialization.

getFactoryDescriptor

public Descriptor getFactoryDescriptor()
Gets the Descriptor from which the object should be created.

Returns:
the Descriptor from which the object should be created.

setFactoryDescriptor

public void setFactoryDescriptor(Descriptor factoryDescriptor)
Sets the Descriptor from which the object should be created. If this Descriptor is set, the className and factoryObjectName should not be set.

Parameters:
factoryDescriptor - the Descriptor from which the object should be created.
See Also:
setClassName(String), setFactoryObjectName(String)

getFactoryObjectName

public java.lang.String getFactoryObjectName()
Gets the name of the object to get from the WireContext.

Returns:
name of the object to get from the WireContext.

setFactoryObjectName

public void setFactoryObjectName(java.lang.String factoryObjectName)
Sets name of the object to get from the WireContext. If this name is set, the className and factoryDescriptor should not be set.

Parameters:
factoryObjectName - name of the object to get from the WireContext.
See Also:
setClassName(String), setFactoryDescriptor(Descriptor)

getMethodName

public java.lang.String getMethodName()
Gets the name of the method to invoke.

Returns:
name of the method to invoke.

setMethodName

public void setMethodName(java.lang.String methodName)
Sets the name of the method to invoke.

Parameters:
methodName - name of the method to invoke.

isAutoWireEnabled

public boolean isAutoWireEnabled()
Checks if auto-wiring is enabled

Returns:
true if auto-wiring is enabled.

setAutoWireEnabled

public void setAutoWireEnabled(boolean isAutoWireEnabled)
Enables/Disables auto wiring mode.

Parameters:
isAutoWireEnabled - true to enable auto-wiring.