Example usage for org.apache.commons.lang SerializationUtils serialize

List of usage examples for org.apache.commons.lang SerializationUtils serialize

Introduction

In this page you can find the example usage for org.apache.commons.lang SerializationUtils serialize.

Prototype

public static byte[] serialize(Serializable obj) 

Source Link

Document

Serializes an Object to a byte array for storage/serialization.

Usage

From source file:org.mule.DefaultMuleMessageSerializationTestCase.java

private MuleMessage serializationRoundtrip(MuleMessage message) throws Exception {
    byte[] serialized = SerializationUtils.serialize(message);
    MuleMessage deserializedMessage = (MuleMessage) SerializationUtils.deserialize(serialized);
    DeserializationPostInitialisable.Implementation.init(deserializedMessage, muleContext);
    return deserializedMessage;
}

From source file:org.mule.message.MessagePropertiesContextTestCase.java

@Test
public void testPropertiesCaseAfterSerialization() throws Exception {
    //Default scope
    MessagePropertiesContext mpc = new MessagePropertiesContext();
    mpc.setProperty("FOO", "BAR", PropertyScope.OUTBOUND);
    mpc.setProperty("ABC", "abc", PropertyScope.OUTBOUND);
    mpc.setProperty("DOO", "DAR", PropertyScope.INVOCATION);
    doTest(mpc);//from   w ww .  ja  v  a2s .  c  o m

    //Serialize and deserialize
    byte[] bytes = SerializationUtils.serialize(mpc);
    mpc = (MessagePropertiesContext) SerializationUtils.deserialize(bytes);
    doTest(mpc);
}

From source file:org.mule.module.apikit.AbstractConfiguration.java

private Raml deepCloneRaml(Raml source) {
    return (Raml) SerializationUtils.deserialize(SerializationUtils.serialize(source));
}

From source file:org.mule.module.redis.RedisUtils.java

public static byte[] toBytes(final Serializable serializable) {
    if (serializable == null) {
        return null;
    }/*w ww.java2  s. c o  m*/

    // preserve strings if possible
    if (serializable instanceof String) {
        return SafeEncoder.encode((String) serializable);
    }
    // serialize anything that isn't a string
    return SerializationUtils.serialize(serializable);
}

From source file:org.mule.test.integration.routing.MessageChunkingTestCase.java

@Test
public void testMessageChunkingObject() throws Exception {
    final AtomicInteger messagePartsCount = new AtomicInteger(0);
    final Latch chunkingReceiverLatch = new Latch();
    final SimpleSerializableObject simpleSerializableObject = new SimpleSerializableObject("Test String", true,
            99);//  ww w  . j  a  v  a  2s .  c  o m

    // find number of chunks
    final int parts = (int) Math
            .ceil((SerializationUtils.serialize(simpleSerializableObject).length / (double) 2));

    // Listen to events fired by the ChunkingReceiver service
    muleContext.registerListener(new FunctionalTestNotificationListener() {
        @Override
        public void onNotification(ServerNotification notification) {
            // Not strictly necessary to test for this as when we register the
            // listener we supply the ComponentName as the subscription filter
            assertEquals("ChunkingObjectReceiver", notification.getResourceIdentifier());
            // Test that we have received all chunks in the correct order
            Object reply = ((FunctionalTestNotification) notification).getEventContext().getMessage()
                    .getPayload();
            // Check if Object is of Correct Type
            assertTrue(reply instanceof SimpleSerializableObject);
            SimpleSerializableObject replySimpleSerializableObject = (SimpleSerializableObject) reply;
            // Check that Contents are Identical
            assertEquals(simpleSerializableObject.b, replySimpleSerializableObject.b);
            assertEquals(simpleSerializableObject.i, replySimpleSerializableObject.i);
            assertEquals(simpleSerializableObject.s, replySimpleSerializableObject.s);
            chunkingReceiverLatch.countDown();
        }
    }, "ChunkingObjectReceiver");

    // Listen to Message Notifications on the Chunking receiver so we can
    // determine how many message parts have been received
    muleContext.registerListener(new EndpointMessageNotificationListener<EndpointMessageNotification>() {
        @Override
        public void onNotification(EndpointMessageNotification notification) {
            if (notification.getAction() == EndpointMessageNotification.MESSAGE_RECEIVED) {
                messagePartsCount.getAndIncrement();
            }
            assertEquals("ChunkingObjectReceiver", notification.getResourceIdentifier());
        }
    }, "ChunkingObjectReceiver");

    MuleClient client = new MuleClient(muleContext);
    client.dispatch("vm://inbound.object.channel", simpleSerializableObject, null);
    // Wait for the message to be received and tested (in the listener above)
    assertTrue(chunkingReceiverLatch.await(20L, TimeUnit.SECONDS));
    // Ensure we processed expected number of message parts
    assertEquals(parts, messagePartsCount.get());
}

