Coverage Report - org.mule.transport.tcp.TcpMessageRequester
 
Classes in this File Line Coverage Branch Coverage Complexity
TcpMessageRequester
71%
17/24
50%
5/10
2.8
 
 1  
 /*
 2  
  * $Id: TcpMessageRequester.java 10961 2008-02-22 19:01:02Z dfeist $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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.MuleMessage;
 15  
 import org.mule.api.endpoint.InboundEndpoint;
 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  10
         super(endpoint);
 32  10
         this.connector = (TcpConnector) endpoint.getConnector();
 33  10
     }
 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  
     protected MuleMessage doRequest(long timeout) throws Exception
 47  
     {
 48  10
         if (timeout > Integer.MAX_VALUE || timeout < 0)
 49  
         {
 50  0
             throw new IllegalArgumentException("Timeout incorrect: " + timeout);
 51  
         }
 52  10
         Socket socket = connector.getSocket(endpoint);
 53  
         try
 54  
         {
 55  10
             Object result = TcpMessageDispatcher.receiveFromSocket(socket, (int)timeout, endpoint);
 56  10
             if (result == null)
 57  
             {
 58  4
                 return null;
 59  
             }
 60  6
             return new DefaultMuleMessage(connector.getMessageAdapter(result));
 61  
         }
 62  0
         catch (SocketTimeoutException e)
 63  
         {
 64  
             // we don't necesarily expect to receive a resonse here
 65  0
             if (logger.isDebugEnabled())
 66  
             {
 67  0
                 logger.debug("Socket timed out normally while doing a synchronous receive on endpointUri: "
 68  
                     + endpoint.getEndpointURI());
 69  
             }
 70  0
             return null;
 71  
         }
 72  
 
 73  
     }
 74  
 
 75  
     protected synchronized void doDispose()
 76  
     {
 77  
         try
 78  
         {
 79  10
             doDisconnect();
 80  
         }
 81  0
         catch (Exception e)
 82  
         {
 83  0
             logger.error("Failed to shutdown the dispatcher.", e);
 84  10
         }
 85  10
     }
 86  
 
 87  
     protected void doConnect() throws Exception
 88  
     {
 89  
         // Test the connection
 90  10
         if (connector.isValidateConnections())
 91  
         {
 92  10
             Socket socket = connector.getSocket(endpoint);
 93  10
             connector.releaseSocket(socket, endpoint);
 94  
         }
 95  10
     }
 96  
 
 97  
     protected void doDisconnect() throws Exception
 98  
     {
 99  
         //nothing to do
 100  20
     }
 101  
 
 102  
 }