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