KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openejb > server > telnet > TextConsole


1 /**
2
3  * Redistribution and use of this software and associated documentation
4
5  * ("Software"), with or without modification, are permitted provided
6
7  * that the following conditions are met:
8
9  *
10
11  * 1. Redistributions of source code must retain copyright
12
13  * statements and notices. Redistributions must also contain a
14
15  * copy of this document.
16
17  *
18
19  * 2. Redistributions in binary form must reproduce the
20
21  * above copyright notice, this list of conditions and the
22
23  * following disclaimer in the documentation and/or other
24
25  * materials provided with the distribution.
26
27  *
28
29  * 3. The name "OpenEJB" must not be used to endorse or promote
30
31  * products derived from this Software without prior written
32
33  * permission of The OpenEJB Group. For written permission,
34
35  * please contact dev@openejb.org.
36
37  *
38
39  * 4. Products derived from this Software may not be called "OpenEJB"
40
41  * nor may "OpenEJB" appear in their names without prior written
42
43  * permission of The OpenEJB Group. OpenEJB is a registered
44
45  * trademark of The OpenEJB Group.
46
47  *
48
49  * 5. Due credit should be given to the OpenEJB Project
50
51  * (http://www.openejb.org/).
52
53  *
54
55  * THIS SOFTWARE IS PROVIDED BY THE OPENEJB GROUP AND CONTRIBUTORS
56
57  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
58
59  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
60
61  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
62
63  * THE OPENEJB GROUP OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
64
65  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
66
67  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
68
69  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70
71  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
72
73  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
74
75  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
76
77  * OF THE POSSIBILITY OF SUCH DAMAGE.
78
79  *
80
81  * Copyright 2001 (C) The OpenEJB Group. All Rights Reserved.
82
83  *
84
85  * $Id: TextConsole.java 1912 2005-06-16 22:29:56Z jlaskowski $
86
87  */

88
89 package org.openejb.server.telnet;
90
91
92
93 import java.io.DataInputStream JavaDoc;
94
95 import java.io.IOException JavaDoc;
96
97 import java.io.InputStream JavaDoc;
98
99 import java.io.PrintStream JavaDoc;
100
101 import java.util.Properties JavaDoc;
102
103 import java.util.StringTokenizer JavaDoc;
104
105 import java.util.Vector JavaDoc;
106
107
108
109 import org.openejb.util.Logger;
110
111
112
113 /**
114
115  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
116
117  */

118
119 public class TextConsole {
120
121
122
123     Logger logger = Logger.getInstance( "OpenEJB.admin", "org.openejb.server.util.resources" );
124
125
126
127     Properties JavaDoc props;
128
129
130
131     public TextConsole() {
132
133     }
134
135
136
137     public void init( Properties JavaDoc props ) throws Exception JavaDoc {
138
139         this.props = props;
140
141     }
142
143
144
145     boolean stop = false;
146
147
148
149     DataInputStream JavaDoc in = null;
150
151     PrintStream JavaDoc out = null;
152
153
154
155     public static final char ESC = ( char ) 27;
156
157
158
159     public static final String JavaDoc TTY_Reset = ESC + "[0m";
160
161     public static final String JavaDoc TTY_Bright = ESC + "[1m";
162
163     public static final String JavaDoc TTY_Dim = ESC + "[2m";
164
165     public static final String JavaDoc TTY_Underscore = ESC + "[4m";
166
167     public static final String JavaDoc TTY_Blink = ESC + "[5m";
168
169     public static final String JavaDoc TTY_Reverse = ESC + "[7m";
170
171     public static final String JavaDoc TTY_Hidden = ESC + "[8m";
172
173
174
175     /* Foreground Colors */
176
177     public static final String JavaDoc TTY_FG_Black = ESC + "[30m";
178
179     public static final String JavaDoc TTY_FG_Red = ESC + "[31m";
180
181     public static final String JavaDoc TTY_FG_Green = ESC + "[32m";
182
183     public static final String JavaDoc TTY_FG_Yellow = ESC + "[33m";
184
185     public static final String JavaDoc TTY_FG_Blue = ESC + "[34m";
186
187     public static final String JavaDoc TTY_FG_Magenta = ESC + "[35m";
188
189     public static final String JavaDoc TTY_FG_Cyan = ESC + "[36m";
190
191     public static final String JavaDoc TTY_FG_White = ESC + "[37m";
192
193
194
195     /* Background Colors */
196
197     public static final String JavaDoc TTY_BG_Black = ESC + "[40m";
198
199     public static final String JavaDoc TTY_BG_Red = ESC + "[41m";
200
201     public static final String JavaDoc TTY_BG_Green = ESC + "[42m";
202
203     public static final String JavaDoc TTY_BG_Yellow = ESC + "[43m";
204
205     public static final String JavaDoc TTY_BG_Blue = ESC + "[44m";
206
207     public static final String JavaDoc TTY_BG_Magenta = ESC + "[45m";
208
209     public static final String JavaDoc TTY_BG_Cyan = ESC + "[46m";
210
211     public static final String JavaDoc TTY_BG_White = ESC + "[47m";
212
213
214
215     static String JavaDoc PROMPT = TTY_Reset + TTY_Bright + "[openejb]$ " + TTY_Reset;
216
217
218
219     protected void exec( InputStream JavaDoc input, PrintStream JavaDoc out ) {
220
221         DataInputStream JavaDoc in = new DataInputStream JavaDoc(input);
222
223         while (!stop) {
224
225             prompt(in,out);
226
227         }
228
229
230
231     }
232
233
234
235     protected void prompt( DataInputStream JavaDoc in, PrintStream JavaDoc out ) {
236
237
238
239         try {
240
241             out.print( PROMPT );
242
243             out.flush();
244
245
246
247             String JavaDoc commandline = in.readLine();
248
249             logger.debug( "command: " + commandline );
250
251             commandline = commandline.trim();
252
253
254
255             if ( commandline.length() < 1 ) return;
256
257
258
259             String JavaDoc command = commandline;
260
261             Command.Arguments args = null;
262
263
264
265             int spacePosition = commandline.indexOf( ' ' );
266
267             int tabPosition = commandline.indexOf( '\t' );
268
269             if ( spacePosition != -1 || tabPosition != -1 ) {
270
271                 int cutPosition = ( spacePosition > tabPosition ? spacePosition : tabPosition );
272
273                 command = commandline.substring( 0, cutPosition );
274
275                 args = new Command.Arguments( commandline.substring( cutPosition + 1 ) );
276
277             }
278
279
280
281             Command cmd = Command.getCommand( command );
282
283
284
285             if ( cmd == null ) {
286
287                 out.print( command );
288
289                 out.println( ": command not found" );
290
291             } else {
292
293                 cmd.exec( args, in, out );
294
295             }
296
297         } catch ( UnsupportedOperationException JavaDoc e ) {
298
299             this.stop = true;
300
301         } catch ( Throwable JavaDoc e ) {
302
303             e.printStackTrace( new PrintStream JavaDoc( out ) );
304
305             //e.printStackTrace( );
306

307             this.stop = true;
308
309         }
310
311     }
312
313
314
315     protected void badCommand( DataInputStream JavaDoc in, PrintStream JavaDoc out ) throws IOException JavaDoc
316
317     {
318
319         //asdf: command not found
320

321     }
322
323 }
324
325
326
Popular Tags