Coverage Report - org.mule.transport.tcp.PollingTcpMessageReceiver
 
Classes in this File Line Coverage Branch Coverage Complexity
PollingTcpMessageReceiver
0%
0/23
0%
0/14
0
 
 1  
 /*
 2  
  * $Id$
 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.tcp;
 12  
 
 13  
 import org.mule.DefaultMuleMessage;
 14  
 import org.mule.api.construct.FlowConstruct;
 15  
 import org.mule.api.endpoint.InboundEndpoint;
 16  
 import org.mule.api.lifecycle.CreateException;
 17  
 import org.mule.api.transport.Connector;
 18  
 import org.mule.transport.AbstractPollingMessageReceiver;
 19  
 import org.mule.transport.tcp.i18n.TcpMessages;
 20  
 import org.mule.util.MapUtils;
 21  
 
 22  
 import java.net.Socket;
 23  
 import java.net.SocketTimeoutException;
 24  
 
 25  
 /**
 26  
  * <code>PollingTcpMessageReceiver</code> acts like a TCP client polling for new
 27  
  * messages.
 28  
  * 
 29  
  * @author esteban.robles
 30  
  */
 31  
 public class PollingTcpMessageReceiver extends AbstractPollingMessageReceiver
 32  
 {
 33  
     private int timeout;
 34  
 
 35  
     private PollingTcpConnector connector;
 36  
 
 37  
     public PollingTcpMessageReceiver(Connector connector, FlowConstruct flowConstruct, InboundEndpoint endpoint)
 38  
         throws CreateException
 39  
     {
 40  0
         super(connector, flowConstruct, endpoint);
 41  
 
 42  0
         if (connector instanceof PollingTcpConnector)
 43  
         {
 44  0
             this.connector = (PollingTcpConnector) connector;
 45  
         }
 46  
         else
 47  
         {
 48  0
             throw new CreateException(TcpMessages.pollingReceiverCannotbeUsed(), this);
 49  
         }
 50  
 
 51  0
         timeout = MapUtils.getIntValue(endpoint.getProperties(), "clientSoTimeout",
 52  
             this.connector.getClientSoTimeout());
 53  
 
 54  0
         if (timeout > Integer.MAX_VALUE || timeout < 0)
 55  
         {
 56  0
             throw new IllegalArgumentException("Timeout incorrect: " + timeout);
 57  
         }
 58  
 
 59  0
         long pollingFrequency = MapUtils.getLongValue(endpoint.getProperties(), "pollingFrequency",
 60  
             this.connector.getPollingFrequency());
 61  0
         if (pollingFrequency > 0)
 62  
         {
 63  0
             this.setFrequency(pollingFrequency);
 64  
         }
 65  0
     }
 66  
 
 67  
     @Override
 68  
     public void poll() throws Exception
 69  
     {
 70  0
         Socket socket = connector.getSocket(endpoint);
 71  
         try
 72  
         {
 73  0
             Object result = TcpMessageDispatcher.receiveFromSocket(socket, (int) timeout, endpoint);
 74  0
             if (!(result == null))
 75  
             {
 76  0
                 this.routeMessage(new DefaultMuleMessage(result, connector.getMuleContext()));
 77  0
                 if (logger.isDebugEnabled())
 78  
                 {
 79  0
                     logger.debug("Routing new message: " + result);
 80  
                 }
 81  
             }
 82  
         }
 83  0
         catch (SocketTimeoutException e)
 84  
         {
 85  0
             if (logger.isDebugEnabled())
 86  
             {
 87  0
                 logger.debug("Socket timed out normally while doing a synchronous receive on endpointUri: "
 88  
                              + endpoint.getEndpointURI());
 89  
             }
 90  
         }
 91  
         finally
 92  
         {
 93  0
             connector.releaseSocket(socket, endpoint);
 94  0
         }
 95  0
     }
 96  
 }