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/07/31 17:09:21 UTC

svn commit: r1508906 - in /sling/branches/SLING-2987-healthcheck-redesign/hc-core: ./ src/main/java/org/apache/sling/hc/impl/ src/main/java/org/apache/sling/hc/util/ src/test/java/org/apache/sling/hc/impl/

Author: bdelacretaz
Date: Wed Jul 31 15:09:20 2013
New Revision: 1508906

URL: http://svn.apache.org/r1508906
Log:
SLING-2987 - HealthCheckMBean and OsgiScriptBinding added

Added:
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckMBean.java
      - copied, changed from r1508827, sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/util/RuleDynamicMBean.java
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckMBeanCreator.java   (with props)
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/OsgiScriptBinding.java   (with props)
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/HealthCheckMBeanTest.java   (with props)
Removed:
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/util/RuleDynamicMBean.java
Modified:
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/pom.xml
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/JmxAttributeHealthCheck.java
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/ScriptableHealthCheck.java
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/util/SimpleConstraintChecker.java
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/JmxAttributeHealthCheckTest.java
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/SimpleConstraintCheckerTest.java

Modified: sling/branches/SLING-2987-healthcheck-redesign/hc-core/pom.xml
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/pom.xml?rev=1508906&r1=1508905&r2=1508906&view=diff
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/pom.xml (original)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/pom.xml Wed Jul 31 15:09:20 2013
@@ -22,13 +22,17 @@
     <inceptionYear>2013</inceptionYear>
     
     <description>
-        The Sling Health Check core OSGi bundle, can also be used outside of an OSGi environment.
+        The Sling Health Check Core
     </description>
 
     <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>
@@ -37,9 +41,8 @@
                             org.apache.sling.hc.api,
                         </Export-Package>
                         <Private-Package>
-                            org.apache.sling.hc.impl.*
+                            org.apache.sling.hc.impl.*,
                             org.apache.sling.hc.util.*
-                            org.apache.sling.hc.deprecated.*
                         </Private-Package>
                     </instructions>
                 </configuration>
@@ -52,6 +55,17 @@
                     <target>1.6</target>
                 </configuration>
             </plugin>
+           <plugin>
+              <groupId>org.codehaus.mojo</groupId>
+              <artifactId>animal-sniffer-maven-plugin</artifactId>
+              <configuration>
+                <signature>
+                  <groupId>org.codehaus.mojo.signature</groupId>
+                  <artifactId>java16</artifactId>
+                  <version>1.0</version>
+                </signature>
+              </configuration>
+            </plugin>
         </plugins>
     </build>
 

Copied: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckMBean.java (from r1508827, sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/util/RuleDynamicMBean.java)
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckMBean.java?p2=sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckMBean.java&p1=sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/util/RuleDynamicMBean.java&r1=1508827&r2=1508906&rev=1508906&view=diff
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/util/RuleDynamicMBean.java (original)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckMBean.java Wed Jul 31 15:09:20 2013
@@ -15,25 +15,48 @@
  * KIND, either express or implied. See the License for the
  * specific language governing permissions and limitations under the License.
  */
-package org.apache.sling.hc.util;
+package org.apache.sling.hc.impl;
 
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.management.Attribute;
+import javax.management.AttributeList;
+import javax.management.AttributeNotFoundException;
 import javax.management.DynamicMBean;
+import javax.management.InvalidAttributeValueException;
+import javax.management.MBeanAttributeInfo;
+import javax.management.MBeanException;
+import javax.management.MBeanInfo;
+import javax.management.ReflectionException;
+import javax.management.openmbean.CompositeDataSupport;
 import javax.management.openmbean.CompositeType;
+import javax.management.openmbean.OpenDataException;
+import javax.management.openmbean.OpenMBeanAttributeInfoSupport;
 import javax.management.openmbean.OpenType;
 import javax.management.openmbean.SimpleType;
+import javax.management.openmbean.TabularData;
+import javax.management.openmbean.TabularDataSupport;
 import javax.management.openmbean.TabularType;
 
