View Javadoc

1   /*
2    * $Id: StreamMessageReceiver.java 7976 2007-08-21 14:26:13Z 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.impl.MuleMessage;
14  import org.mule.providers.AbstractPollingMessageReceiver;
15  import org.mule.umo.UMOComponent;
16  import org.mule.umo.UMOMessage;
17  import org.mule.umo.endpoint.UMOEndpoint;
18  import org.mule.umo.lifecycle.InitialisationException;
19  import org.mule.umo.provider.UMOConnector;
20  import org.mule.util.SystemUtils;
21  
22  import java.io.InputStream;
23  import java.io.PrintStream;
24  import java.io.PushbackInputStream;
25  
26  /**
27   * <code>StreamMessageReceiver</code> is a listener for events from Mule components
28   * which then simply passes the events on to the target components.
29   */
30  public class StreamMessageReceiver extends AbstractPollingMessageReceiver
31  {
32      public static final int DEFAULT_BUFFER_SIZE = 4096;
33  
34      private int bufferSize = DEFAULT_BUFFER_SIZE;
35      private InputStream inputStream;
36      private StreamConnector connector;
37  
38      public StreamMessageReceiver(UMOConnector connector,
39                                   UMOComponent component,
40                                   UMOEndpoint endpoint,
41                                   long checkFrequency) throws InitialisationException
42      {
43          super(connector, component, endpoint);
44          this.setFrequency(checkFrequency);
45  
46          this.connector = (StreamConnector)connector;
47          String streamName = endpoint.getEndpointURI().getAddress();
48          if (StreamConnector.STREAM_SYSTEM_IN.equalsIgnoreCase(streamName))
49          {
50              inputStream = System.in;
51          }
52          else
53          {
54              inputStream = this.connector.getInputStream();
55          }
56  
57          // apply connector-specific properties
58          if (connector instanceof SystemStreamConnector)
59          {
60              SystemStreamConnector ssc = (SystemStreamConnector)connector;
61  
62              String promptMessage = (String)endpoint.getProperties().get("promptMessage");
63              if (promptMessage != null)
64              {
65                  ssc.setPromptMessage(promptMessage);
66              }
67          }
68      }
69  
70      protected void doDispose()
71      {
72          // template method
73      }
74  
75      public void doConnect() throws Exception
76      {
77          if (connector instanceof SystemStreamConnector)
78          {
79              SystemStreamConnector ssc = (SystemStreamConnector)connector;
80              DelayedMessageWriter writer = new DelayedMessageWriter(ssc);
81              writer.start();
82          }
83      }
84  
85      public void doDisconnect() throws Exception
86      {
87          // noop
88      }
89  
90      /*
91       * (non-Javadoc)
92       * 
93       * @see org.mule.util.timer.TimeEventListener#timeExpired(org.mule.util.timer.TimeEvent)
94       */
95      public void poll()
96      {
97          try
98          {
99              if(endpoint.isStreaming())
100             {
101                 PushbackInputStream in = new PushbackInputStream(inputStream);
102 
103                 //Block until we have some data
104                 int i = in.read();
105                 //Roll back our read
106                 in.unread(i);
107                 UMOMessage umoMessage = new MuleMessage(connector.getStreamMessageAdapter(in, null));
108                 routeMessage(umoMessage, endpoint.isSynchronous());
109             }
110             else
111             {
112                 byte[] inputBuffer = new byte[bufferSize];
113                 int len = inputStream.read(inputBuffer);
114 
115                 if (len == -1)
116                 {
117                     return;
118                 }
119 
120                 StringBuffer fullBuffer = new StringBuffer(bufferSize);
121                 while (len > 0)
122                 {
123                     fullBuffer.append(new String(inputBuffer, 0, len));
124                     len = 0; // mark as read
125                     if (inputStream.available() > 0)
126                     {
127                         len = inputStream.read(inputBuffer);
128                     }
129                 }
130 
131                 // remove any trailing CR/LF
132                 String finalMessageString;
133                 int noCRLFLength = fullBuffer.length() - SystemUtils.LINE_SEPARATOR.length();
134                 if (fullBuffer.indexOf(SystemUtils.LINE_SEPARATOR, noCRLFLength) != -1)
135                 {
136                     finalMessageString = fullBuffer.substring(0, noCRLFLength);
137                 }
138                 else
139                 {
140                     finalMessageString = fullBuffer.toString();
141                 }
142 
143                 UMOMessage umoMessage = new MuleMessage(connector.getMessageAdapter(finalMessageString));
144                 routeMessage(umoMessage, endpoint.isSynchronous());
145             }
146 
147             doConnect();
148         }
149         catch (Exception e)
150         {
151             handleException(e);
152         }
153     }
154 
155     public InputStream getInputStream()
156     {
157         return inputStream;
158     }
159 
160     public void setInputStream(InputStream inputStream)
161     {
162         this.inputStream = inputStream;
163     }
164 
165     public int getBufferSize()
166     {
167         return bufferSize;
168     }
169 
170     public void setBufferSize(int bufferSize)
171     {
172         this.bufferSize = bufferSize;
173     }
174 
175     private class DelayedMessageWriter extends Thread
176     {
177         private long delay = 0;
178         private SystemStreamConnector ssc;
179 
180         public DelayedMessageWriter(SystemStreamConnector ssc)
181         {
182             this.delay = ssc.getMessageDelayTime();
183             this.ssc = ssc;
184         }
185 
186         public void run()
187         {
188             if (delay > 0)
189             {
190                 try
191                 {
192                     // Allow all other console message to be printed out first
193                     sleep(delay);
194                 }
195                 catch (InterruptedException e1)
196                 {
197                     // ignore
198                 }
199             }
200             ((PrintStream)ssc.getOutputStream()).println();
201             ((PrintStream)ssc.getOutputStream()).print(ssc.getPromptMessage());
202         }
203     }
204 }