1 22 package org.jboss.console.twiddle.command; 23 24 import java.io.PrintWriter ; 25 26 import javax.management.ObjectName ; 27 import javax.management.MBeanServerConnection ; 28 import javax.management.MBeanInfo ; 29 import javax.management.MBeanAttributeInfo ; 30 import javax.management.MBeanOperationInfo ; 31 import javax.management.MBeanParameterInfo ; 32 33 import gnu.getopt.Getopt; 34 import gnu.getopt.LongOpt; 35 36 import org.jboss.util.Strings; 37 38 43 public class InfoCommand 44 extends MBeanServerCommand 45 { 46 private ObjectName objectName; 47 48 public InfoCommand() 49 { 50 super("info", "Get the metadata for an MBean"); 51 } 52 53 public void displayHelp() 54 { 55 PrintWriter out = context.getWriter(); 56 57 out.println(desc); 58 out.println(); 59 out.println("usage: " + name + " <mbean-name>"); 60 out.println(" Use '*' to query for all attributes"); 61 out.flush(); 62 } 63 64 private boolean processArguments(final String [] args) 65 throws CommandException 66 { 67 log.debug("processing arguments: " + Strings.join(args, ",")); 68 69 if (args.length == 0) 70 { 71 throw new CommandException("Command requires arguments"); 72 } 73 74 String sopts = "-:"; 75 LongOpt[] lopts = 76 { 77 }; 78 79 Getopt getopt = new Getopt(null, args, sopts, lopts); 80 getopt.setOpterr(false); 81 82 int code; 83 int argidx = 0; 84 85 while ((code = getopt.getopt()) != -1) 86 { 87 switch (code) 88 { 89 case ':': 90 throw new CommandException 91 ("Option requires an argument: " + args[getopt.getOptind() - 1]); 92 93 case '?': 94 throw new CommandException 95 ("Invalid (or ambiguous) option: " + args[getopt.getOptind() - 1]); 96 97 case 1: 99 { 100 String arg = getopt.getOptarg(); 101 102 switch (argidx++) 103 { 104 case 0: 105 objectName = createObjectName(arg); 106 log.debug("mbean name: " + objectName); 107 break; 108 } 109 break; 110 } 111 } 112 } 113 114 return true; 115 } 116 117 public void execute(String [] args) throws Exception 118 { 119 processArguments(args); 120 121 if (objectName == null) 122 throw new CommandException("Missing object name"); 123 124 MBeanServerConnection server = getMBeanServer(); 125 MBeanInfo mbeanInfo = server.getMBeanInfo(objectName); 126 MBeanAttributeInfo [] attrInfo = mbeanInfo.getAttributes(); 127 MBeanOperationInfo [] opInfo = mbeanInfo.getOperations(); 128 129 PrintWriter out = context.getWriter(); 130 out.println("Description: "+mbeanInfo.getDescription()); 131 out.println("+++ Attributes:"); 132 int length = attrInfo != null ? attrInfo.length : 0; 133 for(int n = 0; n < length; n ++) 134 { 135 MBeanAttributeInfo info = attrInfo[n]; 136 out.print(" Name: "); 137 out.println(info.getName()); 138 out.print(" Type: "); 139 out.println(info.getType()); 140 String rw = ""; 141 if( info.isReadable() ) 142 rw = "r"; 143 else 144 rw = "-"; 145 if( info.isWritable() ) 146 rw += "w"; 147 else 148 rw += "-"; 149 out.print(" Access: "); 150 out.println(rw); 151 } 152 153 out.println("+++ Operations:"); 154 length = opInfo != null ? opInfo.length : 0; 155 for(int n = 0; n < length; n ++) 156 { 157 MBeanOperationInfo info = opInfo[n]; 158 out.print(' '); 159 out.print(info.getReturnType()); 160 out.print(' '); 161 out.print(info.getName()); 162 out.print('('); 163 MBeanParameterInfo [] sig = info.getSignature(); 164 for(int s = 0; s < sig.length; s ++) 165 { 166 out.print(sig[s].getType()); 167 out.print(' '); 168 out.print(sig[s].getName()); 169 if( s < sig.length-1 ) 170 out.print(','); 171 } 172 out.println(')'); 173 } 174 } 175 } 176 | Popular Tags |