S2Flex2とHibernateの連携で嵌り中(解決編)

http://d.hatena.ne.jp/kare/20071126/1196094289の問題が何とか解決.

HibernateのLazy-fetch時にエンティティに追加されるorg.hibernate.proxy.LazyInitializerフィールドを
シリアライズの対象外として対応.

  1. org.seasar.flex2.core.format.amf3.io.writer.impl.Amf3TypedObjectWriterImplを継承してLazyInitializerを実装したフィールドをシリアライズの対象外にするExAmf3TypedObjectWriterImplを作成
  2. amf3.diconを修正して,Amf3TypedObjectWriterImplの替わりにExAmf3TypedObjectWriterImplを利用するように修正

との手順でできた.

以下,ExAmf3TypedObjectWriterImpl.javaのソース添付しておく.

S2Flex2Hibernateを一緒に使う奇特な人(?)には参考になるかも?
#そんな人はいないか...

package sample.flex;

import java.io.DataOutputStream;
import java.io.IOException;

import org.hibernate.proxy.LazyInitializer;
import org.seasar.flex2.core.format.amf3.Amf3Constants;
import org.seasar.flex2.core.format.amf3.io.writer.impl.Amf3TypedObjectWriterImpl;
import org.seasar.framework.beans.BeanDesc;
import org.seasar.framework.beans.PropertyDesc;
import org.seasar.framework.beans.factory.BeanDescFactory;

public class ExAmf3TypedObjectWriterImpl extends Amf3TypedObjectWriterImpl {
    protected void writeClassDefine(final BeanDesc beanDesc,
            final DataOutputStream outputStream) throws IOException {
        int classDef = Amf3Constants.OBJECT_INLINE;
        classDef |= Amf3Constants.CLASS_DEF_INLINE;
        classDef |= Amf3Constants.OBJECT_PROPERTY_LIST_ENCODED;
        classDef = ((beanDesc.getPropertyDescSize() - countOmittedProperties(beanDesc)) << 4) | classDef;
        writeIntData(classDef, outputStream);
    }

    private int countOmittedProperties(final BeanDesc beanDesc) {
        int result = 0;
        for (int i = 0; i < beanDesc.getPropertyDescSize(); i++) {
            if (isOmittedProperty(beanDesc.getPropertyDesc(i))) {
                result++;
            }
        }
        return result;
    }

    protected boolean isOmittedProperty(PropertyDesc pd) {
        return LazyInitializer.class.isAssignableFrom(pd.getPropertyType());
    	
    }

    protected void writeClassObjectProperties(final Object value,
            final DataOutputStream outputStream) throws IOException {
        final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(value.getClass());
        for (int i = 0; i < beanDesc.getPropertyDescSize(); ++i) {
            if (!isOmittedProperty(beanDesc.getPropertyDesc(i))) {
            	writeClassObjectProperty(beanDesc.getPropertyDesc(i), value, outputStream);
            }
        }
    }

    protected void writeClassProperties(final BeanDesc beanDesc,
            final DataOutputStream outputStream) throws IOException {
        for (int i = 0; i < beanDesc.getPropertyDescSize(); ++i) {
            if (!isOmittedProperty(beanDesc.getPropertyDesc(i))) {
            	writeClassPropertyName(outputStream, beanDesc.getPropertyDesc(i));
            }
        }
    }

    private final void writeClassObjectProperty(final PropertyDesc propertyDef,
            final Object value, final DataOutputStream outputStream)
            throws IOException {
        if (propertyDef.hasReadMethod()) {
        	writeObjectElement(propertyDef.getValue(value), outputStream);
        }
    }

    private final void writeClassPropertyName(
            final DataOutputStream outputStream, final PropertyDesc propertyDef)
            throws IOException {
        if (propertyDef.hasReadMethod()) {
            writeTypeString(propertyDef.getPropertyName(), outputStream);
        }
    }
    
}

ほとんどAmf3TypedObjectWriterImplクラスの実装を拝借してるのは,ご容赦を.