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  
  * $Id: AbstractTcpSocketFactory.java 19191 2010-08-25 21:05:23Z tcarlson $
 3  
  * --------------------------------------------------------------------------------------
 4  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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  0
 public abstract class AbstractTcpSocketFactory implements KeyedPoolableObjectFactory
 26  
 {
 27  
 
 28  
     /**
 29  
      * logger used by this class
 30  
      */
 31  0
     private static final Log logger = LogFactory.getLog(TcpSocketFactory.class);
 32  
 
 33  
     public Object makeObject(Object key) throws Exception
 34  
     {
 35  0
         TcpSocketKey socketKey = (TcpSocketKey) key;
 36  
 
 37  0
         Socket socket = createSocket(socketKey);
 38  0
         socket.setReuseAddress(true);
 39  
 
 40  0
         TcpConnector connector = socketKey.getConnector();
 41  0
         connector.configureSocket(TcpConnector.CLIENT, socket);
 42  
 
 43  0
         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  0
         Socket socket = (Socket) object;
 51  0
         if(!socket.isClosed())
 52  
         {
 53  0
             socket.close();
 54  
         }
 55  0
     }
 56  
 
 57  
     public boolean validateObject(Object key, Object object)
 58  
     {
 59  0
         Socket socket = (Socket) object;
 60  0
         return !socket.isClosed();
 61  
     }
 62  
 
 63  
     public void activateObject(Object key, Object object) throws Exception
 64  
     {
 65  
         // cannot really activate a Socket
 66  0
     }
 67  
 
 68  
     public void passivateObject(Object key, Object object) throws Exception
 69  
     {
 70  0
         TcpSocketKey socketKey = (TcpSocketKey) key;
 71  
 
 72  0
         boolean keepSocketOpen = MapUtils.getBooleanValue(socketKey.getEndpoint().getProperties(),
 73  
             TcpConnector.KEEP_SEND_SOCKET_OPEN_PROPERTY, socketKey.getConnector().isKeepSendSocketOpen());
 74  0
         Socket socket = (Socket) object;
 75  
 
 76  0
         if (!keepSocketOpen)
 77  
         {
 78  
             try
 79  
             {
 80  0
                 if (socket != null)
 81  
                 {
 82  0
                     socket.close();
 83  
                 }
 84  
             }
 85  0
             catch (IOException e)
 86  
             {
 87  0
                 logger.debug("Failed to close socket after dispatch: " + e.getMessage());
 88  0
             }
 89  
         }
 90  0
     }
 91  
 
 92  
 }