/** * Set the {@code ObjectName} used to register the {@code JMXConnectorServer} * itself with the {@code MBeanServer}, as {@code ObjectName} instance * or as {@code String}. * @throws MalformedObjectNameException if the {@code ObjectName} is malformed */ public void setObjectName(Object objectName) throws MalformedObjectNameException { this.objectName = ObjectNameManager.getInstance(objectName); }
/** * Set the {@code ObjectName} of the MBean which calls are routed to, * as {@code ObjectName} instance or as {@code String}. */ public void setObjectName(Object objectName) throws MalformedObjectNameException { this.objectName = ObjectNameManager.getInstance(objectName); }
/** * Returns an instance of {@code ObjectName} based on the identity * of the managed resource. */ @Override public ObjectName getObjectName(Object managedBean, @Nullable String beanKey) throws MalformedObjectNameException { String domain = ClassUtils.getPackageName(managedBean.getClass()); Hashtable<String, String> keys = new Hashtable<>(); keys.put(TYPE_KEY, ClassUtils.getShortName(managedBean.getClass())); keys.put(HASH_CODE_KEY, ObjectUtils.getIdentityHexString(managedBean)); return ObjectNameManager.getInstance(domain, keys); }
protected MBeanInfo getMBeanInfo() throws Exception { return getServer().getMBeanInfo(ObjectNameManager.getInstance(getObjectName())); }
@Test public void testOperationInvocation() throws Exception{ ObjectName objectName = ObjectNameManager.getInstance(getObjectName()); Object result = getServer().invoke(objectName, "add", new Object[] {new Integer(20), new Integer(30)}, new String[] {"int", "int"}); assertEquals("Incorrect result", new Integer(50), result); }
@Test public void lazyNaming() throws Exception { ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("org/springframework/jmx/export/annotation/lazyNaming.xml"); try { MBeanServer server = (MBeanServer) ctx.getBean("server"); ObjectName oname = ObjectNameManager.getInstance("bean:name=testBean4"); assertNotNull(server.getObjectInstance(oname)); String name = (String) server.getAttribute(oname, "Name"); assertEquals("Invalid name returned", "TEST", name); } finally { ctx.close(); } }
@Test public void componentScan() throws Exception { ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext("org/springframework/jmx/export/annotation/componentScan.xml"); try { MBeanServer server = (MBeanServer) ctx.getBean("server"); ObjectName oname = ObjectNameManager.getInstance("bean:name=testBean4"); assertNotNull(server.getObjectInstance(oname)); String name = (String) server.getAttribute(oname, "Name"); assertNull(name); } finally { ctx.close(); } }
@Test public void testAppendIdentityToObjectName() throws MalformedObjectNameException { ObjectName objectName = ObjectNameManager.getInstance("spring:type=Test"); Object managedResource = new Object(); ObjectName uniqueName = JmxUtils.appendIdentityToObjectName(objectName, managedResource); String typeProperty = "type"; assertEquals("Domain of transformed name is incorrect", objectName.getDomain(), uniqueName.getDomain()); assertEquals("Type key is incorrect", objectName.getKeyProperty(typeProperty), uniqueName.getKeyProperty("type")); assertEquals("Identity key is incorrect", ObjectUtils.getIdentityHexString(managedResource), uniqueName.getKeyProperty(JmxUtils.IDENTITY_OBJECT_NAME_KEY)); }
private void validateMBeanAttribute(MBeanServer server, String objectName, String expected) throws Exception { ObjectName oname = ObjectNameManager.getInstance(objectName); assertNotNull(server.getObjectInstance(oname)); String name = (String) server.getAttribute(oname, "Name"); assertEquals("Invalid name returned", expected, name); }
private void assertListener(MockMBeanExporterListener listener) throws MalformedObjectNameException { ObjectName desired = ObjectNameManager.getInstance(OBJECT_NAME); assertEquals("Incorrect number of registrations", 1, listener.getRegistered().size()); assertEquals("Incorrect number of unregistrations", 1, listener.getUnregistered().size()); assertEquals("Incorrect ObjectName in register", desired, listener.getRegistered().get(0)); assertEquals("Incorrect ObjectName in unregister", desired, listener.getUnregistered().get(0)); }
@Test public void testMBean() throws Exception { // start the MBeanExporter ConfigurableApplicationContext ctx = loadContext("org/springframework/jmx/export/notificationPublisherTests.xml"); this.server.addNotificationListener(ObjectNameManager.getInstance("spring:type=PublisherMBean"), listener, null, null); MyNotificationPublisherMBean publisher = (MyNotificationPublisherMBean) ctx.getBean("publisherMBean"); publisher.sendNotification(); assertEquals("Notification not sent", 1, listener.count); }
@Test public void testSetAttribute() throws Exception { ObjectName objectName = ObjectNameManager.getInstance(getObjectName()); getServer().setAttribute(objectName, new Attribute(NAME_ATTRIBUTE, "Rob Harrop")); IJmxTestBean bean = (IJmxTestBean) getContext().getBean("testBean"); assertEquals("Rob Harrop", bean.getName()); }
@Test public void testSimpleBeanRegisteredManually() throws Exception { // start the MBeanExporter ConfigurableApplicationContext ctx = loadContext("org/springframework/jmx/export/notificationPublisherTests.xml"); MBeanExporter exporter = (MBeanExporter) ctx.getBean("exporter"); MyNotificationPublisher publisher = new MyNotificationPublisher(); exporter.registerManagedResource(publisher, ObjectNameManager.getInstance("spring:type=Publisher2")); this.server.addNotificationListener(ObjectNameManager.getInstance("spring:type=Publisher2"), listener, null, null); assertNotNull("NotificationPublisher should not be null", publisher.getNotificationPublisher()); publisher.sendNotification(); assertEquals("Notification not sent", 1, listener.count); }
@Test public void testGetAttribute() throws Exception { ObjectName objectName = ObjectNameManager.getInstance(getObjectName()); getBean().setName("John Smith"); Object val = getServer().getAttribute(objectName, NAME_ATTRIBUTE); assertEquals("Incorrect result", "John Smith", val); }
@Test public void testSimpleBean() throws Exception { // start the MBeanExporter ConfigurableApplicationContext ctx = loadContext("org/springframework/jmx/export/notificationPublisherTests.xml"); this.server.addNotificationListener(ObjectNameManager.getInstance("spring:type=Publisher"), listener, null, null); MyNotificationPublisher publisher = (MyNotificationPublisher) ctx.getBean("publisher"); assertNotNull("NotificationPublisher should not be null", publisher.getNotificationPublisher()); publisher.sendNotification(); assertEquals("Notification not sent", 1, listener.count); }
@Test public void testSelfNaming() throws Exception { ObjectName objectName = ObjectNameManager.getInstance(OBJECT_NAME); SelfNamingTestBean testBean = new SelfNamingTestBean(); testBean.setObjectName(objectName); Map<String, Object> beans = new HashMap<>(); beans.put("foo", testBean); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(server); exporter.setBeans(beans); start(exporter); ObjectInstance instance = server.getObjectInstance(objectName); assertNotNull(instance); }
@Test public void testRegisterExistingMBeanWithUserSuppliedObjectName() throws Exception { ObjectName objectName = ObjectNameManager.getInstance("spring:name=Foo"); ModelMBeanInfo info = new ModelMBeanInfoSupport("myClass", "myDescription", null, null, null, null); RequiredModelMBean bean = new RequiredModelMBean(info); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(getServer()); exporter.registerManagedResource(bean, objectName); MBeanInfo infoFromServer = getServer().getMBeanInfo(objectName); assertEquals(info, infoFromServer); }
@Test public void testRegisterManagedResourceWithUserSuppliedObjectName() throws Exception { ObjectName objectName = ObjectNameManager.getInstance("spring:name=Foo"); JmxTestBean bean = new JmxTestBean(); bean.setName("Rob Harrop"); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(getServer()); exporter.registerManagedResource(bean, objectName); String name = (String) getServer().getAttribute(objectName, "Name"); assertEquals("Incorrect name on MBean", name, bean.getName()); }
@Test public void testIgnoreNullObjectFromFactoryBean() throws MalformedObjectNameException { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); factory.registerBeanDefinition("spring:type=FactoryBean", new RootBeanDefinition(NullSomethingFactoryBean.class)); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(getServer()); exporter.setBeanFactory(factory); exporter.setAutodetectMode(MBeanExporter.AUTODETECT_ALL); start(exporter); assertIsNotRegistered("Null FactoryBean object not registered", ObjectNameManager.getInstance("spring:type=FactoryBean")); }
@Test public void testRegisterFactoryBean() throws MalformedObjectNameException { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); factory.registerBeanDefinition("spring:type=FactoryBean", new RootBeanDefinition(ProperSomethingFactoryBean.class)); MBeanExporter exporter = new MBeanExporter(); exporter.setServer(getServer()); exporter.setBeanFactory(factory); exporter.setAutodetectMode(MBeanExporter.AUTODETECT_ALL); start(exporter); assertIsRegistered("Non-null FactoryBean object registered", ObjectNameManager.getInstance("spring:type=FactoryBean")); }