下面列出了怎么用javax.management.modelmbean.ModelMBeanAttributeInfo的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testRegistrationOnInterface() throws Exception {
Object bean = getContext().getBean("testInterfaceBean");
ModelMBeanInfo inf = getAssembler().getMBeanInfo(bean, "bean:name=interfaceTestBean");
assertNotNull(inf);
assertEquals("My Managed Bean", inf.getDescription());
ModelMBeanOperationInfo op = inf.getOperation("foo");
assertNotNull("foo operation not exposed", op);
assertEquals("invoke foo", op.getDescription());
assertNull("doNotExpose operation should not be exposed", inf.getOperation("doNotExpose"));
ModelMBeanAttributeInfo attr = inf.getAttribute("Bar");
assertNotNull("bar attribute not exposed", attr);
assertEquals("Bar description", attr.getDescription());
ModelMBeanAttributeInfo attr2 = inf.getAttribute("CacheEntries");
assertNotNull("cacheEntries attribute not exposed", attr2);
assertEquals("Metric Type should be COUNTER", "COUNTER",
attr2.getDescriptor().getFieldValue("metricType"));
}
/**
* Builds an AttributeInfo in a default way
* @return the default modelMBeanAttributeInfo object
*/
public static ModelMBeanAttributeInfo buildAttributeInfo(String name, String displayName, String description, String deflt, String operName, String signature){
Descriptor attrDesc = new DescriptorSupport();
attrDesc.setField("name", name);
attrDesc.setField("descriptorType", "attribute");
attrDesc.setField("default", deflt);
attrDesc.setField("displayName", displayName);
attrDesc.setField(
"getMethod",
operName);
ModelMBeanAttributeInfo result=new ModelMBeanAttributeInfo(name,
signature,
description,
true,
false,
false,
attrDesc);
return result;
}
@Override
public ModelMBeanAttributeInfo createAttributeInfo() {
Descriptor desc = new DescriptorSupport(
new String[] {
"name=" + this.displayName,
"descriptorType=attribute",
"currencyTimeLimit=-1", // always stale
"displayName=" + this.displayName,
"getMethod=getValue" });
Assert.assertTrue(this.stat != null, "Stat target object is null!");
desc.setField("targetObject", this.stat);
ModelMBeanAttributeInfo info = new ModelMBeanAttributeInfo(
this.displayName, // name
this.type, // type
this.description, // description
this.readable, // isReadable
this.writeable, // isWritable
this.is, // isIs
desc);
return info;
}
@Test
public void testRegistrationOnInterface() throws Exception {
Object bean = getContext().getBean("testInterfaceBean");
ModelMBeanInfo inf = getAssembler().getMBeanInfo(bean, "bean:name=interfaceTestBean");
assertNotNull(inf);
assertEquals("My Managed Bean", inf.getDescription());
ModelMBeanOperationInfo op = inf.getOperation("foo");
assertNotNull("foo operation not exposed", op);
assertEquals("invoke foo", op.getDescription());
assertNull("doNotExpose operation should not be exposed", inf.getOperation("doNotExpose"));
ModelMBeanAttributeInfo attr = inf.getAttribute("Bar");
assertNotNull("bar attribute not exposed", attr);
assertEquals("Bar description", attr.getDescription());
ModelMBeanAttributeInfo attr2 = inf.getAttribute("CacheEntries");
assertNotNull("cacheEntries attribute not exposed", attr2);
assertEquals("Metric Type should be COUNTER", "COUNTER",
attr2.getDescriptor().getFieldValue("metricType"));
}
@Override
public MBeanInfo getMBeanInfo() {
try {
ModelMBeanAttributeInfo[] attributes = new ModelMBeanAttributeInfo[0];
ModelMBeanConstructorInfo[] constructors = new ModelMBeanConstructorInfo[] {
new ModelMBeanConstructorInfo("-", this.getClass().getConstructor())
};
ModelMBeanOperationInfo[] operations = new ModelMBeanOperationInfo[] {
new ModelMBeanOperationInfo("info", "-", new MBeanParameterInfo[0], Void.class.getName(), ModelMBeanOperationInfo.INFO),
new ModelMBeanOperationInfo("action", "-", new MBeanParameterInfo[0], Void.class.getName(), ModelMBeanOperationInfo.ACTION),
new ModelMBeanOperationInfo("actionInfo", "-", new MBeanParameterInfo[0], Void.class.getName(), ModelMBeanOperationInfo.ACTION_INFO),
new ModelMBeanOperationInfo("unknown", "-", new MBeanParameterInfo[0], Void.class.getName(), ModelMBeanOperationInfo.UNKNOWN)
};
ModelMBeanNotificationInfo[] notifications = new ModelMBeanNotificationInfo[0];
return new ModelMBeanInfoSupport(this.getClass().getName(), "-", attributes, constructors, operations, notifications);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public ModelMBeanAttributeInfo createAttributeInfo() {
Descriptor desc = new DescriptorSupport(
new String[] {
"name=" + this.displayName,
"descriptorType=attribute",
"currencyTimeLimit=-1", // always stale
"displayName=" + this.displayName,
"getMethod=getJmxValue",
"setMethod=setJmxValue"
});
Assert.assertTrue(this.config != null, "Config target object is null!");
desc.setField("targetObject", this.config);
ModelMBeanAttributeInfo info = new ModelMBeanAttributeInfo(
this.displayName, // name
this.type, // type
this.description, // description
this.readable, // isReadable
this.writeable, // isWritable
this.is, // isIs
desc);
return info;
}
public void addAttributeChangeNotificationListener(NotificationListener listener, String attributeName, Object handback) throws MBeanException, RuntimeOperationsException, IllegalArgumentException
{
if (listener == null) throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_LISTENER_CANNOT_BE_NULL.toLocalizedString()));
AttributeChangeNotificationFilter filter = new AttributeChangeNotificationFilter();
if (attributeName != null)
{
filter.enableAttribute(attributeName);
}
else
{
MBeanAttributeInfo[] ai = m_modelMBeanInfo.getAttributes();
for (int i = 0; i < ai.length; i++)
{
Descriptor d = ((ModelMBeanAttributeInfo)ai[i]).getDescriptor();
filter.enableAttribute((String)d.getFieldValue("name"));
}
}
getAttributeChangeBroadcaster().addNotificationListener(listener, filter, handback);
Logger logger = getLogger();
if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Listener " + listener + " for attribute " + attributeName + " added successfully, handback is " + handback);
}
private void removeAttributeChangeNotificationListener(NotificationListener listener, String attributeName, Object handback) throws MBeanException, RuntimeOperationsException, ListenerNotFoundException
{
if (listener == null) throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_LISTENER_CANNOT_BE_NULL.toLocalizedString()));
AttributeChangeNotificationFilter filter = new AttributeChangeNotificationFilter();
if (attributeName != null)
{
filter.enableAttribute(attributeName);
}
else
{
MBeanAttributeInfo[] ai = m_modelMBeanInfo.getAttributes();
for (int i = 0; i < ai.length; i++)
{
Descriptor d = ((ModelMBeanAttributeInfo)ai[i]).getDescriptor();
filter.enableAttribute((String)d.getFieldValue("name"));
}
}
getAttributeChangeBroadcaster().removeNotificationListener(listener, filter, handback);
Logger logger = getLogger();
if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Listener " + listener + " for attribute " + attributeName + " removed successfully, handback is " + handback);
}
@Override
public ModelMBeanAttributeInfo createAttributeInfo() {
Descriptor desc = new DescriptorSupport(
new String[] {
"name=" + this.displayName,
"descriptorType=attribute",
"currencyTimeLimit=-1", // always stale
"displayName=" + this.displayName,
"getMethod=getValue" });
Assert.assertTrue(this.stat != null, "Stat target object is null!");
desc.setField("targetObject", this.stat);
ModelMBeanAttributeInfo info = new ModelMBeanAttributeInfo(
this.displayName, // name
this.type, // type
this.description, // description
this.readable, // isReadable
this.writeable, // isWritable
this.is, // isIs
desc);
return info;
}
public void addAttributeChangeNotificationListener(NotificationListener listener, String attributeName, Object handback) throws MBeanException, RuntimeOperationsException, IllegalArgumentException
{
if (listener == null) throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_LISTENER_CANNOT_BE_NULL.toLocalizedString()));
AttributeChangeNotificationFilter filter = new AttributeChangeNotificationFilter();
if (attributeName != null)
{
filter.enableAttribute(attributeName);
}
else
{
MBeanAttributeInfo[] ai = m_modelMBeanInfo.getAttributes();
for (int i = 0; i < ai.length; i++)
{
Descriptor d = ((ModelMBeanAttributeInfo)ai[i]).getDescriptor();
filter.enableAttribute((String)d.getFieldValue("name"));
}
}
getAttributeChangeBroadcaster().addNotificationListener(listener, filter, handback);
Logger logger = getLogger();
if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Listener " + listener + " for attribute " + attributeName + " added successfully, handback is " + handback);
}
@Test
public void testAttributeFromInterface() throws Exception {
ModelMBeanInfo inf = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo attr = inf.getAttribute("Colour");
assertTrue("The name attribute should be writable", attr.isWritable());
assertTrue("The name attribute should be readable", attr.isReadable());
}
@Test
public void testAttributeDescriptionOnSetter() throws Exception {
ModelMBeanInfo inf = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo attr = inf.getAttribute(AGE_ATTRIBUTE);
assertEquals("The description for the age attribute is incorrect",
"The Age Attribute", attr.getDescription());
}
/**
* Tests the situation where the attribute is only defined on the getter.
*/
@Test
public void testReadOnlyAttribute() throws Exception {
ModelMBeanInfo inf = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo attr = inf.getAttribute(AGE_ATTRIBUTE);
assertFalse("The age attribute should not be writable", attr.isWritable());
}
@Test
public void testReadWriteAttribute() throws Exception {
ModelMBeanInfo inf = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo attr = inf.getAttribute(NAME_ATTRIBUTE);
assertTrue("The name attribute should be writable", attr.isWritable());
assertTrue("The name attribute should be readable", attr.isReadable());
}
/**
* Tests the situation where the property only has a getter.
*/
@Test
public void testWithOnlySetter() throws Exception {
ModelMBeanInfo inf = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo attr = inf.getAttribute("NickName");
assertNotNull("Attribute should not be null", attr);
}
/**
* Tests the situation where the property only has a setter.
*/
@Test
public void testWithOnlyGetter() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo attr = info.getAttribute("Superman");
assertNotNull("Attribute should not be null", attr);
}
@Test
public void testMetricDescription() throws Exception {
ModelMBeanInfo inf = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo metric = inf.getAttribute(QUEUE_SIZE_METRIC);
ModelMBeanOperationInfo operation = inf.getOperation("getQueueSize");
assertEquals("The description for the queue size metric is incorrect",
"The QueueSize metric", metric.getDescription());
assertEquals("The description for the getter operation of the queue size metric is incorrect",
"The QueueSize metric", operation.getDescription());
}
@Test
public void testGetAgeIsReadOnly() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo attr = info.getAttribute(AGE_ATTRIBUTE);
assertTrue(attr.isReadable());
assertFalse(attr.isWritable());
}
@Test
public void testGetAgeIsReadOnly() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo attr = info.getAttribute(AGE_ATTRIBUTE);
assertTrue("Age is not readable", attr.isReadable());
assertFalse("Age is not writable", attr.isWritable());
}
@Test
public void testGetAgeIsReadOnly() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo attr = info.getAttribute(AGE_ATTRIBUTE);
assertTrue("Age is not readable", attr.isReadable());
assertFalse("Age is not writable", attr.isWritable());
}
@Test
public void testGetAgeIsReadOnly() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo attr = info.getAttribute(AGE_ATTRIBUTE);
assertTrue("Age is not readable", attr.isReadable());
assertFalse("Age is not writable", attr.isWritable());
}
@Test
public void testGetAgeIsReadOnly() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo attr = info.getAttribute(AGE_ATTRIBUTE);
assertTrue("Age is not readable", attr.isReadable());
assertTrue("Age is not writable", attr.isWritable());
}
@Test
public void testSupermanIsReadOnly() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo attr = info.getAttribute("Superman");
assertTrue(attr.isReadable());
assertFalse(attr.isWritable());
}
@Test
public void testGetAgeIsReadOnly() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo attr = info.getAttribute(AGE_ATTRIBUTE);
assertTrue("Age is not readable", attr.isReadable());
assertFalse("Age is not writable", attr.isWritable());
}
@Test
public void testGetAgeIsReadOnly() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo attr = info.getAttribute(AGE_ATTRIBUTE);
assertTrue(attr.isReadable());
assertFalse(attr.isWritable());
}
@Test
public void testAttributeFromInterface() throws Exception {
ModelMBeanInfo inf = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo attr = inf.getAttribute("Colour");
assertTrue("The name attribute should be writable", attr.isWritable());
assertTrue("The name attribute should be readable", attr.isReadable());
}
@Test
public void testAttributeDescriptionOnSetter() throws Exception {
ModelMBeanInfo inf = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo attr = inf.getAttribute(AGE_ATTRIBUTE);
assertEquals("The description for the age attribute is incorrect",
"The Age Attribute", attr.getDescription());
}
@Test
public void testAttributeDescriptionOnGetter() throws Exception {
ModelMBeanInfo inf = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo attr = inf.getAttribute(NAME_ATTRIBUTE);
assertEquals("The description for the name attribute is incorrect",
"The Name Attribute", attr.getDescription());
}
/**
* Tests the situation where the attribute is only defined on the getter.
*/
@Test
public void testReadOnlyAttribute() throws Exception {
ModelMBeanInfo inf = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo attr = inf.getAttribute(AGE_ATTRIBUTE);
assertFalse("The age attribute should not be writable", attr.isWritable());
}
@Test
public void testReadWriteAttribute() throws Exception {
ModelMBeanInfo inf = getMBeanInfoFromAssembler();
ModelMBeanAttributeInfo attr = inf.getAttribute(NAME_ATTRIBUTE);
assertTrue("The name attribute should be writable", attr.isWritable());
assertTrue("The name attribute should be readable", attr.isReadable());
}