View Javadoc

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