The MBeanServerTestCase tracks the MBeanServer service and registers a pojo with JMX. It then verifies the JMX access.
public class MBeanActivator implements BundleActivator
{
public void start(BundleContext context)
{
ServiceTracker tracker = new ServiceTracker(context, MBeanServer.class.getName(), null)
{
public Object addingService(ServiceReference reference)
{
MBeanServer mbeanServer = (MBeanServer)super.addingService(reference);
registerMBean(mbeanServer);
return mbeanServer;
}
@Override
public void removedService(ServiceReference reference, Object service)
{
unregisterMBean((MBeanServer)service);
super.removedService(reference, service);
}
};
tracker.open();
}
public void stop(BundleContext context)
{
ServiceReference sref = context.getServiceReference(MBeanServer.class.getName());
if (sref != null)
{
MBeanServer mbeanServer = (MBeanServer)context.getService(sref);
unregisterMBean(mbeanServer);
}
}
...
}public void testMBeanAccess() throws Exception
{
// Provide MBeanServer support
MBeanServer server = ManagementSupport.provideMBeanServer(context, bundle);
// Start the test bundle
bundle.start();
ObjectName oname = ObjectName.getInstance(FooMBean.MBEAN_NAME);
FooMBean foo = ManagementSupport.getMBeanProxy(server, oname, FooMBean.class);
assertEquals("hello", foo.echo("hello"));
}The BundleStateTestCase uses JMX to control the bundle state through the BundleStateMBean.
public void testBundleStateMBean() throws Exception
{
MBeanServer server = ManagementSupport.provideMBeanServer(context, bundle);
ObjectName oname = ObjectName.getInstance(BundleStateMBean.OBJECTNAME);
BundleStateMBean bundleState = ManagementSupport.getMBeanProxy(server, oname, BundleStateMBean.class);
assertNotNull("BundleStateMBean not null", bundleState);
TabularData bundleData = bundleState.listBundles();
assertNotNull("TabularData not null", bundleData);
assertFalse("TabularData not empty", bundleData.isEmpty());
}This test uses the OSGi Repository functionality to provision the runtime with the required support functionality like this
ManagementSupport.provideMBeanServer(context, bundle);
To enable OSGi JMX support in AS7 you would configure these capabilities
<capability name="org.apache.aries:org.apache.aries.util:0.4"/> <capability name="org.apache.aries.jmx:org.apache.aries.jmx:0.3"/>
The MBeanServer service is provided by default in AS7.