📄 xmbean.java
字号:
package book.jmx.examples;
import java.net.*;
import java.io.*;
import java.util.*;
import java.lang.reflect.*;
import javax.management.*;
import javax.management.modelmbean.*;
import javax.management.loading.*;
public class XMBean implements ModelMBean, MBeanRegistration,
XMBeanConstants {
protected MBeanServer server = null;
// sequence numbers for notifications
protected long notifierSequence = 0;
protected long attrNotifierSequence = 0;
// support for generic notification listeners
private NotificationBroadcasterSupport notifier =
new NotificationBroadcasterSupport();
// maps mbean attribute and operation names to
// corresponding XMBeanAttribute and XMBeanOperation
protected Map attributeMap = new HashMap();
protected Map operationMap = new HashMap();
// managed resource, resource type, model mbean metadata
// and object name reference
protected Object resource = null;
protected String resourceType = null;
protected ModelMBeanInfo metadata = null;
protected ObjectName name = null;
// Constructors.
public XMBean() {}
public XMBean(ModelMBeanInfo info) throws MBeanException {
setModelMBeanInfo(info);
try {
load();
} catch (InstanceNotFoundException e) {}
}
public XMBean(String url, String resource) throws MBeanException {
try {
MetaDataBuilder builder = new XMLMetaDataBuilder(resource, url);
setModelMBeanInfo(builder.build());
setManagedResource(DefaultLoaderRepository.loadClass(resource)
.newInstance(), OBJECT_REF);
try {
load();
} catch (InstanceNotFoundException e) {}
}
catch (ClassNotFoundException e) {
throw new MBeanException(
e, "Unable to load class " + resource
);
}
catch (InvalidTargetObjectTypeException e) {
throw new Error("Invalid resource type 'ObjectReference'.");
}
catch (JMException e) {
throw new MBeanException(e);
}
catch (Exception e) {
e.printStackTrace();
throw new MBeanException(e);
}
}
// ModelMbean interface
public void setModelMBeanInfo(ModelMBeanInfo metadata)
throws MBeanException, RuntimeOperationsException {
if (metadata == null) {
throw new IllegalArgumentException(
"The Model MBeanInfo cannot be null."
);
}
if (metadata instanceof XMBeanInfo)
try {
metadata = new XMLMetaDataBuilder("", (String)metadata.getMBeanDescriptor().getFieldValue(XML_DEFINITION_URL)).build();
} catch (Exception e) {
e.printStackTrace();
throw new MBeanException(e);
}
this.metadata = metadata;
}
public void setManagedResource(Object ref, String resourceType)
throws MBeanException,
InstanceNotFoundException,
InvalidTargetObjectTypeException {
if (ref == null) {
throw new IllegalArgumentException(
"Resource reference cannot be null."
);
}
// check that is a supported resource type
if (!isSupportedResourceType(resourceType)) {
throw new InvalidTargetObjectTypeException(
"Unsupported resource type: " + resourceType
);
}
this.resource = ref;
this.resourceType = resourceType;
}
protected boolean isSupportedResourceType(
String resourceType) {
if (resourceType == null) return false;
if (resourceType.equals(OBJECT_REF)) return true;
return false;
}
// ModelMBeanNotificationBroadcaster interface
public void addNotificationListener(
NotificationListener l, NotificationFilter filter,
Object hback) {
notifier.addNotificationListener(l, filter, hback);
}
public void removeNotificationListener(
NotificationListener l)
throws ListenerNotFoundException {
notifier.removeNotificationListener(l);
}
public void addAttributeChangeNotificationListener(
NotificationListener l, String attributeName,
Object hback) throws MBeanException {
AttributeChangeNotificationFilter filter =
new AttributeChangeNotificationFilter();
filter.enableAttribute(attributeName);
notifier.addNotificationListener(l, filter,hback);
}
public void removeAttributeChangeNotificationListener(
NotificationListener l, String attributeName)
throws MBeanException, ListenerNotFoundException {
notifier.removeNotificationListener(l);
}
public void sendNotification(String message)
throws MBeanException {
Notification notif = new Notification(
GENERIC_MODELMBEAN_NOTIFICATION, // type
this, // source
++notifierSequence, // sequence number
message // message
);
sendNotification(notif);
}
public void sendNotification(Notification notif)
throws MBeanException {
notifier.sendNotification(notif);
}
public void sendAttributeChangeNotification(
AttributeChangeNotification notif)
throws MBeanException {
notifier.sendNotification(notif);
}
public void sendAttributeChangeNotification(
Attribute oldValue, Attribute newValue)
throws MBeanException {
String attr = oldValue.getName();
String type = oldValue.getClass().getName();
AttributeChangeNotification notif =
new AttributeChangeNotification(
this, // source
++attrNotifierSequence, // seq. #
System.currentTimeMillis(), // time stamp
"" + attr + " changed from " // message
+ oldValue + " to " + newValue,
attr, type, // name & type
oldValue, newValue // values
);
notifier.sendNotification(notif);
}
public MBeanNotificationInfo[] getNotificationInfo() {
int size = metadata.getNotifications().length;
MBeanNotificationInfo[] notifInfo = metadata.getNotifications();
MBeanNotificationInfo[] modelInfo =
new MBeanNotificationInfo[size + 2];
for (int i = 0; i < size ;++i)
modelInfo[i] = notifInfo[i];
Descriptor descr1 = new DescriptorSupport();
descr1.setField("name", "generic");
descr1.setField("descriptorType", "notification");
descr1.setField("severity", "5");
ModelMBeanNotificationInfo generic = new ModelMBeanNotificationInfo(
new String[] { GENERIC_MODELMBEAN_NOTIFICATION },
"generic",
"A generic Model MBean notification.",
descr1
);
Descriptor descr2 = new DescriptorSupport();
descr2.setField("name", AttributeChangeNotification.class.getName());
descr2.setField("descriptorType", "notification");
ModelMBeanNotificationInfo attrChange = new ModelMBeanNotificationInfo(
new String[] { AttributeChangeNotification.ATTRIBUTE_CHANGE },
AttributeChangeNotification.class.getName(),
"Notifies a change in attribute's value.",
descr2
);
modelInfo[size-2] = generic;
modelInfo[size-1] = attrChange;
return modelInfo;
}
// PersistentMBean interface
public void load() throws MBeanException,
InstanceNotFoundException {
// throw new UnsupportedOperationException();
if (metadata == null)
return;
Descriptor d = metadata.getMBeanDescriptor();
String dir = (String)d.getFieldValue(PERSIST_LOCATION);
String file = (String)d.getFieldValue(PERSIST_NAME);
if (file != null) {
try {
File f = new File(dir, file);
FileInputStream fis = new FileInputStream(f);
ObjectInputStream ois = new ObjectInputStream(fis);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -