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