incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r1005377 - in /sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard: Activator.java JmxWhiteboardSupport.java MBeanHolder.java
Date Thu, 07 Oct 2010 09:15:58 GMT
Author: fmeschbe
Date: Thu Oct  7 09:15:58 2010
New Revision: 1005377

URL: http://svn.apache.org/viewvc?rev=1005377&view=rev
Log:
ARIES-430 Fix Activator license header; Add support for generic MBean services; refactor MBean
service maintenance to allow for easy registration with MBeanServers arriving after MBean
server registration

Added:
    sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard/MBeanHolder.java
  (with props)
Modified:
    sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard/Activator.java
    sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard/JmxWhiteboardSupport.java

Modified: sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard/Activator.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard/Activator.java?rev=1005377&r1=1005376&r2=1005377&view=diff
==============================================================================
--- sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard/Activator.java
(original)
+++ sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard/Activator.java
Thu Oct  7 09:15:58 2010
@@ -1,21 +1,29 @@
 /*
- * Copyright 1997-2010 Day Management AG
- * Barfuesserplatz 6, 4001 Basel, Switzerland
- * All Rights Reserved.
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
  *
- * This software is the confidential and proprietary information of
- * Day Management AG, ("Confidential Information"). You shall not
- * disclose such Confidential Information and shall use it only in
- * accordance with the terms of the license agreement you entered into
- * with Day.
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
  */
 package org.apache.sling.whiteboard.fmeschbe.jmx.whiteboard;
 
-import javax.management.DynamicMBean;
 import javax.management.MBeanServer;
 
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
 import org.osgi.util.tracker.ServiceTracker;
 
