View Javadoc

1   /*
2    * $Id: TcpStreamingMessageReceiver.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  
11  package org.mule.providers.tcp;
12  
13  import org.mule.impl.MuleMessage;
14  import org.mule.providers.AbstractPollingMessageReceiver;
15  import org.mule.providers.ConnectException;
16  import org.mule.providers.tcp.i18n.TcpMessages;
17  import org.mule.umo.UMOComponent;
18  import org.mule.umo.UMOMessage;
19  import org.mule.umo.endpoint.UMOEndpoint;
20  import org.mule.umo.lifecycle.InitialisationException;
21  import org.mule.umo.provider.UMOConnector;
22  import org.mule.umo.provider.UMOMessageAdapter;
23  import org.mule.util.StringUtils;
24  
25  import java.io.BufferedInputStream;
26  import java.io.DataInputStream;
27  import java.net.InetAddress;
28  import java.net.Socket;
29  import java.net.URI;
30  
31  /**
32   * <code>TcpStreamingMessageReceiver</code> establishes a TCP client connection to
33   * an external server and reads the streaming data. No polling frequency is used
34   * since with blocking i/o reads will block, and with non-blocking i/o reads will
35   * occur when data is available. Causing delays between read attempts is unnecessary,
36   * so this forces the pollingFrequency property to zero so no pause occurs in the
37   * PollingMessageReceiver class.
38   */
39  // TODO AC: check how this works with the 1.4 connector scheduler
40  public class TcpStreamingMessageReceiver extends AbstractPollingMessageReceiver
41  {
42      protected Socket clientSocket = null;
43      protected DataInputStream dataIn = null;
44      protected TcpProtocol protocol = null;
45  
46      public TcpStreamingMessageReceiver(UMOConnector connector,
47                                          UMOComponent component,
48                                          UMOEndpoint endpoint) throws InitialisationException
49      {
50          super(connector, component, endpoint);
51          protocol = ((TcpConnector)connector).getTcpProtocol();
52      }
53  
54      protected void doDispose()
55      {
56          // template method
57      }
58  
59      protected void doConnect() throws ConnectException
60      {
61          URI uri = endpoint.getEndpointURI().getUri();
62          String host = StringUtils.defaultIfEmpty(uri.getHost(), "localhost");
63  
64          try
65          {
66              logger.debug("Attempting to connect to server socket");
67              InetAddress inetAddress = InetAddress.getByName(host);
68              clientSocket = new Socket(inetAddress, uri.getPort());
69              TcpConnector connector = (TcpConnector)this.connector;
70              clientSocket.setReceiveBufferSize(connector.getReceiveBufferSize());
71              clientSocket.setSendBufferSize(connector.getSendBufferSize());
72              clientSocket.setSoTimeout(connector.getReceiveTimeout());
73  
74              dataIn = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
75              logger.debug("Connected to server socket");
76          }
77          catch (Exception e)
78          {
79              throw new ConnectException(TcpMessages.failedToBindToUri(uri), e, this);
80          }
81      }
82  
83      protected void doDisconnect() throws Exception
84      {
85          try
86          {
87              if (clientSocket != null && !clientSocket.isClosed())
88              {
89                  clientSocket.shutdownInput();
90                  clientSocket.shutdownOutput();
91                  clientSocket.close();
92              }
93          }
94          finally
95          {
96              clientSocket = null;
97              dataIn = null;
98              logger.info("Closed tcp client socket");
99          }
100     }
101 
102     public void poll() throws Exception
103     {
104         // TODO AC: this seems wrong since 0 is ignored as value
105         setFrequency(0); // make sure this is zero and not overridden via config
106         // TODO AC: check if this cast is ok
107         byte[] data = (byte[])protocol.read(dataIn);
108         if (data != null)
109         {
110             UMOMessageAdapter adapter = connector.getMessageAdapter(data);
111             UMOMessage message = new MuleMessage(adapter);
112             routeMessage(message, endpoint.isSynchronous());
113         }
114     }
115 
116 }