Coverage Report - org.mule.transport.tcp.TcpMessageRequester
 
Classes in this File Line Coverage Branch Coverage Complexity
TcpMessageRequester
0%
0/33
0%
0/12
0
 
 1  
 /*
 2  
  * $Id: TcpMessageRequester.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.tcp;
 12  
 
 13  
 import org.mule.api.MuleMessage;
 14  
 import org.mule.api.endpoint.InboundEndpoint;
 15  
 import org.mule.api.retry.RetryContext;
 16  
 import org.mule.transport.AbstractMessageRequester;
 17  
 
 18  
 import java.net.Socket;
 19  
 import java.net.SocketTimeoutException;
 20  
 
 21  
 /**
 22  
  * Request transformed Mule events from TCP.
 23  
  */
 24  
 public class TcpMessageRequester extends AbstractMessageRequester
 25  
 {
 26  
 
 27  
     private final TcpConnector connector;
 28  
 
 29  
     public TcpMessageRequester(InboundEndpoint endpoint)
 30  
     {
 31  0
         super(endpoint);
 32  0
         this.connector = (TcpConnector) endpoint.getConnector();
 33  0
     }
 34  
 
 35  
     /**
 36  
      * Make a specific request to the underlying transport
 37  
      *
 38  
      * @param timeout the maximum time the operation should block before returning.
 39  
      *            The call should return immediately if there is data available. If
 40  
      *            no data becomes available before the timeout elapses, null will be
 41  
      *            returned
 42  
      * @return the result of the request wrapped in a MuleMessage object. Null will be
 43  
      *         returned if no data was available
 44  
      * @throws Exception if the call to the underlying protocal cuases an exception
 45  
      */
 46  
     @Override
 47  
     protected MuleMessage doRequest(long timeout) throws Exception
 48  
     {
 49  0
         if (timeout > Integer.MAX_VALUE || timeout < 0)
 50  
         {
 51  0
             throw new IllegalArgumentException("Timeout incorrect: " + timeout);
 52  
         }
 53  0
         Socket socket = connector.getSocket(endpoint);
 54  
         try
 55  
         {
 56  0
             Object result = TcpMessageDispatcher.receiveFromSocket(socket, (int)timeout, endpoint);
 57  0
             if (result == null)
 58  
             {
 59  0
                 return null;
 60  
             }
 61  0
             return createMuleMessage(result, endpoint.getEncoding());
 62  
         }
 63  0
         catch (SocketTimeoutException e)
 64  
         {
 65  
             // we don't necesarily expect to receive a resonse here
 66  0
             if (logger.isDebugEnabled())
 67  
             {
 68  0
                 logger.debug("Socket timed out normally while doing a synchronous receive on endpointUri: "
 69  
                     + endpoint.getEndpointURI());
 70  
             }
 71  0
             return null;
 72  
         }
 73  
     }
 74  
 
 75  
     @Override
 76  
     protected synchronized void doDispose()
 77  
     {
 78  
         try
 79  
         {
 80  0
             doDisconnect();
 81  
         }
 82  0
         catch (Exception e)
 83  
         {
 84  0
             logger.error("Failed to shutdown the dispatcher.", e);
 85  0
         }
 86  0
     }
 87  
 
 88  
     @Override
 89  
     protected void doConnect() throws Exception
 90  
     {
 91  
         // nothing, there is an optional validation in validateConnection()
 92  0
     }
 93  
 
 94  
     @Override
 95  
     protected void doDisconnect() throws Exception
 96  
     {
 97  
         //nothing to do
 98  0
     }
 99  
 
 100  
     @Override
 101  
     public RetryContext validateConnection(RetryContext retryContext)
 102  
     {
 103  0
         Socket socket = null;
 104  
         try
 105  
         {
 106  0
             socket = connector.getSocket(endpoint);
 107  
 
 108  0
             retryContext.setOk();
 109  
         }
 110  0
         catch (Exception ex)
 111  
         {
 112  0
             retryContext.setFailed(ex);
 113  
         }
 114  
         finally
 115  
         {
 116  0
             if (socket != null)
 117  
             {
 118  
                 try
 119  
                 {
 120  0
                     connector.releaseSocket(socket, endpoint);
 121  
                 }
 122  0
                 catch (Exception e)
 123  
                 {
 124  0
                     if (logger.isDebugEnabled())
 125  
                     {
 126  0
                         logger.debug("Failed to release a socket " + socket, e);
 127  
                     }
 128  0
                 }
 129  
             }
 130  
         }
 131  
 
 132  0
         return retryContext;
 133  
     }
 134  
 }