SetAttrsCommandpublic class SetAttrsCommand extends MBeanServerCommand Sets one or more attributes in an MBean |
Fields Summary |
---|
private ObjectName | objectName | private List | attributeNamesList. Contains names and values | private boolean | prefix |
Constructors Summary |
---|
public SetAttrsCommand()
super("setattrs", "Set the values of one or more MBean attributes");
|
Methods Summary |
---|
public void | displayHelp()
PrintWriter out = context.getWriter();
out.println(desc);
out.println();
out.println("usage: " + name + " [options] <name> [<attr value>+]");
out.println("options:");
out.println(" --noprefix Do not display attribute name prefixes");
out.println(" -- Stop processing options");
out.flush();
| public void | execute(java.lang.String[] args)
processArguments(args);
if (objectName == null)
throw new CommandException("Missing object name");
log.debug("attribute names: " + attributeNames);
MBeanServerConnection server = getMBeanServer();
if (attributeNames.size() == 0)
{
throw new CommandException("at least 1 attribute and value needs to be defined");
}
MBeanInfo info = server.getMBeanInfo(objectName);
MBeanAttributeInfo[] attribute_info = info.getAttributes();
String type;
AttributeList attrs = new AttributeList(attributeNames.size());
Attribute attr;
String attr_name;
Object attr_value, real_value;
MBeanAttributeInfo attr_info;
for (Iterator it = attributeNames.iterator(); it.hasNext();)
{
attr_name = (String) it.next();
attr_value = it.next();
attr_info = findAttribute(attr_name, attribute_info);
if (attr_info == null)
throw new CommandException("attribute " + attr_name + " not found");
type = attr_info.getType();
PropertyEditor editor = PropertyEditors.getEditor(type);
editor.setAsText((String) attr_value);
real_value = editor.getValue();
attr = new Attribute(attr_name, real_value);
attrs.add(attr);
}
AttributeList ret = server.setAttributes(objectName, attrs);
System.out.println("The following attributes were set successfuly:");
if (ret.size() > 0)
{
for (Iterator it = ret.iterator(); it.hasNext();)
{
Attribute a = (Attribute) it.next();
System.out.println(a.getName() + "=" + a.getValue());
}
}
| private javax.management.MBeanAttributeInfo | findAttribute(java.lang.String attr_name, javax.management.MBeanAttributeInfo[] attribute_info)
for (int i = 0; i < attribute_info.length; i++)
{
MBeanAttributeInfo mBeanAttributeInfo = attribute_info[i];
if (mBeanAttributeInfo.getName().equals(attr_name))
return mBeanAttributeInfo;
}
return null;
| private boolean | processArguments(java.lang.String[] args)
log.debug("processing arguments: " + Strings.join(args, ","));
if (args.length == 0)
{
throw new CommandException("Command requires arguments");
}
String sopts = "-:";
LongOpt[] lopts =
{
new LongOpt("noprefix", LongOpt.NO_ARGUMENT, null, 0x1000),
};
Getopt getopt = new Getopt(null, args, sopts, lopts);
getopt.setOpterr(false);
int code;
int argidx = 0;
while ((code = getopt.getopt()) != -1)
{
switch (code)
{
case ':":
throw new CommandException
("Option requires an argument: " + args[getopt.getOptind() - 1]);
case '?":
{
String arg = args[getopt.getOptind() - 1];
argidx=setArgValues(argidx,arg);
break;
}
case 0x1000:
prefix = false;
break;
// non-option arguments
case 1:
{
String arg = getopt.getOptarg();
argidx=setArgValues(argidx,arg);
break;
}
}
}
return true;
| private int | setArgValues(int argdixValue, java.lang.String argValue)
switch (argdixValue++)
{
case 0:
objectName = createObjectName(argValue);
log.debug("mbean name: " + objectName);
break;
default:
log.debug("adding attribute name: " + argValue);
attributeNames.add(argValue);
break;
}
return argdixValue;
|
|