下面列出了怎么用javax.management.modelmbean.ModelMBeanInfo的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"));
}
private Logger getModelMBeanLogger(String notificationType) throws MBeanException
{
// Get a copy to avoid synchronization
ModelMBeanInfo info = getModelMBeanInfo();
// First look if there is a suitable notification descriptor, otherwise use MBean descriptor
Descriptor descriptor = null;
Logger modelMBeanLogger = null;
if (notificationType != null)
{
descriptor = info.getDescriptor(notificationType, "notification");
modelMBeanLogger = findLogger(descriptor);
}
if (modelMBeanLogger == null)
{
descriptor = info.getMBeanDescriptor();
modelMBeanLogger = findLogger(descriptor);
if (modelMBeanLogger != null) return modelMBeanLogger;
}
return null;
}
@Test
public void testAttributeHasCorrespondingOperations() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
ModelMBeanOperationInfo get = info.getOperation("getName");
assertNotNull("get operation should not be null", get);
assertEquals("get operation should have visibility of four",
get.getDescriptor().getFieldValue("visibility"),
new Integer(4));
assertEquals("get operation should have role \"getter\"", "getter", get.getDescriptor().getFieldValue("role"));
ModelMBeanOperationInfo set = info.getOperation("setName");
assertNotNull("set operation should not be null", set);
assertEquals("set operation should have visibility of four",
set.getDescriptor().getFieldValue("visibility"),
new Integer(4));
assertEquals("set operation should have role \"setter\"", "setter", set.getDescriptor().getFieldValue("role"));
}
@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"));
}
@Test
public void testAttributeHasCorrespondingOperations() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
ModelMBeanOperationInfo get = info.getOperation("getName");
assertNotNull("get operation should not be null", get);
assertEquals("get operation should have visibility of four",
get.getDescriptor().getFieldValue("visibility"),
new Integer(4));
assertEquals("get operation should have role \"getter\"", "getter", get.getDescriptor().getFieldValue("role"));
ModelMBeanOperationInfo set = info.getOperation("setName");
assertNotNull("set operation should not be null", set);
assertEquals("set operation should have visibility of four",
set.getDescriptor().getFieldValue("visibility"),
new Integer(4));
assertEquals("set operation should have role \"setter\"", "setter", set.getDescriptor().getFieldValue("role"));
}
@Test
public void testAttributeHasCorrespondingOperations() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
ModelMBeanOperationInfo get = info.getOperation("getName");
assertNotNull("get operation should not be null", get);
assertEquals("get operation should have visibility of four",
get.getDescriptor().getFieldValue("visibility"),
new Integer(4));
assertEquals("get operation should have role \"getter\"", "getter", get.getDescriptor().getFieldValue("role"));
ModelMBeanOperationInfo set = info.getOperation("setName");
assertNotNull("set operation should not be null", set);
assertEquals("set operation should have visibility of four",
set.getDescriptor().getFieldValue("visibility"),
new Integer(4));
assertEquals("set operation should have role \"setter\"", "setter", set.getDescriptor().getFieldValue("role"));
}
@Test
public void testNickNameIsExposed() throws Exception {
ModelMBeanInfo inf = (ModelMBeanInfo) getMBeanInfo();
MBeanAttributeInfo attr = inf.getAttribute("NickName");
assertNickName(attr);
}
@Test
public void testNickNameIsExposed() throws Exception {
ModelMBeanInfo inf = (ModelMBeanInfo) getMBeanInfo();
MBeanAttributeInfo attr = inf.getAttribute("NickName");
assertNotNull("Nick Name should not be null", attr);
assertTrue("Nick Name should be writable", attr.isWritable());
assertTrue("Nick Name should be readable", attr.isReadable());
}
/**
* 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 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());
}
@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 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());
}
public void setModelMBeanInfo(ModelMBeanInfo modelMBeanInfo) throws MBeanException, RuntimeOperationsException
{
if (modelMBeanInfo == null) throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_MODELMBEANINFO_CANNOT_BE_NULL.toLocalizedString()));
if (!isModelMBeanInfoValid(modelMBeanInfo)) throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_MODELMBEANINFO_IS_INVALID.toLocalizedString()));
m_modelMBeanInfo = (ModelMBeanInfo)modelMBeanInfo.clone();
Logger logger = getLogger();
if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("ModelMBeanInfo successfully set to: " + m_modelMBeanInfo);
// Only now the MBean can be registered in the MBeanServer
m_canBeRegistered = true;
}
@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 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 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 testNotificationMetadata() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
MBeanNotificationInfo[] notifications = info.getNotifications();
assertEquals("Incorrect number of notifications", 1, notifications.length);
assertEquals("Incorrect notification name", "My Notification", notifications[0].getName());
String[] notifTypes = notifications[0].getNotifTypes();
assertEquals("Incorrect number of notification types", 2, notifTypes.length);
assertEquals("Notification type.foo not found", "type.foo", notifTypes[0]);
assertEquals("Notification type.bar not found", "type.bar", notifTypes[1]);
}
public MX4JModelMBean(ModelMBeanInfo info) throws MBeanException, RuntimeOperationsException
{
if (info == null)
throw new RuntimeOperationsException(new IllegalArgumentException(LocalizedStrings.MX4JModelMBean_MODELMBEANINFO_PARAMETER_CANT_BE_NULL.toLocalizedString()));
else
setModelMBeanInfo(info);
}
private void register(Object obj, ObjectName name, ModelMBeanInfo mbi, boolean forceRegistration)
throws JMException {
RequiredModelMBean rtMBean =
(RequiredModelMBean)mbs.instantiate("javax.management.modelmbean.RequiredModelMBean");
rtMBean.setModelMBeanInfo(mbi);
try {
rtMBean.setManagedResource(obj, "ObjectReference");
} catch (InvalidTargetObjectTypeException itotex) {
throw new JMException(itotex.getMessage());
}
registerMBeanWithServer(rtMBean, persist(name), forceRegistration);
}
@Test
public void testGetMBeanAttributeInfo() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
MBeanAttributeInfo[] inf = info.getAttributes();
assertEquals("Invalid number of Attributes returned",
getExpectedAttributeCount(), inf.length);
for (int x = 0; x < inf.length; x++) {
assertNotNull("MBeanAttributeInfo should not be null", inf[x]);
assertNotNull(
"Description for MBeanAttributeInfo should not be null",
inf[x].getDescription());
}
}
@Test
public void testGetMBeanOperationInfo() throws Exception {
ModelMBeanInfo info = getMBeanInfoFromAssembler();
MBeanOperationInfo[] inf = info.getOperations();
assertEquals("Invalid number of Operations returned",
getExpectedOperationCount(), inf.length);
for (int x = 0; x < inf.length; x++) {
assertNotNull("MBeanOperationInfo should not be null", inf[x]);
assertNotNull(
"Description for MBeanOperationInfo should not be null",
inf[x].getDescription());
}
}
@Test
public void testNickNameIsExposed() throws Exception {
ModelMBeanInfo inf = (ModelMBeanInfo) getMBeanInfo();
MBeanAttributeInfo attr = inf.getAttribute("NickName");
assertNotNull("Nick Name should not be null", attr);
assertTrue("Nick Name should be writable", attr.isWritable());
assertTrue("Nick Name should be readable", attr.isReadable());
}
@Test
public void testNotificationMetadata() throws Exception {
ModelMBeanInfo info = (ModelMBeanInfo) getMBeanInfo();
MBeanNotificationInfo[] notifications = info.getNotifications();
assertEquals("Incorrect number of notifications", 1, notifications.length);
assertEquals("Incorrect notification name", "My Notification", notifications[0].getName());
String[] notifTypes = notifications[0].getNotifTypes();
assertEquals("Incorrect number of notification types", 2, notifTypes.length);
assertEquals("Notification type.foo not found", "type.foo", notifTypes[0]);
assertEquals("Notification type.bar not found", "type.bar", notifTypes[1]);
}
@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 testWithFallThrough() throws Exception {
MethodNameBasedMBeanInfoAssembler assembler =
getWithMapping("foobar", "add,myOperation,getName,setName,getAge");
assembler.setManagedMethods("getNickName", "setNickName");
ModelMBeanInfo inf = assembler.getMBeanInfo(getBean(), getObjectName());
MBeanAttributeInfo attr = inf.getAttribute("NickName");
assertNickName(attr);
}
@Test
public void testNickNameIsExposed() throws Exception {
ModelMBeanInfo inf = (ModelMBeanInfo) getMBeanInfo();
MBeanAttributeInfo attr = inf.getAttribute("NickName");
assertNickName(attr);
}
@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());
}
/**
* Gets the {@code ModelMBeanInfo} for the bean with the supplied key
* and of the supplied type.
*/
private ModelMBeanInfo getMBeanInfo(Object managedBean, String beanKey) throws JMException {
ModelMBeanInfo info = this.assembler.getMBeanInfo(managedBean, beanKey);
if (logger.isWarnEnabled() && ObjectUtils.isEmpty(info.getAttributes()) &&
ObjectUtils.isEmpty(info.getOperations())) {
logger.warn("Bean with key '" + beanKey +
"' has been registered as an MBean but has no exposed attributes or operations");
}
return info;
}