org.mule.transformer
Class AbstractTransformer

java.lang.Object
  extended by org.mule.transformer.AbstractTransformer
All Implemented Interfaces:
MuleContextAware, Initialisable, NamedObject, BaseTransformer, Transformer
Direct Known Subclasses:
AbstractCompressionTransformer, AbstractDiscoverableTransformer, AbstractEncryptionTransformer, AbstractEventTransformer, AbstractMessageAwareTransformer, AddBookResponse, Base64Decoder, Base64Encoder, BeanToMap, ByteArrayToHexString, ByteArrayToSerializable, ChatStringToString, CreditProfileXmlToCreditProfile, EmailMessageToString, ErrorMessageToException, ErrorMessageToExceptionBean, ExceptionBeanToErrorMessage, ExceptionToString, GetBeanProperty, HexStringToByteArray, HttpClientMethodResponseToObject, HttpRequestToNameString, HttpResponseToString, IPToSender, JXPathExtractor, LoanQuoteRequestToCreditProfileArgs, MapLookup, MapToBean, MimeMessageToRfc822ByteArray, NameStringToChatString, NoActionTransformer, ObjectArrayToString, ObjectToOutputHandler, ObjectToRemoteInvocationResultTransformer, ObjectToRemoteInvocationTransformer, ObjectToString, OrderToEmailTransformer, Rfc822ByteArraytoMimeMessage, SerializableToByteArray, ServletRequestToOutputHandler, SimpleMathTransformer, StdinToNameString, StringAppendTransformer, StringToNameString, StringToNumber, StringToObjectArray, ValidateResponse, XmlEntityDecoder, XmlEntityEncoder, XmlPrettyPrinter, XPathExtractor

public abstract class AbstractTransformer
extends Object
implements Transformer

AbstractTransformer is a base class for all transformers. Transformations transform one object into another.


Field Summary
protected static int DEFAULT_TRUNCATE_LENGTH
           
protected  ImmutableEndpoint endpoint
          The endpoint that this transformer instance is configured on
protected  Log logger
           
static DataType MULE_MESSAGE_ADAPTER_DATA_TYPE
           
static DataType MULE_MESSAGE_DATA_TYPE
           
protected  MuleContext muleContext
           
protected  String name
          The name that identifies this transformer.
protected  DataType returnType
          The return type that will be returned by the transform(java.lang.Object) method is called
protected  List<DataType<?>> sourceTypes
          A list of supported Class types that the source payload passed into this transformer
 
Fields inherited from interface org.mule.api.lifecycle.Initialisable
PHASE_NAME
 
Constructor Summary
AbstractTransformer()
          default constructor required for discovery
 
Method Summary
protected  Object checkReturnClass(Object object)
           
protected abstract  Object doTransform(Object src, String encoding)
           
protected  String generateTransformerName()
           
protected  String getEncoding(Object src)
           
 ImmutableEndpoint getEndpoint()
          The endpoint that this transformer is attached to
 String getName()
          Gets the name of the object
 Class getReturnClass()
          Specifies the Java type of the result after this transformer has been executed.
 DataType getReturnDataType()
          Specifies the return type of the result after this transformer has been executed.
 List<DataType<?>> getSourceDataTypes()
          Returns an unmodifiable list of Source types registered on this transformer
 List<Class<?>> getSourceTypes()
          Returns an unmodifiable list of Source types registered on this transformer
 void initialise()
          Template method where deriving classes can do any initialisation after the properties have been set on this transformer
 boolean isAcceptNull()
          Does this transformer allow null input?
protected  boolean isConsumed(Class srcCls)
           
 boolean isIgnoreBadInput()
          By default, Mule will throw an exception if a transformer is invoked with a source object that is not compatible with the transformer.
 boolean isSourceDataTypeSupported(DataType dataType)
          Determines if a particular source class can be handled by this transformer
 boolean isSourceDataTypeSupported(DataType dataType, boolean exactMatch)
          Determines whether that data type passed in is supported by this transformer
 boolean isSourceTypeSupported(Class aClass)
          Determines if a particular source class can be handled by this transformer
 boolean isSourceTypeSupported(Class aClass, boolean exactMatch)
          Deprecated. use isSourceDataTypeSupported(org.mule.api.transformer.DataType, boolean)
protected  void registerSourceType(Class<?> aClass)
          Register a supported data type with this transformer.
