Coverage Report - org.mule.transport.stdio.StdioMessageDispatcher
 
Classes in this File Line Coverage Branch Coverage Complexity
StdioMessageDispatcher
0%
0/26
0%
0/12
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.MuleEvent;
 10  
 import org.mule.api.MuleMessage;
 11  
 import org.mule.api.endpoint.OutboundEndpoint;
 12  
 import org.mule.api.transport.DispatchException;
 13  
 import org.mule.transport.AbstractMessageDispatcher;
 14  
 import org.mule.transport.stdio.i18n.StdioMessages;
 15  
 import org.mule.util.StringUtils;
 16  
 
 17  
 import java.io.OutputStream;
 18  
 
 19  
 /**
 20  
  * <code>StdioMessageDispatcher</code> is a simple stream dispatcher that obtains
 21  
  * a stream from the Stream Connector to write to. This is only really useful for
 22  
  * testing purposes right now when writing to System.in and System.out. However, it
 23  
  * is feasible to set any OutputStream on the Stream connector and have that written
 24  
  * to.
 25  
  */
 26  
 
 27  
 public class StdioMessageDispatcher extends AbstractMessageDispatcher
 28  
 {
 29  
     private final StdioConnector connector;
 30  
 
 31  
     public StdioMessageDispatcher(OutboundEndpoint endpoint)
 32  
     {
 33  0
         super(endpoint);
 34  0
         this.connector = (StdioConnector) endpoint.getConnector();
 35  
 
 36  
         // apply connector-specific properties
 37  0
         if (connector instanceof PromptStdioConnector)
 38  
         {
 39  0
             PromptStdioConnector ssc = (PromptStdioConnector)connector;
 40  
 
 41  0
             String outputMessage = (String) endpoint.getProperties().get("outputMessage");
 42  0
             if (outputMessage != null)
 43  
             {
 44  0
                 ssc.setOutputMessage(outputMessage);
 45  
             }
 46  
         }
 47  0
     }
 48  
 
 49  
     @Override
 50  
     protected synchronized void doDispatch(MuleEvent event) throws Exception
 51  
     {
 52  0
         OutputStream out = connector.getOutputStream();
 53  
 
 54  0
         if (out == null)
 55  
         {
 56  0
             throw new DispatchException(StdioMessages.couldNotFindStreamWithName(event.getEndpoint()), 
 57  
                 event, (OutboundEndpoint) endpoint);
 58  
         }
 59  
 
 60  0
         if (connector instanceof PromptStdioConnector)
 61  
         {
 62  0
             PromptStdioConnector ssc = (PromptStdioConnector)connector;
 63  0
             if (StringUtils.isNotBlank(ssc.getOutputMessage()))
 64  
             {
 65  0
                 out.write(ssc.getOutputMessage().getBytes());
 66  
             }
 67  
         }
 68  
 
 69  0
         Object data = event.getMessage().getPayload();
 70  0
         if (data instanceof byte[])
 71  
         {
 72  0
             out.write((byte[])data);
 73  
         }
 74  
         else
 75  
         {
 76  0
             out.write(data.toString().getBytes());
 77  
         }
 78  
 
 79  0
         out.flush();
 80  0
     }
 81  
 
 82  
     @Override
 83  
     protected MuleMessage doSend(MuleEvent event) throws Exception
 84  
     {
 85  0
         doDispatch(event);
 86  0
         return event.getMessage();
 87  
     }
 88  
 
 89  
     @Override
 90  
     protected void doDispose()
 91  
     {
 92  
         // template method
 93  0
     }
 94  
 
 95  
     @Override
 96  
     protected void doConnect() throws Exception
 97  
     {
 98  
         // template method
 99  0
     }
 100  
 
 101  
     @Override
 102  
     protected void doDisconnect() throws Exception
 103  
     {
 104  
         // template method
 105  0
     }
 106  
 }