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