KickJava   Java API By Example, From Geeks To Geeks.

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


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: Command.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.PrintStream JavaDoc;
98
99 import java.util.ArrayList JavaDoc;
100
101 import java.util.HashMap JavaDoc;
102
103 import java.util.Iterator JavaDoc;
104
105 import java.util.StringTokenizer JavaDoc;
106
107
108
109 /**
110
111  * @author <a HREF="mailto:david.blevins@visi.com">David Blevins</a>
112
113  */

114
115 public class Command {
116
117
118
119
120
121     protected static final HashMap JavaDoc commands = new HashMap JavaDoc();
122
123
124
125     static
126
127     {
128
129         loadCommandList();
130
131     }
132
133
134
135     protected static final Command unknownCommand = new Command();
136
137
138
139     protected static void register( String JavaDoc name, Command cmd ) {
140
141         commands.put( name, cmd );
142
143     }
144
145
146
147     protected static void register( String JavaDoc name, Class JavaDoc cmd ) {
148
149         commands.put( name, cmd );
150
151     }
152
153
154
155     public static Command getCommand( String JavaDoc name ) {
156
157         Object JavaDoc cmd = commands.get( name );
158
159
160
161         if ( cmd instanceof Class JavaDoc ) {
162
163             cmd = loadCommand( ( Class JavaDoc ) cmd );
164
165             register( name, ( Command ) cmd );
166
167         }
168
169
170
171         return( Command ) cmd;
172
173     }
174
175
176
177     // - Public methods - //
178

179
180
181     public void exec( Arguments args, DataInputStream JavaDoc in, PrintStream JavaDoc out ) throws IOException JavaDoc
182
183     {
184
185         out.println( "not implemented" );
186
187     }
188
189
190
191
192
193     // - Protected methods - //
194

195     protected static Command loadCommand( Class JavaDoc commandClass ) {
196
197         Command cmd = null;
198
199         try {
200
201             cmd = ( Command ) commandClass.newInstance();
202
203         } catch ( Exception JavaDoc e ) {
204
205             //throw new IOException("Cannot instantiate command class "+commandClass+"\n"+e.getClass().getName()+":\n"+e.getMessage());
206

207         }
208
209
210
211         return cmd;
212
213     }
214
215
216
217
218
219     /*
220
221     TODO:
222
223     - Create the basic list in ant
224
225     - Add the regexp package to the ant scripts
226
227     - update the loadCommandList to read the list
228
229       made in the ant script
230
231
232
233     */

234
235     protected static void loadCommandList() {
236
237         Exit.register();
238
239         Help.register();
240
241         Lookup.register();
242
243         Ls.register();
244
245         Stop.register();
246
247         Version.register();
248
249         GroovySh.register();
250
251     }
252
253
254
255     public static class Arguments {
256
257         // holds the whole string representing what's been typed by a user after a command name
258

259         private String JavaDoc args;
260
261
262
263         private String JavaDoc[] argsArray = new String JavaDoc[0];
264
265
266
267         private boolean alreadyParsed = false;
268
269
270
271         Arguments( String JavaDoc args ) {
272
273             this.args = args;
274
275         }
276
277
278
279         String JavaDoc get() {
280
281             return args;
282
283         }
284
285
286
287         /**
288
289          * @param i
290
291          * @return i-th argument
292
293          */

294
295         String JavaDoc get( int i ) {
296
297             parseArgs();
298
299             return( argsArray != null ? argsArray[i] : null );
300
301         }
302
303
304
305         int count() {
306
307             parseArgs();
308
309             return( argsArray != null ? argsArray.length : 0 );
310
311         }
312
313
314
315         Iterator JavaDoc iterator() {
316
317             return new Iterator JavaDoc() {
318
319                 StringTokenizer JavaDoc st = new StringTokenizer JavaDoc( args );
320
321
322
323                 public boolean hasNext() {
324
325                     return st.hasMoreTokens();
326
327                 }
328
329
330
331                 public Object JavaDoc next() {
332
333                     return st.nextToken();
334
335                 }
336
337
338
339                 public void remove() {
340
341                     // not supported
342

343                 }
344
345             };
346
347         }
348
349
350
351         private void parseArgs() {
352
353             if ( !alreadyParsed ) {
354
355                 ArrayList JavaDoc arrayList = new ArrayList JavaDoc();
356
357                 Iterator JavaDoc it = iterator();
358
359                 while ( it.hasNext() ) {
360
361                     arrayList.add( it.next() );
362
363                 }
364
365                 argsArray = ( String JavaDoc[] ) arrayList.toArray( argsArray );
366
367                 alreadyParsed = true;
368
369             }
370
371         }
372
373     }
374
375 }
376
377
378
Popular Tags