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