Coverage Report - org.mule.transport.tcp.AbstractTcpSocketFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractTcpSocketFactory
0%
0/25
0%
0/8
1.667
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.transport.tcp;
 8  
 
 9  
 import org.mule.util.MapUtils;
 10  
 
 11  
 import java.io.IOException;
 12  
 import java.net.Socket;
 13  
 
 14  
 import org.apache.commons.logging.Log;
 15  
 import org.apache.commons.logging.LogFactory;
 16  
 import org.apache.commons.pool.KeyedPoolableObjectFactory;
 17  
 
 18  
 /**
 19  
  * Creates a client socket using the socket address extracted from the endpoint.  Addtional
 20  
  * socket parameters will also be set from the connector
 21  
  */
 22  0
 public abstract class AbstractTcpSocketFactory implements KeyedPoolableObjectFactory
 23  
 {
 24  
 
 25  
     /**
 26  
      * logger used by this class
 27  
      */
 28  0
     private static final Log logger = LogFactory.getLog(TcpSocketFactory.class);
 29  
 
 30  
     public Object makeObject(Object key) throws Exception
 31  
     {
 32  0
         TcpSocketKey socketKey = (TcpSocketKey) key;
 33  
 
 34  0
         Socket socket = createSocket(socketKey);
 35  0
         socket.setReuseAddress(true);
 36  
 
 37  0
         TcpConnector connector = socketKey.getConnector();
 38  0
         connector.configureSocket(TcpConnector.CLIENT, socket);
 39  
 
 40  0
         return socket;
 41  
     }
 42  
 
 43  
     protected abstract Socket createSocket(TcpSocketKey key) throws IOException;
 44  
 
 45  
     public void destroyObject(Object key, Object object) throws Exception
 46  
     {
 47  0
         Socket socket = (Socket) object;
 48  0
         if(!socket.isClosed())
 49  
         {
 50  0
             socket.close();
 51  
         }
 52  0
     }
 53  
 
 54  
     public boolean validateObject(Object key, Object object)
 55  
     {
 56  0
         Socket socket = (Socket) object;
 57  0
         return !socket.isClosed();
 58  
     }
 59  
 
 60  
     public void activateObject(Object key, Object object) throws Exception
 61  
     {
 62  
         // cannot really activate a Socket
 63  0
     }
 64  
 
 65  
     public void passivateObject(Object key, Object object) throws Exception
 66  
     {
 67  0
         TcpSocketKey socketKey = (TcpSocketKey) key;
 68  
 
 69  0
         boolean keepSocketOpen = MapUtils.getBooleanValue(socketKey.getEndpoint().getProperties(),
 70  
             TcpConnector.KEEP_SEND_SOCKET_OPEN_PROPERTY, socketKey.getConnector().isKeepSendSocketOpen());
 71  0
         Socket socket = (Socket) object;
 72  
 
 73  0
         if (!keepSocketOpen)
 74  
         {
 75  
             try
 76  
             {
 77  0
                 if (socket != null)
 78  
                 {
 79  0
                     socket.close();
 80  
                 }
 81  
             }
 82  0
             catch (IOException e)
 83  
             {
 84  0
                 logger.debug("Failed to close socket after dispatch: " + e.getMessage());
 85  0
             }
 86  
         }
 87  0
     }
 88  
 
 89  
 }