You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2013/08/13 14:21:40 UTC

svn commit: r1513455 - in /sling/trunk/contrib/extensions/healthcheck: jmx/src/main/java/org/apache/sling/hc/jmx/impl/ samples/src/main/resources/SLING-CONTENT/apps/hc/demo/install/

Author: bdelacretaz
Date: Tue Aug 13 12:21:39 2013
New Revision: 1513455

URL: http://svn.apache.org/r1513455
Log:
SLING-2987 - MBean name containing / allows for specifying MBean service name

Modified:
    sling/trunk/contrib/extensions/healthcheck/jmx/src/main/java/org/apache/sling/hc/jmx/impl/HealthCheckMBean.java
    sling/trunk/contrib/extensions/healthcheck/jmx/src/main/java/org/apache/sling/hc/jmx/impl/HealthCheckMBeanCreator.java
    sling/trunk/contrib/extensions/healthcheck/samples/src/main/resources/SLING-CONTENT/apps/hc/demo/install/org.apache.sling.hc.CompositeHealthCheck-1.json

Modified: sling/trunk/contrib/extensions/healthcheck/jmx/src/main/java/org/apache/sling/hc/jmx/impl/HealthCheckMBean.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/jmx/src/main/java/org/apache/sling/hc/jmx/impl/HealthCheckMBean.java?rev=1513455&r1=1513454&r2=1513455&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/jmx/src/main/java/org/apache/sling/hc/jmx/impl/HealthCheckMBean.java (original)
+++ sling/trunk/contrib/extensions/healthcheck/jmx/src/main/java/org/apache/sling/hc/jmx/impl/HealthCheckMBean.java Tue Aug 13 12:21:39 2013
@@ -54,9 +54,11 @@ public class HealthCheckMBean implements
     private static final long serialVersionUID = -90745301105975287L;
     private static final Logger logger = LoggerFactory.getLogger(HealthCheckMBean.class);
     private final String beanName;
+    private final String jmxTypeName;
     private final HealthCheck healthCheck;
     
     public static final String HC_OK_ATTRIBUTE_NAME = "ok";
+    public static final String HC_STATUS_ATTRIBUTE_NAME = "status";
     public static final String LOG_ATTRIBUTE_NAME = "log";
     
     private static CompositeType LOG_ROW_TYPE;
@@ -66,6 +68,8 @@ public class HealthCheckMBean implements
     public static final String LEVEL_COLUMN = "level";
     public static final String MESSAGE_COLUMN = "message";
     
