1 8 9 package test.mx4j.tools.remote.proxy; 10 11 import java.io.IOException ; 12 import java.net.URL ; 13 import javax.management.MBeanServer ; 14 import javax.management.MBeanServerConnection ; 15 import javax.management.MBeanServerDelegateMBean ; 16 import javax.management.MBeanServerInvocationHandler ; 17 import javax.management.Notification ; 18 import javax.management.NotificationListener ; 19 import javax.management.ObjectName ; 20 import javax.management.loading.MLet ; 21 import javax.management.remote.JMXConnector ; 22 import javax.management.remote.JMXConnectorFactory ; 23 import javax.management.remote.JMXConnectorServer ; 24 import javax.management.remote.JMXConnectorServerFactory ; 25 import javax.management.remote.JMXServiceURL ; 26 import javax.management.timer.Timer ; 27 28 import mx4j.tools.remote.proxy.RemoteMBeanProxy; 29 import mx4j.util.Utils; 30 import test.MX4JTestCase; 31 import test.MutableObject; 32 33 36 public class RemoteMBeanProxyTest extends MX4JTestCase 37 { 38 public RemoteMBeanProxyTest(String s) 39 { 40 super(s); 41 } 42 43 public void testNotifications() throws Exception 44 { 45 MBeanServer remoteServer = newMBeanServer(); 47 48 JMXServiceURL address1 = new JMXServiceURL ("rmi", "localhost", 0); 49 JMXConnectorServer connectorServer1 = JMXConnectorServerFactory.newJMXConnectorServer(address1, null, null); 50 ObjectName connectorServerName1 = ObjectName.getInstance(":type=connector,protocol=" + address1.getProtocol()); 51 remoteServer.registerMBean(connectorServer1, connectorServerName1); 52 connectorServer1.start(); 53 address1 = connectorServer1.getAddress(); 54 55 ObjectName remoteDelegateName = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate"); 56 57 MBeanServer localServer = newMBeanServer(); 59 60 JMXConnector cntor = JMXConnectorFactory.newJMXConnector(address1, null); 62 RemoteMBeanProxy proxy = new RemoteMBeanProxy(remoteDelegateName, cntor, null, null); 63 ObjectName proxyName = ObjectName.getInstance(":proxy=" + ObjectName.quote(remoteDelegateName.getCanonicalName())); 64 localServer.registerMBean(proxy, proxyName); 65 66 final MutableObject holder = new MutableObject(null); 68 NotificationListener listener = new NotificationListener () 69 { 70 public void handleNotification(Notification notification, Object handback) 71 { 72 synchronized (holder) 73 { 74 holder.set(notification); 75 } 76 } 77 }; 78 localServer.addNotificationListener(proxyName, listener, null, null); 79 80 sleep(1000); 82 83 MLet remoteMLet = new MLet (); 86 ObjectName remoteMLetName = ObjectName.getInstance(":type=mlet"); 87 remoteServer.registerMBean(remoteMLet, remoteMLetName); 88 89 synchronized (holder) 90 { 91 while (holder.get() == null) holder.wait(10); 92 assertNotNull(holder.get()); 93 holder.set(null); 94 } 95 96 localServer.removeNotificationListener(proxyName, listener); 98 99 remoteServer.unregisterMBean(remoteMLetName); 101 102 assertNull(holder.get()); 103 } 104 105 public void testMultipleRemoteMBeanServers() throws Exception 106 { 107 MBeanServer remoteServer1 = newMBeanServer(); 109 MLet mlet = new MLet (new URL []{new URL ("http", "host", 80, "/path")}); 110 ObjectName remoteMLetName = ObjectName.getInstance(":type=mlet"); 111 remoteServer1.registerMBean(mlet, remoteMLetName); 112 JMXServiceURL address1 = new JMXServiceURL ("rmi", "localhost", 0); 113 JMXConnectorServer connectorServer1 = JMXConnectorServerFactory.newJMXConnectorServer(address1, null, null); 114 ObjectName connectorServerName1 = ObjectName.getInstance(":type=connector,protocol=" + address1.getProtocol()); 115 remoteServer1.registerMBean(connectorServer1, connectorServerName1); 116 connectorServer1.start(); 117 address1 = connectorServer1.getAddress(); 118 119 MBeanServer remoteServer2 = newMBeanServer(); 121 Timer timer = new Timer (); 122 ObjectName remoteTimerName = ObjectName.getInstance(":type=timer"); 123 remoteServer2.registerMBean(timer, remoteTimerName); 124 timer.start(); 125 JMXServiceURL address2 = new JMXServiceURL ("rmi", "localhost", 0); 126 JMXConnectorServer connectorServer2 = JMXConnectorServerFactory.newJMXConnectorServer(address2, null, remoteServer2); 127 connectorServer2.start(); 128 address2 = connectorServer2.getAddress(); 129 130 MBeanServer localServer = newMBeanServer(); 132 RemoteMBeanProxy proxy1 = new RemoteMBeanProxy(remoteMLetName, address1, null, null); 133 JMXConnector cntor = JMXConnectorFactory.connect(address2); 134 RemoteMBeanProxy proxy2 = new RemoteMBeanProxy(remoteTimerName, cntor.getMBeanServerConnection()); 135 ObjectName proxyName1 = ObjectName.getInstance(":proxy=" + ObjectName.quote(remoteMLetName.getCanonicalName())); 136 ObjectName proxyName2 = ObjectName.getInstance(":proxy=" + ObjectName.quote(remoteTimerName.getCanonicalName())); 137 localServer.registerMBean(proxy1, proxyName1); 138 localServer.registerMBean(proxy2, proxyName2); 139 JMXServiceURL address3 = new JMXServiceURL ("local", "localhost", 0); 140 JMXConnectorServer connectorServer3 = JMXConnectorServerFactory.newJMXConnectorServer(address3, null, localServer); 141 connectorServer3.start(); 142 address3 = connectorServer3.getAddress(); 143 144 JMXConnector connector = JMXConnectorFactory.connect(address3); 146 MBeanServerConnection mbsc = connector.getMBeanServerConnection(); 147 148 URL [] urls = (URL [])mbsc.getAttribute(proxyName1, "URLs"); 150 if (!Utils.arrayEquals(urls, mlet.getURLs())) fail(); 151 152 if (!timer.isActive()) fail(); 153 mbsc.invoke(proxyName2, "stop", null, null); 154 if (timer.isActive()) fail(); 155 } 156 157 public void testJMXConnectorCloseOnDeregistration() throws Exception 158 { 159 MBeanServer remoteServer = newMBeanServer(); 161 162 JMXServiceURL address = new JMXServiceURL ("rmi", "localhost", 0); 163 JMXConnectorServer connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(address, null, null); 164 ObjectName connectorServerName = ObjectName.getInstance(":type=connector,protocol=" + address.getProtocol()); 165 remoteServer.registerMBean(connectorServer, connectorServerName); 166 connectorServer.start(); 167 address = connectorServer.getAddress(); 168 169 ObjectName remoteDelegateName = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate"); 170 171 MBeanServer localServer = newMBeanServer(); 173 174 JMXConnector cntor = JMXConnectorFactory.newJMXConnector(address, null); 176 RemoteMBeanProxy proxy = new RemoteMBeanProxy(remoteDelegateName, cntor, null, null); 177 ObjectName proxyName = ObjectName.getInstance(":proxy=" + ObjectName.quote(remoteDelegateName.getCanonicalName())); 178 localServer.registerMBean(proxy, proxyName); 179 180 MBeanServerDelegateMBean mbean = (MBeanServerDelegateMBean )MBeanServerInvocationHandler.newProxyInstance(localServer, proxyName, MBeanServerDelegateMBean .class, true); 182 String vendor = mbean.getImplementationVendor(); 183 assertNotNull(vendor); 184 185 localServer.unregisterMBean(proxyName); 187 try 188 { 189 cntor.getMBeanServerConnection(); 190 fail(); 191 } 192 catch (IOException x) 193 { 194 } 195 } 196 } 197 | Popular Tags |