This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.hc.support-1.0.4 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-hc-support.git
commit e3ba14d0efb5a10eaeb46c641420b7b6c634c57f Author: Carsten Ziegeler <[email protected]> AuthorDate: Fri Aug 30 09:34:59 2013 +0000 A little code cleanup of annotations git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/healthcheck/healthchecks@1518922 13f79535-47bb-0310-9956-ffa450edef68 --- .../hc/healthchecks/impl/CompositeHealthCheck.java | 19 +++++++------ .../impl/DefaultLoginsHealthCheck.java | 23 ++++++++-------- .../healthchecks/impl/JmxAttributeHealthCheck.java | 30 ++++++++++---------- .../healthchecks/impl/ScriptableHealthCheck.java | 32 +++++++++++----------- .../impl/SlingRequestStatusHealthCheck.java | 22 +++++++-------- .../healthchecks/JmxAttributeHealthCheckTest.java | 19 ++++++------- 6 files changed, 70 insertions(+), 75 deletions(-) diff --git a/src/main/java/org/apache/sling/hc/healthchecks/impl/CompositeHealthCheck.java b/src/main/java/org/apache/sling/hc/healthchecks/impl/CompositeHealthCheck.java index 65f400f..57a4a63 100644 --- a/src/main/java/org/apache/sling/hc/healthchecks/impl/CompositeHealthCheck.java +++ b/src/main/java/org/apache/sling/hc/healthchecks/impl/CompositeHealthCheck.java @@ -23,7 +23,9 @@ import java.util.List; import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.ConfigurationPolicy; +import org.apache.felix.scr.annotations.Properties; import org.apache.felix.scr.annotations.Property; +import org.apache.felix.scr.annotations.PropertyUnbounded; import org.apache.felix.scr.annotations.Service; import org.apache.sling.commons.osgi.PropertiesUtil; import org.apache.sling.hc.api.HealthCheck; @@ -44,22 +46,21 @@ import org.slf4j.LoggerFactory; configurationFactory=true, policy=ConfigurationPolicy.REQUIRE, metatype=true) -@Service +@Properties({ + @Property(name=HealthCheck.NAME), + @Property(name=HealthCheck.TAGS, unbounded=PropertyUnbounded.ARRAY), + @Property(name=HealthCheck.MBEAN_NAME) +}) +@Service(value=HealthCheck.class) public class CompositeHealthCheck implements HealthCheck { private final Logger log = LoggerFactory.getLogger(getClass()); private BundleContext bundleContext; - @Property(cardinality=50) - public static final String PROP_TAGS = HealthCheck.TAGS; - - @Property(cardinality=50) - public static final String PROP_FILTER_TAGS = "filter.tags"; + @Property(unbounded=PropertyUnbounded.ARRAY) + private static final String PROP_FILTER_TAGS = "filter.tags"; private String [] filterTags; - @Property - public static final String PROP_MBEAN_NAME = HealthCheck.MBEAN_NAME; - @Activate public void activate(ComponentContext ctx) { bundleContext = ctx.getBundleContext(); diff --git a/src/main/java/org/apache/sling/hc/healthchecks/impl/DefaultLoginsHealthCheck.java b/src/main/java/org/apache/sling/hc/healthchecks/impl/DefaultLoginsHealthCheck.java index 781e76d..72dfbcf 100644 --- a/src/main/java/org/apache/sling/hc/healthchecks/impl/DefaultLoginsHealthCheck.java +++ b/src/main/java/org/apache/sling/hc/healthchecks/impl/DefaultLoginsHealthCheck.java @@ -28,7 +28,9 @@ import javax.jcr.SimpleCredentials; import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.ConfigurationPolicy; +import org.apache.felix.scr.annotations.Properties; import org.apache.felix.scr.annotations.Property; +import org.apache.felix.scr.annotations.PropertyUnbounded; import org.apache.felix.scr.annotations.Reference; import org.apache.felix.scr.annotations.Service; import org.apache.sling.commons.osgi.PropertiesUtil; @@ -47,23 +49,20 @@ import org.slf4j.LoggerFactory; configurationFactory=true, policy=ConfigurationPolicy.REQUIRE, metatype=true) -@Service +@Properties({ + @Property(name=HealthCheck.NAME), + @Property(name=HealthCheck.TAGS, unbounded=PropertyUnbounded.ARRAY), + @Property(name=HealthCheck.MBEAN_NAME) +}) +@Service(value=HealthCheck.class) public class DefaultLoginsHealthCheck implements HealthCheck { private final Logger log = LoggerFactory.getLogger(getClass()); - @Property(cardinality=500) - public static final String PROP_LOGINS = "logins"; - private List<String> logins; - - @Property(cardinality=50) - public static final String PROP_TAGS = HealthCheck.TAGS; + @Property(unbounded=PropertyUnbounded.ARRAY) + private static final String PROP_LOGINS = "logins"; - @Property - public static final String PROP_NAME = HealthCheck.NAME; - - @Property - public static final String PROP_MBEAN_NAME = HealthCheck.MBEAN_NAME; + private List<String> logins; @Reference private SlingRepository repository; diff --git a/src/main/java/org/apache/sling/hc/healthchecks/impl/JmxAttributeHealthCheck.java b/src/main/java/org/apache/sling/hc/healthchecks/impl/JmxAttributeHealthCheck.java index cc67845..305b305 100644 --- a/src/main/java/org/apache/sling/hc/healthchecks/impl/JmxAttributeHealthCheck.java +++ b/src/main/java/org/apache/sling/hc/healthchecks/impl/JmxAttributeHealthCheck.java @@ -18,6 +18,7 @@ package org.apache.sling.hc.healthchecks.impl; import java.lang.management.ManagementFactory; +import java.util.Map; import javax.management.MBeanServer; import javax.management.ObjectName; @@ -25,14 +26,15 @@ import javax.management.ObjectName; import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.ConfigurationPolicy; +import org.apache.felix.scr.annotations.Properties; import org.apache.felix.scr.annotations.Property; +import org.apache.felix.scr.annotations.PropertyUnbounded; import org.apache.felix.scr.annotations.Service; import org.apache.sling.commons.osgi.PropertiesUtil; import org.apache.sling.hc.api.HealthCheck; import org.apache.sling.hc.api.Result; import org.apache.sling.hc.healthchecks.util.FormattingResultLog; import org.apache.sling.hc.healthchecks.util.SimpleConstraintChecker; -import org.osgi.service.component.ComponentContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -42,7 +44,12 @@ import org.slf4j.LoggerFactory; configurationFactory=true, policy=ConfigurationPolicy.REQUIRE, metatype=true) -@Service +@Properties({ + @Property(name=HealthCheck.NAME), + @Property(name=HealthCheck.TAGS, unbounded=PropertyUnbounded.ARRAY), + @Property(name=HealthCheck.MBEAN_NAME) +}) +@Service(value=HealthCheck.class) public class JmxAttributeHealthCheck implements HealthCheck { private final Logger log = LoggerFactory.getLogger(getClass()); @@ -59,23 +66,14 @@ public class JmxAttributeHealthCheck implements HealthCheck { @Property public static final String PROP_CONSTRAINT = "attribute.value.constraint"; - @Property(cardinality=50) - public static final String PROP_TAGS = HealthCheck.TAGS; - - @Property - public static final String PROP_NAME = HealthCheck.NAME; - - @Property - public static final String PROP_MBEAN_NAME = HealthCheck.MBEAN_NAME; - @Activate - public void activate(ComponentContext ctx) { - mbeanName = PropertiesUtil.toString(ctx.getProperties().get(PROP_OBJECT_NAME), ""); - attributeName = PropertiesUtil.toString(ctx.getProperties().get(PROP_ATTRIBUTE_NAME), ""); - constraint = PropertiesUtil.toString(ctx.getProperties().get(PROP_CONSTRAINT), ""); + public void activate(final Map<String, Object> properties) { + mbeanName = PropertiesUtil.toString(properties.get(PROP_OBJECT_NAME), ""); + attributeName = PropertiesUtil.toString(properties.get(PROP_ATTRIBUTE_NAME), ""); + constraint = PropertiesUtil.toString(properties.get(PROP_CONSTRAINT), ""); log.info("Activated with HealthCheck name={}, objectName={}, attribute={}, constraint={}", - new Object[] { ctx.getProperties().get(HealthCheck.NAME), mbeanName, attributeName, constraint }); + new Object[] { properties.get(HealthCheck.NAME), mbeanName, attributeName, constraint }); } @Override diff --git a/src/main/java/org/apache/sling/hc/healthchecks/impl/ScriptableHealthCheck.java b/src/main/java/org/apache/sling/hc/healthchecks/impl/ScriptableHealthCheck.java index 1f0ba8b..a53c5d3 100644 --- a/src/main/java/org/apache/sling/hc/healthchecks/impl/ScriptableHealthCheck.java +++ b/src/main/java/org/apache/sling/hc/healthchecks/impl/ScriptableHealthCheck.java @@ -23,7 +23,9 @@ import javax.script.ScriptEngineManager; import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.ConfigurationPolicy; +import org.apache.felix.scr.annotations.Properties; import org.apache.felix.scr.annotations.Property; +import org.apache.felix.scr.annotations.PropertyUnbounded; import org.apache.felix.scr.annotations.Reference; import org.apache.felix.scr.annotations.Service; import org.apache.sling.commons.osgi.PropertiesUtil; @@ -41,7 +43,12 @@ import org.slf4j.LoggerFactory; configurationFactory=true, policy=ConfigurationPolicy.REQUIRE, metatype=true) -@Service +@Properties({ + @Property(name=HealthCheck.NAME), + @Property(name=HealthCheck.TAGS, unbounded=PropertyUnbounded.ARRAY), + @Property(name=HealthCheck.MBEAN_NAME) +}) +@Service(value=HealthCheck.class) public class ScriptableHealthCheck implements HealthCheck { private final Logger log = LoggerFactory.getLogger(getClass()); @@ -49,23 +56,14 @@ public class ScriptableHealthCheck implements HealthCheck { private String languageExtension; private BundleContext bundleContext; - public static final String DEFAULT_LANGUAGE_EXTENSION = "ecma"; + private static final String DEFAULT_LANGUAGE_EXTENSION = "ecma"; @Property public static final String PROP_EXPRESSION = "expression"; - @Property + @Property(value=DEFAULT_LANGUAGE_EXTENSION) public static final String PROP_LANGUAGE_EXTENSION = "language.extension"; - @Property(cardinality=50) - public static final String PROP_TAGS = HealthCheck.TAGS; - - @Property - public static final String PROP_NAME = HealthCheck.NAME; - - @Property - public static final String PROP_MBEAN_NAME = HealthCheck.MBEAN_NAME; - @Reference private ScriptEngineManager scriptEngineManager; @@ -75,7 +73,9 @@ public class ScriptableHealthCheck implements HealthCheck { expression = PropertiesUtil.toString(ctx.getProperties().get(PROP_EXPRESSION), ""); languageExtension = PropertiesUtil.toString(ctx.getProperties().get(PROP_LANGUAGE_EXTENSION), DEFAULT_LANGUAGE_EXTENSION); - log.info("Activated, name={}, languageExtension={}, expression={}", languageExtension, expression); + log.debug("Activated scriptable health check name={}, languageExtension={}, expression={}", + new Object[] {ctx.getProperties().get(HealthCheck.NAME), + languageExtension, expression}); } @Override @@ -84,7 +84,7 @@ public class ScriptableHealthCheck implements HealthCheck { resultLog.debug("Checking expression [{}], language extension=[{}]", expression, languageExtension); try { final ScriptEngine engine = scriptEngineManager.getEngineByExtension(languageExtension); - if(engine == null) { + if (engine == null) { resultLog.healthCheckError("No ScriptEngine available for extension {}", languageExtension); } else { // TODO pluggable Bindings? Reuse the Sling bindings providers? @@ -92,13 +92,13 @@ public class ScriptableHealthCheck implements HealthCheck { b.put("jmx", new JmxScriptBinding(resultLog)); b.put("osgi", new OsgiScriptBinding(bundleContext, resultLog)); final Object value = engine.eval(expression, b); - if(value!=null && "true".equals(value.toString())) { + if(value!=null && "true".equals(value.toString().toLowerCase())) { resultLog.debug("Expression [{}] evaluates to true as expected", expression); } else { resultLog.warn("Expression [{}] does not evaluate to true as expected, value=[{}]", expression, value); } } - } catch(Exception e) { + } catch (final Exception e) { resultLog.healthCheckError( "Exception while evaluating expression [{}] with language extension [{}]: {}", expression, languageExtension, e); diff --git a/src/main/java/org/apache/sling/hc/healthchecks/impl/SlingRequestStatusHealthCheck.java b/src/main/java/org/apache/sling/hc/healthchecks/impl/SlingRequestStatusHealthCheck.java index ab22c8e..74c9e4b 100644 --- a/src/main/java/org/apache/sling/hc/healthchecks/impl/SlingRequestStatusHealthCheck.java +++ b/src/main/java/org/apache/sling/hc/healthchecks/impl/SlingRequestStatusHealthCheck.java @@ -24,7 +24,9 @@ import javax.servlet.http.HttpServletRequest; import org.apache.felix.scr.annotations.Activate; import org.apache.felix.scr.annotations.Component; import org.apache.felix.scr.annotations.ConfigurationPolicy; +import org.apache.felix.scr.annotations.Properties; import org.apache.felix.scr.annotations.Property; +import org.apache.felix.scr.annotations.PropertyUnbounded; import org.apache.felix.scr.annotations.Reference; import org.apache.felix.scr.annotations.Service; import org.apache.sling.api.resource.ResourceResolver; @@ -44,7 +46,12 @@ import org.slf4j.LoggerFactory; configurationFactory=true, policy=ConfigurationPolicy.REQUIRE, metatype=true) -@Service +@Properties({ + @Property(name=HealthCheck.NAME), + @Property(name=HealthCheck.TAGS, unbounded=PropertyUnbounded.ARRAY), + @Property(name=HealthCheck.MBEAN_NAME) +}) +@Service(value=HealthCheck.class) public class SlingRequestStatusHealthCheck implements HealthCheck { private static final Logger log = LoggerFactory.getLogger(SlingRequestStatusHealthCheck.class); @@ -70,17 +77,8 @@ public class SlingRequestStatusHealthCheck implements HealthCheck { } } - @Property(cardinality=Integer.MAX_VALUE) - public static final String PROP_PATH = "path"; - - @Property(cardinality=50) - public static final String PROP_TAGS = HealthCheck.TAGS; - - @Property - public static final String PROP_NAME = HealthCheck.NAME; - - @Property - public static final String PROP_MBEAN_NAME = HealthCheck.MBEAN_NAME; + @Property(unbounded=PropertyUnbounded.ARRAY) + private static final String PROP_PATH = "path"; @Reference private SlingRequestProcessor requestProcessor; diff --git a/src/test/java/org/apache/sling/hc/healthchecks/JmxAttributeHealthCheckTest.java b/src/test/java/org/apache/sling/hc/healthchecks/JmxAttributeHealthCheckTest.java index 81f68fc..a0d93ff 100644 --- a/src/test/java/org/apache/sling/hc/healthchecks/JmxAttributeHealthCheckTest.java +++ b/src/test/java/org/apache/sling/hc/healthchecks/JmxAttributeHealthCheckTest.java @@ -19,8 +19,8 @@ package org.apache.sling.hc.healthchecks; import static org.junit.Assert.assertEquals; -import java.util.Dictionary; -import java.util.Hashtable; +import java.util.HashMap; +import java.util.Map; import org.apache.sling.hc.api.Result; import org.apache.sling.hc.healthchecks.impl.JmxAttributeHealthCheck; @@ -29,27 +29,26 @@ import org.mockito.Mockito; import org.osgi.service.component.ComponentContext; public class JmxAttributeHealthCheckTest { - + static void assertJmxValue(String objectName, String attributeName, String constraint, boolean expected) { final JmxAttributeHealthCheck hc = new JmxAttributeHealthCheck(); - + final ComponentContext ctx = Mockito.mock(ComponentContext.class); - final Dictionary<String, String> props = new Hashtable<String, String>(); + final Map<String, Object> props = new HashMap<String, Object>(); 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); - + hc.activate(props); + final Result r = hc.execute(); assertEquals("Expected result " + expected, expected, r.isOk()); } - + @Test public void testJmxAttributeMatch() { assertJmxValue("java.lang:type=ClassLoading", "LoadedClassCount", "> 10", true); } - + @Test public void testJmxAttributeNoMatch() { assertJmxValue("java.lang:type=ClassLoading", "LoadedClassCount", "< 10", false); -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
