You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2010/09/29 10:51:28 UTC

svn commit: r1002524 - in /sling/whiteboard/fmeschbe/jmx-whiteboard: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/sling/ src/main/java/org/apache/sling/whiteboard/ src/main/java/org/apache/sling...

Author: fmeschbe
Date: Wed Sep 29 08:51:27 2010
New Revision: 1002524

URL: http://svn.apache.org/viewvc?rev=1002524&view=rev
Log:
First (untested) shot at JMX Whiteboard support

Added:
    sling/whiteboard/fmeschbe/jmx-whiteboard/   (with props)
    sling/whiteboard/fmeschbe/jmx-whiteboard/pom.xml   (with props)
    sling/whiteboard/fmeschbe/jmx-whiteboard/src/
    sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/
    sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/
    sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/
    sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/
    sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/
    sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/
    sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/
    sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/
    sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard/
    sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard/JmxWhiteboardSupport.java   (with props)

Propchange: sling/whiteboard/fmeschbe/jmx-whiteboard/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Wed Sep 29 08:51:27 2010
@@ -0,0 +1,4 @@
+.classpath
+.project
+.settings
+target

Added: sling/whiteboard/fmeschbe/jmx-whiteboard/pom.xml
URL: http://svn.apache.org/viewvc/sling/whiteboard/fmeschbe/jmx-whiteboard/pom.xml?rev=1002524&view=auto
==============================================================================
--- sling/whiteboard/fmeschbe/jmx-whiteboard/pom.xml (added)
+++ sling/whiteboard/fmeschbe/jmx-whiteboard/pom.xml Wed Sep 29 08:51:27 2010
@@ -0,0 +1,96 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+    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.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.sling</groupId>
+        <artifactId>sling</artifactId>
+        <version>9</version>
+        <relativePath>../../parent/pom.xml</relativePath>
+    </parent>
+
+    <artifactId>org.apache.sling.whiteboard.fmeschbe.jmx.whiteboard</artifactId>
+    <packaging>bundle</packaging>
+    <version>0.0.1-SNAPSHOT</version>
+
+    <name>Apache Sling Whiteboard support for JMX DynamicMBean services</name>
+    <description>
+        This bundle implements whiteboard pattern support for DynamicMBean
+        services registered with the OSGi service registry. Each DynamicMBean
+        service is registered with (and unregistered from) all MBeanServer
+        services registered in the OSGi service registry.
+    </description>
+
+    <scm>
+        <connection>scm:svn:http://svn.apache.org/repos/asf/incubator/sling/whiteboard/fmeschbe/jmx-whiteboard</connection>
+        <developerConnection>scm:svn:https://svn.apache.org/repos/asf/incubator/sling/fmeschbe/jmx-whiteboard</developerConnection>
+        <url>http://svn.apache.org/viewvc/incubator/sling/fmeschbe/jmx-whiteboard</url>
+    </scm>
+
+    <properties>
+    </properties>
+    
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-scr-plugin</artifactId>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.felix</groupId>
+                <artifactId>maven-bundle-plugin</artifactId>
+                <extensions>true</extensions>
+                <configuration>
+                    <instructions>
+                        <Bundle-Category>sling</Bundle-Category>
+                        <!-- No Export-Package -->
+                        <Private-Package>
+                            org.apache.sling.extensions.cachecontrol.impl.*,
+                        </Private-Package>
+                    </instructions>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.scr.annotations</artifactId>
+            <version>1.3.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+        </dependency>
+
+        <!-- OSGi Libraries -->
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.core</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>org.osgi.compendium</artifactId>
+        </dependency>
+    </dependencies>
+</project>

Propchange: sling/whiteboard/fmeschbe/jmx-whiteboard/pom.xml
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 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=1002524&view=auto
==============================================================================
--- sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard/JmxWhiteboardSupport.java (added)
+++ sling/whiteboard/fmeschbe/jmx-whiteboard/src/main/java/org/apache/sling/whiteboard/fmeschbe/jmx/whiteboard/JmxWhiteboardSupport.java Wed Sep 29 08:51:27 2010
@@ -0,0 +1,150 @@
+/*
+ * 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.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.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.ReferencePolicy;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Component(metatype = false)
+public class JmxWhiteboardSupport {
+
+    private static final String PROP_OBJECT_NAME = "jmx.objectname";
+
+    /** default log */
+    private final Logger log = LoggerFactory.getLogger(getClass());
+
+    @Reference(referenceInterface = MBeanServer.class, cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, policy = ReferencePolicy.DYNAMIC, bind = "addMBeanServer", unbind = "removeMBeanServer")
+    private MBeanServer[] mbeanServers = new MBeanServer[0];
+
+    @Reference(referenceInterface = DynamicMBean.class, cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, policy = ReferencePolicy.DYNAMIC, bind = "registerMBean", unbind = "unregisterMBean")
+    private final Map<DynamicMBean, Map<MBeanServer, ObjectName>> mbeans = new IdentityHashMap<DynamicMBean, Map<MBeanServer, ObjectName>>();
+
+    protected void addMBeanServer(final MBeanServer mbeanServer) {
+        List<MBeanServer> serverList = 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()) {
+        }
+    }
+
+    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);
+                }
+            }
+        }
+
+        List<MBeanServer> serverList = Arrays.asList(mbeanServers);
+        serverList.remove(mbeanServer);
+        mbeanServers = serverList.toArray(new MBeanServer[serverList.size()]);
+    }
+
+    protected void registerMBean(DynamicMBean mbean, Map<String, Object> props) {
+        ObjectName objectName = getObjectName(props);
+        if (objectName != null || mbean instanceof MBeanRegistration) {
+            Map<MBeanServer, ObjectName> registration = new HashMap<MBeanServer, 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);
+                }
+            }
+            mbeans.put(mbean, registration);
+        }
+    }
+
+    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);
+            }
+        }
+        registration.clear();
+    }
+
+    private ObjectName getObjectName(final Map<String, Object> props) {
+        Object oName = props.get(PROP_OBJECT_NAME);
+        if (oName instanceof ObjectName) {
+            return (ObjectName) oName;
+        } else if (oName instanceof String) {
+            try {
+                return new ObjectName((String) oName);
+            } catch (MalformedObjectNameException e) {
+                log.error("getObjectName: Provided ObjectName property "
+                    + oName + " cannot be used as an ObjectName", e);
+            }
+        } else {
+            log.info(
+                "getObjectName: Missing {} service property (or wrong type); registering if MBean is MBeanRegistration implementation",
+                PROP_OBJECT_NAME);
+        }
+
+        return null;
+    }
+}

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

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