KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > cli > jmx > cmd > CmdReader


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 /*
25  * $Header: /cvs/glassfish/admin-cli/cli-api/src/java/com/sun/cli/jmx/cmd/CmdReader.java,v 1.3 2005/12/25 03:45:31 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:45:31 $
28  */

29  
30 package com.sun.cli.jmx.cmd;
31
32
33 import com.sun.cli.util.stringifier.SmartStringifier;
34 import com.sun.cli.util.LineReader;
35 import com.sun.cli.util.TokenizerImpl;
36
37
38 public class CmdReader implements CmdOutput
39 {
40         public
41     CmdReader( )
42     {
43     }
44     
45             public void
46     print( Object JavaDoc o )
47     {
48         System.out.print( o.toString() );
49     }
50     
51         public void
52     println( Object JavaDoc o )
53     {
54         System.out.println( o.toString() );
55     }
56     
57         public void
58     printError( Object JavaDoc o )
59     {
60         System.err.println( o.toString() );
61     }
62     
63         public void
64     printDebug( Object JavaDoc o )
65     {
66         println( o );
67     }
68     
69     
70         private static void
71     p( Object JavaDoc o )
72     {
73         System.out.println( SmartStringifier.toString( o ) );
74     }
75     
76     
77     private final static String JavaDoc ADVISORY =
78     "Type 'help' for help, 'quit' to quit.\n";
79         
80     private final static String JavaDoc USAGE = initUsage();
81     
82         private static String JavaDoc
83     initUsage()
84     {
85         String JavaDoc usage = "*** Available commands ***\n\n";
86         
87         final CmdStrings.CmdHelp [] allHelp = CmdStrings.getAllHelp();
88         
89         for( int i = 0; i < allHelp.length; ++i )
90         {
91             usage = usage + allHelp[ i ] + "\n\n";
92         }
93         
94         return( usage );
95     }
96     
97     
98         public String JavaDoc
99     readLine( LineReader lineReader )
100         throws Exception JavaDoc
101     {
102         String JavaDoc line = lineReader.readLine( "> ");
103         
104         if ( line != null )
105         {
106             line = line.trim();
107         }
108         
109         return( line );
110     }
111     
112     private final static String JavaDoc DELIM_CHARS = " \t";
113     private final static char ESCAPE_CHAR = '\\';
114     private final static String JavaDoc ESCAPABLE_CHARS = DELIM_CHARS + ESCAPE_CHAR + "\"";
115         static String JavaDoc []
116     lineToTokens( String JavaDoc line )
117     {
118         final TokenizerImpl tk = new TokenizerImpl( line, "" + DELIM_CHARS,
119                                         ESCAPE_CHAR, ESCAPABLE_CHARS);
120         
121         return( tk.getTokens( ) );
122     }
123     
124         public static int
125     processLine( final String JavaDoc line, final CmdRunner cmdRunner )
126     {
127         int errorCode = 0;
128         
129         final String JavaDoc [] tokens = lineToTokens( line );
130         
131         try
132         {
133             final String JavaDoc cmdName = tokens[ 0 ];
134             
135             cmdRunner.execute( cmdName, tokens );
136         }
137         catch (Exception JavaDoc e )
138         {
139             errorCode = -1;
140         }
141         
142         return( errorCode );
143     }
144     
145         public void
146     processCmd( final String JavaDoc line, final CmdRunner cmdRunner )
147     {
148         final String JavaDoc [] tokens = lineToTokens( line );
149         
150         try
151         {
152             final String JavaDoc cmdName = tokens[ 0 ];
153             
154             cmdRunner.execute( cmdName, tokens );
155         }
156         catch (Exception JavaDoc e )
157         {
158         }
159     }
160
161         boolean
162     isQuitLine( String JavaDoc line )
163     {
164         final boolean isQuit =
165             line == null ||
166             line.equalsIgnoreCase( "quit" ) ||
167             line.equalsIgnoreCase( "q" ) ||
168             line.equalsIgnoreCase( "exit" );
169             line.equalsIgnoreCase( "exit" );
170             
171         return( isQuit );
172     }
173     
174         public void
175     goInteractive(
176         final LineReader lineReader,
177         final CmdRunner cmdRunner )
178         throws Exception JavaDoc
179     {
180         p( ADVISORY );
181         
182         while ( true )
183         {
184             final String JavaDoc line = readLine( lineReader );
185             
186             if ( isQuitLine( line ) )
187             {
188                 println( "Quitting..." );
189                 break;
190             }
191             
192             if ( line.length() == 0 )
193                 continue;
194             
195             processLine( line, cmdRunner );
196         }
197         
198     }
199 }
200
201
Popular Tags