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; }
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")); }
/** * 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; }
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); }
public void load() throws MBeanException, RuntimeOperationsException, InstanceNotFoundException { PersisterMBean persister = findPersister(); if (persister != null) { ModelMBeanInfo info = (ModelMBeanInfo) persister.load(); setModelMBeanInfo(info); } }
public void store() throws MBeanException, RuntimeOperationsException, InstanceNotFoundException { PersisterMBean persister = findPersister(); if (persister != null) { // Take a clone to avoid synchronization problems ModelMBeanInfo info = (ModelMBeanInfo) getMBeanInfo(); persister.store(info); } }
@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()); }
/** * 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 testManagedResourceDescriptor() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); Descriptor desc = info.getMBeanDescriptor(); assertEquals("Logging should be set to true", "true", desc.getFieldValue("log")); assertEquals("Log file should be jmx.log", "jmx.log", desc.getFieldValue("logFile")); assertEquals("Currency Time Limit should be 15", "15", desc.getFieldValue("currencyTimeLimit")); assertEquals("Persist Policy should be OnUpdate", "OnUpdate", desc.getFieldValue("persistPolicy")); assertEquals("Persist Period should be 200", "200", desc.getFieldValue("persistPeriod")); assertEquals("Persist Location should be foo", "./foo", desc.getFieldValue("persistLocation")); assertEquals("Persist Name should be bar", "bar.jmx", desc.getFieldValue("persistName")); }
@Test public void testAttributeDescriptor() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); Descriptor desc = info.getAttribute(NAME_ATTRIBUTE).getDescriptor(); assertEquals("Default value should be foo", "foo", desc.getFieldValue("default")); assertEquals("Currency Time Limit should be 20", "20", desc.getFieldValue("currencyTimeLimit")); assertEquals("Persist Policy should be OnUpdate", "OnUpdate", desc.getFieldValue("persistPolicy")); assertEquals("Persist Period should be 300", "300", desc.getFieldValue("persistPeriod")); }
@Test public void testOperationDescriptor() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); Descriptor desc = info.getOperation("myOperation").getDescriptor(); assertEquals("Currency Time Limit should be 30", "30", desc.getFieldValue("currencyTimeLimit")); assertEquals("Role should be \"operation\"", "operation", desc.getFieldValue("role")); }
@Test public void testOperationParameterMetadata() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); ModelMBeanOperationInfo oper = info.getOperation("add"); MBeanParameterInfo[] params = oper.getSignature(); assertEquals("Invalid number of params", 2, params.length); assertEquals("Incorrect name for x param", "x", params[0].getName()); assertEquals("Incorrect type for x param", int.class.getName(), params[0].getType()); assertEquals("Incorrect name for y param", "y", params[1].getName()); assertEquals("Incorrect type for y param", int.class.getName(), params[1].getType()); }
@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 testMetricDescriptor() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); Descriptor desc = info.getAttribute(QUEUE_SIZE_METRIC).getDescriptor(); assertEquals("Currency Time Limit should be 20", "20", desc.getFieldValue("currencyTimeLimit")); assertEquals("Persist Policy should be OnUpdate", "OnUpdate", desc.getFieldValue("persistPolicy")); assertEquals("Persist Period should be 300", "300", desc.getFieldValue("persistPeriod")); assertEquals("Unit should be messages", "messages",desc.getFieldValue("units")); assertEquals("Display Name should be Queue Size", "Queue Size",desc.getFieldValue("displayName")); assertEquals("Metric Type should be COUNTER", "COUNTER",desc.getFieldValue("metricType")); assertEquals("Metric Category should be utilization", "utilization",desc.getFieldValue("metricCategory")); }
@Test public void testMetricDescriptorDefaults() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); Descriptor desc = info.getAttribute(CACHE_ENTRIES_METRIC).getDescriptor(); assertNull("Currency Time Limit should not be populated", desc.getFieldValue("currencyTimeLimit")); assertNull("Persist Policy should not be populated", desc.getFieldValue("persistPolicy")); assertNull("Persist Period should not be populated", desc.getFieldValue("persistPeriod")); assertNull("Unit should not be populated", desc.getFieldValue("units")); assertEquals("Display Name should be populated by default via JMX", CACHE_ENTRIES_METRIC,desc.getFieldValue("displayName")); assertEquals("Metric Type should be GAUGE", "GAUGE",desc.getFieldValue("metricType")); assertNull("Metric Category should not be populated", desc.getFieldValue("metricCategory")); }
@Test public void testGetAgeIsReadOnly() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); ModelMBeanAttributeInfo attr = info.getAttribute(AGE_ATTRIBUTE); assertTrue(attr.isReadable()); assertFalse(attr.isWritable()); }
@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 testDescriptionNotNull() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); assertNotNull("The MBean description should not be null", info.getDescription()); }
@Test public void testAttributeInfoHasDescriptors() throws Exception { ModelMBeanInfo info = getMBeanInfoFromAssembler(); ModelMBeanAttributeInfo attr = info.getAttribute(NAME_ATTRIBUTE); Descriptor desc = attr.getDescriptor(); assertNotNull("getMethod field should not be null", desc.getFieldValue("getMethod")); assertNotNull("setMethod field should not be null", desc.getFieldValue("setMethod")); assertEquals("getMethod field has incorrect value", "getName", desc.getFieldValue("getMethod")); assertEquals("setMethod field has incorrect value", "setName", desc.getFieldValue("setMethod")); }
@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(new String[]{"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 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 testWithFallThrough() throws Exception { InterfaceBasedMBeanInfoAssembler assembler = getWithMapping("foobar", "org.springframework.jmx.export.assembler.ICustomJmxBean"); assembler.setManagedInterfaces(new Class<?>[] {IAdditionalTestMethods.class}); ModelMBeanInfo inf = assembler.getMBeanInfo(getBean(), getObjectName()); MBeanAttributeInfo attr = inf.getAttribute("NickName"); assertNickName(attr); }
@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()); }