View Javadoc

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