View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.udp;
8   
9   import org.mule.api.endpoint.ImmutableEndpoint;
10  import org.mule.api.endpoint.InboundEndpoint;
11  import org.mule.api.transport.Connector;
12  import org.mule.util.MapUtils;
13  
14  import java.io.IOException;
15  import java.net.DatagramSocket;
16  import java.net.InetAddress;
17  import java.net.Socket;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  import org.apache.commons.pool.KeyedPoolableObjectFactory;
22  
23  /**
24   * Creates a client socket using the host and port address supplied in the endpoint URI.  Addtional
25   * socket parameters will also be set from the connector
26   */
27  public class UdpSocketFactory implements KeyedPoolableObjectFactory
28  {
29      /**
30       * logger used by this class
31       */
32      protected transient final Log logger = LogFactory.getLog(UdpSocketFactory.class);
33  
34      public Object makeObject(Object key) throws Exception
35      {
36          ImmutableEndpoint ep = (ImmutableEndpoint)key;
37          DatagramSocket socket;
38  
39          if(ep instanceof InboundEndpoint)
40          {
41              int port = ep.getEndpointURI().getPort();
42              String host = ep.getEndpointURI().getHost();
43              if(port > 0)
44              {
45                  if("null".equalsIgnoreCase(host))
46                  {
47                      socket = createSocket(port);
48                  }
49                  else
50                  {
51                      socket = createSocket(port, InetAddress.getByName(host));
52                  }
53              }
54              else
55              {
56                  socket = createSocket();
57              }
58          }
59          else
60          {
61              //If this is a client socket create a default instance
62              socket = createSocket();
63          }
64  
65          UdpConnector connector = (UdpConnector)ep.getConnector();
66          //There is some overhead in stting socket timeout and buffer size, so we're
67          //careful here only to set if needed
68          if (connector.getReceiveBufferSize() != Connector.INT_VALUE_NOT_SET
69              && socket.getReceiveBufferSize() != connector.getReceiveBufferSize())
70          {
71              socket.setReceiveBufferSize(connector.getReceiveBufferSize());
72          }
73          if (connector.getSendBufferSize() != Connector.INT_VALUE_NOT_SET
74              && socket.getSendBufferSize() != connector.getSendBufferSize())
75          {
76              socket.setSendBufferSize(connector.getSendBufferSize());
77          }
78          if (connector.getTimeout() != Connector.INT_VALUE_NOT_SET
79              && socket.getSoTimeout() != connector.getTimeout())
80          {
81              socket.setSoTimeout(connector.getTimeout());
82          }
83          socket.setBroadcast(connector.isBroadcast());
84          return socket;
85      }
86  
87      public void destroyObject(Object key, Object object) throws Exception
88      {
89          Socket socket = (Socket)object;
90          if(!socket.isClosed())
91          {
92              socket.close();
93          }
94      }
95  
96      public boolean validateObject(Object key, Object object)
97      {
98          DatagramSocket socket = (DatagramSocket)object;
99          return !socket.isClosed();
100     }
101 
102     public void activateObject(Object key, Object object) throws Exception
103     {
104         // nothing to do        
105     }
106 
107     public void passivateObject(Object key, Object object) throws Exception
108     {
109         ImmutableEndpoint ep = (ImmutableEndpoint)key;
110 
111         boolean keepSocketOpen = MapUtils.getBooleanValue(ep.getProperties(),
112             UdpConnector.KEEP_SEND_SOCKET_OPEN_PROPERTY, ((UdpConnector)ep.getConnector()).isKeepSendSocketOpen());
113         DatagramSocket socket = (DatagramSocket)object;
114 
115         if (!keepSocketOpen)
116         {
117             if (socket != null)
118             {
119                 socket.close();
120             }
121         }
122     }
123 
124     protected DatagramSocket createSocket() throws IOException
125     {
126         return new DatagramSocket();
127     }
128 
129     protected DatagramSocket createSocket(int port) throws IOException
130     {
131         return new DatagramSocket(port);
132     }
133 
134     protected DatagramSocket createSocket(int port, InetAddress inetAddress) throws IOException
135     {
136         return new DatagramSocket(port, inetAddress);
137     }
138 }