Coverage Report - org.mule.providers.stream.StreamMessageDispatcher
 
Classes in this File Line Coverage Branch Coverage Complexity
StreamMessageDispatcher
0%
0/27
0%
0/12
2.143
 
 1  
 /*
 2  
  * $Id: StreamMessageDispatcher.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.providers.AbstractMessageDispatcher;
 14  
 import org.mule.providers.stream.i18n.StreamMessages;
 15  
 import org.mule.umo.UMOEvent;
 16  
 import org.mule.umo.UMOMessage;
 17  
 import org.mule.umo.endpoint.UMOImmutableEndpoint;
 18  
 import org.mule.umo.provider.DispatchException;
 19  
 import org.mule.util.StringUtils;
 20  
 
 21  
 import java.io.OutputStream;
 22  
 
 23  
 /**
 24  
  * <code>StreamMessageDispatcher</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 StreamMessageDispatcher extends AbstractMessageDispatcher
 32  
 {
 33  
     private final StreamConnector connector;
 34  
 
 35  
     public StreamMessageDispatcher(UMOImmutableEndpoint endpoint)
 36  
     {
 37  0
         super(endpoint);
 38  0
         this.connector = (StreamConnector)endpoint.getConnector();
 39  
 
 40  
         // apply connector-specific properties
 41  0
         if (connector instanceof SystemStreamConnector)
 42  
         {
 43  0
             SystemStreamConnector ssc = (SystemStreamConnector)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(UMOEvent event) throws Exception
 54  
     {
 55  0
         OutputStream out = connector.getOutputStream();
 56  
 
 57  0
         if (out == null)
 58  
         {
 59  0
             throw new DispatchException(
 60  
                 StreamMessages.couldNotFindStreamWithName(event.getEndpoint().getEndpointURI().getAddress()), 
 61  
                 event.getMessage(), event.getEndpoint());
 62  
         }
 63  
 
 64  0
         if (connector instanceof SystemStreamConnector)
 65  
         {
 66  0
             SystemStreamConnector ssc = (SystemStreamConnector)connector;
 67  0
             if (StringUtils.isNotBlank(ssc.getOutputMessage()))
 68  
             {
 69  0
                 out.write(ssc.getOutputMessage().getBytes());
 70  
             }
 71  
         }
 72  
 
 73  0
         Object data = event.getTransformedMessage();
 74  0
         if (data instanceof byte[])
 75  
         {
 76  0
             out.write((byte[])data);
 77  
         }
 78  
         else
 79  
         {
 80  0
             out.write(data.toString().getBytes());
 81  
         }
 82  
 
 83  0
         out.flush();
 84  0
     }
 85  
 
 86  
     /*
 87  
      * (non-Javadoc)
 88  
      * 
 89  
      * @see org.mule.umo.provider.UMOConnector#send(org.mule.umo.UMOEvent)
 90  
      */
 91  
     protected UMOMessage doSend(UMOEvent event) throws Exception
 92  
     {
 93  0
         doDispatch(event);
 94  0
         return event.getMessage();
 95  
     }
 96  
 
 97  
     /**
 98  
      * Make a specific request to the underlying transport
 99  
      * 
 100  
      * @param timeout the maximum time the operation should block before returning.
 101  
      *            The call should return immediately if there is data available. If
 102  
      *            no data becomes available before the timeout elapses, null will be
 103  
      *            returned
 104  
      * @return the result of the request wrapped in a UMOMessage object. Null will be
 105  
      *         returned if no data was avaialable
 106  
      * @throws Exception if the call to the underlying protocal cuases an exception
 107  
      */
 108  
     protected UMOMessage doReceive(long timeout) throws Exception
 109  
     {
 110  0
         throw new UnsupportedOperationException("doReceive");
 111  
     }
 112  
 
 113  
     protected void doDispose()
 114  
     {
 115  
         // template method
 116  0
     }
 117  
 
 118  
     protected void doConnect() throws Exception
 119  
     {
 120  
         // template method
 121  0
     }
 122  
 
 123  
     protected void doDisconnect() throws Exception
 124  
     {
 125  
         // template method
 126  0
     }
 127  
 
 128  
 
 129  
 
 130  
 }