@@ -75,22 +83,31 @@ public class Activator implements Bundle
 
     private class MBeanTracker extends ServiceTracker {
 
-        public MBeanTracker(BundleContext context) {
-            super(context, DynamicMBean.class.getName(), null);
+        /**
+         * Listens for any services registered with an interface whose name ends
+         * with "MBean". This matches all simple MBeans which have to implement
+         * an interface named after the class with a suffix of MBean. It also
+         * matches DynamicMBeans and all its extensions like open MBeans, model
+         * MBeans and StandardMBeans.
+         */
+        private static final String SIMPLE_MBEAN_FILTER = "("
+            + Constants.OBJECTCLASS + "=*MBean)";
+
+        public MBeanTracker(BundleContext context)
+                throws InvalidSyntaxException {
+            super(context, context.createFilter(SIMPLE_MBEAN_FILTER), null);
         }
 
         @Override
         public Object addingService(ServiceReference reference) {
-            DynamicMBean mbean = (DynamicMBean) super.addingService(reference);
+            Object mbean = super.addingService(reference);
             jmxWhiteBoard.registerMBean(mbean, reference);
             return mbean;
         }
 
         @Override
         public void removedService(ServiceReference reference, Object service) {
-            if (service instanceof DynamicMBean) {
-                jmxWhiteBoard.unregisterMBean((DynamicMBean) service);
-            }
+            jmxWhiteBoard.unregisterMBean(service);
             super.removedService(reference, service);
         }
     }

Modified: sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard/JmxWhiteboardSupport.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard/JmxWhiteboardSupport.java?rev=1005377&r1=1005376&r2=1005377&view=diff
==============================================================================
--- sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard/JmxWhiteboardSupport.java
(original)
+++ sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard/JmxWhiteboardSupport.java
Thu Oct  7 09:15:58 2010
@@ -18,22 +18,13 @@
  */
 package org.apache.sling.whiteboard.fmeschbe.jmx.whiteboard;
 
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.HashMap;
-import java.util.IdentityHashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-
-import javax.management.DynamicMBean;
-import javax.management.InstanceAlreadyExistsException;
-import javax.management.InstanceNotFoundException;
+
 import javax.management.MBeanRegistration;
-import javax.management.MBeanRegistrationException;
 import javax.management.MBeanServer;
 import javax.management.MalformedObjectNameException;
-import javax.management.NotCompliantMBeanException;
-import javax.management.ObjectInstance;
 import javax.management.ObjectName;
 
 import org.osgi.framework.ServiceReference;
@@ -49,78 +40,61 @@ class JmxWhiteboardSupport {
 
     private MBeanServer[] mbeanServers = new MBeanServer[0];
 
-    private final Map<DynamicMBean, Map<MBeanServer, ObjectName>> mbeans = new
IdentityHashMap<DynamicMBean, Map<MBeanServer, ObjectName>>();
+    private final HashMap<MBeanHolder, MBeanHolder> mbeans = new HashMap<MBeanHolder,
MBeanHolder>();
 
     protected void addMBeanServer(final MBeanServer mbeanServer) {
-        List<MBeanServer> serverList = Arrays.asList(mbeanServers);
+
+        log.info("addMBeanServer: Adding MBeanServer " + mbeanServer);
+
+        ArrayList<MBeanServer> serverList = new ArrayList<MBeanServer>(
+            Arrays.asList(mbeanServers));
         serverList.add(mbeanServer);
         mbeanServers = serverList.toArray(new MBeanServer[serverList.size()]);
 
-        // TODO: register all mbeans with the new server
-        for (Entry<DynamicMBean, Map<MBeanServer, ObjectName>> mbean : mbeans.entrySet())
{
+        // register all mbeans with the new server
+        for (MBeanHolder mbean : mbeans.values()) {
+            mbean.register(mbeanServer);
         }
     }
 
     protected void removeMBeanServer(final MBeanServer mbeanServer) {
-        for (Map<MBeanServer, ObjectName> registration : mbeans.values()) {
-            ObjectName name = registration.remove(mbeanServer);
-            if (name != null) {
-                try {
-                    mbeanServer.unregisterMBean(name);
-                } catch (MBeanRegistrationException e) {
-                    log.error("unregisterMBean: Failure unregistering", e);
-                } catch (InstanceNotFoundException e) {
-                    log.error("unregisterMBean: Failure unregistering", e);
-                }
-            }
+
+        log.info("removeMBeanServer: Removing MBeanServer " + mbeanServer);
+
+        // remove all dynamically registered mbeans from the server
+        for (MBeanHolder mbean : mbeans.values()) {
+            mbean.unregister(mbeanServer);
         }
 
-        List<MBeanServer> serverList = Arrays.asList(mbeanServers);
+        ArrayList<MBeanServer> serverList = new ArrayList<MBeanServer>(
+            Arrays.asList(mbeanServers));
         serverList.remove(mbeanServer);
         mbeanServers = serverList.toArray(new MBeanServer[serverList.size()]);
     }
 
-    protected void registerMBean(DynamicMBean mbean,
-            final ServiceReference props) {
+    protected void registerMBean(Object mbean, final ServiceReference props) {
+
+        log.info("registerMBean: Adding MBean " + mbean);
+
         ObjectName objectName = getObjectName(props);
         if (objectName != null || mbean instanceof MBeanRegistration) {
-            Map<MBeanServer, ObjectName> registration = new HashMap<MBeanServer,
ObjectName>();
+            MBeanHolder holder = new MBeanHolder(mbean, objectName);
             MBeanServer[] mbeanServers = this.mbeanServers;
             for (MBeanServer mbeanServer : mbeanServers) {
-                try {
-                    ObjectInstance registeredObject = mbeanServer.registerMBean(
-                        mbean, objectName);
-                    registration.put(mbeanServer,
-                        registeredObject.getObjectName());
-                } catch (InstanceAlreadyExistsException e) {
-                    log.error("registerMBean: Failure registering MBean "
-                        + mbean, e);
-                } catch (MBeanRegistrationException e) {
-                    log.error("registerMBean: Failure registering MBean "
-                        + mbean, e);
-                } catch (NotCompliantMBeanException e) {
-                    log.error("registerMBean: Failure registering MBean "
-                        + mbean, e);
-                }
+                holder.register(mbeanServer);
             }
-            mbeans.put(mbean, registration);
+            mbeans.put(holder, holder);
         }
     }
 
-    protected void unregisterMBean(DynamicMBean mbean) {
-        Map<MBeanServer, ObjectName> registration = mbeans.remove(mbean);
-        for (Entry<MBeanServer, ObjectName> reg : registration.entrySet()) {
-            try {
-                reg.getKey().unregisterMBean(reg.getValue());
-            } catch (MBeanRegistrationException e) {
-                log.error("unregisterMBean: Failure unregistering MBean "
-                    + mbean, e);
-            } catch (InstanceNotFoundException e) {
-                log.error("unregisterMBean: Failure unregistering MBean "
-                    + mbean, e);
-            }
+    protected void unregisterMBean(Object mbean) {
+
+        log.info("unregisterMBean: Removing MBean " + mbean);
+
+        final MBeanHolder holder = mbeans.remove(new MBeanHolder(mbean, null));
+        if (holder != null) {
+            holder.unregister();
         }
-        registration.clear();
     }
 
     private ObjectName getObjectName(final ServiceReference props) {

Added: sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard/MBeanHolder.java
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard/MBeanHolder.java?rev=1005377&view=auto
==============================================================================
--- sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard/MBeanHolder.java
(added)
+++ sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard/MBeanHolder.java
Thu Oct  7 09:15:58 2010
@@ -0,0 +1,113 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.sling.whiteboard.fmeschbe.jmx.whiteboard;
+
+import java.util.IdentityHashMap;
+import java.util.Map;
+import java.util.Map.Entry;
+
+import javax.management.InstanceAlreadyExistsException;
+import javax.management.InstanceNotFoundException;
+import javax.management.MBeanRegistrationException;
+import javax.management.MBeanServer;
+import javax.management.NotCompliantMBeanException;
+import javax.management.ObjectInstance;
+import javax.management.ObjectName;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+final class MBeanHolder {
+
+    /** default log */
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    private final Object mbean;
+
+    private final ObjectName requestedObjectName;
+
+    private final Map<MBeanServer, ObjectName> registrations;
+
+    MBeanHolder(final Object mbean, final ObjectName requestedObjectName) {
+        this.mbean = mbean;
+        this.requestedObjectName = requestedObjectName;
+        this.registrations = new IdentityHashMap<MBeanServer, ObjectName>();
+    }
+
+    void register(final MBeanServer server) {
+        ObjectInstance instance;
+        try {
+            instance = server.registerMBean(mbean, requestedObjectName);
+            registrations.put(server, instance.getObjectName());
+        } catch (InstanceAlreadyExistsException e) {
+            log.error("register: Failure registering MBean " + mbean, e);
+        } catch (MBeanRegistrationException e) {
+            log.error("register: Failure registering MBean " + mbean, e);
+        } catch (NotCompliantMBeanException e) {
+            log.error("register: Failure registering MBean " + mbean, e);
+        }
+    }
+
+    void unregister(final MBeanServer server) {
+        final ObjectName registeredName = registrations.remove(server);
+        if (registeredName != null) {
+            unregister(server, registeredName);
+        }
+    }
+
+    void unregister() {
+        for (Entry<MBeanServer, ObjectName> entry : registrations.entrySet()) {
+            unregister(entry.getKey(), entry.getValue());
+        }
+        registrations.clear();
+    }
+
+    private void unregister(final MBeanServer server, final ObjectName name) {
+        try {
+            server.unregisterMBean(name);
+        } catch (MBeanRegistrationException e) {
+            log.error("unregister: preDeregister of " + name
+                + " threw an exception", e);
+        } catch (InstanceNotFoundException e) {
+            // not really expected !
+            log.error("unregister: Unexpected unregistration problem of MBean "
+                + name, e);
+        }
+    }
+
+    // ---------- Object Overwrite
+
+    @Override
+    public int hashCode() {
+        return mbean.hashCode();
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        if (obj == null) {
+            return false;
+        } else if (obj == this) {
+            return true;
+        } else if (obj instanceof MBeanHolder) {
+            return mbean == ((MBeanHolder) obj).mbean;
+        }
+
+        return false;
+    }
+}

Propchange: sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard/MBeanHolder.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard/MBeanHolder.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev Url



Mime
View raw message