Coverage Report - org.mule.providers.tcp.TcpSocketFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
TcpSocketFactory
91%
32/35
70%
14/20
2.167
 
 1  
 /*
 2  
  * $Id: TcpSocketFactory.java 7963 2007-08-21 08:53:15Z dirk.olmes $
 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  
 package org.mule.providers.tcp;
 11  
 
 12  
 import org.mule.umo.endpoint.UMOImmutableEndpoint;
 13  
 import org.mule.umo.provider.UMOConnector;
 14  
 import org.mule.util.MapUtils;
 15  
 
 16  
 import java.io.IOException;
 17  
 import java.net.InetAddress;
 18  
 import java.net.Socket;
 19  
 
 20  
 import org.apache.commons.logging.Log;
 21  
 import org.apache.commons.logging.LogFactory;
 22  
 
 23  
 /**
 24  
  * Creates a client socket using the host and port address supplied in the endpoint URI.  Addtional
 25  
  * socket parameters will also be set from the connector
 26  
  */
 27  64
 public class TcpSocketFactory implements PooledSocketFactory
 28  
 {
 29  
     /**
 30  
      * logger used by this class
 31  
      */
 32  4
     private static final Log logger = LogFactory.getLog(TcpSocketFactory.class);
 33  
 
 34  
     public Object makeObject(Object key) throws Exception
 35  
     {
 36  2493
         UMOImmutableEndpoint ep = (UMOImmutableEndpoint) key;
 37  2493
         int port = ep.getEndpointURI().getPort();
 38  2493
         InetAddress inetAddress = InetAddress.getByName(ep.getEndpointURI().getHost());
 39  2493
         Socket socket = createSocket(port, inetAddress);
 40  2493
         socket.setReuseAddress(true);
 41  
 
 42  2493
         TcpConnector connector = (TcpConnector)ep.getConnector();
 43  
         //There is some overhead in stting socket timeout and buffer size, so we're
 44  
         //careful here only to set if needed
 45  2493
         if (connector.getReceiveBufferSize() != UMOConnector.INT_VALUE_NOT_SET
 46  
             && socket.getReceiveBufferSize() != connector.getReceiveBufferSize())
 47  
         {
 48  0
             socket.setReceiveBufferSize(connector.getReceiveBufferSize());
 49  
         }
 50  2493
         if (connector.getSendBufferSize() != UMOConnector.INT_VALUE_NOT_SET
 51  
             && socket.getSendBufferSize() != connector.getSendBufferSize())
 52  
         {
 53  4
             socket.setSendBufferSize(connector.getSendBufferSize());
 54  
         }
 55  2493
         if (connector.getReceiveTimeout() != UMOConnector.INT_VALUE_NOT_SET
 56  
             && socket.getSoTimeout() != connector.getReceiveTimeout())
 57  
         {
 58  20
             socket.setSoTimeout(connector.getSendTimeout());
 59  
         }
 60  2493
         socket.setTcpNoDelay(connector.isSendTcpNoDelay());
 61  2493
         socket.setKeepAlive(connector.isKeepAlive());
 62  2493
         return socket;
 63  
     }
 64  
 
 65  
     protected Socket createSocket(int port, InetAddress inetAddress) throws IOException
 66  
     {
 67  2493
         return new Socket(inetAddress, port);
 68  
     }
 69  
 
 70  
     public void destroyObject(Object key, Object object) throws Exception
 71  
     {
 72  2493
         Socket socket = (Socket) object;
 73  2493
         if(!socket.isClosed())
 74  
         {
 75  4
             socket.close();
 76  
         }
 77  2493
     }
 78  
 
 79  
     public boolean validateObject(Object key, Object object)
 80  
     {
 81  7449
         Socket socket = (Socket) object;
 82  7449
         return !socket.isClosed();       
 83  
     }
 84  
 
 85  
     public void activateObject(Object key, Object object) throws Exception
 86  
     {
 87  
         // cannot really activate a Socket
 88  4948
     }
 89  
 
 90  
     public void passivateObject(Object key, Object object) throws Exception
 91  
     {
 92  2487
         UMOImmutableEndpoint ep = (UMOImmutableEndpoint) key;
 93  
 
 94  2487
         boolean keepSocketOpen = MapUtils.getBooleanValue(ep.getProperties(),
 95  
             TcpConnector.KEEP_SEND_SOCKET_OPEN_PROPERTY, ((TcpConnector) ep.getConnector()).isKeepSendSocketOpen());
 96  2487
         Socket socket = (Socket) object;
 97  
 
 98  2487
         if (!keepSocketOpen)
 99  
         {
 100  
             try
 101  
             {
 102  2475
                 if (socket != null)
 103  
                 {
 104  2475
                     socket.close();
 105  
                 }
 106  
             }
 107  0
             catch (IOException e)
 108  
             {
 109  0
                 logger.debug("Failed to close socket after dispatch: " + e.getMessage());
 110  2475
             }
 111  
         }
 112  2487
     }
 113  
     
 114  
 }