Coverage Report - org.mule.providers.tcp.TcpSocketFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
TcpSocketFactory
0%
0/35
0%
0/7
2.167
 
 1  
 /*
 2  
  * $Id: TcpSocketFactory.java 7976 2007-08-21 14:26:13Z 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  0
 public class TcpSocketFactory implements PooledSocketFactory
 28  
 {
 29  
     /**
 30  
      * logger used by this class
 31  
      */
 32  0
     private static final Log logger = LogFactory.getLog(TcpSocketFactory.class);
 33  
 
 34  
     public Object makeObject(Object key) throws Exception
 35  
     {
 36  0
         UMOImmutableEndpoint ep = (UMOImmutableEndpoint) key;
 37  0
         int port = ep.getEndpointURI().getPort();
 38  0
         InetAddress inetAddress = InetAddress.getByName(ep.getEndpointURI().getHost());
 39  0
         Socket socket = createSocket(port, inetAddress);
 40  0
         socket.setReuseAddress(true);
 41  
 
 42  0
         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  0
         if (connector.getReceiveBufferSize() != UMOConnector.INT_VALUE_NOT_SET
 46  
             && socket.getReceiveBufferSize() != connector.getReceiveBufferSize())
 47  
         {
 48  0
             socket.setReceiveBufferSize(connector.getReceiveBufferSize());
 49  
         }
 50  0
         if (connector.getSendBufferSize() != UMOConnector.INT_VALUE_NOT_SET
 51  
             && socket.getSendBufferSize() != connector.getSendBufferSize())
 52  
         {
 53  0
             socket.setSendBufferSize(connector.getSendBufferSize());
 54  
         }
 55  0
         if (connector.getReceiveTimeout() != UMOConnector.INT_VALUE_NOT_SET
 56  
             && socket.getSoTimeout() != connector.getReceiveTimeout())
 57  
         {
 58  0
             socket.setSoTimeout(connector.getSendTimeout());
 59  
         }
 60  0
         socket.setTcpNoDelay(connector.isSendTcpNoDelay());
 61  0
         socket.setKeepAlive(connector.isKeepAlive());
 62  0
         return socket;
 63  
     }
 64  
 
 65  
     protected Socket createSocket(int port, InetAddress inetAddress) throws IOException
 66  
     {
 67  0
         return new Socket(inetAddress, port);
 68  
     }
 69  
 
 70  
     public void destroyObject(Object key, Object object) throws Exception
 71  
     {
 72  0
         Socket socket = (Socket) object;
 73  0
         if(!socket.isClosed())
 74  
         {
 75  0
             socket.close();
 76  
         }
 77  0
     }
 78  
 
 79  
     public boolean validateObject(Object key, Object object)
 80  
     {
 81  0
         Socket socket = (Socket) object;
 82  0
         return !socket.isClosed();       
 83  
     }
 84  
 
 85  
     public void activateObject(Object key, Object object) throws Exception
 86  
     {
 87  
         // cannot really activate a Socket
 88  0
     }
 89  
 
 90  
     public void passivateObject(Object key, Object object) throws Exception
 91  
     {
 92  0
         UMOImmutableEndpoint ep = (UMOImmutableEndpoint) key;
 93  
 
 94  0
         boolean keepSocketOpen = MapUtils.getBooleanValue(ep.getProperties(),
 95  
             TcpConnector.KEEP_SEND_SOCKET_OPEN_PROPERTY, ((TcpConnector) ep.getConnector()).isKeepSendSocketOpen());
 96  0
         Socket socket = (Socket) object;
 97  
 
 98  0
         if (!keepSocketOpen)
 99  
         {
 100  
             try
 101  
             {
 102  0
                 if (socket != null)
 103  
                 {
 104  0
                     socket.close();
 105  
                 }
 106  
             }
 107  0
             catch (IOException e)
 108  
             {
 109  0
                 logger.debug("Failed to close socket after dispatch: " + e.getMessage());
 110  0
             }
 111  
         }
 112  0
     }
 113  
     
 114  
 }