KickJava   Java API By Example, From Geeks To Geeks.

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


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/HelpCmd.java,v 1.3 2005/12/25 03:45:36 tcfujii Exp $
26  * $Revision: 1.3 $
27  * $Date: 2005/12/25 03:45:36 $
28  */

29  
30 package com.sun.cli.jmx.cmd;
31
32 import com.sun.cli.jmx.support.CLISupportMBeanProxy;
33 import com.sun.cli.jmx.support.InspectRequest;
34 import com.sun.cli.jmx.support.InspectResult;
35 import com.sun.cli.jmx.support.InspectResult;
36  
37 import com.sun.cli.util.stringifier.ArrayStringifier;
38
39 public class HelpCmd extends JMXCmd
40 {
41         public
42     HelpCmd( final CmdEnv env )
43     {
44         super( env );
45     }
46     
47         int
48     getNumRequiredOperands()
49     {
50         // there may be some, but there may be none
51
return( 0 );
52     }
53     
54     
55         String JavaDoc
56     getUsage()
57     {
58         String JavaDoc usage = "*** Available commands ***\n\n";
59         
60         final CmdStrings.CmdHelp [] allHelp = CmdStrings.getAllHelp();
61         
62         for( int i = 0; i < allHelp.length; ++i )
63         {
64             usage = usage + allHelp[ i ].getSynopsis() + "\n\n";
65         }
66         
67         return( usage );
68     }
69     
70         public static String JavaDoc []
71     getNames( )
72     {
73         return( new String JavaDoc [] { "help", "h", "--h"} );
74     }
75     
76         private String JavaDoc
77     stripColon( String JavaDoc cmd )
78     {
79         String JavaDoc cmdString = cmd;
80         
81         if ( cmdString.endsWith( ":" ) )
82         {
83             // indicates generic JMX method
84
cmdString = cmdString.substring( 0, cmdString.length() -1);
85         }
86         return( cmdString );
87     }
88     
89         String JavaDoc
90     getHelpUnknown( String JavaDoc cmdString )
91     {
92         cmdString = stripColon( cmdString );
93         
94         String JavaDoc msg = "";
95         
96         try
97         {
98             establishProxy();
99
100             // if there is a proxy, see if any MBeans have matching operations
101
final CLISupportMBeanProxy proxy = getProxy();
102             
103             if ( proxy != null && ! cmdString.equals( "*" ) )
104             {
105                 final InspectRequest request = new InspectRequest();
106                 
107                 // ask for operations only
108
request.includeSummary = true;
109                 request.includeDescription = false;
110                 request.attrs = null;
111                 request.notifications = null;
112                 request.constructors = false;
113                 request.operations = cmdString;
114                 final InspectResult [] results =
115                         proxy.mbeanInspect( request, new String JavaDoc [] { "*" } );
116             
117                 String JavaDoc operationsMsg = "";
118                 
119                 for( int i = 0; i < results.length; ++i )
120                 {
121                     final InspectResult result = results[ i ];
122                     
123                     if ( result.operationsInfo.length != 0 )
124                     {
125                         operationsMsg = operationsMsg + result.objectInstance.getObjectName() + "\n";
126                         operationsMsg = operationsMsg + ArrayStringifier.DEFAULT.stringify( result.operationsInfo, "\n");
127                         operationsMsg = operationsMsg + "\n\n";
128                     }
129                 }
130                 
131                 if ( operationsMsg.length() != 0 )
132                 {
133                     msg = msg + operationsMsg;
134                 }
135             }
136         }
137         catch( Exception JavaDoc e )
138         {
139             // squelch
140
}
141         
142         return( msg );
143     }
144     
145         void
146     executeInternal()
147         throws Exception JavaDoc
148     {
149         final String JavaDoc [] operands = getOperands();
150         
151         if ( operands.length == 0 )
152         {
153             printUsage();
154         }
155         else
156         {
157             for ( int i = 0; i < operands.length; ++i )
158             {
159                 final String JavaDoc cmd = operands[ i ];
160                 
161                 CmdStrings.CmdHelp help = CmdStrings.getHelp( cmd );
162                 
163                 if ( help == null )
164                 {
165                     final Class JavaDoc cmdClass = getCmdFactory().getClass( cmd );
166                     
167                     if ( cmdClass != null )
168                     {
169                         final String JavaDoc [] aka = getCmdNames( cmdClass );
170                         if ( aka != null && aka.length != 0)
171                         {
172                             help = CmdStrings.getHelp( aka[ 0 ] );
173                         }
174                     }
175                 }
176                 
177                 String JavaDoc msg = null;
178                 
179                 if ( help != null )
180                 {
181                     msg = help.toString();
182                     
183                     final Class JavaDoc cmdClass = getCmdFactory().getClass( cmd );
184                     if ( cmdClass != null )
185                     {
186                         msg = msg + "\n" + getAlsoKnownAs( cmdClass );
187                     }
188                 }
189                 else
190                 {
191                     println( "Searching for MBeans with operation \"" + stripColon( cmd ) + "\"" );
192                     msg = getHelpUnknown( cmd );
193                     if ( msg == null || msg.length() == 0 )
194                     {
195                         println( "No matching operations found" );
196                     }
197                 }
198                 
199                 println( msg );
200             }
201         }
202     }
203 }
204
Popular Tags