View Javadoc

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