Coverage Report - org.mule.providers.stream.StreamMessageReceiver
 
Classes in this File Line Coverage Branch Coverage Complexity
StreamMessageReceiver
27%
15/55
17%
3/18
2.182
StreamMessageReceiver$DelayedMessageWriter
0%
0/12
0%
0/2
2.182
 
 1  
 /*
 2  
  * $Id: StreamMessageReceiver.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  
 
 11  
 package org.mule.providers.stream;
 12  
 
 13  
 import org.mule.impl.MuleMessage;
 14  
 import org.mule.providers.AbstractPollingMessageReceiver;
 15  
 import org.mule.umo.UMOComponent;
 16  
 import org.mule.umo.UMOMessage;
 17  
 import org.mule.umo.endpoint.UMOEndpoint;
 18  
 import org.mule.umo.lifecycle.InitialisationException;
 19  
 import org.mule.umo.provider.UMOConnector;
 20  
 import org.mule.util.SystemUtils;
 21  
 
 22  
 import java.io.InputStream;
 23  
 import java.io.PrintStream;
 24  
 import java.io.PushbackInputStream;
 25  
 
 26  
 /**
 27  
  * <code>StreamMessageReceiver</code> is a listener for events from Mule components
 28  
  * which then simply passes the events on to the target components.
 29  
  */
 30  
 public class StreamMessageReceiver extends AbstractPollingMessageReceiver
 31  
 {
 32  
     public static final int DEFAULT_BUFFER_SIZE = 4096;
 33  
 
 34  6
     private int bufferSize = DEFAULT_BUFFER_SIZE;
 35  
     private InputStream inputStream;
 36  
     private StreamConnector connector;
 37  
 
 38  
     public StreamMessageReceiver(UMOConnector connector,
 39  
                                  UMOComponent component,
 40  
                                  UMOEndpoint endpoint,
 41  
                                  long checkFrequency) throws InitialisationException
 42  
     {
 43  6
         super(connector, component, endpoint);
 44  6
         this.setFrequency(checkFrequency);
 45  
 
 46  6
         this.connector = (StreamConnector)connector;
 47  6
         String streamName = endpoint.getEndpointURI().getAddress();
 48  6
         if (StreamConnector.STREAM_SYSTEM_IN.equalsIgnoreCase(streamName))
 49  
         {
 50  0
             inputStream = System.in;
 51  
         }
 52  
         else
 53  
         {
 54  6
             inputStream = this.connector.getInputStream();
 55  
         }
 56  
 
 57  
         // apply connector-specific properties
 58  6
         if (connector instanceof SystemStreamConnector)
 59  
         {
 60  6
             SystemStreamConnector ssc = (SystemStreamConnector)connector;
 61  
 
 62  6
             String promptMessage = (String)endpoint.getProperties().get("promptMessage");
 63  6
             if (promptMessage != null)
 64  
             {
 65  0
                 ssc.setPromptMessage(promptMessage);
 66  
             }
 67  
         }
 68  6
     }
 69  
 
 70  
     protected void doDispose()
 71  
     {
 72  
         // template method
 73  4
     }
 74  
 
 75  
     public void doConnect() throws Exception
 76  
     {
 77  0
         if (connector instanceof SystemStreamConnector)
 78  
         {
 79  0
             SystemStreamConnector ssc = (SystemStreamConnector)connector;
 80  0
             DelayedMessageWriter writer = new DelayedMessageWriter(ssc);
 81  0
             writer.start();
 82  
         }
 83  0
     }
 84  
 
 85  
     public void doDisconnect() throws Exception
 86  
     {
 87  
         // noop
 88  0
     }
 89  
 
 90  
     /*
 91  
      * (non-Javadoc)
 92  
      * 
 93  
      * @see org.mule.util.timer.TimeEventListener#timeExpired(org.mule.util.timer.TimeEvent)
 94  
      */
 95  
     public void poll()
 96  
     {
 97  
         try
 98  
         {
 99  0
             if(endpoint.isStreaming())
 100  
             {
 101  0
                 PushbackInputStream in = new PushbackInputStream(inputStream);
 102  
 
 103  
                 //Block until we have some data
 104  0
                 int i = in.read();
 105  
                 //Roll back our read
 106  0
                 in.unread(i);
 107  0
                 UMOMessage umoMessage = new MuleMessage(connector.getStreamMessageAdapter(in, null));
 108  0
                 routeMessage(umoMessage, endpoint.isSynchronous());
 109  0
             }
 110  
             else
 111  
             {
 112  0
                 byte[] inputBuffer = new byte[bufferSize];
 113  0
                 int len = inputStream.read(inputBuffer);
 114  
 
 115  0
                 if (len == -1)
 116  
                 {
 117  0
                     return;
 118  
                 }
 119  
 
 120  0
                 StringBuffer fullBuffer = new StringBuffer(bufferSize);
 121  0
                 while (len > 0)
 122  
                 {
 123  0
                     fullBuffer.append(new String(inputBuffer, 0, len));
 124  0
                     len = 0; // mark as read
 125  0
                     if (inputStream.available() > 0)
 126  
                     {
 127  0
                         len = inputStream.read(inputBuffer);
 128  
                     }
 129  
                 }
 130  
 
 131  
                 // remove any trailing CR/LF
 132  
                 String finalMessageString;
 133  0
                 int noCRLFLength = fullBuffer.length() - SystemUtils.LINE_SEPARATOR.length();
 134  0
                 if (fullBuffer.indexOf(SystemUtils.LINE_SEPARATOR, noCRLFLength) != -1)
 135  
                 {
 136  0
                     finalMessageString = fullBuffer.substring(0, noCRLFLength);
 137  
                 }
 138  
                 else
 139  
                 {
 140  0
                     finalMessageString = fullBuffer.toString();
 141  
                 }
 142  
 
 143  0
                 UMOMessage umoMessage = new MuleMessage(connector.getMessageAdapter(finalMessageString));
 144  0
                 routeMessage(umoMessage, endpoint.isSynchronous());
 145  
             }
 146  
 
 147  0
             doConnect();
 148  
         }
 149  0
         catch (Exception e)
 150  
         {
 151  0
             handleException(e);
 152  0
         }
 153  0
     }
 154  
 
 155  
     public InputStream getInputStream()
 156  
     {
 157  0
         return inputStream;
 158  
     }
 159  
 
 160  
     public void setInputStream(InputStream inputStream)
 161  
     {
 162  2
         this.inputStream = inputStream;
 163  2
     }
 164  
 
 165  
     public int getBufferSize()
 166  
     {
 167  0
         return bufferSize;
 168  
     }
 169  
 
 170  
     public void setBufferSize(int bufferSize)
 171  
     {
 172  0
         this.bufferSize = bufferSize;
 173  0
     }
 174  
 
 175  
     private class DelayedMessageWriter extends Thread
 176  
     {
 177  0
         private long delay = 0;
 178  
         private SystemStreamConnector ssc;
 179  
 
 180  
         public DelayedMessageWriter(SystemStreamConnector ssc)
 181  0
         {
 182  0
             this.delay = ssc.getMessageDelayTime();
 183  0
             this.ssc = ssc;
 184  0
         }
 185  
 
 186  
         public void run()
 187  
         {
 188  0
             if (delay > 0)
 189  
             {
 190  
                 try
 191  
                 {
 192  
                     // Allow all other console message to be printed out first
 193  0
                     sleep(delay);
 194  
                 }
 195  0
                 catch (InterruptedException e1)
 196  
                 {
 197  
                     // ignore
 198  0
                 }
 199  
             }
 200  0
             ((PrintStream)ssc.getOutputStream()).println();
 201  0
             ((PrintStream)ssc.getOutputStream()).print(ssc.getPromptMessage());
 202  0
         }
 203  
     }
 204  
 }