+import org.apache.sling.hc.api.Constants;
+import org.apache.sling.hc.api.HealthCheck;
+import org.apache.sling.hc.api.Result;
+import org.apache.sling.hc.api.ResultLog;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-/** A {@link DynamicMBean} that gives access to a {@link Rule}'s data */
-public class RuleDynamicMBean { //implements DynamicMBean, Serializable {
+/** A {@link DynamicMBean} used to execute a {@link HealthCheck} service */
+public class HealthCheckMBean implements DynamicMBean, Serializable {
 
     private static final long serialVersionUID = -90745301105975287L;
-    private static final Logger logger = LoggerFactory.getLogger(RuleDynamicMBean.class);
-    //private final String beanName;
+    private static final Logger logger = LoggerFactory.getLogger(HealthCheckMBean.class);
+    private final String beanName;
+    private final HealthCheck healthCheck;
     
-    public static final String RULE_OK_ATTRIBUTE_NAME = "ok";
+    public static final String HC_OK_ATTRIBUTE_NAME = "ok";
     public static final String LOG_ATTRIBUTE_NAME = "log";
     
     private static CompositeType LOG_ROW_TYPE;
@@ -60,21 +83,29 @@ public class RuleDynamicMBean { //implem
         }
     }
 
-    /*
-    public RuleDynamicMBean(Rule r) {
-        beanName = r.toString();
-        rule = r;
+    public HealthCheckMBean(HealthCheck hc) {
+        String name = hc.getInfo().get(Constants.HC_NAME);
+        if(name == null) {
+            name = hc.toString();
+        }
+        beanName = name;
+        healthCheck = hc;
     }
     
     @Override
     public Object getAttribute(String attribute)
             throws AttributeNotFoundException, MBeanException, ReflectionException {
-        if(RULE_OK_ATTRIBUTE_NAME.equals(attribute)) {
-            return !rule.evaluate().anythingToReport();
+        
+        // TODO cache the result of execution for a few seconds?
+        final ResultLog resultLog = new ResultLog(logger);
+        final Result result = healthCheck.execute(resultLog);
+        
+        if(HC_OK_ATTRIBUTE_NAME.equals(attribute)) {
+            return result.isOk();
         } else if(LOG_ATTRIBUTE_NAME.equals(attribute)) {
-            return logData(rule.evaluate());
+            return logData(result);
         } else {
-            final Object o = rule.getInfo().get(attribute);
+            final Object o = healthCheck.getInfo().get(attribute);
             if(o == null) {
                 throw new AttributeNotFoundException(attribute);
             }
@@ -85,11 +116,11 @@ public class RuleDynamicMBean { //implem
     private TabularData logData(Result er) {
         final TabularDataSupport result = new TabularDataSupport(LOG_TABLE_TYPE);
         int i=1;
-        for(Result.LogMessage msg : er.getLogMessages()) {
+        for(ResultLog.Entry e : er.getLogEntries()) {
             final Map<String, Object> data = new HashMap<String, Object>();
             data.put(INDEX_COLUMN, i++);
-            data.put(LEVEL_COLUMN, msg.getLevel().toString());
-            data.put(MESSAGE_COLUMN, msg.getMessage());
+            data.put(LEVEL_COLUMN, e.getLevel().toString());
+            data.put(MESSAGE_COLUMN, e.getMessage());
             try {
                 result.put(new CompositeDataSupport(LOG_ROW_TYPE, data));
             } catch(OpenDataException ode) {
@@ -114,12 +145,12 @@ public class RuleDynamicMBean { //implem
 
     @Override
     public MBeanInfo getMBeanInfo() {
-        final MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[rule.getInfo().size() + 2];
+        final MBeanAttributeInfo[] attrs = new MBeanAttributeInfo[healthCheck.getInfo().size() + 2];
         int i=0;
-        attrs[i++] = new MBeanAttributeInfo(RULE_OK_ATTRIBUTE_NAME, Boolean.class.getName(), "The rule value", true, false, false);
+        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);
         
-        for(String key : rule.getInfo().keySet()) {
+        for(String key : healthCheck.getInfo().keySet()) {
             attrs[i++] = new MBeanAttributeInfo(key, List.class.getName(), "Description of " + key, true, false, false);
         }
         
@@ -143,5 +174,8 @@ public class RuleDynamicMBean { //implem
     public AttributeList setAttributes(AttributeList attributes) {
         throw new UnsupportedOperationException(getClass().getSimpleName() + " does not support setting Rules attributes");
     }
-    */
+    
+    public String getName() {
+        return beanName;
+    }
 }
