aries-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gno...@apache.org
Subject svn commit: r1583139 - in /aries/trunk/jmx/jmx-core: ./ src/main/java/org/apache/aries/jmx/ src/main/java/org/apache/aries/jmx/agent/ src/main/java/org/apache/aries/jmx/framework/ src/main/java/org/apache/aries/jmx/framework/wiring/ src/test/java/org/a...
Date Sun, 30 Mar 2014 13:50:06 GMT
Author: gnodet
Date: Sun Mar 30 13:50:06 2014
New Revision: 1583139

URL: http://svn.apache.org/r1583139
Log:
[ARIES-1169] Concurrency issues in jmx-core

Modified:
    aries/trunk/jmx/jmx-core/pom.xml
    aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/AbstractCompendiumHandler.java
    aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/Activator.java
    aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/MBeanServiceTracker.java
    aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgent.java
    aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentContext.java
    aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentImpl.java
    aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/BundleStateMBeanHandler.java
    aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/FrameworkMBeanHandler.java
    aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/PackageStateMBeanHandler.java
    aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/ServiceStateMBeanHandler.java
    aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/wiring/BundleWiringStateMBeanHandler.java
    aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/CompendiumHandlerTest.java
    aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/BundleStateMBeanHandlerTest.java
    aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/ServiceStateMBeanHandlerTest.java

Modified: aries/trunk/jmx/jmx-core/pom.xml
URL: http://svn.apache.org/viewvc/aries/trunk/jmx/jmx-core/pom.xml?rev=1583139&r1=1583138&r2=1583139&view=diff
==============================================================================
--- aries/trunk/jmx/jmx-core/pom.xml (original)
+++ aries/trunk/jmx/jmx-core/pom.xml Sun Mar 30 13:50:06 2014
@@ -81,12 +81,13 @@
         <dependency>
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.core</artifactId>
-            <version>4.3.0</version>
+            <version>4.3.1</version>
             <scope>provided</scope>
         </dependency>
         <dependency>
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.compendium</artifactId>
+            <version>4.3.1</version>
             <scope>provided</scope>
         </dependency>
         <dependency>
@@ -111,6 +112,11 @@
     <build>
         <plugins>
             <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-compiler-plugin</artifactId>
+                <version>3.1</version>
+            </plugin>
+            <plugin>
                 <groupId>org.apache.aries.versioning</groupId>
                 <artifactId>org.apache.aries.versioning.plugin</artifactId>
                 <version>0.1.0</version>

Modified: aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/AbstractCompendiumHandler.java
URL: http://svn.apache.org/viewvc/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/AbstractCompendiumHandler.java?rev=1583139&r1=1583138&r2=1583139&view=diff
==============================================================================
--- aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/AbstractCompendiumHandler.java
(original)
+++ aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/AbstractCompendiumHandler.java
Sun Mar 30 13:50:06 2014
@@ -17,6 +17,8 @@
 package org.apache.aries.jmx;
 
 import java.util.concurrent.ExecutorService;
+import java.util.concurrent.atomic.AtomicLong;
+
 import javax.management.StandardMBean;
 
 import org.apache.aries.jmx.agent.JMXAgentContext;
@@ -43,7 +45,7 @@ public abstract class AbstractCompendium
 
     protected final JMXAgentContext agentContext;
     protected StandardMBean mbean;