protected  void registerSourceType(DataType dataType)
          Register a supported data type with this transformer.
 void setEndpoint(ImmutableEndpoint endpoint)
          Sets the endpoint associated with with this connector.
 void setIgnoreBadInput(boolean ignoreBadInput)
           
 void setMuleContext(MuleContext context)
           
 void setName(String string)
          Sets the name of the object
 void setReturnClass(Class newClass)
          Sets the expected return type for the transformed data.
 void setReturnDataType(DataType type)
          Sets the expected return type for the transformed data.
 String toString()
           
 Object transform(Object src)
          Thransforms the supplied data and returns the result
 Object transform(Object src, String encoding)
          Thransforms the supplied data and returns the result
protected  void unregisterSourceType(Class<?> aClass)
          Unregister a supported source type from this transformer
protected  void unregisterSourceType(DataType dataType)
          Unregister a supported source type from this transformer
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

MULE_MESSAGE_DATA_TYPE

public static final DataType MULE_MESSAGE_DATA_TYPE

MULE_MESSAGE_ADAPTER_DATA_TYPE

public static final DataType MULE_MESSAGE_ADAPTER_DATA_TYPE

DEFAULT_TRUNCATE_LENGTH

protected static final int DEFAULT_TRUNCATE_LENGTH
See Also:
Constant Field Values

muleContext

protected MuleContext muleContext

logger

protected final Log logger

returnType

protected DataType returnType
The return type that will be returned by the transform(java.lang.Object) method is called


name

protected String name
The name that identifies this transformer. If none is set the class name of the transformer is used


endpoint

protected ImmutableEndpoint endpoint
The endpoint that this transformer instance is configured on


sourceTypes

protected final List<DataType<?>> sourceTypes
A list of supported Class types that the source payload passed into this transformer

Constructor Detail

AbstractTransformer

public AbstractTransformer()
default constructor required for discovery

Method Detail

checkReturnClass

protected Object checkReturnClass(Object object)
                           throws TransformerException
Throws:
TransformerException

registerSourceType

protected void registerSourceType(Class<?> aClass)
Register a supported data type with this transformer. The will allow objects that match this data type to be transformed by this transformer.

Parameters:
aClass - the source type to allow

unregisterSourceType

protected void unregisterSourceType(Class<?> aClass)
Unregister a supported source type from this transformer

Parameters:
aClass - the type to remove

registerSourceType

protected void registerSourceType(DataType dataType)
Register a supported data type with this transformer. The will allow objects that match this data type to be transformed by this transformer.

Parameters:
dataType - the source type to allow

unregisterSourceType

protected void unregisterSourceType(DataType dataType)
Unregister a supported source type from this transformer

Parameters:
dataType - the type to remove

getName

public String getName()
Description copied from interface: NamedObject
Gets the name of the object

Specified by:
getName in interface NamedObject
Returns:
transformer name

setName

public void setName(String string)
Description copied from interface: NamedObject
Sets the name of the object

Specified by:
setName in interface NamedObject
Parameters:
string -

getReturnClass

public Class getReturnClass()
Description copied from interface: Transformer
Specifies the Java type of the result after this transformer has been executed. Mule will use this to validate the return type but also allow users to perform automatic transformations based on the source type of the object to transform and this return type.

Specified by:
getReturnClass in interface Transformer
Returns:
the excepted return type from this transformer

setReturnDataType

public void setReturnDataType(DataType type)
Description copied from interface: Transformer
Sets the expected return type for the transformed data. If the transformed data is not of this class type a TransformerException will be thrown.

This method superseeds Transformer.getReturnClass() because it allows Generics information to be associated with the return type of the transformer

Specified by:
setReturnDataType in interface Transformer
Parameters:
type - the expected return type for this transformer

getReturnDataType

public DataType getReturnDataType()
Description copied from interface: Transformer
Specifies the return type of the result after this transformer has been executed. Mule will use this to validate the return type but also allow users to perform automatic transformations based on the source type of the object to transform and this return type.

This method superseeds Transformer.getReturnClass() because it allows Generics information to be associated with the return type of the transformer

Specified by:
getReturnDataType in interface Transformer
Returns:
the excepted return type for this transformer

setReturnClass

public void setReturnClass(Class newClass)
Description copied from interface: Transformer
Sets the expected return type for the transformed data. If the transformed data is not of this class type a TransformerException will be thrown.

Specified by:
setReturnClass in interface Transformer
Parameters:
newClass - the expected return type class

isSourceTypeSupported

public boolean isSourceTypeSupported(Class aClass)
Description copied from interface: Transformer
Determines if a particular source class can be handled by this transformer

Specified by:
isSourceTypeSupported in interface Transformer
Parameters:
aClass - The class to check for compatability
Returns:
true if the transformer supports this type of class or false otherwise

isSourceDataTypeSupported

public boolean isSourceDataTypeSupported(DataType dataType)
Description copied from interface: Transformer
Determines if a particular source class can be handled by this transformer

