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