Coverage Report - org.mule.transport.stdio.StdioMessageReceiver
 
Classes in this File Line Coverage Branch Coverage Complexity
StdioMessageReceiver
0%
0/55
0%
0/18
0
StdioMessageReceiver$DelayedMessageWriter
0%
0/12
0%
0/2
0
 
 1  
 /*
 2  
  * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
 3  
  * The software in this package is published under the terms of the CPAL v1.0
 4  
  * license, a copy of which has been included with this distribution in the
 5  
  * LICENSE.txt file.
 6  
  */
 7  
 package org.mule.transport.stdio;
 8  
 
 9  
 import org.mule.api.MuleMessage;
 10  
 import org.mule.api.construct.FlowConstruct;
 11  
 import org.mule.api.endpoint.InboundEndpoint;
 12  
 import org.mule.api.lifecycle.CreateException;
 13  
 import org.mule.api.transport.Connector;
 14  
 import org.mule.transport.AbstractPollingMessageReceiver;
 15  
 
 16  
 import java.io.InputStream;
 17  
 import java.io.PrintStream;
 18  
 import java.io.PushbackInputStream;
 19  
 
 20  
 import org.apache.commons.lang.BooleanUtils;
 21  
 import org.apache.commons.lang.SystemUtils;
 22  
 
 23  
 /**
 24  
  * <code>StdioMessageReceiver</code> is a listener for events from Mule components
 25  
  * which then simply passes the events on to the target components.
 26  
  */
 27  
 public class StdioMessageReceiver extends AbstractPollingMessageReceiver
 28  
 {
 29  
     public static final int DEFAULT_BUFFER_SIZE = 4096;
 30  
 
 31  0
     private int bufferSize = DEFAULT_BUFFER_SIZE;
 32  
     private InputStream inputStream;
 33  
     private StdioConnector connector;
 34  
 
 35  
     private boolean sendStream;
 36  
 
 37  
     public StdioMessageReceiver(Connector connector,
 38  
                                 FlowConstruct flowConstruct,
 39  
                                 InboundEndpoint endpoint,
 40  
                                 long checkFrequency) throws CreateException
 41  
     {
 42  0
         super(connector, flowConstruct, endpoint);
 43  0
         this.setFrequency(checkFrequency);
 44  
 
 45  0
         this.connector = (StdioConnector) connector;
 46  0
         String streamName = endpoint.getEndpointURI().getAddress();
 47  0
         if (StdioConnector.STREAM_SYSTEM_IN.equalsIgnoreCase(streamName))
 48  
         {
 49  0
             inputStream = System.in;
 50  
         }
 51  
         else
 52  
         {
 53  0
             inputStream = this.connector.getInputStream();
 54  
         }
 55  
 
 56  
         // apply connector-specific properties
 57  0
         if (connector instanceof PromptStdioConnector)
 58  
         {
 59  0
             PromptStdioConnector ssc = (PromptStdioConnector) connector;
 60  
 
 61  0
             String promptMessage = (String) endpoint.getProperties().get("promptMessage");
 62  0
             if (promptMessage != null)
 63  
             {
 64  0
                 ssc.setPromptMessage(promptMessage);
 65  
             }
 66  
         }
 67  
         
 68  0
         this.sendStream = BooleanUtils.toBoolean((String) endpoint.getProperties().get("sendStream"));
 69  0
     }
 70  
 
 71  
     @Override
 72  
     protected void doDispose()
 73  
     {
 74  
         // template method
 75  0
     }
 76  
 
 77  
     @Override
 78  
     public void doConnect() throws Exception
 79  
     {
 80  0
         if (connector instanceof PromptStdioConnector)
 81  
         {
 82  0
             PromptStdioConnector ssc = (PromptStdioConnector) connector;
 83  0
             DelayedMessageWriter writer = new DelayedMessageWriter(ssc);
 84  0
             writer.start();
 85  
         }
 86  0
     }
 87  
 
 88  
     @Override
 89  
     public void doDisconnect() throws Exception
 90  
     {
 91  
         // noop
 92  0
     }
 93  
 
 94  
     @Override
 95  
     public void poll()
 96  
     {
 97  0
         String encoding = endpoint.getEncoding();
 98  
         try
 99  
         {
 100  0
             if (sendStream)
 101  
             {
 102  0
                 PushbackInputStream in = new PushbackInputStream(inputStream);
 103  
 
 104  
                 //Block until we have some data
 105  0
                 int i = in.read();
 106  
                 //Roll back our read
 107  0
                 in.unread(i);
 108  0
                 MuleMessage message = createMuleMessage(in, encoding);
 109  0
                 routeMessage(message);
 110  0
             }
 111  
             else
 112  
             {
 113  0
                 byte[] inputBuffer = new byte[bufferSize];
 114  0
                 int len = inputStream.read(inputBuffer);
 115  
 
 116  0
                 if (len == -1)
 117  
                 {
 118  0
                     return;
 119  
                 }
 120  
 
 121  0
                 StringBuffer fullBuffer = new StringBuffer(bufferSize);
 122  0
                 while (len > 0)
 123  
                 {
 124  0
                     fullBuffer.append(new String(inputBuffer, 0, len));
 125  0
                     len = 0; // mark as read
 126  0
                     if (inputStream.available() > 0)
 127  
                     {
 128  0
                         len = inputStream.read(inputBuffer);
 129  
                     }
 130  
                 }
 131  
 
 132  
                 // Each line is a separate message
 133  0
                 String[] lines = fullBuffer.toString().split(SystemUtils.LINE_SEPARATOR);
 134  0
                 for (int i = 0; i < lines.length; ++i)
 135  
                 {                
 136  0
                     MuleMessage message = createMuleMessage(lines[i], encoding);
 137  0
                     routeMessage(message);
 138  
                 }
 139  
             }
 140  
 
 141  0
             doConnect();
 142  
         }
 143  0
         catch (Exception e)
 144  
         {
 145  0
             getConnector().getMuleContext().getExceptionListener().handleException(e);
 146  0
         }
 147  0
     }
 148  
 
 149  
     public InputStream getInputStream()
 150  
     {
 151  0
         return inputStream;
 152  
     }
 153  
 
 154  
     public void setInputStream(InputStream inputStream)
 155  
     {
 156  0
         this.inputStream = inputStream;
 157  0
     }
 158  
 
 159  
     public int getBufferSize()
 160  
     {
 161  0
         return bufferSize;
 162  
     }
 163  
 
 164  
     public void setBufferSize(int bufferSize)
 165  
     {
 166  0
         this.bufferSize = bufferSize;
 167  0
     }
 168  
 
 169  
     private class DelayedMessageWriter extends Thread
 170  
     {
 171  0
         private long delay = 0;
 172  
         private PromptStdioConnector ssc;
 173  
 
 174  
         public DelayedMessageWriter(PromptStdioConnector ssc)
 175  0
         {
 176  0
             this.delay = ssc.getMessageDelayTime();
 177  0
             this.ssc = ssc;
 178  0
         }
 179  
 
 180  
         @Override
 181  
         public void run()
 182  
         {
 183  0
             if (delay > 0)
 184  
             {
 185  
                 try
 186  
                 {
 187  
                     // Allow all other console message to be printed out first
 188  0
                     sleep(delay);
 189  
                 }
 190  0
                 catch (InterruptedException e1)
 191  
                 {
 192  
                     // ignore
 193  0
                 }
 194  
             }
 195  0
             ((PrintStream) ssc.getOutputStream()).println();
 196  0
             ((PrintStream) ssc.getOutputStream()).print(ssc.getPromptMessage());
 197  0
         }
 198  
     }
 199  
 }