Coverage Report - org.mule.providers.tcp.TcpStreamingMessageReceiver
 
Classes in this File Line Coverage Branch Coverage Complexity
TcpStreamingMessageReceiver
0%
0/39
0%
0/4
1.8
 
 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  0
     protected Socket clientSocket = null;
 43  0
     protected DataInputStream dataIn = null;
 44  0
     protected TcpProtocol protocol = null;
 45  
 
 46  
     public TcpStreamingMessageReceiver(UMOConnector connector,
 47  
                                         UMOComponent component,
 48  
                                         UMOEndpoint endpoint) throws InitialisationException
 49  
     {
 50  0
         super(connector, component, endpoint);
 51  0
         protocol = ((TcpConnector)connector).getTcpProtocol();
 52  0
     }
 53  
 
 54  
     protected void doDispose()
 55  
     {
 56  
         // template method
 57  0
     }
 58  
 
 59  
     protected void doConnect() throws ConnectException
 60  
     {
 61  0
         URI uri = endpoint.getEndpointURI().getUri();
 62  0
         String host = StringUtils.defaultIfEmpty(uri.getHost(), "localhost");
 63  
 
 64  
         try
 65  
         {
 66  0
             logger.debug("Attempting to connect to server socket");
 67  0
             InetAddress inetAddress = InetAddress.getByName(host);
 68  0
             clientSocket = new Socket(inetAddress, uri.getPort());
 69  0
             TcpConnector connector = (TcpConnector)this.connector;
 70  0
             clientSocket.setReceiveBufferSize(connector.getReceiveBufferSize());
 71  0
             clientSocket.setSendBufferSize(connector.getSendBufferSize());
 72  0
             clientSocket.setSoTimeout(connector.getReceiveTimeout());
 73  
 
 74  0
             dataIn = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
 75  0
             logger.debug("Connected to server socket");
 76  
         }
 77  0
         catch (Exception e)
 78  
         {
 79  0
             throw new ConnectException(TcpMessages.failedToBindToUri(uri), e, this);
 80  0
         }
 81  0
     }
 82  
 
 83  
     protected void doDisconnect() throws Exception
 84  
     {
 85  
         try
 86  
         {
 87  0
             if (clientSocket != null && !clientSocket.isClosed())
 88  
             {
 89  0
                 clientSocket.shutdownInput();
 90  0
                 clientSocket.shutdownOutput();
 91  0
                 clientSocket.close();
 92  
             }
 93  0
         }
 94  
         finally
 95  
         {
 96  0
             clientSocket = null;
 97  0
             dataIn = null;
 98  0
             logger.info("Closed tcp client socket");
 99  0
         }
 100  0
     }
 101  
 
 102  
     public void poll() throws Exception
 103  
     {
 104  
         // TODO AC: this seems wrong since 0 is ignored as value
 105  0
         setFrequency(0); // make sure this is zero and not overridden via config
 106  
         // TODO AC: check if this cast is ok
 107  0
         byte[] data = (byte[])protocol.read(dataIn);
 108  0
         if (data != null)
 109  
         {
 110  0
             UMOMessageAdapter adapter = connector.getMessageAdapter(data);
 111  0
             UMOMessage message = new MuleMessage(adapter);
 112  0
             routeMessage(message, endpoint.isSynchronous());
 113  
         }
 114  0
     }
 115  
 
 116  
 }