Coverage Report - org.mule.transport.udp.UdpConnector
 
Classes in this File Line Coverage Branch Coverage Complexity
UdpConnector
92%
49/53
75%
6/8
1.227
 
 1  
 /*
 2  
  * $Id: UdpConnector.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  
 
 11  
 package org.mule.transport.udp;
 12  
 
 13  
 import org.mule.api.MuleException;
 14  
 import org.mule.api.endpoint.ImmutableEndpoint;
 15  
 import org.mule.api.endpoint.InboundEndpoint;
 16  
 import org.mule.api.lifecycle.InitialisationException;
 17  
 import org.mule.api.service.Service;
 18  
 import org.mule.transport.AbstractConnector;
 19  
 
 20  
 import java.net.DatagramSocket;
 21  
 
 22  
 import org.apache.commons.pool.impl.GenericKeyedObjectPool;
 23  
 
 24  
 /**
 25  
  * <code>UdpConnector</code> can send and receive Mule events as Datagram packets.
 26  
  */
 27  30
 public class UdpConnector extends AbstractConnector
 28  
 {
 29  
 
 30  
     public static final String UDP = "udp";
 31  
     public static final int DEFAULT_SOCKET_TIMEOUT = INT_VALUE_NOT_SET;
 32  
     public static final int DEFAULT_BUFFER_SIZE = 1024 * 16;
 33  
     public static final String KEEP_SEND_SOCKET_OPEN_PROPERTY = "keepSendSocketOpen";
 34  
 
 35  30
     protected int sendTimeout = DEFAULT_SOCKET_TIMEOUT;
 36  30
     protected int receiveTimeout = DEFAULT_SOCKET_TIMEOUT;
 37  30
     protected int sendBufferSize = DEFAULT_BUFFER_SIZE;
 38  30
     protected int receiveBufferSize = DEFAULT_BUFFER_SIZE;
 39  30
     protected boolean keepSendSocketOpen = true;
 40  
     protected boolean broadcast;
 41  30
     protected GenericKeyedObjectPool dispatcherSocketsPool = new GenericKeyedObjectPool();
 42  
 
 43  
 
 44  
     protected void doInitialise() throws InitialisationException
 45  
     {
 46  28
         dispatcherSocketsPool.setFactory(new UdpSocketFactory());
 47  28
         dispatcherSocketsPool.setTestOnBorrow(true);
 48  28
         dispatcherSocketsPool.setTestOnReturn(true);
 49  
         //There should only be one pooled instance per socket (key)
 50  28
         dispatcherSocketsPool.setMaxActive(1);
 51  28
     }
 52  
 
 53  
     protected void doDispose()
 54  
     {
 55  
         try
 56  
         {
 57  48
             dispatcherSocketsPool.close();
 58  
         }
 59  0
         catch (Exception e)
 60  
         {
 61  0
             logger.warn("Failed to close dispatcher socket pool: " + e.getMessage());
 62  48
         }
 63  48
     }
 64  
 
 65  
     protected void doConnect() throws Exception
 66  
     {
 67  
         // template method
 68  26
     }
 69  
 
 70  
     protected void doDisconnect() throws Exception
 71  
     {
 72  26
         dispatcherSocketsPool.clear();
 73  26
     }
 74  
 
 75  
     protected void doStart() throws MuleException
 76  
     {
 77  
         // template method
 78  26
     }
 79  
 
 80  
     protected void doStop() throws MuleException
 81  
     {
 82  
         // template method
 83  26
     }
 84  
 
 85  
     public String getProtocol()
 86  
     {
 87  3070
         return UDP;
 88  
     }
 89  
 
 90  
     public int getSendTimeout()
 91  
     {
 92  2
         return this.sendTimeout;
 93  
     }
 94  
 
 95  
     public void setSendTimeout(int timeout)
 96  
     {
 97  2
         if (timeout < 0)
 98  
         {
 99  0
             timeout = DEFAULT_SOCKET_TIMEOUT;
 100  
         }
 101  2
         this.sendTimeout = timeout;
 102  2
     }
 103  
 
 104  
     public int getReceiveTimeout()
 105  
     {
 106  8
         return receiveTimeout;
 107  
     }
 108  
 
 109  
     public void setReceiveTimeout(int timeout)
 110  
     {
 111  4
         if (timeout < 0)
 112  
         {
 113  2
             timeout = DEFAULT_SOCKET_TIMEOUT;
 114  
         }
 115  4
         this.receiveTimeout = timeout;
 116  4
     }
 117  
 
 118  
     public int getSendBufferSize()
 119  
     {
 120  14
         return sendBufferSize;
 121  
     }
 122  
 
 123  
     public void setSendBufferSize(int sendBufferSize)
 124  
     {
 125  2
         if (sendBufferSize < 1)
 126  
         {
 127  0
             sendBufferSize = DEFAULT_BUFFER_SIZE;
 128  
         }
 129  2
         this.sendBufferSize = sendBufferSize;
 130  2
     }
 131  
 
 132  
     public int getReceiveBufferSize()
 133  
     {
 134  26
         return receiveBufferSize;
 135  
     }
 136  
 
 137  
     public void setReceiveBufferSize(int receiveBufferSize)
 138  
     {
 139  6
         if (receiveBufferSize < 1)
 140  
         {
 141  2
             receiveBufferSize = DEFAULT_BUFFER_SIZE;
 142  
         }
 143  6
         this.receiveBufferSize = receiveBufferSize;
 144  6
     }
 145  
 
 146  
     public boolean isBroadcast()
 147  
     {
 148  6
         return broadcast;
 149  
     }
 150  
 
 151  
     public void setBroadcast(boolean broadcast)
 152  
     {
 153  2
         this.broadcast = broadcast;
 154  2
     }
 155  
 
 156  
 
 157  
     public boolean isKeepSendSocketOpen()
 158  
     {
 159  3004
         return keepSendSocketOpen;
 160  
     }
 161  
 
 162  
     public void setKeepSendSocketOpen(boolean keepSendSocketOpen)
 163  
     {
 164  2
         this.keepSendSocketOpen = keepSendSocketOpen;
 165  2
     }
 166  
 
 167  
     /**
 168  
      * Lookup a socket in the list of dispatcher sockets but don't create a new
 169  
      * socket
 170  
      *
 171  
      * @param endpoint
 172  
      * @return
 173  
      */
 174  
     DatagramSocket getSocket(ImmutableEndpoint endpoint) throws Exception
 175  
     {
 176  3004
         return (DatagramSocket) dispatcherSocketsPool.borrowObject(endpoint);
 177  
     }
 178  
 
 179  
     void releaseSocket(DatagramSocket socket, ImmutableEndpoint endpoint) throws Exception
 180  
     {
 181  3002
         dispatcherSocketsPool.returnObject(endpoint, socket);
 182  3002
     }
 183  
 
 184  
 
 185  
     protected Object getReceiverKey(Service service, InboundEndpoint endpoint)
 186  
     {
 187  22
         return endpoint.getEndpointURI().getAddress() + "/" + service.getName();
 188  
     }
 189  
 }