Coverage Report - org.mule.providers.udp.UdpSocketFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
UdpSocketFactory
82%
32/39
50%
14/28
2.125
 
 1  
 /*
 2  
  * $Id: UdpSocketFactory.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.udp;
 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.DatagramSocket;
 18  
 import java.net.InetAddress;
 19  
 import java.net.Socket;
 20  
 
 21  
 import org.apache.commons.logging.Log;
 22  
 import org.apache.commons.logging.LogFactory;
 23  
 import org.apache.commons.pool.KeyedPoolableObjectFactory;
 24  
 
 25  
 /**
 26  
  * Creates a client socket using the host and port address supplied in the endpoint URI.  Addtional
 27  
  * socket parameters will also be set from the connector
 28  
  */
 29  22
 public class UdpSocketFactory implements KeyedPoolableObjectFactory
 30  
 {
 31  
     /**
 32  
      * logger used by this class
 33  
      */
 34  24
     protected transient final Log logger = LogFactory.getLog(UdpSocketFactory.class);
 35  
 
 36  
     public Object makeObject(Object key) throws Exception
 37  
     {
 38  8
         UMOImmutableEndpoint ep = (UMOImmutableEndpoint)key;
 39  
         DatagramSocket socket;
 40  
 
 41  8
         if(ep.getType() == UMOImmutableEndpoint.ENDPOINT_TYPE_RECEIVER)
 42  
         {
 43  4
             int port = ep.getEndpointURI().getPort();
 44  4
             String host = ep.getEndpointURI().getHost();
 45  4
             if(port > 0)
 46  
             {
 47  4
                 if("null".equalsIgnoreCase(host))
 48  
                 {
 49  2
                     socket = createSocket(port);
 50  
                 }
 51  
                 else
 52  
                 {
 53  2
                     socket = createSocket(port, InetAddress.getByName(host));
 54  
                 }
 55  
             }
 56  
             else
 57  
             {
 58  0
                 socket = createSocket();
 59  
             }
 60  4
         }
 61  
         else
 62  
         {
 63  
             //If this is a client socket create a default instance
 64  4
             socket = createSocket();
 65  
         }
 66  
 
 67  8
         UdpConnector connector = (UdpConnector)ep.getConnector();
 68  
         //There is some overhead in stting socket timeout and buffer size, so we're
 69  
         //careful here only to set if needed
 70  8
         if (connector.getReceiveBufferSize() != UMOConnector.INT_VALUE_NOT_SET
 71  
             && socket.getReceiveBufferSize() != connector.getReceiveBufferSize())
 72  
         {
 73  8
             socket.setReceiveBufferSize(connector.getReceiveBufferSize());
 74  
         }
 75  8
         if (connector.getSendBufferSize() != UMOConnector.INT_VALUE_NOT_SET
 76  
             && socket.getSendBufferSize() != connector.getSendBufferSize())
 77  
         {
 78  8
             socket.setSendBufferSize(connector.getSendBufferSize());
 79  
         }
 80  8
         if (connector.getReceiveTimeout() != UMOConnector.INT_VALUE_NOT_SET
 81  
             && socket.getSoTimeout() != connector.getReceiveTimeout())
 82  
         {
 83  0
             socket.setSoTimeout(connector.getSendTimeout());
 84  
         }
 85  8
         socket.setBroadcast(connector.isBroadcast());
 86  8
         return socket;
 87  
     }
 88  
 
 89  
     public void destroyObject(Object key, Object object) throws Exception
 90  
     {
 91  4
         Socket socket = (Socket)object;
 92  0
         if(!socket.isClosed())
 93  
         {
 94  0
             socket.close();
 95  
         }
 96  0
     }
 97  
 
 98  
     public boolean validateObject(Object key, Object object)
 99  
     {
 100  4016
         DatagramSocket socket = (DatagramSocket)object;
 101  4016
         return !socket.isClosed();
 102  
     }
 103  
 
 104  
     public void activateObject(Object key, Object object) throws Exception
 105  
     {
 106  
         // nothing to do
 107  2010
     }
 108  
 
 109  
     public void passivateObject(Object key, Object object) throws Exception
 110  
     {
 111  2006
         UMOImmutableEndpoint ep = (UMOImmutableEndpoint)key;
 112  
 
 113  2006
         boolean keepSocketOpen = MapUtils.getBooleanValue(ep.getProperties(),
 114  
             UdpConnector.KEEP_SEND_SOCKET_OPEN_PROPERTY, ((UdpConnector)ep.getConnector()).isKeepSendSocketOpen());
 115  2006
         DatagramSocket socket = (DatagramSocket)object;
 116  
 
 117  2006
         if (!keepSocketOpen)
 118  
         {
 119  0
             if (socket != null)
 120  
             {
 121  0
                 socket.close();
 122  
             }
 123  
         }
 124  2006
     }
 125  
 
 126  
     protected DatagramSocket createSocket() throws IOException
 127  
     {
 128  4
         return new DatagramSocket();
 129  
     }
 130  
 
 131  
     protected DatagramSocket createSocket(int port) throws IOException
 132  
     {
 133  2
         return new DatagramSocket(port);
 134  
     }
 135  
 
 136  
     protected DatagramSocket createSocket(int port, InetAddress inetAddress) throws IOException
 137  
     {
 138  2
         return new DatagramSocket(port, inetAddress);
 139  
     }
 140  
 }