From source file:org.mule.transformer.simple.ObjectToInputStreamTestCase.java

@Test
public void testTransformSerializable() {
    Apple apple = new Apple();
    InputStream serializedApple = new ByteArrayInputStream(SerializationUtils.serialize(apple));
    try {/*w  w w .j ava2 s  .co m*/
        assertTrue(compare(serializedApple, (InputStream) transformer.transform(apple)));
    } catch (Exception e) {
        assertTrue(e instanceof TransformerException);
        assertTrue(e.getMessage().contains("does not support source type"));
    }
}

From source file:org.mule.transformer.simple.SerialisedObjectTransformersTestCase.java

public Object getResultData() {
    return SerializationUtils.serialize(testObject);
}

From source file:org.mule.transport.comm.protocols.AbstractByteProtocol.java

public void write(OutputStream os, Object data) throws IOException {
    if (data instanceof InputStream) {
        if (streamOk) {
            InputStream is = (InputStream) data;
            IOUtils.copy(is, os);/*from   ww  w  . j  a v  a  2s.c  o  m*/
            os.flush();
            os.close();
            is.close();
        } else {
            throw new IOException(
                    "Comm protocol " + ClassUtils.getSimpleName(getClass()) + " cannot handle streaming");
        }
    } else if (data instanceof MessageAdapter) {
        write(os, ((MessageAdapter) data).getPayload());
    } else if (data instanceof byte[]) {
        writeByteArray(os, (byte[]) data);
    } else if (data instanceof String) {
        // TODO SF: encoding is lost/ignored; it is probably a good idea to have
        // a separate "stringEncoding" property on the protocol
        writeByteArray(os, ((String) data).getBytes());
    } else if (data instanceof Serializable) {
        writeByteArray(os, SerializationUtils.serialize((Serializable) data));
    } else {
        throw new IllegalArgumentException("Cannot serialize data: " + data);
    }
}

From source file:org.mule.transport.NullPayloadTestCase.java

@Test
public void testUniqueDeserialization() {
    NullPayload result = NullPayload.getInstance();

    byte[] serialized = SerializationUtils.serialize(result);
    assertNotNull(serialized);//from  w  w w.ja  v  a 2s  . c  o  m

    Object deserialized = SerializationUtils.deserialize(serialized);
    assertSame(deserialized, result);
    assertEquals(deserialized, result);
}

From source file:org.mule.transport.tcp.integration.CustomSerializationProtocol.java

@Override
public void write(OutputStream os, Object data) throws IOException {
    if (data instanceof NonSerializableMessageObject) {
        NonSerializableMessageObject in = (NonSerializableMessageObject) data;

        // do serialization... will use normal Serialization to simplify code...
        MessageObject serializableObject = new MessageObject(in.i, in.s, in.b);

        write(os, SerializationUtils.serialize(serializableObject));
    } else {//from   w  w w. j ava 2  s  .  co  m
        super.write(os, data);
    }
}