1 16 17 package org.springframework.jmx.export; 18 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import javax.management.JMException ; 25 import javax.management.MBeanServer ; 26 import javax.management.MBeanServerFactory ; 27 import javax.management.MalformedObjectNameException ; 28 import javax.management.ObjectInstance ; 29 import javax.management.ObjectName ; 30 import javax.management.modelmbean.ModelMBeanInfo ; 31 32 import junit.framework.TestCase; 33 34 import org.springframework.aop.framework.ProxyFactory; 35 import org.springframework.aop.interceptor.NopInterceptor; 36 import org.springframework.beans.factory.xml.XmlBeanFactory; 37 import org.springframework.core.io.ClassPathResource; 38 import org.springframework.jmx.IJmxTestBean; 39 import org.springframework.jmx.JmxTestBean; 40 import org.springframework.jmx.export.assembler.MBeanInfoAssembler; 41 import org.springframework.jmx.export.naming.SelfNaming; 42 import org.springframework.jmx.support.ObjectNameManager; 43 44 47 public class MBeanExporterTests extends TestCase { 48 49 private static final String OBJECT_NAME = "spring:test=jmxMBeanAdaptor"; 50 51 public void testWithSuppliedMBeanServer() throws Exception { 52 MBeanServer server = MBeanServerFactory.newMBeanServer(); 53 try { 54 MBeanExporter adaptor = new MBeanExporter(); 55 adaptor.setBeans(getBeanMap()); 56 adaptor.setServer(server); 57 adaptor.afterPropertiesSet(); 58 assertTrue("The bean was not registered with the MBeanServer", 59 beanExists(server, ObjectNameManager.getInstance(OBJECT_NAME))); 60 } 61 finally { 62 server.unregisterMBean(new ObjectName (OBJECT_NAME)); 63 } 64 } 65 66 public void testWithLocatedMBeanServer() throws Exception { 67 MBeanServer server = MBeanServerFactory.createMBeanServer(); 68 try { 69 MBeanExporter adaptor = new MBeanExporter(); 70 adaptor.setBeans(getBeanMap()); 71 adaptor.afterPropertiesSet(); 72 assertTrue("The bean was not registered with the MBeanServer", 73 beanExists(server, ObjectNameManager.getInstance(OBJECT_NAME))); 74 server.unregisterMBean(new ObjectName (OBJECT_NAME)); 75 } 76 finally { 77 MBeanServerFactory.releaseMBeanServer(server); 78 } 79 } 80 81 public void testUserCreatedMBeanRegWithDynamicMBean() throws Exception { 82 Map map = new HashMap (); 83 map.put("spring:name=dynBean", new TestDynamicMBean()); 84 85 MBeanServer server = MBeanServerFactory.createMBeanServer(); 86 try { 87 InvokeDetectAssembler asm = new InvokeDetectAssembler(); 88 89 MBeanExporter adaptor = new MBeanExporter(); 90 adaptor.setServer(server); 91 adaptor.setBeans(map); 92 adaptor.setAssembler(asm); 93 adaptor.afterPropertiesSet(); 94 95 Object name = server.getAttribute(ObjectNameManager.getInstance("spring:name=dynBean"), "name"); 96 assertEquals("The name attribute is incorrect", "Rob Harrop", name); 97 assertFalse("Assembler should not have been invoked", asm.invoked); 98 } 99 finally { 100 MBeanServerFactory.releaseMBeanServer(server); 101 } 102 } 103 104 public void testAutodetectMBeans() throws Exception { 105 XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("autodetectMBeans.xml", getClass())); 106 try { 107 bf.getBean("exporter"); 108 MBeanServer server = (MBeanServer ) bf.getBean("server"); 109 ObjectInstance instance = server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean=true")); 110 assertNotNull(instance); 111 } 112 finally { 113 bf.destroySingletons(); 114 } 115 } 116 117 public void testAutodetectLazyMBeans() throws Exception { 118 XmlBeanFactory bf = new XmlBeanFactory(new ClassPathResource("autodetectLazyMBeans.xml", getClass())); 119 try { 120 bf.getBean("exporter"); 121 MBeanServer server = (MBeanServer ) bf.getBean("server"); 122 ObjectInstance instance = server.getObjectInstance(ObjectNameManager.getInstance("spring:mbean=true")); 123 assertNotNull(instance); 124 } 125 finally { 126 bf.destroySingletons(); 127 } 128 } 129 130 public void testWithMBeanExporterListeners() throws Exception { 131 MockMBeanExporterListener listener1 = new MockMBeanExporterListener(); 132 MockMBeanExporterListener listener2 = new MockMBeanExporterListener(); 133 134 MBeanServer server = MBeanServerFactory.newMBeanServer(); 135 MBeanExporter adaptor = null; 136 try { 137 adaptor = new MBeanExporter(); 138 adaptor.setBeans(getBeanMap()); 139 adaptor.setServer(server); 140 adaptor.setListeners(new MBeanExporterListener[] {listener1, listener2}); 141 adaptor.afterPropertiesSet(); 142 } 143 finally { 144 if (adaptor != null) { 145 adaptor.destroy(); 146 } 147 } 148 149 assertListener(listener1); 150 assertListener(listener2); 151 } 152 153 private void assertListener(MockMBeanExporterListener listener) throws MalformedObjectNameException { 154 ObjectName desired = ObjectNameManager.getInstance(OBJECT_NAME); 155 assertEquals("Incorrect number of registrations", 1, listener.getRegistered().size()); 156 assertEquals("Incorrect number of unregistrations", 1, listener.getUnregistered().size()); 157 assertEquals("Incorrect ObjectName in register", desired, listener.getRegistered().get(0)); 158 assertEquals("Incorrect ObjectName in unregister", desired, listener.getUnregistered().get(0)); 159 } 160 161 public void testExportJdkProxy() throws Exception { 162 JmxTestBean bean = new JmxTestBean(); 163 bean.setName("Rob Harrop"); 164 165 ProxyFactory factory = new ProxyFactory(); 166 factory.setTarget(bean); 167 factory.addAdvice(new NopInterceptor()); 168 factory.setInterfaces(new Class []{IJmxTestBean.class}); 169 170 IJmxTestBean proxy = (IJmxTestBean) factory.getProxy(); 171 String name = "bean:proxy=true"; 172 173 Map beans = new HashMap (); 174 beans.put(name, proxy); 175 MBeanServer server = MBeanServerFactory.newMBeanServer(); 176 177 MBeanExporter exporter = new MBeanExporter(); 178 exporter.setServer(server); 179 exporter.setBeans(beans); 180 exporter.registerBeans(); 181 182 ObjectName oname = ObjectName.getInstance(name); 183 Object nameValue = server.getAttribute(oname, "Name"); 184 assertEquals("Rob Harrop", nameValue); 185 } 186 187 public void testSelfNaming() throws Exception { 188 ObjectName objectName = ObjectNameManager.getInstance(OBJECT_NAME); 189 SelfNamingTestBean testBean = new SelfNamingTestBean(); 190 testBean.setObjectName(objectName); 191 192 Map beans = new HashMap (); 193 beans.put("foo", testBean); 194 195 196 MBeanServer server = MBeanServerFactory.createMBeanServer(); 197 try { 198 199 MBeanExporter adaptor = new MBeanExporter(); 200 adaptor.setServer(server); 201 adaptor.setBeans(beans); 202 203 adaptor.afterPropertiesSet(); 204 205 ObjectInstance instance = server.getObjectInstance(objectName); 206 assertNotNull(instance); 207 } 208 finally { 209 MBeanServerFactory.releaseMBeanServer(server); 210 } 211 } 212 213 private Map getBeanMap() { 214 Map map = new HashMap (); 215 map.put(OBJECT_NAME, new JmxTestBean()); 216 return map; 217 } 218 219 private boolean beanExists(MBeanServer server, ObjectName objectName) throws Exception { 220 ObjectInstance inst = server.getObjectInstance(objectName); 221 return (inst != null); 222 } 223 224 225 private static class InvokeDetectAssembler implements MBeanInfoAssembler { 226 227 private boolean invoked = false; 228 229 public ModelMBeanInfo getMBeanInfo(Object managedResource, String beanKey) throws JMException { 230 invoked = true; 231 return null; 232 } 233 } 234 235 236 private static class MockMBeanExporterListener implements MBeanExporterListener { 237 238 private List registered = new ArrayList (); 239 240 private List unregistered = new ArrayList (); 241 242 public void mbeanRegistered(ObjectName objectName) { 243 registered.add(objectName); 244 } 245 246 public void mbeanUnregistered(ObjectName objectName) { 247 unregistered.add(objectName); 248 } 249 250 public List getRegistered() { 251 return registered; 252 } 253 254 public List getUnregistered() { 255 return unregistered; 256 } 257 } 258 259 private static class SelfNamingTestBean implements SelfNaming { 260 261 private ObjectName objectName; 262 263 public void setObjectName(ObjectName objectName) { 264 this.objectName = objectName; 265 } 266 267 public ObjectName getObjectName() throws MalformedObjectNameException { 268 return this.objectName; 269 } 270 } 271 272 } 273 | Popular Tags |