Coverage Report - org.mule.providers.udp.UdpConnector
 
Classes in this File Line Coverage Branch Coverage Complexity
UdpConnector
0%
0/61
0%
0/5
1.24
 
 1  
 /*
 2  
  * $Id: UdpConnector.java 7976 2007-08-21 14:26:13Z 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  
 
 11  
 package org.mule.providers.udp;
 12  
 
 13  
 import org.mule.providers.AbstractConnector;
 14  
 import org.mule.umo.UMOComponent;
 15  
 import org.mule.umo.UMOException;
 16  
 import org.mule.umo.endpoint.UMOEndpoint;
 17  
 import org.mule.umo.endpoint.UMOImmutableEndpoint;
 18  
 import org.mule.umo.lifecycle.InitialisationException;
 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  0
 public class UdpConnector extends AbstractConnector
 28  
 {
 29  
     public static final int DEFAULT_SOCKET_TIMEOUT = INT_VALUE_NOT_SET;
 30  
     public static final int DEFAULT_BUFFER_SIZE = 1024 * 16;
 31  
 
 32  
     public static final String KEEP_SEND_SOCKET_OPEN_PROPERTY = "keepSendSocketOpen";
 33  
 
 34  
 
 35  0
     protected int sendTimeout = DEFAULT_SOCKET_TIMEOUT;
 36  
 
 37  0
     protected int receiveTimeout = DEFAULT_SOCKET_TIMEOUT;
 38  
 
 39  0
     protected int sendBufferSize = DEFAULT_BUFFER_SIZE;
 40  
 
 41  0
     protected int receiveBufferSize = DEFAULT_BUFFER_SIZE;
 42  
 
 43  0
     protected boolean keepSendSocketOpen = true;
 44  
 
 45  
     protected boolean broadcast;
 46  
 
 47  0
     protected GenericKeyedObjectPool dispatcherSocketsPool = new GenericKeyedObjectPool();
 48  
 
 49  
 
 50  
     protected void doInitialise() throws InitialisationException
 51  
     {
 52  0
         dispatcherSocketsPool.setFactory(new UdpSocketFactory());
 53  0
         dispatcherSocketsPool.setTestOnBorrow(true);
 54  0
         dispatcherSocketsPool.setTestOnReturn(true);
 55  
         //There should only be one pooled instance per socket (key)
 56  0
         dispatcherSocketsPool.setMaxActive(1);
 57  0
     }
 58  
 
 59  
     protected void doDispose()
 60  
     {
 61  
         try
 62  
         {
 63  0
             dispatcherSocketsPool.close();
 64  
         }
 65  0
         catch (Exception e)
 66  
         {
 67  0
             logger.warn("Failed to close dispatcher socket pool: " + e.getMessage());
 68  0
         }
 69  0
     }
 70  
 
 71  
     protected void doConnect() throws Exception
 72  
     {
 73  
         // template method
 74  0
     }
 75  
 
 76  
     protected void doDisconnect() throws Exception
 77  
     {
 78  0
         dispatcherSocketsPool.clear();
 79  0
     }
 80  
 
 81  
     protected void doStart() throws UMOException
 82  
     {
 83  
         // template method
 84  0
     }
 85  
 
 86  
     protected void doStop() throws UMOException
 87  
     {
 88  
         // template method
 89  0
     }
 90  
 
 91  
     public String getProtocol()
 92  
     {
 93  0
         return "udp";
 94  
     }
 95  
 
 96  
     /**
 97  
      * A shorthand property setting timeout for both SEND and RECEIVE sockets.
 98  
      *
 99  
      * @deprecated The time out should be set explicitly for each
 100  
      */
 101  
     public void setTimeout(int timeout)
 102  
     {
 103  0
         setSendTimeout(timeout);
 104  0
         setReceiveTimeout(timeout);
 105  0
     }
 106  
 
 107  
     public int getSendTimeout()
 108  
     {
 109  0
         return this.sendTimeout;
 110  
     }
 111  
 
 112  
     public void setSendTimeout(int timeout)
 113  
     {
 114  0
         if (timeout < 0)
 115  
         {
 116  0
             timeout = DEFAULT_SOCKET_TIMEOUT;
 117  
         }
 118  0
         this.sendTimeout = timeout;
 119  0
     }
 120  
 
 121  
     public int getReceiveTimeout()
 122  
     {
 123  0
         return receiveTimeout;
 124  
     }
 125  
 
 126  
     public void setReceiveTimeout(int timeout)
 127  
     {
 128  0
         if (timeout < 0)
 129  
         {
 130  0
             timeout = DEFAULT_SOCKET_TIMEOUT;
 131  
         }
 132  0
         this.receiveTimeout = timeout;
 133  0
     }
 134  
 
 135  
     /**
 136  
      * @return
 137  
      * @deprecated Should use {@link #getSendBufferSize()} or {@link #getReceiveBufferSize()}
 138  
      */
 139  
     public int getBufferSize()
 140  
     {
 141  0
         return sendBufferSize;
 142  
     }
 143  
 
 144  
     /**
 145  
      * @param bufferSize
 146  
      * @deprecated Should use {@link #setSendBufferSize(int)} or {@link #setReceiveBufferSize(int)}
 147  
      */
 148  
     public void setBufferSize(int bufferSize)
 149  
     {
 150  0
         if (bufferSize < 1)
 151  
         {
 152  0
             bufferSize = DEFAULT_BUFFER_SIZE;
 153  
         }
 154  0
         this.sendBufferSize = bufferSize;
 155  0
     }
 156  
 
 157  
 
 158  
     public int getSendBufferSize()
 159  
     {
 160  0
         return sendBufferSize;
 161  
     }
 162  
 
 163  
     public void setSendBufferSize(int sendBufferSize)
 164  
     {
 165  0
         if (sendBufferSize < 1)
 166  
         {
 167  0
             sendBufferSize = DEFAULT_BUFFER_SIZE;
 168  
         }
 169  0
         this.sendBufferSize = sendBufferSize;
 170  0
     }
 171  
 
 172  
     public int getReceiveBufferSize()
 173  
     {
 174  0
         return receiveBufferSize;
 175  
     }
 176  
 
 177  
     public void setReceiveBufferSize(int receiveBufferSize)
 178  
     {
 179  0
         if (receiveBufferSize < 1)
 180  
         {
 181  0
             receiveBufferSize = DEFAULT_BUFFER_SIZE;
 182  
         }
 183  0
         this.receiveBufferSize = receiveBufferSize;
 184  0
     }
 185  
 
 186  
     public boolean isBroadcast()
 187  
     {
 188  0
         return broadcast;
 189  
     }
 190  
 
 191  
     public void setBroadcast(boolean broadcast)
 192  
     {
 193  0
         this.broadcast = broadcast;
 194  0
     }
 195  
 
 196  
 
 197  
     public boolean isKeepSendSocketOpen()
 198  
     {
 199  0
         return keepSendSocketOpen;
 200  
     }
 201  
 
 202  
     public void setKeepSendSocketOpen(boolean keepSendSocketOpen)
 203  
     {
 204  0
         this.keepSendSocketOpen = keepSendSocketOpen;
 205  0
     }
 206  
 
 207  
     /**
 208  
      * Lookup a socket in the list of dispatcher sockets but don't create a new
 209  
      * socket
 210  
      *
 211  
      * @param endpoint
 212  
      * @return
 213  
      */
 214  
     DatagramSocket getSocket(UMOImmutableEndpoint endpoint) throws Exception
 215  
     {
 216  0
         return (DatagramSocket) dispatcherSocketsPool.borrowObject(endpoint);
 217  
     }
 218  
 
 219  
     void releaseSocket(DatagramSocket socket, UMOImmutableEndpoint endpoint) throws Exception
 220  
     {
 221  0
         dispatcherSocketsPool.returnObject(endpoint, socket);
 222  0
     }
 223  
 
 224  
 
 225  
     protected Object getReceiverKey(UMOComponent component, UMOEndpoint endpoint)
 226  
     {
 227  0
         return endpoint.getEndpointURI().getAddress() + "/" + component.getDescriptor().getName();
 228  
     }
 229  
 }