[jboss-osgi-commits] JBoss-OSGI SVN: r87875 - in projects/jboss-osgi/trunk/bundle: blueprint/src/test/java/org/jboss/test/osgi/blueprint/microcontainer/bundle and 4 other directories.
jboss-osgi-commits at lists.jboss.org
jboss-osgi-commits at lists.jboss.org
Mon Apr 27 12:03:47 EDT 2009
Author: thomas.diesler at jboss.com
Date: 2009-04-27 12:03:47 -0400 (Mon, 27 Apr 2009)
New Revision: 87875
Added:
projects/jboss-osgi/trunk/bundle/jmx/src/main/java/org/jboss/osgi/jmx/ObjectNameFactory.java
projects/jboss-osgi/trunk/bundle/microcontainer/src/main/java/org/jboss/osgi/microcontainer/MicrocontainerServiceMBean.java
Modified:
projects/jboss-osgi/trunk/bundle/blueprint/src/test/java/org/jboss/test/osgi/blueprint/microcontainer/MicrocontainerTestCase.java
projects/jboss-osgi/trunk/bundle/blueprint/src/test/java/org/jboss/test/osgi/blueprint/microcontainer/bundle/MicrocontainerTestService.java
projects/jboss-osgi/trunk/bundle/microcontainer/pom.xml
projects/jboss-osgi/trunk/bundle/microcontainer/src/main/java/org/jboss/osgi/microcontainer/MicrocontainerService.java
projects/jboss-osgi/trunk/bundle/microcontainer/src/main/java/org/jboss/osgi/microcontainer/internal/MicrocontainerServiceImpl.java
projects/jboss-osgi/trunk/bundle/microcontainer/src/main/java/org/jboss/osgi/microcontainer/internal/ServiceActivator.java
Log:
Add MicrocontainerServiceMBean
Modified: projects/jboss-osgi/trunk/bundle/blueprint/src/test/java/org/jboss/test/osgi/blueprint/microcontainer/MicrocontainerTestCase.java
===================================================================
--- projects/jboss-osgi/trunk/bundle/blueprint/src/test/java/org/jboss/test/osgi/blueprint/microcontainer/MicrocontainerTestCase.java 2009-04-27 15:22:20 UTC (rev 87874)
+++ projects/jboss-osgi/trunk/bundle/blueprint/src/test/java/org/jboss/test/osgi/blueprint/microcontainer/MicrocontainerTestCase.java 2009-04-27 16:03:47 UTC (rev 87875)
@@ -23,7 +23,14 @@
//$Id$
+import static org.jboss.osgi.microcontainer.MicrocontainerServiceMBean.MBEAN_MICROCONTAINER_SERVICE;
+import static org.jboss.osgi.microcontainer.MicrocontainerService.BEAN_SYSTEM_BUNDLE_CONTEXT;
+
+import java.util.List;
+
+import org.jboss.osgi.microcontainer.MicrocontainerServiceMBean;
import org.jboss.osgi.spi.framework.OSGiFramework;
+import org.jboss.osgi.spi.management.MBeanProxy;
import org.jboss.test.osgi.blueprint.BlueprintTest;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
@@ -47,6 +54,9 @@
Bundle bundle = installBundle(sysContext, "microcontainer-test.jar", true);
assertEquals("Test bundle ACTIVE", Bundle.ACTIVE, bundle.getState());
+ MicrocontainerServiceMBean mbean = MBeanProxy.get(MicrocontainerServiceMBean.class, MBEAN_MICROCONTAINER_SERVICE, getMBeanServer());
+ List<String> registeredBeans = mbean.getRegisteredBeans();
+ assertTrue("MicrocontainerService registered with MC", registeredBeans.contains(BEAN_SYSTEM_BUNDLE_CONTEXT));
}
finally
{
Modified: projects/jboss-osgi/trunk/bundle/blueprint/src/test/java/org/jboss/test/osgi/blueprint/microcontainer/bundle/MicrocontainerTestService.java
===================================================================
--- projects/jboss-osgi/trunk/bundle/blueprint/src/test/java/org/jboss/test/osgi/blueprint/microcontainer/bundle/MicrocontainerTestService.java 2009-04-27 15:22:20 UTC (rev 87874)
+++ projects/jboss-osgi/trunk/bundle/blueprint/src/test/java/org/jboss/test/osgi/blueprint/microcontainer/bundle/MicrocontainerTestService.java 2009-04-27 16:03:47 UTC (rev 87875)
@@ -44,7 +44,7 @@
if (mcService == null)
throw new IllegalStateException("No MicrocontainerService");
- Object sysContextBean = mcService.getRegisteredBean(MicrocontainerService.SERVICE_NAME_SYSTEM_BUNDLE_CONTEXT);
+ Object sysContextBean = mcService.getRegisteredBean(MicrocontainerService.BEAN_SYSTEM_BUNDLE_CONTEXT);
if (sysContextBean == null)
throw new IllegalStateException("No OSGi System Context as MC Bean");
}
Added: projects/jboss-osgi/trunk/bundle/jmx/src/main/java/org/jboss/osgi/jmx/ObjectNameFactory.java
===================================================================
--- projects/jboss-osgi/trunk/bundle/jmx/src/main/java/org/jboss/osgi/jmx/ObjectNameFactory.java (rev 0)
+++ projects/jboss-osgi/trunk/bundle/jmx/src/main/java/org/jboss/osgi/jmx/ObjectNameFactory.java 2009-04-27 16:03:47 UTC (rev 87875)
@@ -0,0 +1,74 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.osgi.jmx;
+
+// $Id$
+
+import java.util.Hashtable;
+
+import javax.management.MalformedObjectNameException;
+import javax.management.ObjectName;
+
+/**
+ * A simple factory for creating safe object names.
+ *
+ * @author Thomas.Diesler at jboss.org
+ * @since 08-May-2006
+ */
+public class ObjectNameFactory
+{
+ public static ObjectName create(String name)
+ {
+ try
+ {
+ return new ObjectName(name);
+ }
+ catch (MalformedObjectNameException e)
+ {
+ throw new Error("Invalid ObjectName: " + name + "; " + e);
+ }
+ }
+
+ public static ObjectName create(String domain, String key, String value)
+ {
+ try
+ {
+ return new ObjectName(domain, key, value);
+ }
+ catch (MalformedObjectNameException e)
+ {
+ throw new Error("Invalid ObjectName: " + domain + "," + key + "," + value + "; " + e);
+ }
+ }
+
+ public static ObjectName create(String domain, Hashtable<String, String> table)
+ {
+ try
+ {
+ return new ObjectName(domain, table);
+ }
+ catch (MalformedObjectNameException e)
+ {
+ throw new Error("Invalid ObjectName: " + domain + "," + table + "; " + e);
+ }
+ }
+}
Property changes on: projects/jboss-osgi/trunk/bundle/jmx/src/main/java/org/jboss/osgi/jmx/ObjectNameFactory.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Modified: projects/jboss-osgi/trunk/bundle/microcontainer/pom.xml
===================================================================
--- projects/jboss-osgi/trunk/bundle/microcontainer/pom.xml 2009-04-27 15:22:20 UTC (rev 87874)
+++ projects/jboss-osgi/trunk/bundle/microcontainer/pom.xml 2009-04-27 16:03:47 UTC (rev 87875)
@@ -28,6 +28,11 @@
<version>${version}</version>
</dependency>
<dependency>
+ <groupId>org.jboss.osgi</groupId>
+ <artifactId>jboss-osgi-jmx</artifactId>
+ <version>${version}</version>
+ </dependency>
+ <dependency>
<groupId>org.jboss.deployers</groupId>
<artifactId>jboss-deployers-client-spi</artifactId>
</dependency>
@@ -96,6 +101,7 @@
org.jboss.metadata.*;resolution:=optional,
org.jboss.metatype.*;resolution:=optional,
org.jboss.osgi.common.log,
+ org.jboss.osgi.jmx,
org.jboss.reflect.*;resolution:=optional,
org.jboss.test;resolution:=optional,
org.jboss.util*;resolution:=optional,
@@ -103,6 +109,7 @@
org.jboss.xb.*,
org.osgi.framework;version=1.4,
org.osgi.service.log,
+ org.osgi.util.tracker,
org.w3c.dom*,
org.xml.sax*,
sun.io;resolution:=optional
Modified: projects/jboss-osgi/trunk/bundle/microcontainer/src/main/java/org/jboss/osgi/microcontainer/MicrocontainerService.java
===================================================================
--- projects/jboss-osgi/trunk/bundle/microcontainer/src/main/java/org/jboss/osgi/microcontainer/MicrocontainerService.java 2009-04-27 15:22:20 UTC (rev 87874)
+++ projects/jboss-osgi/trunk/bundle/microcontainer/src/main/java/org/jboss/osgi/microcontainer/MicrocontainerService.java 2009-04-27 16:03:47 UTC (rev 87875)
@@ -35,8 +35,10 @@
*/
public interface MicrocontainerService
{
- /** The bean name under which the system bundle context is registered */
- String SERVICE_NAME_SYSTEM_BUNDLE_CONTEXT = "jboss.osgi:service=SystemBundleContext";
+ /**
+ * The bean name under which the system bundle context is registered: 'jboss.osgi:service=SystemBundleContext'
+ */
+ String BEAN_SYSTEM_BUNDLE_CONTEXT = "jboss.osgi:service=SystemBundleContext";
/**
* Get the Microcontainer Kernel
Added: projects/jboss-osgi/trunk/bundle/microcontainer/src/main/java/org/jboss/osgi/microcontainer/MicrocontainerServiceMBean.java
===================================================================
--- projects/jboss-osgi/trunk/bundle/microcontainer/src/main/java/org/jboss/osgi/microcontainer/MicrocontainerServiceMBean.java (rev 0)
+++ projects/jboss-osgi/trunk/bundle/microcontainer/src/main/java/org/jboss/osgi/microcontainer/MicrocontainerServiceMBean.java 2009-04-27 16:03:47 UTC (rev 87875)
@@ -0,0 +1,49 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2005, JBoss Inc., and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.osgi.microcontainer;
+
+//$Id$
+
+import java.util.List;
+
+import javax.management.ObjectName;
+
+import org.jboss.osgi.jmx.ObjectNameFactory;
+
+/**
+ * An OSGi Service the gives access to the Kernel and MBeanServer.
+ *
+ * @author thomas.diesler at jboss.com
+ * @since 23-Jan-2009
+ */
+public interface MicrocontainerServiceMBean
+{
+ /**
+ * The object name under which the MicrocontainerService is registered: 'jboss.osgi:service=MicrocontainerService'
+ */
+ ObjectName MBEAN_MICROCONTAINER_SERVICE = ObjectNameFactory.create("jboss.osgi:service=MicrocontainerService");
+
+ /**
+ * Get the list of registered beans.
+ */
+ List<String> getRegisteredBeans();
+}
\ No newline at end of file
Property changes on: projects/jboss-osgi/trunk/bundle/microcontainer/src/main/java/org/jboss/osgi/microcontainer/MicrocontainerServiceMBean.java
___________________________________________________________________
Name: svn:keywords
+ Id Revision
Name: svn:eol-style
+ LF
Modified: projects/jboss-osgi/trunk/bundle/microcontainer/src/main/java/org/jboss/osgi/microcontainer/internal/MicrocontainerServiceImpl.java
===================================================================
--- projects/jboss-osgi/trunk/bundle/microcontainer/src/main/java/org/jboss/osgi/microcontainer/internal/MicrocontainerServiceImpl.java 2009-04-27 15:22:20 UTC (rev 87874)
+++ projects/jboss-osgi/trunk/bundle/microcontainer/src/main/java/org/jboss/osgi/microcontainer/internal/MicrocontainerServiceImpl.java 2009-04-27 16:03:47 UTC (rev 87875)
@@ -35,6 +35,7 @@
import org.jboss.dependency.spi.ControllerState;
import org.jboss.kernel.Kernel;
import org.jboss.osgi.microcontainer.MicrocontainerService;
+import org.jboss.osgi.microcontainer.MicrocontainerServiceMBean;
import org.osgi.framework.BundleContext;
/**
@@ -43,7 +44,7 @@
* @author thomas.diesler at jboss.com
* @since 23-Jan-2009
*/
-public class MicrocontainerServiceImpl implements MicrocontainerService
+public class MicrocontainerServiceImpl implements MicrocontainerService, MicrocontainerServiceMBean
{
private Kernel kernel;
@@ -56,7 +57,7 @@
try
{
AbstractControllerContextActions actions = new AbstractControllerContextActions(new HashMap<ControllerState, ControllerContextAction>());
- ControllerContext ctrlContext = new AbstractControllerContext(SERVICE_NAME_SYSTEM_BUNDLE_CONTEXT, actions, null, context);
+ ControllerContext ctrlContext = new AbstractControllerContext(BEAN_SYSTEM_BUNDLE_CONTEXT, actions, null, context);
kernel.getController().install(ctrlContext);
}
catch (RuntimeException rte)
@@ -65,7 +66,7 @@
}
catch (Throwable ex)
{
- throw new IllegalStateException("Cannot register: " + SERVICE_NAME_SYSTEM_BUNDLE_CONTEXT, ex);
+ throw new IllegalStateException("Cannot register: " + BEAN_SYSTEM_BUNDLE_CONTEXT, ex);
}
}
Modified: projects/jboss-osgi/trunk/bundle/microcontainer/src/main/java/org/jboss/osgi/microcontainer/internal/ServiceActivator.java
===================================================================
--- projects/jboss-osgi/trunk/bundle/microcontainer/src/main/java/org/jboss/osgi/microcontainer/internal/ServiceActivator.java 2009-04-27 15:22:20 UTC (rev 87874)
+++ projects/jboss-osgi/trunk/bundle/microcontainer/src/main/java/org/jboss/osgi/microcontainer/internal/ServiceActivator.java 2009-04-27 16:03:47 UTC (rev 87875)
@@ -23,12 +23,18 @@
//$Id$
+import javax.management.MBeanServer;
+import javax.management.StandardMBean;
+
import org.jboss.osgi.common.log.LogServiceTracker;
import org.jboss.osgi.microcontainer.MicrocontainerService;
+import org.jboss.osgi.microcontainer.MicrocontainerServiceMBean;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.log.LogService;
+import org.osgi.util.tracker.ServiceTracker;
/**
* A BundleActivator that registers the Microcontainer Service
@@ -39,20 +45,32 @@
public class ServiceActivator implements BundleActivator
{
private ServiceRegistration registration;
+ private MBeanServerTracker mbeanServerTracker;
private LogService log;
-
+
public void start(BundleContext context)
{
log = new LogServiceTracker(context);
+ // Register the MicrocontainerService as OSGi service
+ MicrocontainerService mcService = registerOSGiService(context);
+
+ // Track the MBeanServer to register the MicrocontainerServiceMBean
+ mbeanServerTracker = new MBeanServerTracker(context, (MicrocontainerServiceMBean)mcService);
+ mbeanServerTracker.open();
+ }
+
+ private MicrocontainerService registerOSGiService(BundleContext context)
+ {
ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader();
try
{
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
- MicrocontainerServiceImpl mcService = new MicrocontainerServiceImpl(context);
-
+ MicrocontainerService service = new MicrocontainerServiceImpl(context);
+
log.log(LogService.LOG_DEBUG, "Register MicrocontainerService");
- registration = context.registerService(MicrocontainerService.class.getName(), mcService, null);
+ registration = context.registerService(MicrocontainerService.class.getName(), service, null);
+ return service;
}
finally
{
@@ -60,13 +78,65 @@
}
}
+ private void unregisterMBean(BundleContext context)
+ {
+ MBeanServer server = (MBeanServer)mbeanServerTracker.getService();
+ if (server != null)
+ {
+ try
+ {
+ log.log(LogService.LOG_DEBUG, "Unregister MicrocontainerServiceMBean");
+ server.unregisterMBean(MicrocontainerServiceMBean.MBEAN_MICROCONTAINER_SERVICE);
+ }
+ catch (Exception ex)
+ {
+ log.log(LogService.LOG_ERROR, "Cannot register MicrocontainerServiceMBean", ex);
+ }
+ }
+ }
+
public void stop(BundleContext context)
{
if (registration != null)
{
log.log(LogService.LOG_DEBUG, "Unregister MicrocontainerService");
registration.unregister();
+ unregisterMBean(context);
registration = null;
}
}
+
+ class MBeanServerTracker extends ServiceTracker
+ {
+ private MicrocontainerServiceMBean mcService;
+
+ public MBeanServerTracker(BundleContext context, MicrocontainerServiceMBean mcService)
+ {
+ super(context, MBeanServer.class.getName(), null);
+ this.mcService = mcService;
+ }
+
+ public Object addingService(ServiceReference reference)
+ {
+ MBeanServer server = (MBeanServer)super.addingService(reference);
+ try
+ {
+ log.log(LogService.LOG_DEBUG, "Register MicrocontainerServiceMBean");
+ StandardMBean mbean = new StandardMBean(mcService, MicrocontainerServiceMBean.class);
+ server.registerMBean(mbean, MicrocontainerServiceMBean.MBEAN_MICROCONTAINER_SERVICE);
+ }
+ catch (Exception ex)
+ {
+ throw new IllegalStateException("Cannot register MicrocontainerServiceMBean", ex);
+ }
+ return server;
+ }
+
+ @Override
+ public void removedService(ServiceReference reference, Object service)
+ {
+ unregisterMBean(context);
+ super.removedService(reference, service);
+ }
+ }
}
\ No newline at end of file
More information about the jboss-osgi-commits
mailing list