View Javadoc

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  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      protected int sendTimeout = DEFAULT_SOCKET_TIMEOUT;
36      protected int receiveTimeout = DEFAULT_SOCKET_TIMEOUT;
37      protected int sendBufferSize = DEFAULT_BUFFER_SIZE;
38      protected int receiveBufferSize = DEFAULT_BUFFER_SIZE;
39      protected boolean keepSendSocketOpen = true;
40      protected boolean broadcast;
41      protected GenericKeyedObjectPool dispatcherSocketsPool = new GenericKeyedObjectPool();
42  
43  
44      protected void doInitialise() throws InitialisationException
45      {
46          dispatcherSocketsPool.setFactory(new UdpSocketFactory());
47          dispatcherSocketsPool.setTestOnBorrow(true);
48          dispatcherSocketsPool.setTestOnReturn(true);
49          //There should only be one pooled instance per socket (key)
50          dispatcherSocketsPool.setMaxActive(1);
51      }
52  
53      protected void doDispose()
54      {
55          try
56          {
57              dispatcherSocketsPool.close();
58          }
59          catch (Exception e)
60          {
61              logger.warn("Failed to close dispatcher socket pool: " + e.getMessage());
62          }
63      }
64  
65      protected void doConnect() throws Exception
66      {
67          // template method
68      }
69  
70      protected void doDisconnect() throws Exception
71      {
72          dispatcherSocketsPool.clear();
73      }
74  
75      protected void doStart() throws MuleException
76      {
77          // template method
78      }
79  
80      protected void doStop() throws MuleException
81      {
82          // template method
83      }
84  
85      public String getProtocol()
86      {
87          return UDP;
88      }
89  
90      public int getSendTimeout()
91      {
92          return this.sendTimeout;
93      }
94  
95      public void setSendTimeout(int timeout)
96      {
97          if (timeout < 0)
98          {
99              timeout = DEFAULT_SOCKET_TIMEOUT;
100         }
101         this.sendTimeout = timeout;
102     }
103 
104     public int getReceiveTimeout()
105     {
106         return receiveTimeout;
107     }
108 
109     public void setReceiveTimeout(int timeout)
110     {
111         if (timeout < 0)
112         {
113             timeout = DEFAULT_SOCKET_TIMEOUT;
114         }
115         this.receiveTimeout = timeout;
116     }
117 
118     public int getSendBufferSize()
119     {
120         return sendBufferSize;
121     }
122 
123     public void setSendBufferSize(int sendBufferSize)
124     {
125         if (sendBufferSize < 1)
126         {
127             sendBufferSize = DEFAULT_BUFFER_SIZE;
128         }
129         this.sendBufferSize = sendBufferSize;
130     }
131 
132     public int getReceiveBufferSize()
133     {
134         return receiveBufferSize;
135     }
136 
137     public void setReceiveBufferSize(int receiveBufferSize)
138     {
139         if (receiveBufferSize < 1)
140         {
141             receiveBufferSize = DEFAULT_BUFFER_SIZE;
142         }
143         this.receiveBufferSize = receiveBufferSize;
144     }
145 
146     public boolean isBroadcast()
147     {
148         return broadcast;
149     }
150 
151     public void setBroadcast(boolean broadcast)
152     {
153         this.broadcast = broadcast;
154     }
155 
156 
157     public boolean isKeepSendSocketOpen()
158     {
159         return keepSendSocketOpen;
160     }
161 
162     public void setKeepSendSocketOpen(boolean keepSendSocketOpen)
163     {
164         this.keepSendSocketOpen = keepSendSocketOpen;
165     }
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         return (DatagramSocket) dispatcherSocketsPool.borrowObject(endpoint);
177     }
178 
179     void releaseSocket(DatagramSocket socket, ImmutableEndpoint endpoint) throws Exception
180     {
181         dispatcherSocketsPool.returnObject(endpoint, socket);
182     }
183 
184 
185     protected Object getReceiverKey(Service service, InboundEndpoint endpoint)
186     {
187         return endpoint.getEndpointURI().getAddress() + "/" + service.getName();
188     }
189 }