\ No newline at end of file

Added: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckMBeanCreator.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckMBeanCreator.java?rev=1508906&view=auto
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckMBeanCreator.java (added)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckMBeanCreator.java Wed Jul 31 15:09:20 2013
@@ -0,0 +1,88 @@
+/*
+ * 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 SF 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.hc.impl;
+
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+import javax.management.DynamicMBean;
+
+import org.apache.felix.scr.annotations.Activate;
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Deactivate;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.ReferenceCardinality;
+import org.apache.felix.scr.annotations.ReferencePolicy;
+import org.apache.sling.hc.api.HealthCheck;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Creates an {@link HealthCheckMbean} for every {@link HealthCheckMBean} service */
+@Component
+@Reference(
+        name="HealthCheck",
+        referenceInterface=HealthCheck.class, 
+        cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE, 
+        policy=ReferencePolicy.DYNAMIC)
+public class HealthCheckMBeanCreator {
+    
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    private Map<ServiceReference, ServiceRegistration> registeredServices;
+    private BundleContext bundleContext;
+    
+    @Activate
+    protected void activate(ComponentContext ctx) {
+        bundleContext = ctx.getBundleContext();
+        registeredServices = new HashMap<ServiceReference, ServiceRegistration>();
+    }
+    
+    @Deactivate
+    protected void deactivate(ComponentContext ctx) {
+        for(ServiceRegistration r : registeredServices.values()) {
+            r.unregister();
+        }
+        registeredServices = null;
+    }
+    
+    protected void bindHealthCheck(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());
+        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) {
+        final ServiceRegistration reg = registeredServices.remove(ref);
+        if(reg == null) {
+            log.warn("ServiceRegistration not found for {}", ref);
+        } else {
+            reg.unregister();
+            log.debug("Ungegistered {}", reg);
+        }
+    }
+}
\ No newline at end of file

