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.ByteArrayInputStream;
14  import java.io.InputStream;
15  import java.io.ObjectInputStream;
16  
17  import org.springframework.remoting.support.RemoteInvocation;
18  
19  /**
20   * Transforms a byte[] into an ObjectInputStream and then into a Spring RemoteInvocation instance.
21   */
22  public class ObjectToRemoteInvocationTransformer extends AbstractTransformer
23  {
24  
25      public ObjectToRemoteInvocationTransformer()
26      {
27          super();
28          this.registerSourceType(DataTypeFactory.create(RemoteInvocation.class));
29          this.registerSourceType(DataTypeFactory.BYTE_ARRAY);
30          this.registerSourceType(DataTypeFactory.INPUT_STREAM);
31          this.setReturnDataType(DataTypeFactory.create(RemoteInvocation.class));
32      }
33  
34      @Override
35      protected Object doTransform(Object src, String encoding) throws TransformerException
36      {
37          if (src instanceof RemoteInvocation)
38          {
39              return src;
40          }
41  
42          Object o = null;
43  
44          if (src instanceof InputStream)
45          {
46              try
47              {
48                  o = new ObjectInputStream((InputStream) src).readObject();
49              }
50              catch (Exception e)
51              {
52                  throw new TransformerException(this, e);
53              }
54          }
55          else
56          {
57              byte[] data = (byte[]) src;
58              ByteArrayInputStream bais = new ByteArrayInputStream(data);
59              try
60              {
61                  ObjectInputStream ois = new ObjectInputStream(bais);
62                  o = ois.readObject();
63              }
64              catch (Exception e)
65              {
66                  throw new TransformerException(this, e);
67              }
68          }
69  
70          RemoteInvocation ri = (RemoteInvocation) o;
71          if (logger.isDebugEnabled())
72          {
73              logger.debug("request to execute " + ri.getMethodName());
74              for (int i = 0; i < ri.getArguments().length; i++)
75              {
76                  Object currentArgument = ri.getArguments()[i];
77                  
78                  StringBuilder buf = new StringBuilder(64);
79                  buf.append("with argument (");
80                  buf.append(currentArgument == null ? "<null>" : currentArgument.toString());
81                  buf.append(")");
82                  
83                  logger.debug(buf);
84              }
85          }
86          return ri;
87      }
88  
89  }