View Javadoc

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