View Javadoc
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          super(endpoint);
34          this.connector = (StdioConnector) endpoint.getConnector();
35  
36          // apply connector-specific properties
37          if (connector instanceof PromptStdioConnector)
38          {
39              PromptStdioConnector ssc = (PromptStdioConnector)connector;
40  
41              String outputMessage = (String) endpoint.getProperties().get("outputMessage");
42              if (outputMessage != null)
43              {
44                  ssc.setOutputMessage(outputMessage);
45              }
46          }
47      }
48  
49      @Override
50      protected synchronized void doDispatch(MuleEvent event) throws Exception
51      {
52          OutputStream out = connector.getOutputStream();
53  
54          if (out == null)
55          {
56              throw new DispatchException(StdioMessages.couldNotFindStreamWithName(event.getEndpoint()), 
57                  event, (OutboundEndpoint) endpoint);
58          }
59  
60          if (connector instanceof PromptStdioConnector)
61          {
62              PromptStdioConnector ssc = (PromptStdioConnector)connector;
63              if (StringUtils.isNotBlank(ssc.getOutputMessage()))
64              {
65                  out.write(ssc.getOutputMessage().getBytes());
66              }
67          }
68  
69          Object data = event.getMessage().getPayload();
70          if (data instanceof byte[])
71          {
72              out.write((byte[])data);
73          }
74          else
75          {
76              out.write(data.toString().getBytes());
77          }
78  
79          out.flush();
80      }
81  
82      @Override
83      protected MuleMessage doSend(MuleEvent event) throws Exception
84      {
85          doDispatch(event);
86          return event.getMessage();
87      }
88  
89      @Override
90      protected void doDispose()
91      {
92          // template method
93      }
94  
95      @Override
96      protected void doConnect() throws Exception
97      {
98          // template method
99      }
100 
101     @Override
102     protected void doDisconnect() throws Exception
103     {
104         // template method
105     }
106 }