通过 BlazeDS 从 Java 到 Flex 的自定义编组 [英] Custom Marshalling from Java to Flex via BlazeDS

查看:18
本文介绍了通过 BlazeDS 从 Java 到 Flex 的自定义编组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的团队正在使用 BlazeDS 在基于 Spring 的服务器上构建一个概念验证 Flex 应用程序.

My team are putting together a proof-of-concept Flex application sitting on top of a Spring-based server using BlazeDS.

我们进行了大量日期计算,因此我们在整个代码和域模型中广泛使用了 Joda Time.

We do quite a lot of date calculations, so we use Joda Time extensively throughout the code and in our domain model.

我们现在正试图弄清楚如何在通过 BlazeDS 与 Flex 前端来回发送的 DTO 中继续使用 Joda Time.

We're now trying to figure out how we can continue to use Joda Time in our DTOs that are sent back-and-forth with the Flex frontend via BlazeDS.

我们的目标是在 Flex 端使用 Actionscript 3 数据类型 Date 并将该映射映射到我们使用 Joda 时间的 DateTimeLocalDate 和 LocalTime 类型.

Our goal is to use the Actionscript 3 data type Date on the Flex side and have that map to our use of Joda time's DateTime, LocalDate and LocalTime types on the Java side.

我们可以解决在使用插入到 BlazeDS 的自定义类型编组器调用 Java 时转换 Actionscript 3 的 Date 类型的问题,但这似乎只在 Flex->Java/BlazeDS 方向调用,并且不适用于 Java/BlazeDS->Flex 方向.

We can solve the problem of converting Actionscript 3's Date type when calling Java with a custom type marshaller plugged into BlazeDS, but this appears to only be invoked for the Flex->Java/BlazeDS direction and not for the Java/BlazeDS->Flex direction.

我现在正在研究 BlazeDS 的自定义 PropertyProxy 实现,但这看起来也不正确.

I'm now looking at custom PropertyProxy implementations for BlazeDS, but this doesn't look like the right thing either.

另一个想法是在我们的 Java DTO 上实现 Externalizable,但这似乎工作量太大,尤其是当我查看 BlazeDS 的竞争对手 GraniteDS 并显示在他们的文档中插入 Joda Time 支持时使用简单的类型转换器!

The other idea was to implement Externalizable on our Java DTOs, but this seems like too much work, especially when I look at the BlazeDS rival GraniteDS and that shows plugging in Joda Time support in their documentation with a simple type converter!

任何想法表示赞赏.

推荐答案

好的 - 我自己找到了答案.这涉及编写我自己的 AMF 端点类 + 相关的序列化类.我不得不说,http://flexblog.faratasystems.com 上的那些人是对 BlazeDS 进行黑客攻击的重要灵感来源.

OK - I've found the answer on my own. This involved writing my own AMF endpoint class + related serializing classes. I've gotta say that the guys over at http://flexblog.faratasystems.com have been a great source of inspiration on hacking BlazeDS.

这段代码真的应该被合并到 BlazeDS 本身或一些开源扩展项目中——它太基础了.

This code should really be incorporated into BlazeDS itself or some Open Source extension project - it's so basic.

    <channel-definition id="my-amf" class="mx.messaging.channels.AMFChannel">
        <endpoint url="http://{server.name}:{server.port}/{context.root}/messagebroker/amf" class="ch.hedgesphere.core.blazeds.endpoint.AMFEndpoint"/>

         <properties>
            <serialization>
                <type-marshaller>ch.hedgesphere.core.blazeds.translator.HedgesphereASTranslator</type-marshaller>
            </serialization>
        </properties>

    </channel-definition>

自定义 AMF 端点

package ch.hedgesphere.core.blazeds.endpoint;

import ch.hedgesphere.core.blazeds.serialization.Serializer;

    public class AMFEndpoint extends flex.messaging.endpoints.AMFEndpoint {

    @Override
    protected String getSerializerClassName() {
        return Serializer.class.getName();
        }

    }

自定义序列化程序

package ch.hedgesphere.core.blazeds.serialization;

import java.io.OutputStream;

import flex.messaging.io.MessageIOConstants;
import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.AmfMessageSerializer;
import flex.messaging.io.amf.AmfTrace;

public class Serializer extends AmfMessageSerializer {

    @Override
    public void initialize(SerializationContext context, OutputStream out, AmfTrace trace)
    {
        amfOut = new AMF0Output(context);
        amfOut.setOutputStream(out);
        amfOut.setAvmPlus(version >= MessageIOConstants.AMF3);

        debugTrace = trace;
        isDebug = trace != null;
        amfOut.setDebugTrace(debugTrace);
    }
}

自定义 AMF 0 处理

package ch.hedgesphere.core.blazeds.serialization;

import flex.messaging.io.SerializationContext;

public class AMF0Output extends flex.messaging.io.amf.Amf0Output {

public AMF0Output(SerializationContext context) {
    super(context);
}

@Override
    protected void createAMF3Output()
    {
        avmPlusOutput = new AMF3Output(context);
        avmPlusOutput.setOutputStream(out);
        avmPlusOutput.setDebugTrace(trace);
    }
}

自定义 AMF 3 处理

package ch.hedgesphere.core.blazeds.serialization;

import java.io.IOException;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;

import flex.messaging.io.SerializationContext;

public class AMF3Output extends flex.messaging.io.amf.Amf3Output {

public AMF3Output(SerializationContext context) {
    super(context);
}

@Override
public void writeObject(Object value) throws IOException {
    if(value instanceof DateTime) {
        value = convertToDate((DateTime)value);
    }
    if(value instanceof LocalDate) {
        value = convertToDate((LocalDate)value);
    }
    if(value instanceof LocalTime) {
    value = convertToDate((LocalTime)value);
    }
    super.writeObject(value);
}

private Object convertToDate(LocalTime time) {
    return time.toDateTimeToday().toDate();
}

private Object convertToDate(LocalDate date) {
    return date.toDateMidnight().toDate();
}

private Object convertToDate(DateTime dateTime) {
    return dateTime.toDate();
}   
}

用于 Flex->Java 调用的自定义编组器

package ch.hedgesphere.core.blazeds.translator;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;

import flex.messaging.io.amf.translator.ASTranslator;


public class HedgesphereASTranslator extends ASTranslator {

@SuppressWarnings({"rawtypes"})
@Override
public Object convert(Object originalValue, Class type) {
    if( type.equals(DateTime.class)) {
        return convertToDateTime(originalValue);
    }
    if( type.equals(LocalDate.class)) {
    return convertToLocalDate(originalValue); 
    }
    if( type.equals(LocalTime.class)) {
        return convertToLocalTime(originalValue);
    }

    return super.convert(originalValue, type);
}

private Object convertToLocalTime(Object originalValue) {
    return originalValue == null ? null : new LocalTime(originalValue);
}

private Object convertToLocalDate(Object originalValue) {
    return originalValue == null ? null : new LocalDate(originalValue); 
}

private Object convertToDateTime(Object originalValue) {
    return originalValue == null ? null : new DateTime(originalValue);
}

@SuppressWarnings({"rawtypes"})
@Override
public Object createInstance(Object source, Class type) {
    return super.createInstance(source, type);
}
}

这篇关于通过 BlazeDS 从 Java 到 Flex 的自定义编组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