View Javadoc

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