View Javadoc

1   /*
2    * $Id: TcpMessageRequester.java 10961 2008-02-22 19:01:02Z dfeist $
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  
11  package org.mule.transport.tcp;
12  
13  import org.mule.DefaultMuleMessage;
14  import org.mule.api.MuleMessage;
15  import org.mule.api.endpoint.InboundEndpoint;
16  import org.mule.transport.AbstractMessageRequester;
17  
18  import java.net.Socket;
19  import java.net.SocketTimeoutException;
20  
21  /**
22   * Request transformed Mule events from TCP.
23   */
24  public class TcpMessageRequester extends AbstractMessageRequester
25  {
26  
27      private final TcpConnector connector;
28  
29      public TcpMessageRequester(InboundEndpoint endpoint)
30      {
31          super(endpoint);
32          this.connector = (TcpConnector) endpoint.getConnector();
33      }
34  
35      /**
36       * Make a specific request to the underlying transport
37       *
38       * @param timeout the maximum time the operation should block before returning.
39       *            The call should return immediately if there is data available. If
40       *            no data becomes available before the timeout elapses, null will be
41       *            returned
42       * @return the result of the request wrapped in a MuleMessage object. Null will be
43       *         returned if no data was available
44       * @throws Exception if the call to the underlying protocal cuases an exception
45       */
46      protected MuleMessage doRequest(long timeout) throws Exception
47      {
48          if (timeout > Integer.MAX_VALUE || timeout < 0)
49          {
50              throw new IllegalArgumentException("Timeout incorrect: " + timeout);
51          }
52          Socket socket = connector.getSocket(endpoint);
53          try
54          {
55              Object result = TcpMessageDispatcher.receiveFromSocket(socket, (int)timeout, endpoint);
56              if (result == null)
57              {
58                  return null;
59              }
60              return new DefaultMuleMessage(connector.getMessageAdapter(result));
61          }
62          catch (SocketTimeoutException e)
63          {
64              // we don't necesarily expect to receive a resonse here
65              if (logger.isDebugEnabled())
66              {
67                  logger.debug("Socket timed out normally while doing a synchronous receive on endpointUri: "
68                      + endpoint.getEndpointURI());
69              }
70              return null;
71          }
72  
73      }
74  
75      protected synchronized void doDispose()
76      {
77          try
78          {
79              doDisconnect();
80          }
81          catch (Exception e)
82          {
83              logger.error("Failed to shutdown the dispatcher.", e);
84          }
85      }
86  
87      protected void doConnect() throws Exception
88      {
89          // Test the connection
90          if (connector.isValidateConnections())
91          {
92              Socket socket = connector.getSocket(endpoint);
93              connector.releaseSocket(socket, endpoint);
94          }
95      }
96  
97      protected void doDisconnect() throws Exception
98      {
99          //nothing to do
100     }
101 
102 }