+    public static final String DEFAULT_JMX_TYPE_NAME = "HealthCheck";
+    
     static {
         try {
             // Define the log row and table types
@@ -95,7 +99,16 @@ public class HealthCheckMBean implements
         if(empty(name)) {
             name = hc.toString();
         }
-        beanName = name;
+        
+        final int pos = name.indexOf('/');
+        if(pos > 0) {
+            jmxTypeName = name.substring(0, pos);
+            beanName = name.substring(pos + 1);
+        } else {
+            jmxTypeName = DEFAULT_JMX_TYPE_NAME;
+            beanName = name;
+        }
+        
         healthCheck = hc;
     }
     
@@ -114,6 +127,8 @@ public class HealthCheckMBean implements
             return result.isOk();
         } else if(LOG_ATTRIBUTE_NAME.equals(attribute)) {
             return logData(result);
+        } else if(HC_STATUS_ATTRIBUTE_NAME.equals(attribute)) {
+            return result.getStatus().toString();
         } else {
             final Object o = healthCheck.getInfo().get(attribute);
             if(o == null) {
@@ -155,10 +170,11 @@ public class HealthCheckMBean implements
 
     @Override
     public MBeanInfo getMBeanInfo() {
-        final MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[healthCheck.getInfo().size() + 2];
+        final MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[healthCheck.getInfo().size() + 3];
         int i=0;
         attrs[i++] = new MBeanAttributeInfo(HC_OK_ATTRIBUTE_NAME, Boolean.class.getName(), "The HealthCheck result", true, false, false);
         attrs[i++] = new OpenMBeanAttributeInfoSupport(LOG_ATTRIBUTE_NAME, "The rule log", LOG_TABLE_TYPE, true, false, false);
+        attrs[i++] = new MBeanAttributeInfo(HC_STATUS_ATTRIBUTE_NAME, String.class.getName(), "The HealthCheck status", true, false, false);
         
         for(String key : healthCheck.getInfo().keySet()) {
             attrs[i++] = new MBeanAttributeInfo(key, List.class.getName(), "Description of " + key, true, false, false);
@@ -188,4 +204,8 @@ public class HealthCheckMBean implements
     public String getName() {
         return beanName;
     }
+    
+    public String getJmxTypeName() {
+        return jmxTypeName;
+    }
 }
\ No newline at end of file

Modified: sling/trunk/contrib/extensions/healthcheck/jmx/src/main/java/org/apache/sling/hc/jmx/impl/HealthCheckMBeanCreator.java
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/jmx/src/main/java/org/apache/sling/hc/jmx/impl/HealthCheckMBeanCreator.java?rev=1513455&r1=1513454&r2=1513455&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/jmx/src/main/java/org/apache/sling/hc/jmx/impl/HealthCheckMBeanCreator.java (original)
+++ sling/trunk/contrib/extensions/healthcheck/jmx/src/main/java/org/apache/sling/hc/jmx/impl/HealthCheckMBeanCreator.java Tue Aug 13 12:21:39 2013
@@ -17,9 +17,11 @@
  */
 package org.apache.sling.hc.jmx.impl;
 
+import java.util.ArrayList;
 import java.util.Dictionary;
 import java.util.HashMap;
 import java.util.Hashtable;
+import java.util.List;
 import java.util.Map;
 
 import javax.management.DynamicMBean;
@@ -50,11 +52,24 @@ public class HealthCheckMBeanCreator {
     private final Logger log = LoggerFactory.getLogger(getClass());
     private Map<ServiceReference, ServiceRegistration> registeredServices;
     private BundleContext bundleContext;
+    private List<ServiceReference> boundRefs = new ArrayList<ServiceReference>();
     
     @Activate
     protected void activate(ComponentContext ctx) {
-        bundleContext = ctx.getBundleContext();
         registeredServices = new HashMap<ServiceReference, ServiceRegistration>();
+        
+        final List<ServiceReference> toAdd = new ArrayList<ServiceReference>();
+        if(!boundRefs.isEmpty()) {
+            synchronized (boundRefs) {
+                toAdd.addAll(boundRefs);
+                boundRefs.clear();
+            }
+        }
+        
+        bundleContext = ctx.getBundleContext();
+        for(ServiceReference ref : toAdd) {
+            processHealthCheckRef(ref);
+        }
     }
     
     @Deactivate
@@ -66,17 +81,33 @@ public class HealthCheckMBeanCreator {
     }
     
     protected void bindHealthCheck(ServiceReference ref) {
+        if(bundleContext == null) {
+            // Not sure why the bundle context can still be null here, is this
+            // called before activate??
+            synchronized (boundRefs) {
+                boundRefs.add(ref);
+            }
+        } else {
+            processHealthCheckRef(ref);
+        }
+    }
+    
+    private void processHealthCheckRef(ServiceReference ref) {
         final HealthCheck hc = (HealthCheck)bundleContext.getService(ref);
         final HealthCheckMBean mbean = new HealthCheckMBean(hc);
         
         final Dictionary<String, String> mbeanProps = new Hashtable<String, String>();
-        mbeanProps.put("jmx.objectname", "org.apache.sling.healthcheck:type=HealthCheck,service=" + mbean.getName());
+        mbeanProps.put("jmx.objectname", "org.apache.sling.healthcheck:type=" + mbean.getJmxTypeName() + ",service=" + mbean.getName());
         final ServiceRegistration reg = bundleContext.registerService(DynamicMBean.class.getName(), mbean, mbeanProps);
         registeredServices.put(ref, reg);
         log.debug("Registered {} with properties {}", mbean, mbeanProps);
     }
     
     protected void unbindHealthCheck(ServiceReference ref) {
+        if(registeredServices == null) {
+            log.debug("No registeredServices, nothing to unregister");
+            return;
+        }
         final ServiceRegistration reg = registeredServices.remove(ref);
         if(reg == null) {
             log.warn("ServiceRegistration not found for {}", ref);

Modified: sling/trunk/contrib/extensions/healthcheck/samples/src/main/resources/SLING-CONTENT/apps/hc/demo/install/org.apache.sling.hc.CompositeHealthCheck-1.json
URL: http://svn.apache.org/viewvc/sling/trunk/contrib/extensions/healthcheck/samples/src/main/resources/SLING-CONTENT/apps/hc/demo/install/org.apache.sling.hc.CompositeHealthCheck-1.json?rev=1513455&r1=1513454&r2=1513455&view=diff
==============================================================================
--- sling/trunk/contrib/extensions/healthcheck/samples/src/main/resources/SLING-CONTENT/apps/hc/demo/install/org.apache.sling.hc.CompositeHealthCheck-1.json (original)
+++ sling/trunk/contrib/extensions/healthcheck/samples/src/main/resources/SLING-CONTENT/apps/hc/demo/install/org.apache.sling.hc.CompositeHealthCheck-1.json Tue Aug 13 12:21:39 2013
@@ -3,5 +3,5 @@
     "hc.name" : "CompositeHealthCheck: execute all HealthCheck tagged with 'script'", 
     "hc.tags" : [composite],
     "filter.tags" : [script],
-    "hc.mbean.name" : "composite:script" 
+    "hc.mbean.name" : "CompositeHealthCheck/script" 
 }