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.MuleContext;
10  import org.mule.api.construct.FlowConstruct;
11  import org.mule.api.endpoint.InboundEndpoint;
12  import org.mule.api.processor.MessageProcessor;
13  import org.mule.api.transport.MessageReceiver;
14  import org.mule.transport.AbstractConnector;
15  import org.mule.transport.AbstractPollingMessageReceiver;
16  
17  import java.io.InputStream;
18  import java.io.OutputStream;
19  
20  import org.apache.commons.io.IOUtils;
21  
22  /**
23   * <code>StdioConnector</code> can send and receive Mule events over IO streams.
24   */
25  
26  public abstract class StdioConnector extends AbstractConnector
27  {
28  
29      public static final String STDIO = "stdio";
30      public static final String STREAM_SYSTEM_IN = "system.in";
31      public static final String STREAM_SYSTEM_OUT = "system.out";
32      public static final String STREAM_SYSTEM_ERR = "system.err";
33  
34      protected OutputStream outputStream;
35      protected InputStream inputStream;
36  
37      public StdioConnector(MuleContext context)
38      {
39          super(context);
40      }
41      
42      @Override
43      public MessageReceiver createReceiver(FlowConstruct flowConstruct, InboundEndpoint endpoint) throws Exception
44      {
45          return serviceDescriptor.createMessageReceiver(this, flowConstruct, endpoint,
46                                                         AbstractPollingMessageReceiver.DEFAULT_POLL_FREQUENCY);
47      }
48  
49      @Override
50      public void doStop()
51      {
52          // template method
53      }
54  
55      @Override
56      protected void doDispose()
57      {
58          IOUtils.closeQuietly(inputStream);
59          IOUtils.closeQuietly(outputStream);
60      }
61  
62      @Override
63      public void doStart()
64      {
65          // template method
66      }
67  
68      public String getProtocol()
69      {
70          return STDIO;
71      }
72  
73      public InputStream getInputStream()
74      {
75          return inputStream;
76      }
77  
78      public void setInputStream(InputStream inputStream)
79      {
80          this.inputStream = inputStream;
81      }
82  
83      public OutputStream getOutputStream()
84      {
85          return outputStream;
86      }
87  
88      public void setOutputStream(OutputStream outputStream)
89      {
90          this.outputStream = outputStream;
91      }
92  
93      public void registerListener(InboundEndpoint endpoint, MessageProcessor listener, FlowConstruct flowConstruct) throws Exception
94      {
95          if (receivers.size() > 0)
96          {
97              throw new UnsupportedOperationException(
98                  "You can only register one listener per system stream connector");
99          }
100         super.registerListener(endpoint, listener, flowConstruct);
101     }
102 }