1 22 package org.jboss.console.twiddle.command; 23 24 import java.io.PrintWriter ; 25 import java.util.List ; 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 29 import javax.management.ObjectName ; 30 import javax.management.MBeanServerConnection ; 31 import javax.management.Attribute ; 32 import javax.management.AttributeList ; 33 import javax.management.MBeanInfo ; 34 import javax.management.MBeanAttributeInfo ; 35 36 import gnu.getopt.Getopt; 37 import gnu.getopt.LongOpt; 38 39 import org.jboss.util.Strings; 40 41 48 public class GetCommand 49 extends MBeanServerCommand 50 { 51 private ObjectName objectName; 52 53 private List attributeNames = new ArrayList (5); 54 55 private boolean prefix = true; 56 57 public GetCommand() 58 { 59 super("get", "Get the values of one or more MBean attributes"); 60 } 61 62 public void displayHelp() 63 { 64 PrintWriter out = context.getWriter(); 65 66 out.println(desc); 67 out.println(); 68 out.println("usage: " + name + " [options] <name> [<attr>+]"); 69 out.println(" If no attribute names are given all readable attributes are retrieved"); 70 out.println("options:"); 71 out.println(" --noprefix Do not display attribute name prefixes"); 72 out.println(" -- Stop processing options"); 73 74 out.flush(); 75 } 76 77 private boolean processArguments(final String [] args) 78 throws CommandException 79 { 80 log.debug("processing arguments: " + Strings.join(args, ",")); 81 82 if (args.length == 0) 83 { 84 throw new CommandException("Command requires arguments"); 85 } 86 87 String sopts = "-:"; 88 LongOpt[] lopts = 89 { 90 new LongOpt("noprefix", LongOpt.NO_ARGUMENT, null, 0x1000), 91 }; 92 93 Getopt getopt = new Getopt(null, args, sopts, lopts); 94 getopt.setOpterr(false); 95 96 int code; 97 int argidx = 0; 98 99 while ((code = getopt.getopt()) != -1) 100 { 101 switch (code) 102 { 103 case ':': 104 throw new CommandException 105 ("Option requires an argument: " + args[getopt.getOptind() - 1]); 106 107 case '?': 108 throw new CommandException 109 ("Invalid (or ambiguous) option: " + args[getopt.getOptind() - 1]); 110 111 case 0x1000: 112 prefix = false; 113 break; 114 115 case 1: 117 { 118 String arg = getopt.getOptarg(); 119 120 switch (argidx++) 121 { 122 case 0: 123 objectName = createObjectName(arg); 124 log.debug("mbean name: " + objectName); 125 break; 126 127 default: 128 log.debug("adding attribute name: " + arg); 129 attributeNames.add(arg); 130 break; 131 } 132 break; 133 } 134 } 135 } 136 137 return true; 138 } 139 140 public void execute(String [] args) throws Exception 141 { 142 processArguments(args); 143 144 if (objectName == null) 145 throw new CommandException("Missing object name"); 146 147 log.debug("attribute names: " + attributeNames); 148 149 MBeanServerConnection server = getMBeanServer(); 150 if (attributeNames.size() == 0) 151 { 152 attributeNames.clear(); 154 MBeanInfo info = server.getMBeanInfo(objectName); 155 MBeanAttributeInfo [] attrInfos = info.getAttributes(); 156 for (int a = 0; a < attrInfos.length; a++) 157 { 158 MBeanAttributeInfo attrInfo = attrInfos[a]; 159 if (attrInfo.isReadable()) 160 attributeNames.add(attrInfo.getName()); 161 } 162 } 163 164 String [] names = new String [attributeNames.size()]; 165 attributeNames.toArray(names); 166 log.debug("as string[]: " + Strings.join(names, ",")); 167 168 AttributeList attrList = server.getAttributes(objectName, names); 169 log.debug("attribute list: " + attrList); 170 171 if (attrList.size() == 0) 172 { 173 throw new CommandException("No matching attributes"); 174 } 175 else if (attrList.size() != names.length) 176 { 177 log.warn("Not all specified attributes were found"); 178 } 179 180 PrintWriter out = context.getWriter(); 181 182 Iterator iter = attrList.iterator(); 183 while (iter.hasNext()) 184 { 185 Attribute attr = (Attribute ) iter.next(); 186 if (prefix) 187 { 188 out.print(attr.getName()); 189 out.print("="); 190 } 191 out.println(attr.getValue()); 192 } 193 } 194 } 195 | Popular Tags |