1 8 9 package test.mx4j.server; 10 11 import java.lang.reflect.Method ; 12 import java.net.URL ; 13 import java.util.Iterator ; 14 import javax.management.Attribute ; 15 import javax.management.AttributeList ; 16 import javax.management.AttributeNotFoundException ; 17 import javax.management.MBeanException ; 18 import javax.management.MBeanInfo ; 19 import javax.management.MBeanServer ; 20 import javax.management.ObjectName ; 21 import javax.management.ReflectionException ; 22 import javax.management.loading.MLet ; 23 24 import mx4j.MBeanDescriptionAdapter; 25 import mx4j.MX4JSystemKeys; 26 import mx4j.server.MBeanMetaData; 27 import mx4j.server.MBeanRepository; 28 import mx4j.server.interceptor.DefaultMBeanServerInterceptor; 29 import mx4j.server.interceptor.MBeanServerInterceptor; 30 import mx4j.server.interceptor.MBeanServerInterceptorConfigurator; 31 import test.MX4JTestCase; 32 import test.MutableInteger; 33 34 37 public class MX4JMBeanServerTest extends MX4JTestCase 38 { 39 public MX4JMBeanServerTest(String s) 40 { 41 super(s); 42 } 43 44 public void testCustomMBeanRepository() throws Exception 45 { 46 String property = MX4JSystemKeys.MX4J_MBEANSERVER_REPOSITORY; 47 try 48 { 49 System.setProperty(property, MX4JMBeanServerTest.TestMBeanRepository.class.getName()); 50 MBeanServer server = newMBeanServer(); 51 Method method = server.getClass().getDeclaredMethod("getMBeanRepository", new Class [0]); 52 method.setAccessible(true); 53 Object repository = method.invoke(server, new Object [0]); 54 if (!(repository instanceof TestMBeanRepository)) fail("Custom Repository does not work"); 55 } 56 finally 57 { 58 System.getProperties().remove(property); 59 } 60 } 61 62 public void testContextClassLoaderOnMethodCalls() throws Exception 63 { 64 URL testCodebase = getClass().getProtectionDomain().getCodeSource().getLocation(); 66 URL implCodebase = MBeanServer .class.getProtectionDomain().getCodeSource().getLocation(); 67 MLet mlet = new MLet (new URL []{testCodebase, implCodebase}, getClass().getClassLoader().getParent()); 68 MBeanServer server = newMBeanServer(); 69 70 ObjectName loader = new ObjectName (":mbean=loader"); 72 server.registerMBean(mlet, loader); 73 74 server.setAttribute(ObjectName.getInstance("JMImplementation", "interceptor", "contextclassloader"), new Attribute ("Enabled", Boolean.TRUE)); 76 77 ObjectName name = new ObjectName ("Test:mbean=ccl"); 79 server.createMBean("test.mx4j.server.MX4JMBeanServerTest$CCL", name, loader, null, null); 80 81 server.invoke(name, "method", null, null); 82 } 83 84 public void testAddRemoveMBeanServerInterceptor() throws Exception 85 { 86 MBeanServer server = newMBeanServer(); 87 ObjectName configurator = new ObjectName (MBeanServerInterceptorConfigurator.OBJECT_NAME); 88 89 MutableInteger integer = new MutableInteger(0); 90 TestMBeanServerInterceptor tester = new TestMBeanServerInterceptor(integer); 91 server.invoke(configurator, "addInterceptor", new Object []{tester}, new String []{MBeanServerInterceptor.class.getName()}); 92 93 server.getAttribute(configurator, "Running"); 94 if (integer.get() != 1) fail("Interceptor not installed"); 95 96 server.invoke(configurator, "clearInterceptors", null, null); 97 server.getAttribute(configurator, "Running"); 98 99 if (integer.get() != 1) fail("Interceptor not removed"); 101 } 102 103 public void testRegisterRemoveMBeanServerInterceptor() throws Exception 104 { 105 MBeanServer server = newMBeanServer(); 106 ObjectName configurator = new ObjectName (MBeanServerInterceptorConfigurator.OBJECT_NAME); 107 108 MutableInteger integer = new MutableInteger(0); 109 TestMBeanServerInterceptor tester = new TestMBeanServerInterceptor(integer); 110 ObjectName name = new ObjectName ("Interceptor:category=MBeanServer,type=Test"); 111 server.invoke(configurator, "registerInterceptor", new Object []{tester, name}, new String []{MBeanServerInterceptor.class.getName(), ObjectName .class.getName()}); 112 113 server.getMBeanInfo(configurator); 114 if (integer.get() != 1) fail("Interceptor not installed"); 115 116 server.setAttribute(name, new Attribute ("Enabled", Boolean.FALSE)); 118 119 server.getMBeanInfo(configurator); 121 if (integer.get() != 1) fail("Interceptor not registered"); 122 123 AttributeList list = new AttributeList (); 124 list.add(new Attribute ("Enabled", Boolean.TRUE)); 125 server.setAttributes(name, list); 126 127 server.getMBeanInfo(configurator); 128 if (integer.get() != 2) fail("Interceptor not enabled"); 129 130 server.invoke(configurator, "clearInterceptors", null, null); 131 server.getAttribute(configurator, "Running"); 132 133 if (integer.get() != 2) fail("Interceptor not removed"); 135 } 136 137 public void testMBeanDescription() throws Exception 138 { 139 MBeanServer server = newMBeanServer(); 140 ObjectName name = ObjectName.getInstance(":mbean=description"); 141 server.registerMBean(new Description(), name); 142 MBeanInfo info = server.getMBeanInfo(name); 143 assertEquals(info.getDescription(), new DescriptionMBeanDescription().getMBeanDescription()); 144 } 145 146 public interface DescriptionMBean 147 { 148 } 149 150 public static class Description implements DescriptionMBean 151 { 152 } 153 154 public static class DescriptionMBeanDescription extends MBeanDescriptionAdapter 155 { 156 public String getMBeanDescription() 157 { 158 return "test"; 159 } 160 } 161 162 public static class TestMBeanServerInterceptor extends DefaultMBeanServerInterceptor 163 { 164 private MutableInteger m_integer; 165 166 public TestMBeanServerInterceptor(MutableInteger integer) 167 { 168 m_integer = integer; 169 } 170 171 public String getType() 172 { 173 return "test"; 174 } 175 176 public Object getAttribute(MBeanMetaData metadata, String attribute) throws MBeanException , AttributeNotFoundException , ReflectionException 177 { 178 if (isEnabled()) m_integer.set(m_integer.get() + 1); 179 return super.getAttribute(metadata, attribute); 180 } 181 182 public MBeanInfo getMBeanInfo(MBeanMetaData metadata) 183 { 184 if (isEnabled()) m_integer.set(m_integer.get() + 1); 185 return super.getMBeanInfo(metadata); 186 } 187 } 188 189 public static class TestMBeanRepository implements MBeanRepository 190 { 191 public MBeanMetaData get(ObjectName name) 192 { 193 return null; 194 } 195 196 public void put(ObjectName name, MBeanMetaData metadata) 197 { 198 } 199 200 public void remove(ObjectName name) 201 { 202 } 203 204 public int size() 205 { 206 return 0; 207 } 208 209 public Iterator iterator() 210 { 211 return null; 212 } 213 214 public Object clone() 215 { 216 return null; 217 } 218 } 219 220 public interface CCLMBean 221 { 222 public void method(); 223 } 224 225 public static class CCL implements CCLMBean 226 { 227 public CCL() 228 { 229 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 230 String loaderClass = cl.getClass().getName(); 231 if (!loaderClass.endsWith("MLet")) 232 { 233 throw new RuntimeException (); 234 } 235 } 236 237 public void method() 238 { 239 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 240 String loaderClass = cl.getClass().getName(); 241 if (!loaderClass.endsWith("MLet")) 242 { 243 throw new RuntimeException (); 244 } 245 } 246 } 247 } 248 | Popular Tags |