Welcome!

ColdFusion Authors: Yakov Fain, Maureen O'Gara, Nancy Y. Nee, Tad Anderson, Daniel Kaar

Related Topics: Open Source, ColdFusion, Adobe Flex, AJAX & REA

Open Source: Article

Using Custom Type Marshallers in BlazeDS

Changing behavior of endpoints in BlazeDS

I'd like to use this opportunity and invite Flex developers living in Europe to attend our Advanced Flex Master Class in Brussels, Belgium on March 1 and 2, 2010.

In my previous post I pointed to the BlazeDS classes that need to be replaced in order to prevent ActionScript Number.NaN from turning into Long or Integer zeroes on the MessageBroker side.

The recommendation boiled down to re-jaring flex-messaging-core.jar or placing the modified classes somewhere earlier in the application server's classpath.

If neither option is allowed, you may configure your endpoint with the custom type marshaller, like the one below:

<channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
<endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf"
class="flex.messaging.endpoints.AMFEndpoint">
</endpoint>
<properties>   
<serialization>
<type-marshaller>com.farata.messaging.io.CustomTypeMarshaller</type-marshaller>
</serialization>
</properties>   
</channel-definition>

In BlazeDS 4 you will base your type marshaller on flex.messaging.io.ASTranslator, in BlazeDS 3 they already have one level of extension called Java15TypeMarshaller, so build on top of that one:
package com.farata.messaging.io;

import com.farata.messaging.io.amf.translator.decoder.NumberDecoder;
import flex.messaging.io.amf.translator.decoder.DecoderFactory;


// For BlazeDS 4 you should extend flex.messaging.io.ASTranslator
public class CustomTypeMarshaller extends
flex.messaging.io.Java15TypeMarshaller {

private static final NumberDecoder numberDecoder = new NumberDecoder();

public Object convert(Object source, Class desiredClass) {
if ( DecoderFactory.isNumber(desiredClass)){
return numberDecoder.decodeObject(source, desiredClass);
}else{
return super.convert(source, desiredClass);
}
}
}

This route does not require neither server-level deployment nor modification of the flex-messaging-core.jar.

Please note that in my example I repackaged NumberDecoder as <strong>com.farata</strong>.messaging.io.amf.translator.decoder.NumberDecoder

I'd like to use this opportunity and invite Flex developers living in Europe to attend our Advanced Flex Master Class in Brussels, Belgium on March 1 and 2, 2010.

More Stories By Victor Rasputnis

Dr. Victor Rasputnis is a Managing Principal of Farata Systems. He's responsible for providing architectural design, implementation management and mentoring to companies migrating to XML Internet technologies. He holds a PhD in computer science from the Moscow Institute of Robotics. You can reach him at [email protected]

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.