📄 xmlmetadatabuilder.java
字号:
package book.jmx.examples;
import java.net.*;
import java.util.*;
import javax.management.MBeanParameterInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanAttributeInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanConstructorInfo;
import javax.management.JMException;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.Descriptor;
import javax.management.modelmbean.*;
import org.jdom.*;
import org.jdom.input.*;
public class XMLMetaDataBuilder
implements MetaDataBuilder, XMBeanConstants {
private URL url = null;
private String className = null;
// Constructors.
public XMLMetaDataBuilder(String resourceClassName, URL url) {
this.url = url;
this.className = resourceClassName;
}
public XMLMetaDataBuilder(String resourceClassName, String url)
throws MalformedURLException {
this(resourceClassName, new URL(url));
}
// MetaDataBuilder implementation.
public ModelMBeanInfo build() throws JMException {
try {
SAXBuilder builder = new SAXBuilder();
builder.setValidation(true);
Element root = builder.build(url).getRootElement();
List constructors = root.getChildren("constructor");
List operations = root.getChildren("operation");
List attributes = root.getChildren("attribute");
List notifications = root.getChildren("notifications");
String description = root.getChildText("description");
Attribute persistPolicy = root.getAttribute(PERSIST_POLICY);
Attribute persistPeriod = root.getAttribute(PERSIST_PERIOD);
Attribute persistLocation = root.getAttribute(PERSIST_LOCATION);
Attribute persistName = root.getAttribute(PERSIST_NAME);
Attribute currTimeLimit = root.getAttribute(CURRENCY_TIME_LIMIT);
Descriptor descr = new DescriptorSupport();
descr.setField("name", className);
descr.setField("descriptorType", "mbean");
if (persistPolicy != null)
descr.setField(PERSIST_POLICY, persistPolicy.getValue());
if (persistPeriod != null)
descr.setField(PERSIST_PERIOD, persistPeriod.getValue());
if (persistLocation != null)
descr.setField(PERSIST_LOCATION, persistLocation.getValue());
if (persistName != null)
descr.setField(PERSIST_NAME, persistName.getValue());
if (currTimeLimit != null)
descr.setField(CURRENCY_TIME_LIMIT, currTimeLimit.getValue());
ModelMBeanInfo info = buildMBeanMetaData(
description, constructors, operations,
attributes, notifications, descr
);
return info;
}
catch (JDOMException e) {
throw new MBeanException(e, "Error parsing the XML file.");
}
}
// builder methods
protected ModelMBeanInfo buildMBeanMetaData(String description,
List constructors, List operations, List attributes,
List notifications, Descriptor descr) {
ModelMBeanOperationInfo[] operInfo =
buildOperationInfo(operations);
ModelMBeanAttributeInfo[] attrInfo =
buildAttributeInfo(attributes);
ModelMBeanConstructorInfo[] constrInfo =
buildConstructorInfo(constructors);
ModelMBeanNotificationInfo[] notifInfo =
buildNotificationInfo(notifications);
ModelMBeanInfo info = new ModelMBeanInfoSupport(
className, description, attrInfo, constrInfo,
operInfo, notifInfo, descr
);
return info;
}
protected ModelMBeanConstructorInfo[] buildConstructorInfo(
List constructors) {
Iterator it = constructors.iterator();
List infos = new ArrayList();
while (it.hasNext()) {
Element constr = (Element)it.next();
String name = constr.getChildTextTrim("name");
String descr = constr.getChildTextTrim("description");
List params = constr.getChildren("parameter");
MBeanParameterInfo[] paramInfo =
buildParameterInfo(params);
ModelMBeanConstructorInfo info =
new ModelMBeanConstructorInfo(name, descr, paramInfo);
infos.add(info);
}
return (ModelMBeanConstructorInfo[])infos.toArray(
new ModelMBeanConstructorInfo[0]);
}
protected ModelMBeanOperationInfo[]
buildOperationInfo(List operations) {
Iterator it = operations.iterator();
List infos = new ArrayList();
while (it.hasNext()) {
Element oper = (Element)it.next();
String name = oper.getChildTextTrim("name");
String descr = oper.getChildTextTrim("description");
String type = oper.getChildTextTrim("return-type");
String impact = oper.getChildTextTrim("impact");
List params = oper.getChildren("parameter");
MBeanParameterInfo[] paramInfo =
buildParameterInfo(params);
// defaults to ACTION_INFO
int operImpact = MBeanOperationInfo.ACTION_INFO;
if (impact != null) {
if (impact.equals("INFO"))
operImpact = MBeanOperationInfo.INFO;
else if (impact.equals("ACTION"))
operImpact = MBeanOperationInfo.ACTION;
else if (impact.equals("ACTION_INFO"))
operImpact = MBeanOperationInfo.ACTION_INFO;
}
// default return-type is void
if (type == null)
type = "void";
ModelMBeanOperationInfo info = new ModelMBeanOperationInfo(
name, descr, paramInfo, type, operImpact
);
infos.add(info);
}
return (ModelMBeanOperationInfo[])infos.toArray(
new ModelMBeanOperationInfo[0]);
}
protected ModelMBeanNotificationInfo[]
buildNotificationInfo(List notifications) {
Iterator it = notifications.iterator();
List infos = new ArrayList();
while (it.hasNext()) {
Element notif = (Element)it.next();
String name = notif.getChildTextTrim("name");
String descr = notif.getChildTextTrim("description");
List notifTypes = notif.getChildren("notification-type");
Iterator iterator = notifTypes.iterator();
List types = new ArrayList();
while (iterator.hasNext()) {
Element type = (Element)iterator.next();
types.add(type.getTextTrim());
}
ModelMBeanNotificationInfo info = new ModelMBeanNotificationInfo(
(String[])types.toArray(), name, descr
);
infos.add(info);
}
return (ModelMBeanNotificationInfo[])infos.toArray(
new ModelMBeanNotificationInfo[0]
);
}
protected ModelMBeanAttributeInfo[]
buildAttributeInfo(List attributes) {
Iterator it = attributes.iterator();
List infos = new ArrayList();
while (it.hasNext()) {
Element attr = (Element)it.next();
String name = attr.getChildTextTrim("name");
String description = attr.getChildTextTrim("description");
String type = attr.getChildTextTrim("type");
String access = attr.getChildTextTrim("access");
Attribute persistPolicy = attr.getAttribute(PERSIST_POLICY);
Attribute persistPeriod = attr.getAttribute(PERSIST_PERIOD);
Attribute setMethod = attr.getAttribute(SET_METHOD);
Attribute getMethod = attr.getAttribute(GET_METHOD);
Attribute currTimeLimit = attr.getAttribute(CURRENCY_TIME_LIMIT);
Descriptor descr = new DescriptorSupport();
descr.setField("name", name);
descr.setField("descriptorType", "attribute");
if (persistPolicy != null)
descr.setField(PERSIST_POLICY, persistPolicy.getValue());
if (persistPeriod != null)
descr.setField(PERSIST_PERIOD, persistPeriod.getValue());
if (setMethod != null)
descr.setField(SET_METHOD, setMethod.getValue());
if (getMethod != null)
descr.setField(GET_METHOD, getMethod.getValue());
if (currTimeLimit != null)
descr.setField(CURRENCY_TIME_LIMIT, currTimeLimit.getValue());
// defaults read-write
boolean isReadable = true;
boolean isWritable = true;
if (access.equalsIgnoreCase("read-only"))
isWritable = false;
else if (access.equalsIgnoreCase("write-only"))
isReadable = false;
ModelMBeanAttributeInfo info = new ModelMBeanAttributeInfo(
name, type, description, isReadable, isWritable, false, descr
);
infos.add(info);
}
return (ModelMBeanAttributeInfo[])infos.toArray(
new ModelMBeanAttributeInfo[0]
);
}
protected MBeanParameterInfo[] buildParameterInfo(List parameters) {
Iterator it = parameters.iterator();
List infos = new ArrayList();
while (it.hasNext()) {
Element param = (Element)it.next();
String name = param.getChildTextTrim("name");
String type = param.getChildTextTrim("type");
String descr = param.getChildTextTrim("description");
MBeanParameterInfo info = new MBeanParameterInfo(name, type, descr);
infos.add(info);
}
return (MBeanParameterInfo[])infos.toArray(new MBeanParameterInfo[0]);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -