Coverage Report - org.mule.transport.polling.MessageProcessorPollingMessageReceiver
 
Classes in this File Line Coverage Branch Coverage Complexity
MessageProcessorPollingMessageReceiver
0%
0/26
0%
0/16
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.polling;
 8  
 
 9  
 import org.mule.DefaultMuleEvent;
 10  
 import org.mule.DefaultMuleMessage;
 11  
 import org.mule.api.MuleEvent;
 12  
 import org.mule.api.MuleMessage;
 13  
 import org.mule.api.construct.FlowConstruct;
 14  
 import org.mule.api.endpoint.ImmutableEndpoint;
 15  
 import org.mule.api.endpoint.InboundEndpoint;
 16  
 import org.mule.api.endpoint.OutboundEndpoint;
 17  
 import org.mule.api.lifecycle.CreateException;
 18  
 import org.mule.api.lifecycle.InitialisationException;
 19  
 import org.mule.api.processor.MessageProcessor;
 20  
 import org.mule.api.transport.Connector;
 21  
 import org.mule.config.i18n.CoreMessages;
 22  
 import org.mule.session.DefaultMuleSession;
 23  
 import org.mule.transport.AbstractConnector;
 24  
 import org.mule.transport.AbstractPollingMessageReceiver;
 25  
 import org.mule.transport.NullPayload;
 26  
 import org.mule.util.StringUtils;
 27  
 
 28  
 import java.util.Map;
 29  
 
 30  
 public class MessageProcessorPollingMessageReceiver extends AbstractPollingMessageReceiver
 31  
 {
 32  
     public static final String SOURCE_MESSAGE_PROCESSOR_PROPERTY_NAME = "sourceMessageProcessor";
 33  
 
 34  
     protected MessageProcessor sourceMessageProcessor;
 35  
 
 36  
     public MessageProcessorPollingMessageReceiver(Connector connector,
 37  
                                                   FlowConstruct flowConstruct,
 38  
                                                   InboundEndpoint endpoint) throws CreateException
 39  
     {
 40  0
         super(connector, flowConstruct, endpoint);
 41  0
     }
 42  
 
 43  
     @Override
 44  
     protected void doInitialise() throws InitialisationException
 45  
     {
 46  0
         super.doInitialise();
 47  
 
 48  0
         sourceMessageProcessor = (MessageProcessor) endpoint.getProperty(SOURCE_MESSAGE_PROCESSOR_PROPERTY_NAME);
 49  
 
 50  0
         if (sourceMessageProcessor instanceof OutboundEndpoint
 51  
             && !((OutboundEndpoint) sourceMessageProcessor).getExchangePattern().hasResponse())
 52  
         {
 53  
             // TODO DF: i18n
 54  0
             throw new InitialisationException(CoreMessages.createStaticMessage(String.format(
 55  
                 "The endpoint %s does not return responses and therefore can't be used for polling.",
 56  
                 sourceMessageProcessor)), this);
 57  
         }
 58  
 
 59  0
         Long tempPolling = (Long) endpoint.getProperties().get(AbstractConnector.PROPERTY_POLLING_FREQUENCY);
 60  0
         if (tempPolling != null)
 61  
         {
 62  0
             setFrequency(tempPolling);
 63  
         }
 64  0
     }
 65  
 
 66  
     @Override
 67  
     public void poll() throws Exception
 68  
     {
 69  0
         MuleMessage request = new DefaultMuleMessage(StringUtils.EMPTY, (Map<String, Object>) null,
 70  
             connector.getMuleContext());
 71  0
         ImmutableEndpoint ep = endpoint;
 72  0
         if (sourceMessageProcessor instanceof ImmutableEndpoint)
 73  
         {
 74  0
             ep = (ImmutableEndpoint) sourceMessageProcessor;
 75  
         }
 76  
 
 77  0
         MuleEvent event = new DefaultMuleEvent(request, ep, new DefaultMuleSession(flowConstruct,
 78  
             connector.getMuleContext()));
 79  
 
 80  0
         MuleEvent sourceEvent = sourceMessageProcessor.process(event);
 81  0
         if (isNewMessage(sourceEvent))
 82  
         {
 83  0
             routeMessage(sourceEvent.getMessage());
 84  
         }
 85  
         else
 86  
         {
 87  
             // TODO DF: i18n
 88  0
             logger.info(String.format("Polling of '%s' returned null, the flow will not be invoked.",
 89  
                 sourceMessageProcessor));
 90  
         }
 91  0
     }
 92  
 
 93  
     // Only consider response for source message processor a new message if it is not
 94  
     // null and payload is not NullPayload
 95  
     protected boolean isNewMessage(MuleEvent sourceEvent)
 96  
     {
 97  0
         if (sourceEvent != null && sourceEvent.getMessage() != null)
 98  
         {
 99  0
             MuleMessage message = sourceEvent.getMessage();
 100  0
             if (message.getPayload().equals(NullPayload.getInstance()))
 101  
             {
 102  0
                 return false;
 103  
             }
 104  
             else
 105  
             {
 106  0
                 return true;
 107  
             }
 108  
         }
 109  0
         return false;
 110  
     }
 111  
 }