Coverage Report - org.mule.providers.rmi.RmiMessageDispatcher
 
Classes in this File Line Coverage Branch Coverage Complexity
RmiMessageDispatcher
0%
0/31
0%
0/6
2.125
 
 1  
 /*
 2  
  * $Id: RmiMessageDispatcher.java 7976 2007-08-21 14:26:13Z dirk.olmes $
 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.providers.rmi;
 12  
 
 13  
 import org.mule.impl.MuleMessage;
 14  
 import org.mule.providers.AbstractMessageDispatcher;
 15  
 import org.mule.umo.UMOEvent;
 16  
 import org.mule.umo.UMOMessage;
 17  
 import org.mule.umo.endpoint.UMOImmutableEndpoint;
 18  
 import org.mule.umo.transformer.TransformerException;
 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(UMOImmutableEndpoint endpoint)
 36  
     {
 37  0
         super(endpoint);
 38  0
         this.connector = (RmiConnector)endpoint.getConnector();
 39  0
     }
 40  
 
 41  
     protected void doConnect() throws Exception
 42  
     {
 43  0
         if (remoteObject == null)
 44  
         {
 45  
             // Shouldn't all this be in the connector?
 46  0
             String rmiPolicyPath = connector.getSecurityPolicy();
 47  0
             System.setProperty("java.security.policy", rmiPolicyPath);
 48  
 
 49  
             // Set security manager
 50  0
             if (System.getSecurityManager() == null)
 51  
             {
 52  0
                 System.setSecurityManager(new RMISecurityManager());
 53  
             }
 54  
 
 55  0
             remoteObject = connector.getRemoteObject(endpoint);
 56  
         }
 57  0
     }
 58  
 
 59  
     protected void doDisconnect() throws Exception
 60  
     {
 61  0
         remoteObject = null;
 62  0
         invokedMethod = null;
 63  0
     }
 64  
 
 65  
     private Object[] getArgs(UMOEvent event) throws TransformerException
 66  
     {
 67  0
         Object payload = event.getTransformedMessage();
 68  0
         if (payload instanceof Object[])
 69  
         {
 70  0
             return (Object[])payload;
 71  
         }
 72  
         else
 73  
         {
 74  0
             return new Object[]{payload};
 75  
         }
 76  
     }
 77  
 
 78  
     /*
 79  
      * (non-Javadoc)
 80  
      * 
 81  
      * @see org.mule.umo.provider.UMOConnectorSession#dispatch(org.mule.umo.UMOEvent)
 82  
      */
 83  
     protected void doDispatch(UMOEvent event) throws Exception
 84  
     {
 85  0
         Object[] arguments = getArgs(event);
 86  0
         if (invokedMethod == null)
 87  
         {
 88  0
             invokedMethod = connector.getMethodObject(remoteObject, event);
 89  
         }
 90  0
         invokedMethod.invoke(remoteObject, arguments);
 91  0
     }
 92  
 
 93  
     /*
 94  
      * (non-Javadoc)
 95  
      * 
 96  
      * @see org.mule.umo.provider.UMOConnectorSession#send(org.mule.umo.UMOEvent)
 97  
      */
 98  
     public UMOMessage doSend(UMOEvent event) throws Exception
 99  
     {
 100  0
         if (invokedMethod == null)
 101  
         {
 102  0
             invokedMethod = connector.getMethodObject(remoteObject, event);
 103  
         }
 104  
 
 105  0
         Object[] arguments = getArgs(event);
 106  0
         Object result = invokedMethod.invoke(remoteObject, arguments);
 107  
 
 108  0
         if (result == null)
 109  
         {
 110  0
             return null;
 111  
         }
 112  
         else
 113  
         {
 114  0
             return new MuleMessage(connector.getMessageAdapter(result).getPayload(), Collections.EMPTY_MAP);
 115  
         }
 116  
     }
 117  
 
 118  
     /**
 119  
      * Make a specific request to the underlying transport
 120  
      * 
 121  
      * @param endpoint the endpoint to use when connecting to the resource
 122  
      * @param timeout the maximum time the operation should block before returning.
 123  
      *            The call should return immediately if there is data available. If
 124  
      *            no data becomes available before the timeout elapses, null will be
 125  
      *            returned
 126  
      * @return the result of the request wrapped in a UMOMessage object. Null will be
 127  
      *         returned if no data was avaialable
 128  
      * @throws Exception if the call to the underlying protocal cuases an exception
 129  
      */
 130  
     protected UMOMessage doReceive(long timeout) throws Exception
 131  
     {
 132  0
         throw new UnsupportedOperationException("doReceive");
 133  
     }
 134  
 
 135  
     protected void doDispose()
 136  
     {
 137  
         // template method
 138  0
     }
 139  
 }