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