KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > stream > SystemStreamConnector


1 /*
2  * $Id: SystemStreamConnector.java 3937 2006-11-20 16:04:25Z lajos $
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 MuleSource MPL
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 java.io.InputStream JavaDoc;
14 import java.io.OutputStream JavaDoc;
15
16 import org.mule.umo.UMOComponent;
17 import org.mule.umo.UMOException;
18 import org.mule.umo.endpoint.UMOEndpoint;
19 import org.mule.umo.provider.UMOConnector;
20 import org.mule.umo.provider.UMOMessageReceiver;
21
22 /**
23  * <code>SystemStreamConnector</code> connects to the System streams in and out by
24  * default and add some basic fuctionality for writing out prompt messages.
25  */

26 public class SystemStreamConnector extends StreamConnector
27 {
28
29     private String JavaDoc promptMessage;
30     private String JavaDoc outputMessage;
31     private long messageDelayTime = 3000;
32     private boolean firstTime = true;
33
34     public SystemStreamConnector()
35     {
36         super();
37         inputStream = System.in;
38         outputStream = System.out;
39     }
40
41     /*
42      * (non-Javadoc)
43      *
44      * @see org.mule.providers.stream.StreamConnector#getInputStream()
45      */

46     public InputStream JavaDoc getInputStream()
47     {
48         return inputStream;
49     }
50
51     /*
52      * (non-Javadoc)
53      *
54      * @see org.mule.providers.AbstractConnector#doStart()
55      */

56     public void doStart()
57     {
58         firstTime = false;
59     }
60
61     /*
62      * (non-Javadoc)
63      *
64      * @see org.mule.providers.stream.StreamConnector#getOutputStream()
65      */

66     public OutputStream JavaDoc getOutputStream()
67     {
68         return outputStream;
69     }
70
71     /**
72      * @return Returns the promptMessage.
73      */

74     public String JavaDoc getPromptMessage()
75     {
76         return promptMessage;
77     }
78
79     /**
80      * @param promptMessage The promptMessage to set.
81      */

82     public void setPromptMessage(String JavaDoc promptMessage)
83     {
84         this.promptMessage = promptMessage;
85     }
86
87     /**
88      * @return Returns the outputMessage.
89      */

90     public String JavaDoc getOutputMessage()
91     {
92         return outputMessage;
93     }
94
95     /**
96      * @param outputMessage The outputMessage to set.
97      */

98     public void setOutputMessage(String JavaDoc outputMessage)
99     {
100         this.outputMessage = outputMessage;
101     }
102
103     /*
104      * (non-Javadoc)
105      *
106      * @see org.mule.umo.provider.UMOMessageDispatcher#getConnector()
107      */

108     public UMOConnector getConnector()
109     {
110         return this;
111     }
112
113     /*
114      * (non-Javadoc)
115      *
116      * @see org.mule.umo.provider.UMOMessageDispatcher#getDelegateSession()
117      */

118     public Object JavaDoc getDelegateSession() throws UMOException
119     {
120         return null;
121     }
122
123     public UMOMessageReceiver registerListener(UMOComponent component, UMOEndpoint endpoint) throws Exception JavaDoc
124     {
125         if (receivers.size() > 0)
126         {
127             throw new UnsupportedOperationException JavaDoc(
128                 "You can only register one listener per system stream connector");
129         }
130         UMOMessageReceiver receiver = super.registerListener(component, endpoint);
131         return receiver;
132     }
133
134     public long getMessageDelayTime()
135     {
136         if (firstTime)
137         {
138             return messageDelayTime + 4000;
139         }
140         else
141         {
142             return messageDelayTime;
143         }
144     }
145
146     public void setMessageDelayTime(long messageDelayTime)
147     {
148         this.messageDelayTime = messageDelayTime;
149     }
150
151     protected void doDispose()
152     {
153         // Override as a no-op.
154
// The reason is System.in/out shouldn't be closed.
155
// It is valid for them to remain open (consider, e.g. tail -F).
156
// Trying to close System.in will result in I/O block, and
157
// available() will always return 0 bytes for System.in.
158

159         // There is a scheme to get a ref to System.in via NIO,
160
// e.g. :
161
// FileInputStream fis = new FileInputStream(FileDescriptor.in);
162
// InputStream is = Channels.newInputStream(fis.getChannel);
163
//
164
// It is then possible to register a watchdog thread for the caller
165
// which will interrupt this (now wrapped with NIO) read() call.
166

167         // Well, it isn't absolutely required for the reasons stated above,
168
// just following the KISS principle.
169
}
170 }
171
Popular Tags