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  
  * $Id: UdpSocketFactory.java 20321 2010-11-24 15:21:24Z dfeist $
 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.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  0
 public class UdpSocketFactory implements KeyedPoolableObjectFactory
 31  
 {
 32  
     /**
 33  
      * logger used by this class
 34  
      */
 35  0
     protected transient final Log logger = LogFactory.getLog(UdpSocketFactory.class);
 36  
 
 37  
     public Object makeObject(Object key) throws Exception
 38  
     {
 39  0
         ImmutableEndpoint ep = (ImmutableEndpoint)key;
 40  
         DatagramSocket socket;
 41  
 
 42  0
         if(ep instanceof InboundEndpoint)
 43  
         {
 44  0
             int port = ep.getEndpointURI().getPort();
 45  0
             String host = ep.getEndpointURI().getHost();
 46  0
             if(port > 0)
 47  
             {
 48  0
                 if("null".equalsIgnoreCase(host))
 49  
                 {
 50  0
                     socket = createSocket(port);
 51  
                 }
 52  
                 else
 53  
                 {
 54  0
                     socket = createSocket(port, InetAddress.getByName(host));
 55  
                 }
 56  
             }
 57  
             else
 58  
             {
 59  0
                 socket = createSocket();
 60  
             }
 61  0
         }
 62  
         else
 63  
         {
 64  
             //If this is a client socket create a default instance
 65  0
             socket = createSocket();
 66  
         }
 67  
 
 68  0
         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  0
         if (connector.getReceiveBufferSize() != Connector.INT_VALUE_NOT_SET
 72  
             && socket.getReceiveBufferSize() != connector.getReceiveBufferSize())
 73  
         {
 74  0
             socket.setReceiveBufferSize(connector.getReceiveBufferSize());
 75  
         }
 76  0
         if (connector.getSendBufferSize() != Connector.INT_VALUE_NOT_SET
 77  
             && socket.getSendBufferSize() != connector.getSendBufferSize())
 78  
         {
 79  0
             socket.setSendBufferSize(connector.getSendBufferSize());
 80  
         }
 81  0
         if (connector.getReceiveTimeout() != Connector.INT_VALUE_NOT_SET
 82  
             && socket.getSoTimeout() != connector.getReceiveTimeout())
 83  
         {
 84  0
             socket.setSoTimeout(connector.getSendTimeout());
 85  
         }
 86  0
         socket.setBroadcast(connector.isBroadcast());
 87  0
         return socket;
 88  
     }
 89  
 
 90  
     public void destroyObject(Object key, Object object) throws Exception
 91  
     {
 92  0
         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  0
         DatagramSocket socket = (DatagramSocket)object;
 102  0
         return !socket.isClosed();
 103  
     }
 104  
 
 105  
     public void activateObject(Object key, Object object) throws Exception
 106  
     {
 107  
         // nothing to do        
 108  0
     }
 109  
 
 110  
     public void passivateObject(Object key, Object object) throws Exception
 111  
     {
 112  0
         ImmutableEndpoint ep = (ImmutableEndpoint)key;
 113  
 
 114  0
         boolean keepSocketOpen = MapUtils.getBooleanValue(ep.getProperties(),
 115  
             UdpConnector.KEEP_SEND_SOCKET_OPEN_PROPERTY, ((UdpConnector)ep.getConnector()).isKeepSendSocketOpen());
 116  0
         DatagramSocket socket = (DatagramSocket)object;
 117  
 
 118  0
         if (!keepSocketOpen)
 119  
         {
 120  0
             if (socket != null)
 121  
             {
 122  0
                 socket.close();
 123  
             }
 124  
         }
 125  0
     }
 126  
 
 127  
     protected DatagramSocket createSocket() throws IOException
 128  
     {
 129  0
         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  0
         return new DatagramSocket(port, inetAddress);
 140  
     }
 141  
 }