1 23 24 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 return( 0 ); 52 } 53 54 55 String 56 getUsage() 57 { 58 String 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 [] 71 getNames( ) 72 { 73 return( new String [] { "help", "h", "--h"} ); 74 } 75 76 private String 77 stripColon( String cmd ) 78 { 79 String cmdString = cmd; 80 81 if ( cmdString.endsWith( ":" ) ) 82 { 83 cmdString = cmdString.substring( 0, cmdString.length() -1); 85 } 86 return( cmdString ); 87 } 88 89 String 90 getHelpUnknown( String cmdString ) 91 { 92 cmdString = stripColon( cmdString ); 93 94 String msg = ""; 95 96 try 97 { 98 establishProxy(); 99 100 final CLISupportMBeanProxy proxy = getProxy(); 102 103 if ( proxy != null && ! cmdString.equals( "*" ) ) 104 { 105 final InspectRequest request = new InspectRequest(); 106 107 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 [] { "*" } ); 116 117 String 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 e ) 138 { 139 } 141 142 return( msg ); 143 } 144 145 void 146 executeInternal() 147 throws Exception 148 { 149 final String [] 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 cmd = operands[ i ]; 160 161 CmdStrings.CmdHelp help = CmdStrings.getHelp( cmd ); 162 163 if ( help == null ) 164 { 165 final Class cmdClass = getCmdFactory().getClass( cmd ); 166 167 if ( cmdClass != null ) 168 { 169 final String [] aka = getCmdNames( cmdClass ); 170 if ( aka != null && aka.length != 0) 171 { 172 help = CmdStrings.getHelp( aka[ 0 ] ); 173 } 174 } 175 } 176 177 String msg = null; 178 179 if ( help != null ) 180 { 181 msg = help.toString(); 182 183 final Class 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 |