View Javadoc

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