Overview

Translators are classes which are deployed within the SSB to transform data from one format to another. This page describes how to create and deploy translators.

To define a translator, you need to provide three things:

  • The message type of the data to translate from.
  • The message type of the data to translate to.
  • A transformer which transforms data from the input type to the output type.

Defining a Transformer

The first step is to define a transformer class. Transformers indicate the class of the input and output type and provide a method which transforms the input data to the output data. Here is a simple transformer, which translates strings into Boolean values. The string “true” is converted to Boolean.TRUE and all other values are converted to Boolean.FALSE.

/**
 * Provides a sample transformer which transforms strings to Boolean values.  The string "true" is transformed to 
 * {@link Boolean#TRUE} and all other values are transformed to {@link Boolean#FALSE}.
 */
public class StringToBooleanTransformer implements PayloadTransformer<String, Boolean>
{
    /**
     * {@inheritDoc}
     */
    public Class<String> getPayloadType()
    {
        return String.class;
    }
 
    /**
     * {@inheritDoc}
     */
    public Class<Boolean> getReturnType()
    {
        return Boolean.class;
    }
 
    /**
     * {@inheritDoc}
     */
    public Boolean transform(final String value) throws ScienceServiceBusException
    {
        Validate.notNull(value, "null string value");
        
        return value.equals("true");
    }
 
}

Deploying the Transformer

To deploy a translator you need to do the following:

  • Make your transformer class available to SSB.
  • Register the transformer in the Mule configuration file.
  • Add your translator to the list of available translators

To register a transformer in the Mule configuration file, you will need to copy a bit of boilerplate XML. For instance, to register the transformer defined above, you will need to include something like:

    <!-- insert a unique name here -->
    <transformer name="stringToBoolean" className="org.ssb.server.util.TypedTransformer">
        <properties>
            <spring-property name="payloadTransformer">
                <!-- insert your class name here -->
                <bean class="org.ssb.sample.StringToBoolean"/>
            </spring-property>
        </properties>
    </transformer>

You then need to add the translator to the list of translators the SSB knows about. You do this by editing the Mule configuration file.

First define a bean for your message type. For example, the bean for the “agents changed JSON” message type looks like this:

    <bean id="org.ssb.server.broker.BrokerClientMessageTypes.AGENTS_CHANGED_JSON"
        class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean"/>

Then find the bean called “translatorRegistry” and add an entry for your translator. The entry includes the source and destination message types and the transformer to use. For example, the entry for translating “agents changed” messages to the JSON version looks like this:

    <bean class="org.ssb.server.translatorregistry.TranslatorInformation">
	<constructor-arg>
	    <ref local="org.ssb.server.broker.BrokerClientMessageTypes.AGENTS_CHANGED"/>
    	</constructor-arg>
    	<constructor-arg>
    	    <ref local="org.ssb.server.broker.BrokerClientMessageTypes.AGENTS_CHANGED_JSON"/>
	</constructor-arg>
	<constructor-arg>
	    <value>agentsChangedMessageToJSON</value>
	</constructor-arg>					
    </bean>
 
translatoruserguide.txt · Last modified: 2009/01/09 03:40 by etellman
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki