aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r1341248 - in /aries/branches/blueprint-0.3.2-fixes/jmx-core/src: main/java/org/apache/aries/jmx/ main/java/org/apache/aries/jmx/agent/ test/java/org/apache/aries/jmx/
Date Mon, 21 May 2012 22:53:52 GMT
Author: gnodet
Date: Mon May 21 22:53:52 2012
New Revision: 1341248

URL: http://svn.apache.org/viewvc?rev=1341248&view=rev
Log:
[ARIES-663] Fix problems with JMX mbeans registration / unregistration

Modified:
    aries/branches/blueprint-0.3.2-fixes/jmx-core/src/main/java/org/apache/aries/jmx/AbstractCompendiumHandler.java
    aries/branches/blueprint-0.3.2-fixes/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgent.java
    aries/branches/blueprint-0.3.2-fixes/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentContext.java
    aries/branches/blueprint-0.3.2-fixes/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentImpl.java
    aries/branches/blueprint-0.3.2-fixes/jmx-core/src/test/java/org/apache/aries/jmx/CompendiumHandlerTest.java

Modified: aries/branches/blueprint-0.3.2-fixes/jmx-core/src/main/java/org/apache/aries/jmx/AbstractCompendiumHandler.java
URL: http://svn.apache.org/viewvc/aries/branches/blueprint-0.3.2-fixes/jmx-core/src/main/java/org/apache/aries/jmx/AbstractCompendiumHandler.java?rev=1341248&r1=1341247&r2=1341248&view=diff
==============================================================================
--- aries/branches/blueprint-0.3.2-fixes/jmx-core/src/main/java/org/apache/aries/jmx/AbstractCompendiumHandler.java
(original)
+++ aries/branches/blueprint-0.3.2-fixes/jmx-core/src/main/java/org/apache/aries/jmx/AbstractCompendiumHandler.java
Mon May 21 22:53:52 2012
@@ -116,7 +116,7 @@ public abstract class AbstractCompendium
             ExecutorService executor = agentContext.getRegistrationExecutor();
             executor.submit(new Runnable() {
                 public void run() {
-                    agentContext.unregisterMBean(getName());
+                    agentContext.unregisterMBean(AbstractCompendiumHandler.this);
                 }
             });
             trackedId = null;

Modified: aries/branches/blueprint-0.3.2-fixes/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgent.java
URL: http://svn.apache.org/viewvc/aries/branches/blueprint-0.3.2-fixes/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgent.java?rev=1341248&r1=1341247&r2=1341248&view=diff
==============================================================================
--- aries/branches/blueprint-0.3.2-fixes/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgent.java
(original)
+++ aries/branches/blueprint-0.3.2-fixes/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgent.java
Mon May 21 22:53:52 2012
@@ -57,7 +57,14 @@ public interface JMXAgent {
 
     /**
      * Unregisters MBean with all available MBeanServers.
+     * @param mBeanHandler handler which contains MBean info.
+     */
+    void unregisterMBean(final MBeanHandler mBeanHandler);
+
+    /**
+     * Unregisters MBean with all available MBeanServers.
      * @param name of MBean to be unregistered.
+     * @deprecated
      */
     void unregisterMBean(final String name);
 

Modified: aries/branches/blueprint-0.3.2-fixes/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentContext.java
URL: http://svn.apache.org/viewvc/aries/branches/blueprint-0.3.2-fixes/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentContext.java?rev=1341248&r1=1341247&r2=1341248&view=diff
==============================================================================
--- aries/branches/blueprint-0.3.2-fixes/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentContext.java
(original)
+++ aries/branches/blueprint-0.3.2-fixes/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentContext.java
Mon May 21 22:53:52 2012
@@ -68,7 +68,7 @@ public class JMXAgentContext {
 
     /**
      * Delegates invocation to JMX agent.
-     * @see org.apache.aries.jmx.agent.JMXAgentl#registerMBean(MBeanHandler)
+     * @see org.apache.aries.jmx.agent.JMXAgent#registerMBean(MBeanHandler)
      */
     public void registerMBean(final MBeanHandler mbeanData) {
         agent.registerMBean(mbeanData);
@@ -76,7 +76,16 @@ public class JMXAgentContext {
 
     /**
      * Delegates invocation to JMX agent.
+     * @see org.apache.aries.jmx.agent.JMXAgent#unregisterMBean(MBeanHandler)
+     */
+    public void unregisterMBean(final MBeanHandler mBeanHandler) {
+        agent.unregisterMBean(mBeanHandler);
+    }
+
+    /**
+     * Delegates invocation to JMX agent.
      * @see org.apache.aries.jmx.agent.JMXAgent#unregisterMBean(String)
+     * @deprecated
      */
     public void unregisterMBean(final String name) {
         agent.unregisterMBean(name);

Modified: aries/branches/blueprint-0.3.2-fixes/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentImpl.java
URL: http://svn.apache.org/viewvc/aries/branches/blueprint-0.3.2-fixes/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentImpl.java?rev=1341248&r1=1341247&r2=1341248&view=diff
==============================================================================
--- aries/branches/blueprint-0.3.2-fixes/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentImpl.java
(original)
+++ aries/branches/blueprint-0.3.2-fixes/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentImpl.java
Mon May 21 22:53:52 2012
@@ -16,11 +16,10 @@
  */
 package org.apache.aries.jmx.agent;
 
-import java.util.HashSet;
-import java.util.Set;
+import java.util.HashMap;
+import java.util.Map;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
-
 import javax.management.InstanceAlreadyExistsException;
 import javax.management.InstanceNotFoundException;
 import javax.management.MBeanRegistrationException;
@@ -61,7 +60,7 @@ public class JMXAgentImpl implements JMX
     /**
      * {@link MBeanHandler} store.
      */
-    private Set<MBeanHandler> mbeansHandlers;
+    private Map<MBeanHandler, Boolean> mbeansHandlers;
     private JMXAgentContext agentContext;
     private Logger logger;
 
@@ -77,40 +76,40 @@ public class JMXAgentImpl implements JMX
      */
     public JMXAgentImpl(Logger logger) {
         this.logger = logger;
-        this.mbeansHandlers = new HashSet<MBeanHandler>();
+        this.mbeansHandlers = new HashMap<MBeanHandler, Boolean>();
         this.registrationExecutor = Executors.newSingleThreadExecutor(new JMXThreadFactory("JMX
OSGi Agent"));
     }
 
     /**
      * @see org.apache.aries.jmx.agent.JMXAgent#start()
      */
-    public void start() {
+    public synchronized void start() {
         logger.log(LogService.LOG_INFO, "Starting JMX OSGi agent");
         BundleContext bc = agentContext.getBundleContext();
         MBeanHandler frameworkHandler = new FrameworkMBeanHandler(bc, logger);
+        mbeansHandlers.put(frameworkHandler, Boolean.FALSE);
         frameworkHandler.open();
-        mbeansHandlers.add(frameworkHandler);
         MBeanHandler bundleStateHandler = new BundleStateMBeanHandler(bc, logger);
+        mbeansHandlers.put(bundleStateHandler, Boolean.FALSE);
         bundleStateHandler.open();
-        mbeansHandlers.add(bundleStateHandler);
         MBeanHandler serviceStateHandler = new ServiceStateMBeanHandler(bc, logger);
+        mbeansHandlers.put(serviceStateHandler, Boolean.FALSE);
         serviceStateHandler.open();
-        mbeansHandlers.add(serviceStateHandler);
         MBeanHandler packageStateHandler = new PackageStateMBeanHandler(bc, logger);
+        mbeansHandlers.put(packageStateHandler, Boolean.FALSE);
         packageStateHandler.open();
-        mbeansHandlers.add(packageStateHandler);
         MBeanHandler permissionAdminHandler = new PermissionAdminMBeanHandler(agentContext);
+        mbeansHandlers.put(permissionAdminHandler, Boolean.FALSE);
         permissionAdminHandler.open();
-        mbeansHandlers.add(permissionAdminHandler);
         MBeanHandler userAdminHandler = new UserAdminMBeanHandler(agentContext);
+        mbeansHandlers.put(userAdminHandler, Boolean.FALSE);
         userAdminHandler.open();
-        mbeansHandlers.add(userAdminHandler);
         MBeanHandler configAdminHandler = new ConfigurationAdminMBeanHandler(agentContext);
+        mbeansHandlers.put(configAdminHandler, Boolean.FALSE);
         configAdminHandler.open();
-        mbeansHandlers.add(configAdminHandler);
         MBeanHandler provServiceHandler = new ProvisioningServiceMBeanHandler(agentContext);
+        mbeansHandlers.put(provServiceHandler, Boolean.FALSE);
         provServiceHandler.open();
-        mbeansHandlers.add(provServiceHandler);
         mbeanServiceTracker = new MBeanServiceTracker(agentContext);
         mbeanServiceTracker.open();
     }
@@ -118,25 +117,28 @@ public class JMXAgentImpl implements JMX
     /**
      * @see org.apache.aries.jmx.agent.JMXAgent#registerMBeans(javax.management.MBeanServer)
      */
-    public void registerMBeans(final MBeanServer server) {
-        for (MBeanHandler mbeanHandler : mbeansHandlers) {
-            String name = mbeanHandler.getName();
-            StandardMBean mbean = mbeanHandler.getMbean();
-            if (mbean != null) {
-                try {
-                    logger.log(LogService.LOG_INFO, "Registering " + mbean.getMBeanInterface().getName()
-                            + " to MBeanServer " + server + " with name " + name);
-                    server.registerMBean(mbean, new ObjectName(name));
-                } catch (InstanceAlreadyExistsException e) {
-                    logger.log(LogService.LOG_ERROR, "MBean is already registered", e);
-                } catch (MBeanRegistrationException e) {
-                    logger.log(LogService.LOG_ERROR, "Can't register MBean", e);
-                } catch (NotCompliantMBeanException e) {
-                    logger.log(LogService.LOG_ERROR, "MBean is not compliant MBean", e);
-                } catch (MalformedObjectNameException e) {
-                    logger.log(LogService.LOG_ERROR, "Try to register with no valid objectname",
e);
-                } catch (NullPointerException e) {
-                    logger.log(LogService.LOG_ERROR, "Name of objectname can't be null",
e);
+    public synchronized void registerMBeans(final MBeanServer server) {
+        for (MBeanHandler mbeanHandler : mbeansHandlers.keySet()) {
+            if (mbeansHandlers.get(mbeanHandler) == Boolean.FALSE) {
+                String name = mbeanHandler.getName();
+                StandardMBean mbean = mbeanHandler.getMbean();
+                if (mbean != null) {
+                    try {
+                        logger.log(LogService.LOG_INFO, "Registering " + mbean.getMBeanInterface().getName()
+                                + " to MBeanServer " + server + " with name " + name);
+                        server.registerMBean(mbean, new ObjectName(name));
+                        mbeansHandlers.put(mbeanHandler, Boolean.TRUE);
+                    } catch (InstanceAlreadyExistsException e) {
+                        logger.log(LogService.LOG_ERROR, "MBean is already registered", e);
+                    } catch (MBeanRegistrationException e) {
+                        logger.log(LogService.LOG_ERROR, "Can't register MBean", e);
+                    } catch (NotCompliantMBeanException e) {
+                        logger.log(LogService.LOG_ERROR, "MBean is not compliant MBean",
e);
+                    } catch (MalformedObjectNameException e) {
+                        logger.log(LogService.LOG_ERROR, "Try to register with no valid objectname",
e);
+                    } catch (NullPointerException e) {
+                        logger.log(LogService.LOG_ERROR, "Name of objectname can't be null",
e);
+                    }
                 }
             }
         }
@@ -146,35 +148,38 @@ public class JMXAgentImpl implements JMX
     /**
      * @see org.apache.aries.jmx.agent.JMXAgent#unregisterMBeans(javax.management.MBeanServer)
      */
-    public void unregisterMBeans(final MBeanServer server) {
-        for (MBeanHandler mBeanHandler : mbeansHandlers) {
-            try
-            {
-               String name = mBeanHandler.getName();
-               StandardMBean mbean = mBeanHandler.getMbean();
-               if (mbean != null) {
-                   logger.log(LogService.LOG_INFO, "Unregistering " + mbean.getMBeanInterface().getName()
-                         + " to MBeanServer " + server + " with name " + name);
-                   server.unregisterMBean(new ObjectName(name));
-               }
-            } catch (MBeanRegistrationException e) {
-               logger.log(LogService.LOG_ERROR, "Can't unregister MBean", e);
-            } catch (InstanceNotFoundException e) {
-               logger.log(LogService.LOG_ERROR, "Mbena doesn't exist in the repository",
e);
-            } catch (MalformedObjectNameException e) {
-               logger.log(LogService.LOG_ERROR, "Try to unregister with no valid objectname",
e);
-            } catch (NullPointerException e) {
-               logger.log(LogService.LOG_ERROR, "Name of objectname can't be null ", e);
-            } catch (Exception e) {
-               logger.log(LogService.LOG_ERROR, "Cannot unregister MBean: " + mBeanHandler,
e);
-            } 
+    public synchronized void unregisterMBeans(final MBeanServer server) {
+        for (MBeanHandler mBeanHandler : mbeansHandlers.keySet()) {
+            if (mbeansHandlers.get(mBeanHandler) == Boolean.TRUE) {
+                try
+                {
+                   String name = mBeanHandler.getName();
+                   StandardMBean mbean = mBeanHandler.getMbean();
+                   if (mbean != null) {
+                       logger.log(LogService.LOG_INFO, "Unregistering " + mbean.getMBeanInterface().getName()
+                             + " to MBeanServer " + server + " with name " + name);
+                       server.unregisterMBean(new ObjectName(name));
+                       mbeansHandlers.put(mBeanHandler, Boolean.FALSE);
+                   }
+                } catch (MBeanRegistrationException e) {
+                   logger.log(LogService.LOG_ERROR, "Can't unregister MBean", e);
+                } catch (InstanceNotFoundException e) {
+                   logger.log(LogService.LOG_ERROR, "MBean doesn't exist in the repository",
e);
+                } catch (MalformedObjectNameException e) {
+                   logger.log(LogService.LOG_ERROR, "Try to unregister with no valid objectname",
e);
+                } catch (NullPointerException e) {
+                   logger.log(LogService.LOG_ERROR, "Name of objectname can't be null ",
e);
+                } catch (Exception e) {
+                   logger.log(LogService.LOG_ERROR, "Cannot unregister MBean: " + mBeanHandler,
e);
+                }
+            }
         }
     }
 
     /**
      * @see org.apache.aries.jmx.agent.JMXAgent#registerMBean(org.apache.aries.jmx.MBeanHandler)
      */
-    public void registerMBean(final MBeanHandler mBeanHandler) {
+    public synchronized void registerMBean(final MBeanHandler mBeanHandler) {
         Object[] servers = getMBeanServers();
         if (servers == null) {
             logger.log(LogService.LOG_WARNING, "There are no MBean servers registred, can't
register MBeans");
@@ -188,7 +193,7 @@ public class JMXAgentImpl implements JMX
                 logger.log(LogService.LOG_INFO, "Registering " + mbean.getMBeanInterface().getName()
                         + " to MBeanServer " + server + " with name " + name);
                 ((MBeanServer) server).registerMBean(mbean, new ObjectName(name));
-
+                mbeansHandlers.put(mBeanHandler, Boolean.TRUE);
             } catch (InstanceAlreadyExistsException e) {
                 logger.log(LogService.LOG_ERROR, "MBean is already registered", e);
             } catch (MBeanRegistrationException e) {
@@ -204,16 +209,41 @@ public class JMXAgentImpl implements JMX
                 return;
             }
         }
+    }
+
+    /**
+     * @see org.apache.aries.jmx.agent.JMXAgent#unregisterMBean(org.apache.aries.jmx.MBeanHandler)
+     */
+    public synchronized void unregisterMBean(final MBeanHandler mBeanHandler) {
+        Object[] servers = getMBeanServers();
+        for (Object server : servers) {
+            String name = mBeanHandler.getName();
+            try {
+                logger.log(LogService.LOG_INFO, "Unregistering mbean " + " to MBeanServer
" + server + " with name "
+                        + name);
+                ((MBeanServer) server).unregisterMBean(new ObjectName(name));
+                mbeansHandlers.put(mBeanHandler, Boolean.FALSE);
+            } catch (MBeanRegistrationException e) {
+                logger.log(LogService.LOG_ERROR, "Can't register MBean", e);
+            } catch (InstanceNotFoundException e) {
+                logger.log(LogService.LOG_ERROR, "MBean doesn't exist in the repository",
e);
+            } catch (MalformedObjectNameException e) {
+                logger.log(LogService.LOG_ERROR, "Try to register with no valid objectname,
Stopping registration", e);
+                return;
+            } catch (NullPointerException e) {
+                logger.log(LogService.LOG_ERROR, "Name of objectname can't be null, Stopping
registration", e);
+                return;
+            }
 
+        }
     }
 
     /**
      * @see org.apache.aries.jmx.agent.JMXAgent#unregisterMBean(java.lang.String)
      */
-    public void unregisterMBean(final String name) {
+    public synchronized void unregisterMBean(final String name) {
         Object[] servers = getMBeanServers();
         for (Object server : servers) {
-
             try {
                 logger.log(LogService.LOG_INFO, "Unregistering mbean " + " to MBeanServer
" + server + " with name "
                         + name);
@@ -221,7 +251,7 @@ public class JMXAgentImpl implements JMX
             } catch (MBeanRegistrationException e) {
                 logger.log(LogService.LOG_ERROR, "Can't register MBean", e);
             } catch (InstanceNotFoundException e) {
-                logger.log(LogService.LOG_ERROR, "Mbena doesn't exist in the repository",
e);
+                logger.log(LogService.LOG_ERROR, "MBean doesn't exist in the repository",
e);
             } catch (MalformedObjectNameException e) {
                 logger.log(LogService.LOG_ERROR, "Try to register with no valid objectname,
Stopping registration", e);
                 return;
@@ -229,17 +259,16 @@ public class JMXAgentImpl implements JMX
                 logger.log(LogService.LOG_ERROR, "Name of objectname can't be null, Stopping
registration", e);
                 return;
             }
-
         }
     }
 
     /**
      * @see org.apache.aries.jmx.agent.JMXAgent#stop()
      */
-    public void stop() {
+    public synchronized void stop() {
         logger.log(LogService.LOG_INFO, "Stopping JMX OSGi agent");
         mbeanServiceTracker.close();
-        for (MBeanHandler mBeanHandler : mbeansHandlers) {
+        for (MBeanHandler mBeanHandler : mbeansHandlers.keySet()) {
             mBeanHandler.close();
         }
         if (registrationExecutor != null && !registrationExecutor.isShutdown()) {

Modified: aries/branches/blueprint-0.3.2-fixes/jmx-core/src/test/java/org/apache/aries/jmx/CompendiumHandlerTest.java
URL: http://svn.apache.org/viewvc/aries/branches/blueprint-0.3.2-fixes/jmx-core/src/test/java/org/apache/aries/jmx/CompendiumHandlerTest.java?rev=1341248&r1=1341247&r2=1341248&view=diff
==============================================================================
--- aries/branches/blueprint-0.3.2-fixes/jmx-core/src/test/java/org/apache/aries/jmx/CompendiumHandlerTest.java
(original)
+++ aries/branches/blueprint-0.3.2-fixes/jmx-core/src/test/java/org/apache/aries/jmx/CompendiumHandlerTest.java
Mon May 21 22:53:52 2012
@@ -16,16 +16,9 @@
  */
 package org.apache.aries.jmx;
 
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.spy;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
-
 import javax.management.StandardMBean;
 
 import org.apache.aries.jmx.agent.JMXAgentContext;
@@ -35,6 +28,8 @@ import org.osgi.framework.BundleContext;
 import org.osgi.framework.Filter;
 import org.osgi.framework.ServiceReference;
 
+import static org.mockito.Mockito.*;
+
 public class CompendiumHandlerTest {
 
     protected AbstractCompendiumHandler target;
@@ -106,7 +101,7 @@ public class CompendiumHandlerTest {
         //service unget
         verify(bundleContext).ungetService(reference);
         //unregister is invoked on context
-        verify(agentContext).unregisterMBean(name);
+        verify(agentContext).unregisterMBean(target);
         
     }
 



Mime
View raw message