Coverage Report - org.mule.transport.rmi.RmiMessageReceiver
 
Classes in this File Line Coverage Branch Coverage Complexity
RmiMessageReceiver
0%
0/36
0%
0/10
0
 
 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.RequestContext;
 10  
 import org.mule.api.config.MuleProperties;
 11  
 import org.mule.api.construct.FlowConstruct;
 12  
 import org.mule.api.endpoint.InboundEndpoint;
 13  
 import org.mule.api.lifecycle.CreateException;
 14  
 import org.mule.api.transport.Connector;
 15  
 import org.mule.transport.AbstractPollingMessageReceiver;
 16  
 import org.mule.transport.ConnectException;
 17  
 import org.mule.transport.rmi.i18n.RmiMessages;
 18  
 import org.mule.util.ClassUtils;
 19  
 
 20  
 import java.lang.reflect.Method;
 21  
 import java.rmi.RMISecurityManager;
 22  
 import java.rmi.Remote;
 23  
 import java.util.List;
 24  
 
 25  
 import org.apache.commons.collections.MapUtils;
 26  
 
 27  
 /**
 28  
  * Will repeatedly call a method on a Remote object. If the method takes parameters A
 29  
  * List of objects can be specified on the endpoint called
 30  
  * <code>methodArgumentTypes</code>, If this property is ommitted it is assumed
 31  
  * that the method takes no parameters
 32  
  */
 33  
 
 34  
 public class RmiMessageReceiver extends AbstractPollingMessageReceiver
 35  
 {
 36  
     protected RmiConnector connector;
 37  
 
 38  
     protected Remote remoteObject;
 39  
 
 40  
     protected Method invokeMethod;
 41  
 
 42  0
     protected Object[] methodArguments = null;
 43  
 
 44  
     public RmiMessageReceiver(Connector connector,
 45  
                               FlowConstruct flowConstruct,
 46  
                               InboundEndpoint endpoint,
 47  
                               long frequency) throws CreateException
 48  
     {
 49  0
         super(connector, flowConstruct, endpoint);
 50  0
         this.setFrequency(frequency);
 51  0
         this.connector = (RmiConnector) connector;
 52  0
     }
 53  
 
 54  
     @Override
 55  
     protected void doDispose()
 56  
     {
 57  
         // template method
 58  0
     }
 59  
 
 60  
     @SuppressWarnings("unchecked")
 61  
     @Override
 62  
     protected void doConnect() throws Exception
 63  
     {
 64  0
         System.setProperty("java.security.policy", connector.getSecurityPolicy());
 65  
 
 66  
         // Set security manager
 67  0
         if (System.getSecurityManager() == null)
 68  
         {
 69  0
             System.setSecurityManager(new RMISecurityManager());
 70  
         }        
 71  
 
 72  
         // Get methodName
 73  0
         String methodName = MapUtils.getString(endpoint.getEndpointURI().getParams(),
 74  
                 MuleProperties.MULE_METHOD_PROPERTY, null);
 75  0
         if (null == methodName)
 76  
         {
 77  0
             methodName = (String) endpoint.getProperty(MuleProperties.MULE_METHOD_PROPERTY);
 78  
 
 79  0
             if (null == methodName)
 80  
             {
 81  0
                 throw new ConnectException(RmiMessages.messageParamServiceMethodNotSet(), this);
 82  
             }
 83  
         }
 84  
 
 85  
         // Get remoteObject
 86  0
         remoteObject = connector.getRemoteObject(getEndpoint());
 87  
 
 88  
         // Set methodArguments
 89  0
         List<Object> args = (List<Object>) endpoint.getProperty(RmiConnector.PROPERTY_SERVICE_METHOD_PARAMS_LIST);
 90  0
         Class[] argTypes = new Class[]{};
 91  0
         if (args == null)
 92  
         {
 93  0
             logger.info(RmiConnector.PROPERTY_SERVICE_METHOD_PARAMS_LIST
 94  
                     + " not set on endpoint, assuming method call has no arguments");
 95  0
             methodArguments = ClassUtils.NO_ARGS;
 96  
         }
 97  
         else
 98  
         {
 99  0
             argTypes = connector.getArgTypes(endpoint.getProperty(RmiConnector.PROPERTY_SERVICE_METHOD_PARAM_TYPES), RequestContext.getEvent());
 100  0
             methodArguments = new Object[args.size()];
 101  0
             methodArguments = args.toArray(methodArguments);
 102  
         }
 103  
         
 104  
         // Set invokeMethod
 105  0
         invokeMethod = remoteObject.getClass().getMethod(methodName, argTypes);
 106  0
     }
 107  
 
 108  
     @Override
 109  
     protected void doDisconnect()
 110  
     {
 111  0
         invokeMethod = null;
 112  0
         remoteObject = null;
 113  0
     }
 114  
 
 115  
     public void poll()
 116  
     {
 117  
         try
 118  
         {
 119  0
             Object result = invokeMethod.invoke(remoteObject, getMethodArguments());
 120  
 
 121  0
             if (null != result)
 122  
             {
 123  0
                 routeMessage(createMuleMessage(result));
 124  
             }
 125  
         }
 126  0
         catch (Exception e)
 127  
         {
 128  0
             getConnector().getMuleContext().getExceptionListener().handleException(e);
 129  0
         }
 130  0
     }
 131  
 
 132  
     /**
 133  
      * Returns the method arguments to use when invoking the method on the Remote
 134  
      * object. This method can be overloaded to enable dynamic method arguments
 135  
      */
 136  
     protected Object[] getMethodArguments()
 137  
     {
 138  0
         return methodArguments;
 139  
     }
 140  
 }