View Javadoc

1   /*
2    * $Id: TcpSocketFactory.java 7963 2007-08-21 08:53:15Z 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.tcp;
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.InetAddress;
18  import java.net.Socket;
19  
20  import org.apache.commons.logging.Log;
21  import org.apache.commons.logging.LogFactory;
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 TcpSocketFactory implements PooledSocketFactory
28  {
29      /**
30       * logger used by this class
31       */
32      private static final Log logger = LogFactory.getLog(TcpSocketFactory.class);
33  
34      public Object makeObject(Object key) throws Exception
35      {
36          UMOImmutableEndpoint ep = (UMOImmutableEndpoint) key;
37          int port = ep.getEndpointURI().getPort();
38          InetAddress inetAddress = InetAddress.getByName(ep.getEndpointURI().getHost());
39          Socket socket = createSocket(port, inetAddress);
40          socket.setReuseAddress(true);
41  
42          TcpConnector connector = (TcpConnector)ep.getConnector();
43          //There is some overhead in stting socket timeout and buffer size, so we're
44          //careful here only to set if needed
45          if (connector.getReceiveBufferSize() != UMOConnector.INT_VALUE_NOT_SET
46              && socket.getReceiveBufferSize() != connector.getReceiveBufferSize())
47          {
48              socket.setReceiveBufferSize(connector.getReceiveBufferSize());
49          }
50          if (connector.getSendBufferSize() != UMOConnector.INT_VALUE_NOT_SET
51              && socket.getSendBufferSize() != connector.getSendBufferSize())
52          {
53              socket.setSendBufferSize(connector.getSendBufferSize());
54          }
55          if (connector.getReceiveTimeout() != UMOConnector.INT_VALUE_NOT_SET
56              && socket.getSoTimeout() != connector.getReceiveTimeout())
57          {
58              socket.setSoTimeout(connector.getSendTimeout());
59          }
60          socket.setTcpNoDelay(connector.isSendTcpNoDelay());
61          socket.setKeepAlive(connector.isKeepAlive());
62          return socket;
63      }
64  
65      protected Socket createSocket(int port, InetAddress inetAddress) throws IOException
66      {
67          return new Socket(inetAddress, port);
68      }
69  
70      public void destroyObject(Object key, Object object) throws Exception
71      {
72          Socket socket = (Socket) object;
73          if(!socket.isClosed())
74          {
75              socket.close();
76          }
77      }
78  
79      public boolean validateObject(Object key, Object object)
80      {
81          Socket socket = (Socket) object;
82          return !socket.isClosed();       
83      }
84  
85      public void activateObject(Object key, Object object) throws Exception
86      {
87          // cannot really activate a Socket
88      }
89  
90      public void passivateObject(Object key, Object object) throws Exception
91      {
92          UMOImmutableEndpoint ep = (UMOImmutableEndpoint) key;
93  
94          boolean keepSocketOpen = MapUtils.getBooleanValue(ep.getProperties(),
95              TcpConnector.KEEP_SEND_SOCKET_OPEN_PROPERTY, ((TcpConnector) ep.getConnector()).isKeepSendSocketOpen());
96          Socket socket = (Socket) object;
97  
98          if (!keepSocketOpen)
99          {
100             try
101             {
102                 if (socket != null)
103                 {
104                     socket.close();
105                 }
106             }
107             catch (IOException e)
108             {
109                 logger.debug("Failed to close socket after dispatch: " + e.getMessage());
110             }
111         }
112     }
113     
114 }