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.transport.rmi;
8   
9   import org.mule.api.MuleEvent;
10  import org.mule.api.MuleMessage;
11  import org.mule.api.endpoint.OutboundEndpoint;
12  import org.mule.api.transformer.TransformerException;
13  import org.mule.transport.AbstractMessageDispatcher;
14  import org.mule.transport.NullPayload;
15  
16  import java.lang.reflect.Method;
17  import java.rmi.RMISecurityManager;
18  import java.rmi.Remote;
19  
20  /**
21   * <code>RmiMessageDispatcher</code> will send transformed mule events over
22   * RMI-JRMP.
23   */
24  public class RmiMessageDispatcher extends AbstractMessageDispatcher
25  {
26      private final RmiConnector connector;
27      protected volatile Remote remoteObject;
28      protected volatile Method invokedMethod;
29  
30      public RmiMessageDispatcher(OutboundEndpoint endpoint)
31      {
32          super(endpoint);
33          this.connector = (RmiConnector)endpoint.getConnector();
34      }
35  
36      protected void doConnect() throws Exception
37      {
38          if (remoteObject == null)
39          {
40              // Shouldn't all this be in the connector?
41              String rmiPolicyPath = connector.getSecurityPolicy();
42              System.setProperty("java.security.policy", rmiPolicyPath);
43  
44              // Set security manager
45              if (System.getSecurityManager() == null)
46              {
47                  System.setSecurityManager(new RMISecurityManager());
48              }
49  
50              remoteObject = connector.getRemoteObject(endpoint);
51          }
52      }
53  
54      protected void doDisconnect() throws Exception
55      {
56          remoteObject = null;
57          invokedMethod = null;
58      }
59  
60      private Object[] getArgs(MuleEvent event) throws TransformerException
61      {
62          Object payload = event.getMessage().getPayload();
63          if (payload instanceof Object[])
64          {
65              return (Object[])payload;
66          }
67          else
68          {
69              return new Object[]{payload};
70          }
71      }
72  
73      protected void doDispatch(MuleEvent event) throws Exception
74      {
75          Object[] arguments = getArgs(event);
76          if (invokedMethod == null)
77          {
78              invokedMethod = connector.getMethodObject(remoteObject, event);
79          }
80          invokedMethod.invoke(remoteObject, arguments);
81      }
82  
83      public MuleMessage doSend(MuleEvent event) throws Exception
84      {
85          if (invokedMethod == null)
86          {
87              invokedMethod = connector.getMethodObject(remoteObject, event);
88          }
89  
90          Object[] arguments = getArgs(event);
91          Object result = invokedMethod.invoke(remoteObject, arguments);
92  
93          if (result == null)
94          {
95              return createMuleMessage(NullPayload.getInstance());
96          }
97          else
98          {
99              return createMuleMessage(result);
100         }
101     }
102 
103     protected void doDispose()
104     {
105         // template method
106     }
107 }