View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.tcp.integration;
8   
9   import org.mule.transport.tcp.protocols.DirectProtocol;
10  
11  import java.io.IOException;
12  import java.io.InputStream;
13  import java.io.OutputStream;
14  
15  import org.apache.commons.lang.SerializationUtils;
16  
17  public class CustomSerializationProtocol extends DirectProtocol
18  {
19  
20      @Override
21      public void write(OutputStream os, Object data) throws IOException
22      {
23          if (data instanceof NonSerializableMessageObject)
24          {
25              NonSerializableMessageObject in = (NonSerializableMessageObject)data;
26  
27              // do serialization... will use normal Serialization to simplify code...
28              MessageObject serializableObject = new MessageObject(in.i, in.s, in.b);
29  
30              write(os, SerializationUtils.serialize(serializableObject));
31          }
32          else
33          {
34              super.write(os, data);
35          }
36      }
37  
38      @Override
39      public Object read(InputStream is) throws IOException
40      {
41          byte[] tmp = (byte[]) super.read(is);
42  
43          if (tmp == null)
44          {
45              return null;
46          }
47          else
48          {
49              MessageObject serializableObject = (MessageObject)SerializationUtils.deserialize(tmp);
50              return new NonSerializableMessageObject(serializableObject.i, serializableObject.s,
51                  serializableObject.b);
52          }
53      }
54  
55  }