package org.skyscreamer.nevado.jms.util;

import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;
import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.Serializable;

public class SerializeUtil
{
    public static Serializable copy( Serializable serializable ) throws IOException
    {
        return deserialize(serialize(serializable));
    }

    public static String serializeToString( Serializable serializable ) throws IOException
    {
        byte[] data = serialize(serializable);
        return new String( Base64.encodeBase64(data) );
    }

    public static byte[] serialize( Serializable serializable ) throws IOException {
        // Initialize buffer and converter
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        Hessian2Output hessian2Output = new Hessian2Output( byteArrayOutputStream );

        // Serialize objects
        hessian2Output.startMessage();
        if (serializable instanceof Character) {
            // Hessian doesn't properly serialize java.lang.Character
            serializable = new CharWrapper((Character)serializable);
        }
        hessian2Output.writeObject( serializable );
        hessian2Output.completeMessage();
        hessian2Output.close();
        return byteArrayOutputStream.toByteArray();
    }

    public static Serializable deserializeFromString(String s) throws IOException
    {
        // Initialize buffer and converter
        byte [] dataBytes = Base64.decodeBase64(s.getBytes("UTF-8"));
        return deserialize(dataBytes);
    }

    public static Serializable deserialize(byte[] dataBytes) throws IOException {
        Hessian2Input hessian2Input = new Hessian2Input( new ByteArrayInputStream(  dataBytes ) );

        // Convert
        hessian2Input.startMessage();
        Serializable serializable = (Serializable)hessian2Input.readObject();
        if (serializable instanceof CharWrapper) {
            serializable = ((CharWrapper)serializable).charValue();
        }
        hessian2Input.completeMessage();
        hessian2Input.close();

        // Return strings
        return serializable;
    }
}

@@ -120,24 +120,72 @@
 ian2Output;%0A
+import com.caucho.hessian.io.SerializerFactory;%0A
 import org.a
@@ -219,16 +219,16 @@
 Base64;%0A
-
 %0Aimport 
@@ -1034,16 +1034,126 @@
 tream );
+%0A        hessian2Output.setSerializerFactory( new SerializerFactory( SerializeUtil.class.getClassLoader() ) );
 %0A%0A      
@@ -1918,32 +1918,32 @@
 s IOException %7B%0A
-
         Hessian2
@@ -2020,24 +2020,133 @@
 taBytes ) );
+%0A        hessian2Input.setSerializerFactory( new SerializerFactory( SerializeUtil.class.getClassLoader() ) );
 %0A%0A        //

fix issues with ClassLoader during de/serialization on OSGi