View Javadoc

1   /*
2    * $Id: ObjectToRemoteInvocationTransformer.java 10866 2008-02-18 20:29:34Z dfeist $
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.module.spring.remoting;
12  
13  import org.mule.api.transformer.TransformerException;
14  import org.mule.transformer.AbstractTransformer;
15  
16  import java.io.ByteArrayInputStream;
17  import java.io.InputStream;
18  import java.io.ObjectInputStream;
19  
20  import org.springframework.remoting.support.RemoteInvocation;
21  
22  public class ObjectToRemoteInvocationTransformer extends AbstractTransformer
23  {
24  
25      public ObjectToRemoteInvocationTransformer()
26      {
27          super();
28          this.registerSourceType(RemoteInvocation.class);
29          this.registerSourceType(byte[].class);
30          this.registerSourceType(InputStream.class);
31          this.setReturnClass(RemoteInvocation.class);
32      }
33  
34      protected Object doTransform(Object src, String encoding) throws TransformerException
35      {
36          if (src instanceof RemoteInvocation)
37          {
38              return src;
39          }
40  
41          Object o = null;
42  
43          if (src instanceof InputStream)
44          {
45              try
46              {
47                  o = new ObjectInputStream((InputStream) src).readObject();
48              }
49              catch (Exception e)
50              {
51                  throw new TransformerException(this, e);
52              }
53          }
54          else
55          {
56              byte[] data = (byte[]) src;
57              ByteArrayInputStream bais = new ByteArrayInputStream(data);
58              try
59              {
60                  ObjectInputStream ois = new ObjectInputStream(bais);
61                  o = ois.readObject();
62              }
63              catch (Exception e)
64              {
65                  throw new TransformerException(this, e);
66              }
67          }
68  
69          RemoteInvocation ri = (RemoteInvocation) o;
70          if (logger.isDebugEnabled())
71          {
72              logger.debug("request to execute " + ri.getMethodName());
73              for (int i = 0; i < ri.getArguments().length; i++)
74              {
75                  Object a = ri.getArguments()[i];
76                  logger.debug("with argument (" + a.toString() + ")");
77              }
78          }
79          return ri;
80  
81      }
82  
83  }