📄 dynamicuser.java
字号:
package book.jmx.examples;
import javax.management.*;
import java.util.Iterator;
public class DynamicUser
extends NotificationBroadcasterSupport
implements DynamicMBean {
// mgmt attribute name constants
final static String ID = "ID";
final static String NAME = "Name";
final static String ADDRESS = "Address";
final static String NUMBERS = "PhoneNumbers";
final static String PASSWD = "Password";
// mgmt operation name constants
final static String PRINT_INFO = "printInfo";
final static String ADD_NUMBER = "addPhoneNumber";
final static String REMOVE_NUMBER = "removePhoneNumber";
// fields for attribute values
private long id = System.currentTimeMillis();
private String name = "";
private String address = "";
private String passwd = null;
private String[] numbers = new String[3];
// getAttribute implementation
public Object getAttribute(String attribute) throws
AttributeNotFoundException,
MBeanException,
ReflectionException {
if (attribute == null || attribute.equals(""))
throw new IllegalArgumentException(
"null or empty attribute name"
);
// map the named attributes to fields
if (attribute.equals(NAME))
return name;
if (attribute.equals(ADDRESS))
return address;
if (attribute.equals(NUMBERS))
return numbers;
if (attribute.equals(ID))
return new Long(id);
throw new AttributeNotFoundException(
"Attribute " + attribute + " not found."
);
}
// getAttributes implementation
public AttributeList getAttributes(String[] attributes) {
if (attributes == null)
throw new IllegalArgumentException("null array");
AttributeList list = new AttributeList();
for (int i = 0; i < attributes.length; ++i) {
try {
list.add(new Attribute(
attributes[i], getAttribute(attributes[i])
));
}
catch (JMException ignored) {
// if the attribute could not be retrieved, skip it
}
}
return list;
}
// setAttribute implementation
public void setAttribute(Attribute attribute) throws
AttributeNotFoundException,
InvalidAttributeValueException,
MBeanException,
ReflectionException {
if (attribute == null) throw new
AttributeNotFoundException("null attribute");
// map attributes to fields
try {
if (attribute.getName().equals(NAME))
this.name = (String)attribute.getValue();
else if (attribute.getName().equals(ADDRESS))
this.address = (String)attribute.getValue();
else if (attribute.getName().equals(NUMBERS))
this.numbers = (String[])attribute.getValue();
else if (attribute.getName().equals(PASSWD))
this.passwd = (String)attribute.getValue();
else
throw new AttributeNotFoundException(
"attribute "+attribute.getName()+" not found."
);
}
catch (ClassCastException e) {
throw new InvalidAttributeValueException(
"Invalid attribute type " +
attribute.getValue().getClass().getName()
);
}
}
// setAttributes implementation
public AttributeList setAttributes(AttributeList list) {
if (list == null)
throw new IllegalArgumentException("null list");
AttributeList results = new AttributeList();
Iterator it = list.iterator();
while (it.hasNext()) {
try {
Attribute attr = (Attribute)it.next();
setAttribute(attr);
results.add(attr);
}
catch (JMException ignored) {
// if unable to set the attribute, skip it
}
}
return results;
}
// invoke implementation
public Object invoke(String actionName,
Object[] params,
String[] signature)
throws MBeanException,
ReflectionException {
if (actionName == null || actionName.equals(""))
throw new IllegalArgumentException("no operation");
// map operation names to methods
if (actionName.equals(PRINT_INFO))
return printInfo();
else if (actionName.equals(ADD_NUMBER)) {
addPhoneNumber((String)params[0]);
return null;
}
else if (actionName.equals(REMOVE_NUMBER)) {
removePhoneNumber(((Integer)params[0]).intValue());
return null;
}
else
throw new UnsupportedOperationException(
"unknown operation " + actionName
);
}
// getMBeanInfo implementation
public MBeanInfo getMBeanInfo() {
final boolean READABLE = true;
final boolean WRITABLE = true;
final boolean IS_GETTER = true;
// MBean class and description
String className = getClass().getName();
String description =
"User resource with dynamic management interface";
// meta data for 'ID' attribute.
MBeanAttributeInfo id = new MBeanAttributeInfo(
ID, // name
long.class.getName(), // type
"Unique identifier of user.", // description
READABLE, !WRITABLE, !IS_GETTER // access
);
// meta data for 'Name' attribute.
MBeanAttributeInfo name = new MBeanAttributeInfo(
NAME, // name
String.class.getName(), // type
"User's name.", // description
READABLE, WRITABLE, !IS_GETTER // access
);
// meta data for 'Address' attribute.
MBeanAttributeInfo address = new MBeanAttributeInfo(
ADDRESS, // name
String.class.getName(), // type
"User's address.", // description
READABLE, WRITABLE, !IS_GETTER // access
);
// meta data for 'PhoneNumbers' attribute.
MBeanAttributeInfo numbers = new MBeanAttributeInfo(
NUMBERS, // name
String[].class.getName(), // type
"User's phone numbers.", // description
READABLE, WRITABLE, !IS_GETTER // access
);
// meta data for 'Password' attribute.
MBeanAttributeInfo passwd = new MBeanAttributeInfo(
PASSWD, // name
String.class.getName(), // type
"User's password.", // description
!READABLE, WRITABLE, !IS_GETTER // access
);
// meta data for 'printInfo' operation
MBeanOperationInfo printInfo = new MBeanOperationInfo(
PRINT_INFO, // name
"String representation of the user.", // description
null, // signature
String.class.getName(), // return type
MBeanOperationInfo.INFO // impact
);
// meta data for 'addPhoneNumber' operation.
MBeanOperationInfo addPhoneNumber =
new MBeanOperationInfo(
ADD_NUMBER, // name
"Adds phone number for the user.", // description
new MBeanParameterInfo[] { // signature
new MBeanParameterInfo(
"number",
String.class.getName(),
"The number to add."
)
},
void.class.getName(), // return type
MBeanOperationInfo.ACTION // impact
);
// meta data for 'removePhoneNumber' operation.
MBeanOperationInfo removePhoneNumber =
new MBeanOperationInfo(
REMOVE_NUMBER, // name
"Removes phone number from user.", // description
new MBeanParameterInfo[] { // signature
new MBeanParameterInfo(
"index",
int.class.getName(),
"The index number."
)
},
void.class.getName(), // return type
MBeanOperationInfo.ACTION // impact
);
// mbean constructors
MBeanConstructorInfo defaultConstructor =
new MBeanConstructorInfo(
"Default Constructor",
"Creates a new user instance.", null
);
// attribute, constructor and operation lists
MBeanAttributeInfo[] attributes =
new MBeanAttributeInfo[] {
id, name, address, numbers, passwd
};
MBeanConstructorInfo[] constructors =
new MBeanConstructorInfo[] {
defaultConstructor
};
MBeanOperationInfo[] operations =
new MBeanOperationInfo[] {
printInfo, addPhoneNumber, removePhoneNumber
};
// return the MBeanInfo
return new MBeanInfo(
className, description, attributes,
constructors, operations, null
);
}
// management operation implementations
public String printInfo() {
return
"User: " + name +"\n"+
"Address: " + address +"\n"+
"Phone #: " + numbers[0] +"\n"+
"Phone #: " + numbers[1] +"\n"+
"Phone #: " + numbers[2] +"\n";
}
public void addPhoneNumber(String number) {
for (int i = 0; i < numbers.length; ++i)
if (numbers[i] == null) {
numbers[i] = number;
break;
}
}
public void removePhoneNumber(int index) {
if (index < 0 || index >= numbers.length)
return;
numbers[index] = null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -