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.module.spring.remoting;
8   
9   import org.mule.api.transformer.TransformerException;
10  import org.mule.transformer.AbstractTransformer;
11  import org.mule.transformer.types.DataTypeFactory;
12  
13  import java.io.ObjectOutputStream;
14  
15  import org.apache.commons.io.output.ByteArrayOutputStream;
16  import org.springframework.remoting.support.RemoteInvocationResult;
17  
18  /**
19   * Converts an Object to a Spring RemoteInvocationResult and then into a byte[].
20   */
21  
22  public class ObjectToRemoteInvocationResultTransformer extends AbstractTransformer
23  {
24      public ObjectToRemoteInvocationResultTransformer()
25      {
26          super();
27          setReturnDataType(DataTypeFactory.BYTE_ARRAY);
28      }
29  
30      @Override
31      protected Object doTransform(Object src, String outputEncoding) throws TransformerException
32      {
33          try
34          {
35              if (logger.isDebugEnabled())
36              {
37                  logger.debug("ObjectToRemoteInvocationResult.doTransform(" + src + ")");
38              }
39  
40              RemoteInvocationResult rval;
41  
42              if (src instanceof RemoteInvocationResult)
43              {
44                  rval = (RemoteInvocationResult)src;
45              }
46              else
47              {
48                  rval = new RemoteInvocationResult(src);
49              }
50  
51              ByteArrayOutputStream baos = new ByteArrayOutputStream();
52              ObjectOutputStream oos = new ObjectOutputStream(baos);
53              oos.writeObject(rval);
54              oos.close();
55              return baos.toByteArray();
56          }
57          catch (Exception e)
58          {
59              throw new TransformerException(this, e);
60          }
61      }
62  }