View Javadoc

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