Propchange: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckMBeanCreator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckMBeanCreator.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/JmxAttributeHealthCheck.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/JmxAttributeHealthCheck.java?rev=1508906&r1=1508905&r2=1508906&view=diff
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/JmxAttributeHealthCheck.java (original)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/JmxAttributeHealthCheck.java Wed Jul 31 15:09:20 2013
@@ -36,12 +36,15 @@ import org.apache.sling.hc.api.Result;
 import org.apache.sling.hc.api.ResultLog;
 import org.apache.sling.hc.util.SimpleConstraintChecker;
 import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /** {@link HealthCheck} that checks a single JMX attribute */
 @Component(configurationFactory=true, policy=ConfigurationPolicy.REQUIRE, metatype=true)
 @Service
 public class JmxAttributeHealthCheck implements HealthCheck {
 
+    private final Logger log = LoggerFactory.getLogger(getClass());
     private final Map<String, String> info = new HashMap<String, String>();
     private String mbeanName;
     private String attributeName;
@@ -59,6 +62,9 @@ public class JmxAttributeHealthCheck imp
     @Property(cardinality=50)
     public static final String PROP_TAGS = Constants.HC_TAGS;
     
+    @Property
+    public static final String PROP_NAME = Constants.HC_NAME;
+    
     @Activate
     public void activate(ComponentContext ctx) {
         mbeanName = PropertiesUtil.toString(ctx.getProperties().get(PROP_OBJECT_NAME), "");
@@ -68,6 +74,10 @@ public class JmxAttributeHealthCheck imp
         info.put(PROP_OBJECT_NAME, mbeanName);
         info.put(PROP_ATTRIBUTE_NAME, attributeName);
         info.put(PROP_CONSTRAINT, constraint);
+        info.put(Constants.HC_NAME, PropertiesUtil.toString(ctx.getProperties().get(Constants.HC_NAME), ""));
+        
+        log.info("Activated with HealthCheck name={}, objectName={}, attribute={}, constraint={}", 
+                new Object[] { info.get(Constants.HC_NAME), mbeanName, attributeName, constraint });
     }
     
     @Override

Added: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/OsgiScriptBinding.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/OsgiScriptBinding.java?rev=1508906&view=auto
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/OsgiScriptBinding.java (added)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/OsgiScriptBinding.java Wed Jul 31 15:09:20 2013
@@ -0,0 +1,68 @@
+/*
+ * 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 SF 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.hc.impl;
+
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.slf4j.Logger;
+
+/** The OsgiBinding is meant to be bound as an "osgi" global variable
+ *  in scripted rules, to allow for checking some OSGi states in
+ *  a simple way
+ */
+public class OsgiScriptBinding {
+    private final Logger logger;
+    private final BundleContext bundleContext;
+    
+    OsgiScriptBinding(BundleContext ctx, Logger logger) {
+        this.logger = logger;
+        this.bundleContext = ctx;
+    }
+    
+    public int inactiveBundlesCount() {
+        int count = 0;
+        for(Bundle b : bundleContext.getBundles()) {
+            if(!isActive(b)) {
+                count++;
+            }
+        }
+        logger.debug("inactiveBundlesCount={}", count);
+        return count;
+    }
+    
+    private boolean isActive(Bundle b) {
+        boolean result = true;
+        if(!isFragment(b) && Bundle.ACTIVE != b.getState()) {
+            result = false;
+            logger.info("Bundle {} is not active, state={} ({})", 
+                    new Object[] { b.getSymbolicName(), b.getState(), b.getState()});
+        }
+        return result;
+    }
+    
+    private boolean isFragment(Bundle b) {
+        final String header = (String) b.getHeaders().get( Constants.FRAGMENT_HOST );
+        if(header!= null && header.trim().length() > 0) {
+            logger.debug("{} is a fragment bundle, state won't be checked", b);
+            return true;
+        } else {
+            return false;
+        }
+    }
+}
\ No newline at end of file

Propchange: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/OsgiScriptBinding.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/OsgiScriptBinding.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/ScriptableHealthCheck.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/ScriptableHealthCheck.java?rev=1508906&r1=1508905&r2=1508906&view=diff
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/ScriptableHealthCheck.java (original)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/ScriptableHealthCheck.java Wed Jul 31 15:09:20 2013
@@ -35,16 +35,21 @@ import org.apache.sling.hc.api.Constants
 import org.apache.sling.hc.api.HealthCheck;
 import org.apache.sling.hc.api.Result;
 import org.apache.sling.hc.api.ResultLog;
+import org.osgi.framework.BundleContext;
 import org.osgi.service.component.ComponentContext;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /** {@link HealthCheck} that checks a scriptable expression */
 @Component(configurationFactory=true, policy=ConfigurationPolicy.REQUIRE, metatype=true)
 @Service
 public class ScriptableHealthCheck implements HealthCheck {
 
+    private final Logger log = LoggerFactory.getLogger(getClass());
     private final Map<String, String> info = new HashMap<String, String>();
     private String expression;
     private String languageExtension;
+    private BundleContext bundleContext;
     
     public static final String DEFAULT_LANGUAGE_EXTENSION = "ecma";
 
@@ -57,16 +62,23 @@ public class ScriptableHealthCheck imple
     @Property(cardinality=50)
     public static final String PROP_TAGS = Constants.HC_TAGS;
     
+    @Property
+    public static final String PROP_NAME = Constants.HC_NAME;
+    
     @Reference
     private ScriptEngineManager scriptEngineManager;
     
     @Activate
     public void activate(ComponentContext ctx) {
+        bundleContext = ctx.getBundleContext();
         expression = PropertiesUtil.toString(ctx.getProperties().get(PROP_EXPRESSION), "");
         languageExtension = PropertiesUtil.toString(ctx.getProperties().get(PROP_LANGUAGE_EXTENSION), DEFAULT_LANGUAGE_EXTENSION);
         
         info.put(PROP_EXPRESSION, expression);
         info.put(PROP_LANGUAGE_EXTENSION, languageExtension);
+        info.put(Constants.HC_NAME, PropertiesUtil.toString(ctx.getProperties().get(Constants.HC_NAME), ""));
+        
+        log.info("Activated, name={}, languageExtension={}, expression={}", languageExtension, expression);
     }
     
     @Override
@@ -81,6 +93,7 @@ public class ScriptableHealthCheck imple
                 // TODO pluggable Bindings? Reuse the Sling bindings providers?
                 final Bindings b = engine.createBindings();
                 b.put("jmx", new JmxScriptBinding(log));
+                b.put("osgi", new OsgiScriptBinding(bundleContext, log));
                 final Object value = engine.eval(expression, b);
                 if(value!=null && "true".equals(value.toString())) {
                     log.debug("Expression [{}] evaluates to true as expected", expression);

Modified: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/util/SimpleConstraintChecker.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/util/SimpleConstraintChecker.java?rev=1508906&r1=1508905&r2=1508906&view=diff
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/util/SimpleConstraintChecker.java (original)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/util/SimpleConstraintChecker.java Wed Jul 31 15:09:20 2013
@@ -25,6 +25,8 @@ import org.slf4j.Logger;
  */
 public class SimpleConstraintChecker {
     
+    public static final String CONTAINS = "contains";
+    
     /** Check value against expression and report to logger */
     public void check(Object inputValue, String constraint, Logger logger) {
         
@@ -52,6 +54,10 @@ public class SimpleConstraintChecker {
                 final int upperBound = Integer.valueOf(parts[3]);
                 matches = value > lowerBound && value < upperBound;
                 
+            } else if(parts.length >1 && CONTAINS.equalsIgnoreCase(parts[0])) {
+                final String pattern = constraint.substring(CONTAINS.length()).trim();
+                matches = stringValue.contains(pattern);        
+                
             } else {
                 matches = constraint.equals(stringValue); 
             }

Added: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/HealthCheckMBeanTest.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/HealthCheckMBeanTest.java?rev=1508906&view=auto
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/HealthCheckMBeanTest.java (added)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/HealthCheckMBeanTest.java Wed Jul 31 15:09:20 2013
@@ -0,0 +1,83 @@
+/*
+ * 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 SF 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.hc.impl;
+
+import java.lang.management.ManagementFactory;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.management.MBeanServer;
+import javax.management.ObjectName;
+
+import org.apache.sling.hc.api.HealthCheck;
+import org.apache.sling.hc.api.Result;
+import org.apache.sling.hc.api.ResultLog;
+import org.junit.Test;
+
+public class HealthCheckMBeanTest {
+    private final MBeanServer jmxServer = ManagementFactory.getPlatformMBeanServer();
+    private boolean resultOk;
+    private static final Map<String, String> info = new HashMap<String, String>();
+    public static final String OBJECT_NAME = "org.apache.sling.testing:type=HealthCheckMBeanTest";
+    
+    static {
+        info.put("foo",  "bar");
+        info.put("Another entry",  "ok");
+    }
+    
+    private HealthCheck testHealthCheck = new HealthCheck() {
+
+        @Override
+        public Result execute(ResultLog log) {
+            final Result result = new Result(this, log);
+            if(resultOk) {
+                log.debug("Nothing to report, result ok");
+            } else {
+                log.warn("Result is not ok!");
+            }
+            return result;
+        }
+
+        @Override
+        public Map<String, String> getInfo() {
+            return info;
+        }
+    };
+    
+    @Test
+    public void testBean() throws Exception {
+        final HealthCheckMBean mbean = new HealthCheckMBean(testHealthCheck);
+        final ObjectName name = new ObjectName(OBJECT_NAME);
+        jmxServer.registerMBean(mbean, name);
+        try {
+            JmxAttributeHealthCheckTest.assertJmxValue(OBJECT_NAME, "foo", "bar", true);
+            JmxAttributeHealthCheckTest.assertJmxValue(OBJECT_NAME, "Another entry", "ok", true);
+            
+            resultOk = true;
+            JmxAttributeHealthCheckTest.assertJmxValue(OBJECT_NAME, "ok", "true", true);
+            
+            resultOk = false;
+            JmxAttributeHealthCheckTest.assertJmxValue(OBJECT_NAME, "ok", "true", false);
+            
+            JmxAttributeHealthCheckTest.assertJmxValue(OBJECT_NAME, "log", "contains message=Result is not ok!", true);
+        } finally {
+            jmxServer.unregisterMBean(name);
+        }
+    }
+
+}
\ No newline at end of file

Propchange: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/HealthCheckMBeanTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/HealthCheckMBeanTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision Rev URL

Modified: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/JmxAttributeHealthCheckTest.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/JmxAttributeHealthCheckTest.java?rev=1508906&r1=1508905&r2=1508906&view=diff
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/JmxAttributeHealthCheckTest.java (original)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/JmxAttributeHealthCheckTest.java Wed Jul 31 15:09:20 2013
@@ -17,15 +17,13 @@
  */
 package org.apache.sling.hc.impl;
 
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.util.Dictionary;
 import java.util.Hashtable;
 
 import org.apache.sling.hc.api.Result;
 import org.apache.sling.hc.api.ResultLog;
-import org.junit.Before;
 import org.junit.Test;
 import org.mockito.Mockito;
 import org.osgi.service.component.ComponentContext;
@@ -33,38 +31,31 @@ import org.slf4j.LoggerFactory;
 
 public class JmxAttributeHealthCheckTest {
     
-    private JmxAttributeHealthCheck hc;
-    private ResultLog log;
-    private Dictionary<String, String> props;
-    private ComponentContext ctx;
-
-    @Before
-    public void setup() {
-        hc = new JmxAttributeHealthCheck();
-        log = new ResultLog(LoggerFactory.getLogger(getClass()));
+    static void assertJmxValue(String objectName, String attributeName, String constraint, boolean expected) {
+        final JmxAttributeHealthCheck hc = new JmxAttributeHealthCheck();
+        final ResultLog log = new ResultLog(LoggerFactory.getLogger(JmxAttributeHealthCheckTest.class));
         
-        ctx = Mockito.mock(ComponentContext.class);
-        props = new Hashtable<String, String>();
-        props.put(JmxAttributeHealthCheck.PROP_OBJECT_NAME, "java.lang:type=ClassLoading");
-        props.put(JmxAttributeHealthCheck.PROP_ATTRIBUTE_NAME, "LoadedClassCount");
+        final ComponentContext ctx = Mockito.mock(ComponentContext.class);
+        final Dictionary<String, String> props = new Hashtable<String, String>();
+        props.put(JmxAttributeHealthCheck.PROP_OBJECT_NAME, objectName);
+        props.put(JmxAttributeHealthCheck.PROP_ATTRIBUTE_NAME, attributeName);
+        props.put(JmxAttributeHealthCheck.PROP_CONSTRAINT, constraint);
         Mockito.when(ctx.getProperties()).thenReturn(props);
+        hc.activate(ctx);
+        
+        final Result r = hc.execute(log);
+        if(r.isOk() != expected) {
+            fail("HealthCheck result is " + r.isOk() + ", log=" + r.getLogEntries());
+        }
     }
     
     @Test
     public void testJmxAttributeMatch() {
-        props.put(JmxAttributeHealthCheck.PROP_CONSTRAINT, "> 10");
-        hc.activate(ctx);
-        final Result r = hc.execute(log);
-        assertTrue(r.isOk());
-        assertFalse(r.getLogEntries().isEmpty());
+        assertJmxValue("java.lang:type=ClassLoading", "LoadedClassCount", "> 10", true);
     }
     
     @Test
     public void testJmxAttributeNoMatch() {
-        props.put(JmxAttributeHealthCheck.PROP_CONSTRAINT, "< 10");
-        hc.activate(ctx);
-        final Result r = hc.execute(log);
-        assertFalse(r.isOk());
-        assertFalse(r.getLogEntries().isEmpty());
+        assertJmxValue("java.lang:type=ClassLoading", "LoadedClassCount", "< 10", false);
     }
 }

Modified: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/SimpleConstraintCheckerTest.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/SimpleConstraintCheckerTest.java?rev=1508906&r1=1508905&r2=1508906&view=diff
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/SimpleConstraintCheckerTest.java (original)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/test/java/org/apache/sling/hc/impl/SimpleConstraintCheckerTest.java Wed Jul 31 15:09:20 2013
@@ -149,4 +149,22 @@ public class SimpleConstraintCheckerTest
         checker.check("foo", "between 12 and 16", resultLog);
         assertFalse(result.isOk());
     }
+    
+    @Test
+    public void testContainsA() {
+        checker.check("This is a NICE STRING ok?", "contains NICE STRING", resultLog);
+        assertTrue(result.isOk());
+    }
+    
+    @Test
+    public void testContainsB() {
+        checker.check("This is a NICE TOUCH ok?", "contains NICE STRING", resultLog);
+        assertFalse(result.isOk());
+    }
+    
+    @Test
+    public void testContainsC() {
+        checker.check("This is a NICE TOUCH ok?", "contains NICE", resultLog);
+        assertTrue(result.isOk());
+    }
 }