Specified by:
isSourceDataTypeSupported in interface Transformer
Parameters:
dataType - The DataType to check for compatability
Returns:
true if the transformer supports this type of class or false otherwise

isSourceTypeSupported

@Deprecated
public boolean isSourceTypeSupported(Class aClass,
                                                boolean exactMatch)
Deprecated. use isSourceDataTypeSupported(org.mule.api.transformer.DataType, boolean)

Determines whether that data type passed in is supported by this transformer

Parameters:
aClass - the type to check against
exactMatch - if the source type on this transformer is open (can be anything) it will return true unless an exact match is requested using this flag
Returns:
true if the source type is supported by this transformer, false otherwise

isSourceDataTypeSupported

public boolean isSourceDataTypeSupported(DataType dataType,
                                         boolean exactMatch)
Determines whether that data type passed in is supported by this transformer

Parameters:
dataType - the type to check against
exactMatch - if set to true, this method will look for an exact match to the data type, if false it will look for a compatible data type.
Returns:
true if the source type is supported by this transformer, false otherwise

transform

public final Object transform(Object src)
                       throws TransformerException
Description copied from interface: Transformer
Thransforms the supplied data and returns the result

Specified by:
transform in interface Transformer
Parameters:
src - the data to transform
Returns:
the transformed data
Throws:
TransformerException - if a error occurs transforming the data or if the expected returnClass isn't the same as the transformed data

transform

public Object transform(Object src,
                        String encoding)
                 throws TransformerException
Description copied from interface: Transformer
Thransforms the supplied data and returns the result

Specified by:
transform in interface Transformer
Parameters:
src - the data to transform
encoding - the encoding to use by this transformer. many transformations will not need encoding unless dealing with text so you only need to use this method if yo wish to customize the encoding
Returns:
the transformed data
Throws:
TransformerException - if a error occurs transforming the data or if the expected returnClass isn't the same as the transformed data

getEncoding

protected String getEncoding(Object src)

isConsumed

protected boolean isConsumed(Class srcCls)

getEndpoint

public ImmutableEndpoint getEndpoint()
Description copied from interface: BaseTransformer
The endpoint that this transformer is attached to

Specified by:
getEndpoint in interface BaseTransformer
Returns:
the endpoint associated with the transformer

setEndpoint

public void setEndpoint(ImmutableEndpoint endpoint)
Description copied from interface: BaseTransformer
Sets the endpoint associated with with this connector. This should be idempotent, since endpoints do not guarantee it will only be called once.

Specified by:
setEndpoint in interface BaseTransformer
Parameters:
endpoint - sets the endpoint associated with the transfromer

doTransform

protected abstract Object doTransform(Object src,
                                      String encoding)
                               throws TransformerException
Throws:
TransformerException

initialise

public void initialise()
                throws InitialisationException
Template method where deriving classes can do any initialisation after the properties have been set on this transformer

Specified by:
initialise in interface Initialisable
Throws:
InitialisationException
RecoverableException - if an error occurs that can be recovered from

generateTransformerName

protected String generateTransformerName()

getSourceTypes

public List<Class<?>> getSourceTypes()
Description copied from interface: Transformer
Returns an unmodifiable list of Source types registered on this transformer

Specified by:
getSourceTypes in interface Transformer
Returns:
an unmodifiable list of Source types registered on this transformer

getSourceDataTypes

public List<DataType<?>> getSourceDataTypes()
Description copied from interface: Transformer
Returns an unmodifiable list of Source types registered on this transformer

Specified by:
getSourceDataTypes in interface Transformer
Returns:
an unmodifiable list of Source types registered on this transformer

isIgnoreBadInput

public boolean isIgnoreBadInput()
Description copied from interface: Transformer
By default, Mule will throw an exception if a transformer is invoked with a source object that is not compatible with the transformer. Since transformers are often chained, it is useful to be able to ignore a transformer in the chain and move to the next one.

Specified by:
isIgnoreBadInput in interface Transformer
Returns:
true if the transformer can be ignorred if the currnet source type is not supported, false if an exception should be throw due to an incompatible source type being passed in.

setIgnoreBadInput

public void setIgnoreBadInput(boolean ignoreBadInput)

toString

public String toString()
Overrides:
toString in class Object

isAcceptNull

public boolean isAcceptNull()
Description copied from interface: Transformer
Does this transformer allow null input?

Specified by:
isAcceptNull in interface Transformer
Returns:
true if this transformer can accept null input

setMuleContext

public void setMuleContext(MuleContext context)
Specified by:
setMuleContext in interface MuleContextAware


Copyright © 2003-2010 MuleSoft, Inc.. All Rights Reserved.