[jboss-cvs] JBossAS SVN: r88147 - projects/jboss-osgi/trunk/bundle/blueprint/src/test/java/org/jboss/test/osgi/blueprint/jmx.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Mon May 4 09:17:32 EDT 2009
Author: thomas.diesler at jboss.com
Date: 2009-05-04 09:17:32 -0400 (Mon, 04 May 2009)
New Revision: 88147
Modified:
projects/jboss-osgi/trunk/bundle/blueprint/src/test/java/org/jboss/test/osgi/blueprint/jmx/JMXTestCase.java
Log:
bind/lookup of MBeanServerConnection ok
Modified: projects/jboss-osgi/trunk/bundle/blueprint/src/test/java/org/jboss/test/osgi/blueprint/jmx/JMXTestCase.java
===================================================================
--- projects/jboss-osgi/trunk/bundle/blueprint/src/test/java/org/jboss/test/osgi/blueprint/jmx/JMXTestCase.java 2009-05-04 13:13:34 UTC (rev 88146)
+++ projects/jboss-osgi/trunk/bundle/blueprint/src/test/java/org/jboss/test/osgi/blueprint/jmx/JMXTestCase.java 2009-05-04 13:17:32 UTC (rev 88147)
@@ -24,7 +24,11 @@
//$Id$
import java.io.IOException;
+import java.io.Serializable;
+import java.net.InetAddress;
+import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
+import java.rmi.registry.Registry;
import java.util.Hashtable;
import java.util.Set;
@@ -56,15 +60,23 @@
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.Name;
+import javax.naming.NamingException;
+import javax.naming.RefAddr;
+import javax.naming.Reference;
+import javax.naming.StringRefAddr;
import javax.naming.spi.ObjectFactory;
-import org.jboss.osgi.jndi.internal.JNPServer;
+import org.jboss.logging.Logger;
+import org.jboss.net.sockets.DefaultSocketFactory;
import org.jboss.osgi.spi.framework.OSGiFramework;
import org.jboss.osgi.spi.management.MBeanProxy;
import org.jboss.test.osgi.blueprint.BlueprintTest;
import org.jboss.test.osgi.blueprint.jmx.bundle.Foo;
import org.jboss.test.osgi.blueprint.jmx.bundle.FooMBean;
+import org.jnp.interfaces.Naming;
import org.jnp.server.Main;
+import org.jnp.server.NamingBean;
+import org.jnp.server.NamingServer;
import org.osgi.framework.BundleContext;
/**
@@ -75,51 +87,114 @@
*/
public class JMXTestCase extends BlueprintTest
{
- public void _testJMXConnector() throws Exception
+ private static final Logger log = Logger.getLogger(JMXTestCase.class);
+
+ private Main namingMain;
+ private Naming namingServer;
+
+ public void testJMXConnector() throws Exception
{
// Get the MBeanServer and register an MBean
- MBeanServer mbs = getMBeanServer();
+ MBeanServer mbeanServer = getMBeanServer();
ObjectName oname = new ObjectName("jboss.osgi:service=mbean-test-service");
- mbs.registerMBean(new Foo(), oname);
-
+ mbeanServer.registerMBean(new Foo(), oname);
+
// Access the MBean
- FooMBean foo = (FooMBean)MBeanProxy.get(FooMBean.class, oname, getMBeanServer());
+ FooMBean foo = (FooMBean)MBeanProxy.get(FooMBean.class, oname, mbeanServer);
assertEquals("hello", foo.echo("hello"));
-
- Main namingMain = new Main();
+
+ NamingBean namingBean = new NamingBean()
+ {
+ public Naming getNamingInstance()
+ {
+ if (namingServer == null)
+ {
+ try
+ {
+ namingServer = new NamingServer();
+ }
+ catch (NamingException ex)
+ {
+ throw new IllegalStateException("Cannot create NamingServer", ex);
+ }
+ }
+ return namingServer;
+ }
+ };
+
+ // the address to expose in the urls
+ String host = System.getProperty("java.rmi.server.hostname");
+ if (host == null)
+ host = "localhost";
+
+ String jndiPath = "/jmxconnector";
+ InetAddress bindAddress = InetAddress.getByName(host);
+ int rmiPort = 1098;
+
+ namingMain = new Main();
+ namingMain.setNamingInfo(namingBean);
+ namingMain.setBindAddress(host);
+ namingMain.setRmiPort(rmiPort);
+ namingMain.setPort(1099);
+
namingMain.start();
-
- // Create the RMI registry
- LocateRegistry.createRegistry(1099);
-
- // Create and start the the JMXConnectorServer
- JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:1099/server");
- JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
- cs.start();
-
- // Create a client JMXConnector and get the MBeanServerConnection
- MBeanServerConnection rmiAdaptor = new RMIAdaptor(url);
+ // check to see if registry already created
+ Registry rmiRegistry = LocateRegistry.getRegistry(host, rmiPort);
+ if (rmiRegistry != null)
+ {
+ try
+ {
+ rmiRegistry.list();
+ }
+ catch (RemoteException e)
+ {
+ log.debug("No registry running at host '" + host + "', port '" + rmiPort + "'. Will create one.");
+ rmiRegistry = LocateRegistry.createRegistry(rmiPort, null, new DefaultSocketFactory(bindAddress));
+ }
+ }
+ else
+ {
+ rmiRegistry = LocateRegistry.createRegistry(rmiPort, null, new DefaultSocketFactory(bindAddress));
+ }
+
+ String serviceURL = "service:jmx:rmi://" + host + "/jndi/rmi://" + host + ":" + rmiPort + jndiPath;
+
+ final JMXServiceURL url = new JMXServiceURL(serviceURL);
+
+ // create new connector server and start it
+ JMXConnectorServer connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbeanServer);
+ connectorServer.start();
+
+ log.info("JMX Connector server: " + serviceURL);
+
+ // Bind the ObjectFactory that creates the MBeanServerConnection
InitialContext iniCtx = new InitialContext();
- iniCtx.bind("jmx/invoker/RMIAdaptor", new RMIAdaptorFactory(url));
- //MBeanServerConnection rmiAdaptor = mbsc; //(MBeanServerConnection)iniCtx.lookup("jmx/invoker/RMIAdaptor");
-
+ iniCtx.createSubcontext("jmx").createSubcontext("invoker");
+ StringRefAddr addr = new StringRefAddr(JMXServiceURL.class.getName(), serviceURL);
+ Reference ref = new Reference(MBeanServerConnection.class.getName(), addr, RMIAdaptorFactory.class.getName(), null);
+ iniCtx.bind("jmx/invoker/RMIAdaptor", ref);
+
+ // Lookup the MBeanServerConnection
+ MBeanServerConnection rmiAdaptor = (MBeanServerConnection)iniCtx.lookup("jmx/invoker/RMIAdaptor");
+
// Access the MBean through the MBeanServerConnection
FooMBean remfoo = (FooMBean)MBeanProxy.get(FooMBean.class, oname, rmiAdaptor);
assertEquals("hello", remfoo.echo("hello"));
+ rmiAdaptor.unregisterMBean(oname);
}
-
+
public void testMBeanAccess() throws Exception
{
OSGiFramework framework = getBootstrapProvider().getFramework();
-
+
try
{
BundleContext sysContext = framework.getSystemBundleContext();
-
+
installBundle(sysContext, "jmx-test.jar", true);
-
+
ObjectName oname = new ObjectName("jboss.osgi:service=mbean-test-service");
FooMBean foo = (FooMBean)MBeanProxy.get(FooMBean.class, oname, getMBeanServer());
assertEquals("hello", foo.echo("hello"));
@@ -129,32 +204,33 @@
framework.stop();
}
}
-
- static class RMIAdaptorFactory implements ObjectFactory
+
+ public static class RMIAdaptorFactory implements ObjectFactory, Serializable
{
- private JMXServiceURL url;
-
- public RMIAdaptorFactory(JMXServiceURL url)
- {
- this.url = url;
- }
+ private static final long serialVersionUID = 2560477127430087074L;
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception
{
+ Reference ref = (Reference)obj;
+ RefAddr refAddr = ref.get(JMXServiceURL.class.getName());
+ String serviceURL = (String)refAddr.getContent();
+ JMXServiceURL url = new JMXServiceURL(serviceURL);
return new RMIAdaptor(url);
}
}
-
- static class RMIAdaptor implements MBeanServerConnection
+
+ public static class RMIAdaptor implements MBeanServerConnection, Serializable
{
+ private static final long serialVersionUID = 6066226353118090215L;
+
private MBeanServerConnection delegate;
public RMIAdaptor(JMXServiceURL url) throws IOException
{
- JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
- delegate = jmxc.getMBeanServerConnection();
+ JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
+ delegate = jmxc.getMBeanServerConnection();
}
-
+
public void addNotificationListener(ObjectName name, NotificationListener listener, NotificationFilter filter, Object handback)
throws InstanceNotFoundException, IOException
{
More information about the jboss-cvs-commits
mailing list