-    protected Long trackedId;
+    protected final AtomicLong trackedId = new AtomicLong();
     
     /**
      * 
@@ -73,25 +75,19 @@ public abstract class AbstractCompendium
     public Object addingService(ServiceReference reference) {
         Logger logger = agentContext.getLogger();
         Object trackedService = null;
-        Long serviceId = (Long) reference.getProperty(Constants.SERVICE_ID);
+        long serviceId = (Long) reference.getProperty(Constants.SERVICE_ID);
         //API stipulates versions for compendium services with static ObjectName
         //This shouldn't happen but added as a consistency check
-        if (trackedId != null) {
-            String serviceDescription = getServiceDescription(reference);
-            logger.log(LogService.LOG_WARNING, "Detected secondary ServiceReference for ["
+ serviceDescription
-                    + "] with " + Constants.SERVICE_ID + " [" + serviceId + "] Only 1 instance
will be JMX managed");
-        } else {
+        if (trackedId.compareAndSet(0, serviceId)) {
             logger.log(LogService.LOG_INFO, "Registering MBean with ObjectName [" + getName()
+ "] for service with "
                     + Constants.SERVICE_ID + " [" + serviceId + "]");
             trackedService = context.getService(reference);
             mbean = constructInjectMBean(trackedService);
-            ExecutorService executor = agentContext.getRegistrationExecutor();
-            executor.submit(new Runnable() {
-                public void run() {
-                    agentContext.registerMBean(AbstractCompendiumHandler.this);
-                }
-            });
-            trackedId = serviceId;
+            agentContext.registerMBean(AbstractCompendiumHandler.this);
+        } else {
+            String serviceDescription = getServiceDescription(reference);
+            logger.log(LogService.LOG_WARNING, "Detected secondary ServiceReference for ["
+ serviceDescription
+                    + "] with " + Constants.SERVICE_ID + " [" + serviceId + "] Only 1 instance
will be JMX managed");
         }
         return trackedService;
     }
@@ -103,22 +99,16 @@ public abstract class AbstractCompendium
      */
     public void removedService(ServiceReference reference, Object service) {
         Logger logger = agentContext.getLogger();
-        Long serviceID = (Long) reference.getProperty(Constants.SERVICE_ID);
-        if (trackedId != null && !trackedId.equals(serviceID)) {
-            String serviceDescription = getServiceDescription(reference);
-            logger.log(LogService.LOG_WARNING, "ServiceReference for [" + serviceDescription
+ "] with "
-                    + Constants.SERVICE_ID + " [" + serviceID + "] is not currently JMX managed");
-        } else {
+        long serviceID = (Long) reference.getProperty(Constants.SERVICE_ID);
+        if (trackedId.compareAndSet(serviceID, 0)) {
             logger.log(LogService.LOG_INFO, "Unregistering MBean with ObjectName [" + getName()
+ "] for service with "
                     + Constants.SERVICE_ID + " [" + serviceID + "]"); 
-            ExecutorService executor = agentContext.getRegistrationExecutor();
-            executor.submit(new Runnable() {
-                public void run() {
-                    agentContext.unregisterMBean(AbstractCompendiumHandler.this);
-                }
-            });
-            trackedId = null;
+            agentContext.unregisterMBean(AbstractCompendiumHandler.this);
             context.ungetService(reference);
+        } else {
+            String serviceDescription = getServiceDescription(reference);
+            logger.log(LogService.LOG_WARNING, "ServiceReference for [" + serviceDescription
+ "] with "
+                    + Constants.SERVICE_ID + " [" + serviceID + "] is not currently JMX managed");
         }
     }
 
@@ -126,7 +116,7 @@ public abstract class AbstractCompendium
         String serviceDescription = (String) reference.getProperty(Constants.SERVICE_DESCRIPTION);
         if (serviceDescription == null) {
             Object obj = reference.getProperty(Constants.OBJECTCLASS);
-            if (obj instanceof String) {
+            if (obj instanceof String[]) {
                 StringBuilder sb = new StringBuilder();
                 for (String s : (String[]) obj) {
                     if (sb.length() > 0) {

Modified: aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/Activator.java
URL: http://svn.apache.org/viewvc/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/Activator.java?rev=1583139&r1=1583138&r2=1583139&view=diff
==============================================================================
--- aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/Activator.java (original)
+++ aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/Activator.java Sun Mar 30
13:50:06 2014
@@ -44,9 +44,7 @@ public class Activator implements Bundle
         //starting logger
         logger.open();
         logger.log(LogService.LOG_DEBUG, "Starting JMX OSGi bundle");
-        agent = new JMXAgentImpl(logger);
-        JMXAgentContext agentContext = new JMXAgentContext(context, agent, logger);
-        agent.setAgentContext(agentContext);
+        agent = new JMXAgentImpl(context, logger);
         agent.start();
     }
 

Modified: aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/MBeanServiceTracker.java
URL: http://svn.apache.org/viewvc/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/MBeanServiceTracker.java?rev=1583139&r1=1583138&r2=1583139&view=diff
==============================================================================
--- aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/MBeanServiceTracker.java (original)
+++ aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/MBeanServiceTracker.java Sun
Mar 30 13:50:06 2014
@@ -51,19 +51,11 @@ public class MBeanServiceTracker extends
      * @see ServiceTracker#addingService(ServiceReference)
      */
     public Object addingService(final ServiceReference reference) {
-        final MBeanServer mbeanServer = (MBeanServer) context.getService(reference);
+        final MBeanServer mbeanServer = (MBeanServer) super.addingService(reference);
         Logger logger = agentContext.getLogger();
         logger.log(LogService.LOG_DEBUG, "Discovered MBean server " + mbeanServer);
-        ExecutorService executor = agentContext.getRegistrationExecutor();
-        executor.submit(new Runnable() {
-
-            public void run() {
-                agentContext.registerMBeans(mbeanServer);
-
-            }
-        });
-
-        return super.addingService(reference);
+        agentContext.registerMBeans(mbeanServer);
+        return mbeanServer;
     }
 
     /**
@@ -73,16 +65,10 @@ public class MBeanServiceTracker extends
      * @see ServiceTracker#removedService(ServiceReference, Object)
      */
     public void removedService(final ServiceReference reference, Object service) {
-        final MBeanServer mbeanServer = (MBeanServer) context.getService(reference);
+        final MBeanServer mbeanServer = (MBeanServer) service;
         Logger logger = agentContext.getLogger();
-        logger.log(LogService.LOG_DEBUG, "MBean server " + mbeanServer+ " is unregistered
from SeviceRegistry");
-        ExecutorService executor = agentContext.getRegistrationExecutor();
-        executor.submit(new Runnable() {
-
-            public void run() {
-                agentContext.unregisterMBeans(mbeanServer);
-            }
-        });
+        logger.log(LogService.LOG_DEBUG, "MBean server " + mbeanServer+ " is unregistered
from ServiceRegistry");
+        agentContext.unregisterMBeans(mbeanServer);
         super.removedService(reference, service);
     }
 

Modified: aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgent.java
URL: http://svn.apache.org/viewvc/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgent.java?rev=1583139&r1=1583138&r2=1583139&view=diff
==============================================================================
--- aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgent.java (original)
+++ aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgent.java Sun Mar
30 13:50:06 2014
@@ -62,34 +62,9 @@ public interface JMXAgent {
     void unregisterMBean(final MBeanHandler mBeanHandler);
 
     /**
-     * Unregisters MBean with all available MBeanServers.
-     * @param name of MBean to be unregistered.
-     * @deprecated
-     */
-    void unregisterMBean(final String name);
-
-    /**
      * Stops JMXAgent.
      * This method stops MBeanServiceTracker and all MBean handlers.
      */
     void stop();
 
-    /**
-     * Gets JMXAgentContext @see {@link JMXAgentContext}.
-     * @return JMXAgentContext instance.
-     */
-    JMXAgentContext getAgentContext();
-
-    /**
-     * Sets JMXAgentContext for this agent.
-     * @param agentContext JMXAgentContext instance created for this agent.
-     */
-    void setAgentContext(JMXAgentContext agentContext);
-
-    /**
-     * Gets registration {@link ExecutorService}.
-     * @return registration executor.
-     */
-    ExecutorService getRegistrationExecutor();
-
 }
\ No newline at end of file

Modified: aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentContext.java
URL: http://svn.apache.org/viewvc/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentContext.java?rev=1583139&r1=1583138&r2=1583139&view=diff
==============================================================================
--- aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentContext.java
(original)
+++ aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentContext.java
Sun Mar 30 13:50:06 2014
@@ -83,15 +83,6 @@ public class JMXAgentContext {
     }
 
     /**
-     * Delegates invocation to JMX agent.
-     * @see org.apache.aries.jmx.agent.JMXAgent#unregisterMBean(String)
-     * @deprecated
-     */
-    public void unregisterMBean(final String name) {
-        agent.unregisterMBean(name);
-    }
-
-    /**
      * Gets bundle context.
      * @return bundle context.
      */
@@ -107,11 +98,4 @@ public class JMXAgentContext {
         return logger;
     }
 
-    /**
-     * Delegates invocation to JMX agent.
-     * @see org.apache.aries.jmx.agent.JMXAgent#getRegistrationExecutor()
-     */
-    public ExecutorService getRegistrationExecutor() {
-        return agent.getRegistrationExecutor();
-    }
 }

Modified: aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentImpl.java
URL: http://svn.apache.org/viewvc/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentImpl.java?rev=1583139&r1=1583138&r2=1583139&view=diff
==============================================================================
--- aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentImpl.java (original)
+++ aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/agent/JMXAgentImpl.java Sun
Mar 30 13:50:06 2014
@@ -16,7 +16,9 @@
  */
 package org.apache.aries.jmx.agent;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
@@ -62,24 +64,21 @@ public class JMXAgentImpl implements JMX
     /**
      * {@link MBeanHandler} store.
      */
+    private List<MBeanServer> mbeanServers;
     private Map<MBeanHandler, Boolean> mbeansHandlers;
-    private JMXAgentContext agentContext;
+    private BundleContext context;
     private Logger logger;
 
     /**
-     * Registration {@link ExecutorService}.
-     */
-    private ExecutorService registrationExecutor;
-
-    /**
      * Constructs new JMXAgent.
      *
      * @param logger @see org.apache.aries.jmx.Logger
      */
-    public JMXAgentImpl(Logger logger) {
+    public JMXAgentImpl(BundleContext context, Logger logger) {
+        this.context = context;
         this.logger = logger;
+        this.mbeanServers = new ArrayList<MBeanServer>();
         this.mbeansHandlers = new HashMap<MBeanHandler, Boolean>();
-        this.registrationExecutor = Executors.newSingleThreadExecutor(new JMXThreadFactory("JMX
OSGi Agent"));
     }
 
     /**
@@ -87,20 +86,22 @@ public class JMXAgentImpl implements JMX
      */
     public synchronized void start() {
         logger.log(LogService.LOG_INFO, "Starting JMX OSGi agent");
-        BundleContext bc = agentContext.getBundleContext();
-        MBeanHandler frameworkHandler = new FrameworkMBeanHandler(bc, logger);
+        // Initialize static handlers
+        // Those handlers do not track dependencies
+        JMXAgentContext agentContext = new JMXAgentContext(context, this, logger);
+        MBeanHandler frameworkHandler = new FrameworkMBeanHandler(agentContext);
         mbeansHandlers.put(frameworkHandler, Boolean.FALSE);
         frameworkHandler.open();
-        MBeanHandler bundleStateHandler = new BundleStateMBeanHandler(bc, logger);
+        MBeanHandler bundleStateHandler = new BundleStateMBeanHandler(agentContext);
         mbeansHandlers.put(bundleStateHandler, Boolean.FALSE);
         bundleStateHandler.open();
-        MBeanHandler revisionsStateHandler = new BundleWiringStateMBeanHandler(bc, logger);
+        MBeanHandler revisionsStateHandler = new BundleWiringStateMBeanHandler(agentContext);
         mbeansHandlers.put(revisionsStateHandler, Boolean.FALSE);
         revisionsStateHandler.open();
-        MBeanHandler serviceStateHandler = new ServiceStateMBeanHandler(bc, logger);
+        MBeanHandler serviceStateHandler = new ServiceStateMBeanHandler(agentContext);
         mbeansHandlers.put(serviceStateHandler, Boolean.FALSE);
         serviceStateHandler.open();
-        MBeanHandler packageStateHandler = new PackageStateMBeanHandler(bc, logger);
+        MBeanHandler packageStateHandler = new PackageStateMBeanHandler(agentContext);
         mbeansHandlers.put(packageStateHandler, Boolean.FALSE);
         packageStateHandler.open();
         MBeanHandler permissionAdminHandler = new PermissionAdminMBeanHandler(agentContext);
@@ -115,6 +116,7 @@ public class JMXAgentImpl implements JMX
         MBeanHandler provServiceHandler = new ProvisioningServiceMBeanHandler(agentContext);
         mbeansHandlers.put(provServiceHandler, Boolean.FALSE);
         provServiceHandler.open();
+        // Track mbean servers
         mbeanServiceTracker = new MBeanServiceTracker(agentContext);
         mbeanServiceTracker.open();
     }
@@ -124,7 +126,7 @@ public class JMXAgentImpl implements JMX
      */
     public synchronized void registerMBeans(final MBeanServer server) {
         for (MBeanHandler mbeanHandler : mbeansHandlers.keySet()) {
-            if (mbeansHandlers.get(mbeanHandler) == Boolean.FALSE) {
+            if (mbeansHandlers.get(mbeanHandler) == Boolean.TRUE) {
                 String name = mbeanHandler.getName();
                 StandardMBean mbean = mbeanHandler.getMbean();
                 if (mbean != null) {
@@ -132,7 +134,6 @@ public class JMXAgentImpl implements JMX
                         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) {
@@ -147,6 +148,7 @@ public class JMXAgentImpl implements JMX
                 }
             }
         }
+        mbeanServers.add(server);
     }
 
     /**
@@ -163,7 +165,6 @@ public class JMXAgentImpl implements JMX
                        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);
@@ -178,22 +179,21 @@ public class JMXAgentImpl implements JMX
                 }
             }
         }
+        mbeanServers.remove(server);
     }
 
     /**
      * @see org.apache.aries.jmx.agent.JMXAgent#registerMBean(org.apache.aries.jmx.MBeanHandler)
      */
     public synchronized void registerMBean(final MBeanHandler mBeanHandler) {
-        Object[] servers = getMBeanServers();
-        for (Object server : servers) {
+        for (MBeanServer server : mbeanServers) {
             String name = mBeanHandler.getName();
             StandardMBean mbean = mBeanHandler.getMbean();
             try {
                 logger.log(LogService.LOG_INFO, "Registering " + mbean.getMBeanInterface().getName()
                         + " to MBeanServer " + server + " with name " + name);
-                ((MBeanServer) server).registerMBean(mbean, new ObjectName(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) {
@@ -209,45 +209,19 @@ public class JMXAgentImpl implements JMX
                 return;
             }
         }
+        mbeansHandlers.put(mBeanHandler, Boolean.TRUE);
     }
 
     /**
      * @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) {
+        for (MBeanServer server : mbeanServers) {
             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 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);
-                ((MBeanServer) server).unregisterMBean(new ObjectName(name));
+                server.unregisterMBean(new ObjectName(name));
             } catch (MBeanRegistrationException e) {
                 logger.log(LogService.LOG_ERROR, "Can't register MBean", e);
             } catch (InstanceNotFoundException e) {
@@ -260,6 +234,7 @@ public class JMXAgentImpl implements JMX
                 return;
             }
         }
+        mbeansHandlers.put(mBeanHandler, Boolean.FALSE);
     }
 
     /**
@@ -273,45 +248,6 @@ public class JMXAgentImpl implements JMX
                 mBeanHandler.close();
             }
         }
-        if (registrationExecutor != null && !registrationExecutor.isShutdown()) {
-            registrationExecutor.shutdown();
-            try {
-                registrationExecutor.awaitTermination(5 * 60, TimeUnit.SECONDS);
-            } catch (InterruptedException e) {
-                logger.log(LogService.LOG_WARNING, "Interrupted while waiting for executor
shutdown", e);
-            }
-        }
-    }
-
-    /**
-     * @see org.apache.aries.jmx.agent.JMXAgent#getAgentContext()
-     */
-    public JMXAgentContext getAgentContext() {
-        return agentContext;
-    }
-
-    /**
-     * @see org.apache.aries.jmx.agent.JMXAgent#setAgentContext(org.apache.aries.jmx.agent.JMXAgentContext)
-     */
-    public void setAgentContext(JMXAgentContext agentContext) {
-        this.agentContext = agentContext;
-    }
-
-    /**
-     * Gets all MBeanServers from MBeanServiceTracker.
-     *
-     * @return array of MBean servers.
-     */
-    private Object[] getMBeanServers() {
-        Object[] servers = mbeanServiceTracker.getServices();
-        return servers != null ? servers : new Object[0];
-    }
-
-    /**
-     * @see org.apache.aries.jmx.agent.JMXAgent#getRegistrationExecutor()
-     */
-    public ExecutorService getRegistrationExecutor() {
-        return registrationExecutor;
     }
 
 }

Modified: aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/BundleStateMBeanHandler.java
URL: http://svn.apache.org/viewvc/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/BundleStateMBeanHandler.java?rev=1583139&r1=1583138&r2=1583139&view=diff
==============================================================================
--- aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/BundleStateMBeanHandler.java
(original)
+++ aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/BundleStateMBeanHandler.java
Sun Mar 30 13:50:06 2014
@@ -23,6 +23,7 @@ import javax.management.StandardMBean;
 
 import org.apache.aries.jmx.Logger;
 import org.apache.aries.jmx.MBeanHandler;
+import org.apache.aries.jmx.agent.JMXAgentContext;
 import org.apache.aries.jmx.util.ObjectNameUtils;
 import org.apache.aries.jmx.util.shared.RegistrableStandardEmitterMBean;
 import org.osgi.framework.BundleContext;
@@ -43,6 +44,7 @@ import org.osgi.service.startlevel.Start
  */
 public class BundleStateMBeanHandler implements MBeanHandler {
 
+    private JMXAgentContext agentContext;
     private Logger logger;
     private String name;
     private StandardMBean mbean;
@@ -52,9 +54,10 @@ public class BundleStateMBeanHandler imp
     private ServiceReference startLevelRef;
 
 
-    public BundleStateMBeanHandler(BundleContext bundleContext, Logger logger) {
-        this.bundleContext = bundleContext;
-        this.logger = logger;
+    public BundleStateMBeanHandler(JMXAgentContext agentContext) {
+        this.agentContext = agentContext;
+        this.bundleContext = agentContext.getBundleContext();
+        this.logger = agentContext.getLogger();
         this.name = ObjectNameUtils.createFullObjectName(bundleContext, OBJECTNAME);
     }
 
@@ -72,6 +75,7 @@ public class BundleStateMBeanHandler imp
         } catch (NotCompliantMBeanException e) {
             logger.log(LogService.LOG_ERROR, "Failed to instantiate MBean for " + BundleStateMBean.class.getName(),
e);
         }
+        agentContext.registerMBean(this);
     }
 
     /**
@@ -93,6 +97,7 @@ public class BundleStateMBeanHandler imp
      * @see org.apache.aries.jmx.MBeanHandler#close()
      */
     public void close() {
+        agentContext.unregisterMBean(this);
         if (packageAdminRef != null) {
             try {
                 bundleContext.ungetService(packageAdminRef);

Modified: aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/FrameworkMBeanHandler.java
URL: http://svn.apache.org/viewvc/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/FrameworkMBeanHandler.java?rev=1583139&r1=1583138&r2=1583139&view=diff
==============================================================================
--- aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/FrameworkMBeanHandler.java
(original)
+++ aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/FrameworkMBeanHandler.java
Sun Mar 30 13:50:06 2014
@@ -21,6 +21,7 @@ import javax.management.StandardMBean;
 
 import org.apache.aries.jmx.Logger;
 import org.apache.aries.jmx.MBeanHandler;
+import org.apache.aries.jmx.agent.JMXAgentContext;
 import org.apache.aries.jmx.util.ObjectNameUtils;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
@@ -40,6 +41,7 @@ import org.osgi.service.startlevel.Start
  */
 public class FrameworkMBeanHandler implements MBeanHandler {
 
+    private JMXAgentContext agentContext;
     private String name;
     private StandardMBean mbean;
     private BundleContext context;
@@ -48,13 +50,13 @@ public class FrameworkMBeanHandler imple
     /**
      * Constructs new FrameworkMBeanHandler.
      *
-     * @param context bundle context of JMX bundle.
-     * @param logger @see {@link Logger}.
+     * @param agentContext agent context
      */
-    public FrameworkMBeanHandler(BundleContext context, Logger logger) {
-        this.context = context;
+    public FrameworkMBeanHandler(JMXAgentContext agentContext) {
+        this.agentContext = agentContext;
+        this.context = agentContext.getBundleContext();
+        this.logger = agentContext.getLogger();
         this.name = ObjectNameUtils.createFullObjectName(context, FrameworkMBean.OBJECTNAME);
-        this.logger = logger;
     }
 
     /**
@@ -78,13 +80,14 @@ public class FrameworkMBeanHandler imple
         } catch (NotCompliantMBeanException e) {
             logger.log(LogService.LOG_ERROR, "Not compliant MBean", e);
         }
+        agentContext.registerMBean(this);
     }
 
     /**
      * @see org.apache.aries.jmx.MBeanHandler#close()
      */
     public void close() {
-        //not used
+        agentContext.unregisterMBean(this);
     }
 
     /**

Modified: aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/PackageStateMBeanHandler.java
URL: http://svn.apache.org/viewvc/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/PackageStateMBeanHandler.java?rev=1583139&r1=1583138&r2=1583139&view=diff
==============================================================================
--- aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/PackageStateMBeanHandler.java
(original)
+++ aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/PackageStateMBeanHandler.java
Sun Mar 30 13:50:06 2014
@@ -21,6 +21,7 @@ import javax.management.StandardMBean;
 
 import org.apache.aries.jmx.Logger;
 import org.apache.aries.jmx.MBeanHandler;
+import org.apache.aries.jmx.agent.JMXAgentContext;
 import org.apache.aries.jmx.util.ObjectNameUtils;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
@@ -39,6 +40,7 @@ import org.osgi.service.packageadmin.Pac
  */
 public class PackageStateMBeanHandler implements MBeanHandler {
 
+    private JMXAgentContext agentContext;
     private String name;
     private StandardMBean mbean;
     private BundleContext context;
@@ -46,14 +48,12 @@ public class PackageStateMBeanHandler im
 
     /**
      * Constructs new PackageStateMBeanHandler.
-S     *
-     * @param context bundle context of JMX bundle.
-     * @param logger @see {@link Logger}.
      */
-    public PackageStateMBeanHandler(BundleContext context, Logger logger) {
-        this.context = context;
+    public PackageStateMBeanHandler(JMXAgentContext agentContext) {
+        this.agentContext = agentContext;
+        this.context = agentContext.getBundleContext();
+        this.logger = agentContext.getLogger();
         this.name = ObjectNameUtils.createFullObjectName(context, PackageStateMBean.OBJECTNAME);
-        this.logger = logger;
     }
 
     /**
@@ -75,13 +75,14 @@ S     *
         } catch (NotCompliantMBeanException e) {
             logger.log(LogService.LOG_ERROR, "Not compliant MBean", e);
         }
+        agentContext.registerMBean(this);
     }
 
     /**
      * @see org.apache.aries.jmx.MBeanHandler#close()
      */
     public void close() {
-        //not used
+        agentContext.unregisterMBean(this);
     }
 
     /**

Modified: aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/ServiceStateMBeanHandler.java
URL: http://svn.apache.org/viewvc/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/ServiceStateMBeanHandler.java?rev=1583139&r1=1583138&r2=1583139&view=diff
==============================================================================
--- aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/ServiceStateMBeanHandler.java
(original)
+++ aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/ServiceStateMBeanHandler.java
Sun Mar 30 13:50:06 2014
@@ -23,6 +23,7 @@ import javax.management.StandardMBean;
 
 import org.apache.aries.jmx.Logger;
 import org.apache.aries.jmx.MBeanHandler;
+import org.apache.aries.jmx.agent.JMXAgentContext;
 import org.apache.aries.jmx.util.ObjectNameUtils;
 import org.apache.aries.jmx.util.shared.RegistrableStandardEmitterMBean;
 import org.osgi.framework.BundleContext;
@@ -40,6 +41,7 @@ import org.osgi.service.log.LogService;
  */
 public class ServiceStateMBeanHandler implements MBeanHandler {
 
+    private JMXAgentContext agentContext;
     private String name;
     private StandardMBean mbean;
     private ServiceState serviceStateMBean;
@@ -47,9 +49,10 @@ public class ServiceStateMBeanHandler im
     private Logger logger;
 
 
-    public ServiceStateMBeanHandler(BundleContext bundleContext, Logger logger) {
-        this.bundleContext = bundleContext;
-        this.logger = logger;
+    public ServiceStateMBeanHandler(JMXAgentContext agentContext) {
+        this.agentContext = agentContext;
+        this.bundleContext = agentContext.getBundleContext();
+        this.logger = agentContext.getLogger();
         this.name = ObjectNameUtils.createFullObjectName(bundleContext, OBJECTNAME);
     }
 
@@ -63,6 +66,7 @@ public class ServiceStateMBeanHandler im
         } catch (NotCompliantMBeanException e) {
             logger.log(LogService.LOG_ERROR, "Failed to instantiate MBean for " + ServiceStateMBean.class.getName(),
e);
         }
+        agentContext.registerMBean(this);
     }
 
     /**
@@ -83,6 +87,7 @@ public class ServiceStateMBeanHandler im
      * @see org.apache.aries.jmx.MBeanHandler#close()
      */
     public void close() {
+        agentContext.unregisterMBean(this);
        // ensure dispatcher is shutdown even if postDeRegister is not honored
        if (serviceStateMBean != null) {
            serviceStateMBean.shutDownDispatcher();

Modified: aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/wiring/BundleWiringStateMBeanHandler.java
URL: http://svn.apache.org/viewvc/aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/wiring/BundleWiringStateMBeanHandler.java?rev=1583139&r1=1583138&r2=1583139&view=diff
==============================================================================
--- aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/wiring/BundleWiringStateMBeanHandler.java
(original)
+++ aries/trunk/jmx/jmx-core/src/main/java/org/apache/aries/jmx/framework/wiring/BundleWiringStateMBeanHandler.java
Sun Mar 30 13:50:06 2014
@@ -21,6 +21,7 @@ import javax.management.StandardMBean;
 
 import org.apache.aries.jmx.Logger;
 import org.apache.aries.jmx.MBeanHandler;
+import org.apache.aries.jmx.agent.JMXAgentContext;
 import org.apache.aries.jmx.util.ObjectNameUtils;
 import org.apache.aries.jmx.util.shared.RegistrableStandardEmitterMBean;
 import org.osgi.framework.BundleContext;
@@ -28,6 +29,8 @@ import org.osgi.jmx.framework.wiring.Bun
 import org.osgi.service.log.LogService;
 
 public class BundleWiringStateMBeanHandler implements MBeanHandler {
+
+    private JMXAgentContext agentContext;
     private final String name;
     private final BundleContext bundleContext;
     private final Logger logger;
@@ -35,9 +38,10 @@ public class BundleWiringStateMBeanHandl
     private StandardMBean mbean;
     private BundleWiringState revisionsStateMBean;
 
-    public BundleWiringStateMBeanHandler(BundleContext bundleContext, Logger logger) {
-        this.bundleContext = bundleContext;
-        this.logger = logger;
+    public BundleWiringStateMBeanHandler(JMXAgentContext agentContext) {
+        this.agentContext = agentContext;
+        this.bundleContext = agentContext.getBundleContext();
+        this.logger = agentContext.getLogger();
         this.name = ObjectNameUtils.createFullObjectName(bundleContext, BundleWiringStateMBean.OBJECTNAME);
     }
 
@@ -51,6 +55,7 @@ public class BundleWiringStateMBeanHandl
         } catch (NotCompliantMBeanException e) {
             logger.log(LogService.LOG_ERROR, "Failed to instantiate MBean for " + BundleWiringStateMBean.class.getName(),
e);
         }
+        agentContext.registerMBean(this);
     }
 
     /* (non-Javadoc)
@@ -65,7 +70,7 @@ public class BundleWiringStateMBeanHandl
      * @see org.apache.aries.jmx.MBeanHandler#close()
      */
     public void close() {
-        // not used
+        agentContext.unregisterMBean(this);
     }
 
     /* (non-Javadoc)

Modified: aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/CompendiumHandlerTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/CompendiumHandlerTest.java?rev=1583139&r1=1583138&r2=1583139&view=diff
==============================================================================
--- aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/CompendiumHandlerTest.java
(original)
+++ aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/CompendiumHandlerTest.java
Sun Mar 30 13:50:06 2014
@@ -55,6 +55,9 @@ public class CompendiumHandlerTest {
         Object service = new Object();
 
         ServiceReference reference = mock(ServiceReference.class);
+        when(reference.getProperty(Constants.SERVICE_ID)).thenReturn(1L);
+        when(reference.getProperty(Constants.OBJECTCLASS)).thenReturn("the class");
+
         BundleContext bundleContext = mock(BundleContext.class);
         when(bundleContext.getProperty(Constants.FRAMEWORK_UUID)).thenReturn("some-uuid");
         when(bundleContext.getService(reference)).thenReturn(service);
@@ -64,17 +67,12 @@ public class CompendiumHandlerTest {
         JMXAgentContext agentContext = mock(JMXAgentContext.class);
         when(agentContext.getBundleContext()).thenReturn(bundleContext);
         when(agentContext.getLogger()).thenReturn(agentLogger);
-        ExecutorService executor = Executors.newSingleThreadExecutor();
-        when(agentContext.getRegistrationExecutor()).thenReturn(executor);
 
         AbstractCompendiumHandler concreteHandler = new CompendiumHandler(agentContext, "org.osgi.service.Xxx");
         target = spy(concreteHandler);
 
         target.addingService(reference);
 
-        executor.shutdown();
-        executor.awaitTermination(2, TimeUnit.SECONDS);
-
         //service only got once
         verify(bundleContext).getService(reference);
         //template method is invoked
@@ -89,26 +87,24 @@ public class CompendiumHandlerTest {
 
         Object service = new Object();
         ServiceReference reference = mock(ServiceReference.class);
+        when(reference.getProperty(Constants.SERVICE_ID)).thenReturn(1L);
+        when(reference.getProperty(Constants.OBJECTCLASS)).thenReturn("the class");
 
         BundleContext bundleContext = mock(BundleContext.class);
         Logger agentLogger = mock(Logger.class);
         JMXAgentContext agentContext = mock(JMXAgentContext.class);
         when(agentContext.getBundleContext()).thenReturn(bundleContext);
         when(agentContext.getLogger()).thenReturn(agentLogger);
-        ExecutorService executor = Executors.newSingleThreadExecutor();
-        when(agentContext.getRegistrationExecutor()).thenReturn(executor);
 
         AbstractCompendiumHandler concreteHandler = new CompendiumHandler(agentContext, "org.osgi.service.Xxx");
         target = spy(concreteHandler);
+        target.trackedId.set(1);
 
         String name = "osgi.compendium:service=xxx,version=1.0";
         doReturn(name).when(target).getName();
 
         target.removedService(reference, service);
 
-        executor.shutdown();
-        executor.awaitTermination(2, TimeUnit.SECONDS);
-
         //service unget
         verify(bundleContext).ungetService(reference);
         //unregister is invoked on context

Modified: aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/BundleStateMBeanHandlerTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/BundleStateMBeanHandlerTest.java?rev=1583139&r1=1583138&r2=1583139&view=diff
==============================================================================
--- aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/BundleStateMBeanHandlerTest.java
(original)
+++ aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/BundleStateMBeanHandlerTest.java
Sun Mar 30 13:50:06 2014
@@ -22,6 +22,8 @@ import static org.mockito.Mockito.verify
 import static org.mockito.Mockito.when;
 
 import org.apache.aries.jmx.Logger;
+import org.apache.aries.jmx.agent.JMXAgent;
+import org.apache.aries.jmx.agent.JMXAgentContext;
 import org.junit.Test;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
@@ -59,7 +61,10 @@ public class BundleStateMBeanHandlerTest
         when(context.getServiceReference(StartLevel.class.getName())).thenReturn(startLevelRef);
         when(context.getService(startLevelRef)).thenReturn(startLevel);
 
-        BundleStateMBeanHandler handler = new BundleStateMBeanHandler(context, logger);
+        JMXAgent agent = mock(JMXAgent.class);
+        JMXAgentContext agentContext = new JMXAgentContext(context, agent, logger);
+
+        BundleStateMBeanHandler handler = new BundleStateMBeanHandler(agentContext);
         handler.open();
 
         assertNotNull(handler.getMbean());

Modified: aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/ServiceStateMBeanHandlerTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/ServiceStateMBeanHandlerTest.java?rev=1583139&r1=1583138&r2=1583139&view=diff
==============================================================================
--- aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/ServiceStateMBeanHandlerTest.java
(original)
+++ aries/trunk/jmx/jmx-core/src/test/java/org/apache/aries/jmx/framework/ServiceStateMBeanHandlerTest.java
Sun Mar 30 13:50:06 2014
@@ -21,6 +21,8 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
 import org.apache.aries.jmx.Logger;
+import org.apache.aries.jmx.agent.JMXAgent;
+import org.apache.aries.jmx.agent.JMXAgentContext;
 import org.junit.Test;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
@@ -46,7 +48,10 @@ public class ServiceStateMBeanHandlerTes
         when(mockSystemBundle.getSymbolicName()).thenReturn("the.sytem.bundle");
         when(context.getBundle(0)).thenReturn(mockSystemBundle);
 
-        ServiceStateMBeanHandler handler = new ServiceStateMBeanHandler(context, logger);
+        JMXAgent agent = mock(JMXAgent.class);
+        JMXAgentContext agentContext = new JMXAgentContext(context, agent, logger);
+
+        ServiceStateMBeanHandler handler = new ServiceStateMBeanHandler(agentContext);
         handler.open();
 
         assertNotNull(handler.getMbean());



Mime
View raw message