A.J.

Someday somehow someone is going to steal your carbon.

Posts Tagged ‘hessian

Using custom hessian Serializers with Spring

with 2 comments

In my last post I showed an example of a custom hessian serializer and deserializer. In order to make this all work with spring some steps need to be performed.

Normally with hessian you add your custom SerializerFactory to the main hessian SerializerFactory via the addFactory method. This is particularly spring friendly. You will need to create a subclass of SerializerFactory like the following:

package com.foo;

import com.caucho.hessian.io.SerializerFactory;
import com.caucho.hessian.io.AbstractSerializerFactory;

import java.util.List;

public class SpringExtensibleSerializerFactory extends SerializerFactory
{

    public void setSerializerFactories(List<AbstractSerializerFactory> factories) {
        for (AbstractSerializerFactory factory : factories)
        {
            addFactory(factory);
        }
    }

}

This will allow you to add a list of custom SerializerFactory to the main hessian SerializerFactory. This SerializerFactory will need to be setup on both the client and server.

<bean id="hessianSerializerFactory"
          class="com.foo.SpringExtensibleSerializerFactory">
<property name="serializerFactories">
        <util:list>
             <bean class="com.foo.BigIntegerSerializerFactory"/>
        </util:list>
    </property>
 </bean>

Next on the server side I create an abstract hessian exporter bean.

<bean id="baseHessianService" class="org.springframework.remoting.caucho.HessianServiceExporter" abstract="true">
<property name="serializerFactory" ref="hessianSerializerFactory" />
</bean>

Services can then be exposed simply by extended the baseHessianService

<bean id="myHessianService" parent="baseHessianProxyFactory">
<property name="serviceUrl"
            value="http://foo.com/hessian/MyHessianService" />
<property name="serviceInterface"
            value="com.foo.MyHessianService" />
</bean>

On the client side create an abstract bean for HessianProxyFactoryBean with an instance of the hessianSerializerFactory described above.

<bean id="baseHessianProxyFactory" class="org.springframework.remoting.caucho.HessianProxyFactoryBean"
          abstract="true" >
<property name="serializerFactory" ref="hessianSerializerFactory" />
</bean>

Services can then be consumed by extending this abstract bean

<bean id="myHessianServiceProxy" parent="baseHessianProxyFactory">
<property name="serviceUrl"
            value="http://foo.com/hessian/MyHessianService" />
<property name="serviceInterface"
            value="com.foo.MyHessianService" />
</bean>
Advertisements

Written by AJ

February 12, 2009 at 9:20 am

Posted in java

Tagged with , ,

BigInteger and hessian.

leave a comment »

Out of the box hessian does not have support for java.math.BigInteger.
It easy to add custom hessian serializers to hessian to extend it to handle cases like these.

The deserializer:


package com.foo.hessian;

import com.caucho.hessian.io.AbstractDeserializer;
import com.caucho.hessian.io.AbstractHessianInput;

import java.io.IOException;
import java.math.BigInteger;

public class BigIntegerDeSerializer extends AbstractDeserializer
{

    public Class getType()
    {
        return BigInteger.class;
    }

    public Object readMap(AbstractHessianInput in)
            throws IOException
    {
        int ref = in.addRef(null);

        String initValue = null;

        while (!in.isEnd())
        {
            String key = in.readString();

            if (key.equals("value"))
                initValue = in.readString();
            else
                in.readString();
        }

        in.readMapEnd();

        Object value = new BigInteger(initValue);

        in.setRef(ref, value);

        return value;
    }

    public Object readObject(AbstractHessianInput in, String[] fieldNames)
            throws IOException
    {
        int ref = in.addRef(null);

        String initValue = null;

        for (String key : fieldNames)
        {
            if (key.equals("value"))
                initValue = in.readString();
            else
                in.readObject();
        }

        Object value = new BigInteger(initValue);

        in.setRef(ref, value);

        return value;
    }

}

The serializer:

package com.foo.hessian;

import com.caucho.hessian.io.AbstractSerializer;
import com.caucho.hessian.io.AbstractHessianOutput;

import java.io.IOException;
import java.math.BigInteger;

public class BigIntegerSerializer extends AbstractSerializer
{
    public void writeObject(Object obj, AbstractHessianOutput out) throws IOException
    {

        if (obj == null)
            out.writeNull();
        else
        {
            Class cl = obj.getClass();

            if (out.addRef(obj))
                return;

            int ref = out.writeObjectBegin(cl.getName());

            BigInteger bi = (BigInteger) obj;

            if (ref < -1)             {                 out.writeString("value");                 out.writeString(bi.toString());                 out.writeMapEnd();             }             else             {                 if (ref == -1)                 {                     out.writeInt(1);                     out.writeString("value");                     out.writeObjectBegin(cl.getName());                 }                 out.writeString(bi.toString());             }         }     } } [/sourcecode] Next you will need a serializer factory for the BigInteger Serializer/Deserializer [sourcecode language='java'] public class BigIntegerSerializerFactory extends AbstractSerializerFactory { public Serializer getSerializer(Class cl) throws HessianProtocolException { if (BigInteger.class.isAssignableFrom(cl)) { return new BigIntegerSerializer(); } return null; } public Deserializer getDeserializer(Class cl) throws HessianProtocolException { if (BigInteger.class.isAssignableFrom(cl)) { return new BigIntegerDeSerializer(); } return null; } } [/sourcecode] Finally add this to the main via the SerializerFactory#addFactory method. In the next post I will show how to tie this all into spring.

Written by AJ

February 11, 2009 at 3:56 pm

Posted in java

Tagged with , ,