1 22 package org.jboss.console.twiddle.command; 23 24 import java.beans.PropertyEditor ; 25 import java.io.PrintWriter ; 26 import java.util.ArrayList ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import javax.management.Attribute ; 31 import javax.management.AttributeList ; 32 import javax.management.MBeanAttributeInfo ; 33 import javax.management.MBeanInfo ; 34 import javax.management.MBeanServerConnection ; 35 import javax.management.ObjectName ; 36 37 import gnu.getopt.Getopt; 38 import gnu.getopt.LongOpt; 39 import org.jboss.util.Strings; 40 import org.jboss.util.propertyeditor.PropertyEditors; 41 42 48 public class SetAttrsCommand 49 extends MBeanServerCommand 50 { 51 private ObjectName objectName; 52 53 54 private List attributeNames = new ArrayList (5); 55 56 57 public SetAttrsCommand() 58 { 59 super("setattrs", "Set 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 value>+]"); 69 out.println("options:"); 70 out.println(" --noprefix Do not display attribute name prefixes"); 71 out.println(" -- Stop processing options"); 72 73 out.flush(); 74 } 75 76 private boolean processArguments(final String [] args) 77 throws CommandException 78 { 79 log.debug("processing arguments: " + Strings.join(args, ",")); 80 81 if (args.length == 0) 82 { 83 throw new CommandException("Command requires arguments"); 84 } 85 86 String sopts = "-:"; 87 LongOpt[] lopts = 88 { 89 new LongOpt("noprefix", LongOpt.NO_ARGUMENT, null, 0x1000), 90 }; 91 92 Getopt getopt = new Getopt(null, args, sopts, lopts); 93 getopt.setOpterr(false); 94 95 int code; 96 int argidx = 0; 97 98 while ((code = getopt.getopt()) != -1) 99 { 100 switch (code) 101 { 102 case ':': 103 throw new CommandException 104 ("Option requires an argument: " + args[getopt.getOptind() - 1]); 105 106 case '?': 107 throw new CommandException 108 ("Invalid (or ambiguous) option: " + args[getopt.getOptind() - 1]); 109 110 case 0x1000: 111 break; 112 113 case 1: 115 { 116 String arg = getopt.getOptarg(); 117 118 switch (argidx++) 119 { 120 case 0: 121 objectName = createObjectName(arg); 122 log.debug("mbean name: " + objectName); 123 break; 124 125 default: 126 log.debug("adding attribute name: " + arg); 127 attributeNames.add(arg); 128 break; 129 } 130 break; 131 } 132 } 133 } 134 135 return true; 136 } 137 138 public void execute(String [] args) throws Exception 139 { 140 processArguments(args); 141 142 if (objectName == null) 143 throw new CommandException("Missing object name"); 144 145 log.debug("attribute names: " + attributeNames); 146 147 MBeanServerConnection server = getMBeanServer(); 148 if (attributeNames.size() == 0) 149 { 150 throw new CommandException("at least 1 attribute and value needs to be defined"); 151 } 152 153 MBeanInfo info = server.getMBeanInfo(objectName); 154 MBeanAttributeInfo [] attribute_info = info.getAttributes(); 155 String type; 156 AttributeList attrs = new AttributeList (attributeNames.size()); 157 Attribute attr; 158 String attr_name; 159 Object attr_value, real_value; 160 MBeanAttributeInfo attr_info; 161 162 163 for (Iterator it = attributeNames.iterator(); it.hasNext();) 164 { 165 attr_name = (String ) it.next(); 166 attr_value = it.next(); 167 168 attr_info = findAttribute(attr_name, attribute_info); 169 if (attr_info == null) 170 throw new CommandException("attribute " + attr_name + " not found"); 171 type = attr_info.getType(); 172 173 PropertyEditor editor = PropertyEditors.getEditor(type); 174 editor.setAsText((String ) attr_value); 175 real_value = editor.getValue(); 176 177 attr = new Attribute (attr_name, real_value); 178 attrs.add(attr); 179 } 180 181 AttributeList ret = server.setAttributes(objectName, attrs); 182 System.out.println("The following attributes were set successfuly:"); 183 if (ret.size() > 0) 184 { 185 for (Iterator it = ret.iterator(); it.hasNext();) 186 { 187 Attribute a = (Attribute ) it.next(); 188 System.out.println(a.getName() + "=" + a.getValue()); 189 } 190 } 191 } 192 193 private MBeanAttributeInfo findAttribute(String attr_name, 194 MBeanAttributeInfo [] attribute_info) 195 { 196 for (int i = 0; i < attribute_info.length; i++) 197 { 198 MBeanAttributeInfo mBeanAttributeInfo = attribute_info[i]; 199 if (mBeanAttributeInfo.getName().equals(attr_name)) 200 return mBeanAttributeInfo; 201 } 202 return null; 203 } 204 } 205 | Popular Tags |