1 22 package org.jboss.console.twiddle.command; 23 24 import gnu.getopt.Getopt; 25 import gnu.getopt.LongOpt; 26 27 import java.beans.PropertyEditor ; 28 import java.io.PrintWriter ; 29 import java.util.ArrayList ; 30 import java.util.List ; 31 32 import javax.management.Attribute ; 33 import javax.management.MBeanAttributeInfo ; 34 import javax.management.MBeanInfo ; 35 import javax.management.MBeanServerConnection ; 36 import javax.management.ObjectName ; 37 38 import org.jboss.util.Strings; 39 import org.jboss.util.propertyeditor.PropertyEditors; 40 41 42 48 public class SetCommand 49 extends MBeanServerCommand 50 { 51 52 private ObjectName objectName; 53 private List attributeNames = new ArrayList (5); 54 private boolean prefix = true; 55 private String query; 56 57 public SetCommand() 58 { 59 super("set", "Set the value of one MBean attribute"); 60 } 61 62 63 public void displayHelp() 64 { 65 PrintWriter out = context.getWriter(); 66 67 out.println(desc); 68 out.println(); 69 out.println("usage: " + name + " [options] <name> <attr> <val>"); 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 141 public void execute(String [] args) throws Exception 142 { 143 144 processArguments(args); 145 146 String theAttr = (String )attributeNames.toArray()[0]; 147 String theVal = (String )attributeNames.toArray()[1]; 148 149 if (objectName == null) 150 throw new CommandException("Missing object name"); 151 152 log.debug("attribute names: " + attributeNames); 153 if (attributeNames.size() != 2 ) 154 { 155 throw new CommandException("Wrong number of arguments"); 156 } 157 MBeanServerConnection server = getMBeanServer(); 158 159 MBeanInfo info = server.getMBeanInfo(objectName); 160 161 MBeanAttributeInfo [] attrs = info.getAttributes(); 162 163 MBeanAttributeInfo attr=null; 164 165 boolean found = false; 166 for (int i=0;i < attrs.length ; i++ ) 167 { 168 if (attrs[i].getName().equals(theAttr) && 169 attrs[i].isWritable()) { 170 171 found=true; 172 attr= attrs[i]; 173 break; 174 } 175 } 176 177 if (found == false) 178 { 179 180 throw new CommandException("No matching attribute found"); 181 } 182 else 183 { 184 Object oVal = convert(theVal,attr.getType()); 185 Attribute at = new Attribute (theAttr,oVal); 186 server.setAttribute(objectName,at); 187 188 if (!context.isQuiet()) 190 { 191 PrintWriter out = context.getWriter(); 192 Object nat = server.getAttribute(objectName,theAttr); 193 if (nat==null) 194 out.println("null"); 195 else 196 if (prefix) 197 out.print(theAttr+"="); 198 out.println(nat.toString()); 199 } 200 } 201 202 203 } 204 205 212 private Object convert (String val,String oType) throws Exception 213 { 214 PropertyEditor editor = PropertyEditors.getEditor(oType); 215 editor.setAsText(val); 216 return editor.getValue(); 217 } 218 } 219 | Popular Tags |