View Javadoc

1   /*
2    * $Id: AbstractTcpSocketFactory.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.tcp;
11  
12  import org.mule.util.MapUtils;
13  
14  import java.io.IOException;
15  import java.net.Socket;
16  
17  import org.apache.commons.logging.Log;
18  import org.apache.commons.logging.LogFactory;
19  import org.apache.commons.pool.KeyedPoolableObjectFactory;
20  
21  /**
22   * Creates a client socket using the socket address extracted from the endpoint.  Addtional
23   * socket parameters will also be set from the connector
24   */
25  public abstract class AbstractTcpSocketFactory implements KeyedPoolableObjectFactory
26  {
27  
28      /**
29       * logger used by this class
30       */
31      private static final Log logger = LogFactory.getLog(TcpSocketFactory.class);
32  
33      public Object makeObject(Object key) throws Exception
34      {
35          TcpSocketKey socketKey = (TcpSocketKey) key;
36  
37          Socket socket = createSocket(socketKey);
38          socket.setReuseAddress(true);
39  
40          TcpConnector connector = socketKey.getConnector();
41          connector.configureSocket(TcpConnector.CLIENT, socket);
42  
43          return socket;
44      }
45  
46      protected abstract Socket createSocket(TcpSocketKey key) throws IOException;
47  
48      public void destroyObject(Object key, Object object) throws Exception
49      {
50          Socket socket = (Socket) object;
51          if(!socket.isClosed())
52          {
53              socket.close();
54          }
55      }
56  
57      public boolean validateObject(Object key, Object object)
58      {
59          Socket socket = (Socket) object;
60          return !socket.isClosed();
61      }
62  
63      public void activateObject(Object key, Object object) throws Exception
64      {
65          // cannot really activate a Socket
66      }
67  
68      public void passivateObject(Object key, Object object) throws Exception
69      {
70          TcpSocketKey socketKey = (TcpSocketKey) key;
71  
72          boolean keepSocketOpen = MapUtils.getBooleanValue(socketKey.getEndpoint().getProperties(),
73              TcpConnector.KEEP_SEND_SOCKET_OPEN_PROPERTY, socketKey.getConnector().isKeepSendSocketOpen());
74          Socket socket = (Socket) object;
75  
76          if (!keepSocketOpen)
77          {
78              try
79              {
80                  if (socket != null)
81                  {
82                      socket.close();
83                  }
84              }
85              catch (IOException e)
86              {
87                  logger.debug("Failed to close socket after dispatch: " + e.getMessage());
88              }
89          }
90      }
91  
92  }