View Javadoc

1   /*
2    * $Id: UdpConnector.java 7963 2007-08-21 08:53:15Z 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  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      protected int sendTimeout = DEFAULT_SOCKET_TIMEOUT;
36  
37      protected int receiveTimeout = DEFAULT_SOCKET_TIMEOUT;
38  
39      protected int sendBufferSize = DEFAULT_BUFFER_SIZE;
40  
41      protected int receiveBufferSize = DEFAULT_BUFFER_SIZE;
42  
43      protected boolean keepSendSocketOpen = true;
44  
45      protected boolean broadcast;
46  
47      protected GenericKeyedObjectPool dispatcherSocketsPool = new GenericKeyedObjectPool();
48  
49  
50      protected void doInitialise() throws InitialisationException
51      {
52          dispatcherSocketsPool.setFactory(new UdpSocketFactory());
53          dispatcherSocketsPool.setTestOnBorrow(true);
54          dispatcherSocketsPool.setTestOnReturn(true);
55          //There should only be one pooled instance per socket (key)
56          dispatcherSocketsPool.setMaxActive(1);
57      }
58  
59      protected void doDispose()
60      {
61          try
62          {
63              dispatcherSocketsPool.close();
64          }
65          catch (Exception e)
66          {
67              logger.warn("Failed to close dispatcher socket pool: " + e.getMessage());
68          }
69      }
70  
71      protected void doConnect() throws Exception
72      {
73          // template method
74      }
75  
76      protected void doDisconnect() throws Exception
77      {
78          dispatcherSocketsPool.clear();
79      }
80  
81      protected void doStart() throws UMOException
82      {
83          // template method
84      }
85  
86      protected void doStop() throws UMOException
87      {
88          // template method
89      }
90  
91      public String getProtocol()
92      {
93          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         setSendTimeout(timeout);
104         setReceiveTimeout(timeout);
105     }
106 
107     public int getSendTimeout()
108     {
109         return this.sendTimeout;
110     }
111 
112     public void setSendTimeout(int timeout)
113     {
114         if (timeout < 0)
115         {
116             timeout = DEFAULT_SOCKET_TIMEOUT;
117         }
118         this.sendTimeout = timeout;
119     }
120 
121     public int getReceiveTimeout()
122     {
123         return receiveTimeout;
124     }
125 
126     public void setReceiveTimeout(int timeout)
127     {
128         if (timeout < 0)
129         {
130             timeout = DEFAULT_SOCKET_TIMEOUT;
131         }
132         this.receiveTimeout = timeout;
133     }
134 
135     /**
136      * @return
137      * @deprecated Should use {@link #getSendBufferSize()} or {@link #getReceiveBufferSize()}
138      */
139     public int getBufferSize()
140     {
141         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         if (bufferSize < 1)
151         {
152             bufferSize = DEFAULT_BUFFER_SIZE;
153         }
154         this.sendBufferSize = bufferSize;
155     }
156 
157 
158     public int getSendBufferSize()
159     {
160         return sendBufferSize;
161     }
162 
163     public void setSendBufferSize(int sendBufferSize)
164     {
165         if (sendBufferSize < 1)
166         {
167             sendBufferSize = DEFAULT_BUFFER_SIZE;
168         }
169         this.sendBufferSize = sendBufferSize;
170     }
171 
172     public int getReceiveBufferSize()
173     {
174         return receiveBufferSize;
175     }
176 
177     public void setReceiveBufferSize(int receiveBufferSize)
178     {
179         if (receiveBufferSize < 1)
180         {
181             receiveBufferSize = DEFAULT_BUFFER_SIZE;
182         }
183         this.receiveBufferSize = receiveBufferSize;
184     }
185 
186     public boolean isBroadcast()
187     {
188         return broadcast;
189     }
190 
191     public void setBroadcast(boolean broadcast)
192     {
193         this.broadcast = broadcast;
194     }
195 
196 
197     public boolean isKeepSendSocketOpen()
198     {
199         return keepSendSocketOpen;
200     }
201 
202     public void setKeepSendSocketOpen(boolean keepSendSocketOpen)
203     {
204         this.keepSendSocketOpen = keepSendSocketOpen;
205     }
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         return (DatagramSocket) dispatcherSocketsPool.borrowObject(endpoint);
217     }
218 
219     void releaseSocket(DatagramSocket socket, UMOImmutableEndpoint endpoint) throws Exception
220     {
221         dispatcherSocketsPool.returnObject(endpoint, socket);
222     }
223 
224 
225     protected Object getReceiverKey(UMOComponent component, UMOEndpoint endpoint)
226     {
227         return endpoint.getEndpointURI().getAddress() + "/" + component.getDescriptor().getName();
228     }
229 }