View Javadoc

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