Coverage Report - org.mule.transport.tcp.AbstractTcpSocketFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractTcpSocketFactory
92%
23/25
88%
7/8
1.667
 
 1  
 /*
 2  
  * $Id: AbstractTcpSocketFactory.java 10489 2008-01-23 17:53:38Z 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  
 package org.mule.transport.tcp;
 11  
 
 12  
 import org.mule.util.MapUtils;
 13  
 
 14  
 import java.io.IOException;
 15  
 import java.net.Socket;
 16  
 
 17  
 import org.apache.commons.logging.Log;
 18  
 import org.apache.commons.logging.LogFactory;
 19  
 import org.apache.commons.pool.KeyedPoolableObjectFactory;
 20  
 
 21  
 /**
 22  
  * Creates a client socket using the socket address extracted from the endpoint.  Addtional
 23  
  * socket parameters will also be set from the connector
 24  
  */
 25  132
 public abstract class AbstractTcpSocketFactory implements KeyedPoolableObjectFactory
 26  
 {
 27  
 
 28  
     /**
 29  
      * logger used by this class
 30  
      */
 31  2
     private static final Log logger = LogFactory.getLog(TcpSocketFactory.class);
 32  
 
 33  
     public Object makeObject(Object key) throws Exception
 34  
     {
 35  2577
         TcpSocketKey socketKey = (TcpSocketKey) key;
 36  
 
 37  2577
         Socket socket = createSocket(socketKey);
 38  1491
         socket.setReuseAddress(true);
 39  
 
 40  1491
         TcpConnector connector = socketKey.getConnector();
 41  1491
         connector.configureSocket(TcpConnector.CLIENT, socket);
 42  
 
 43  1491
         return socket;
 44  
     }
 45  
 
 46  
     protected abstract Socket createSocket(TcpSocketKey key) throws IOException;
 47  
 
 48  
     public void destroyObject(Object key, Object object) throws Exception
 49  
     {
 50  1491
         Socket socket = (Socket) object;
 51  1491
         if(!socket.isClosed())
 52  
         {
 53  22
             socket.close();
 54  
         }
 55  1491
     }
 56  
 
 57  
     public boolean validateObject(Object key, Object object)
 58  
     {
 59  5086
         Socket socket = (Socket) object;
 60  5086
         return !socket.isClosed();
 61  
     }
 62  
 
 63  
     public void activateObject(Object key, Object object) throws Exception
 64  
     {
 65  
         // cannot really activate a Socket
 66  3249
     }
 67  
 
 68  
     public void passivateObject(Object key, Object object) throws Exception
 69  
     {
 70  1823
         TcpSocketKey socketKey = (TcpSocketKey) key;
 71  
 
 72  1823
         boolean keepSocketOpen = MapUtils.getBooleanValue(socketKey.getEndpoint().getProperties(),
 73  
             TcpConnector.KEEP_SEND_SOCKET_OPEN_PROPERTY, socketKey.getConnector().isKeepSendSocketOpen());
 74  1823
         Socket socket = (Socket) object;
 75  
 
 76  1823
         if (!keepSocketOpen)
 77  
         {
 78  
             try
 79  
             {
 80  1455
                 if (socket != null)
 81  
                 {
 82  1455
                     socket.close();
 83  
                 }
 84  
             }
 85  0
             catch (IOException e)
 86  
             {
 87  0
                 logger.debug("Failed to close socket after dispatch: " + e.getMessage());
 88  1455
             }
 89  
         }
 90  1823
     }
 91  
 
 92  
 }