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 155f3a4deb30bd8a23ac0cf989562bb0c02a4227 Author: Bertrand Delacretaz <[email protected]> AuthorDate: Tue Sep 3 12:25:48 2013 +0000 SLING-3034 - move 'core' HealthCheck services to api bundle git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/contrib/extensions/healthcheck/healthchecks@1519663 13f79535-47bb-0310-9956-ffa450edef68 --- pom.xml | 16 +- .../hc/healthchecks/impl/CompositeHealthCheck.java | 111 -------------- .../impl/DefaultLoginsHealthCheck.java | 2 +- .../healthchecks/impl/JmxAttributeHealthCheck.java | 99 ------------ .../hc/healthchecks/impl/JmxScriptBinding.java | 57 ------- .../hc/healthchecks/impl/OsgiScriptBinding.java | 67 --------- .../healthchecks/impl/ScriptableHealthCheck.java | 108 ------------- .../impl/SlingRequestStatusHealthCheck.java | 2 +- .../hc/healthchecks/util/FormattingResultLog.java | 49 ------ .../healthchecks/util/SimpleConstraintChecker.java | 80 ---------- .../sling/hc/healthchecks/util/package-info.java | 24 --- .../healthchecks/JmxAttributeHealthCheckTest.java | 56 ------- .../hc/healthchecks/JmxScriptBindingTest.java | 37 ----- .../hc/healthchecks/OsgiScriptBindingTest.java | 63 -------- .../hc/healthchecks/ScriptableHealthCheckTest.java | 94 ------------ .../util/SimpleConstraintCheckerTest.java | 167 --------------------- 16 files changed, 8 insertions(+), 1024 deletions(-) diff --git a/pom.xml b/pom.xml index a3c65a3..4594916 100644 --- a/pom.xml +++ b/pom.xml @@ -45,11 +45,12 @@ <dependencies> <dependency> <groupId>org.osgi</groupId> - <artifactId>org.osgi.core</artifactId> + <artifactId>org.osgi.compendium</artifactId> </dependency> <dependency> - <groupId>org.osgi</groupId> - <artifactId>org.osgi.compendium</artifactId> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.scr.annotations</artifactId> + <scope>provided</scope> </dependency> <dependency> <groupId>org.apache.sling</groupId> @@ -90,7 +91,7 @@ <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> </dependency> - <dependency> + <dependency> <groupId>javax.jcr</groupId> <artifactId>jcr</artifactId> </dependency> @@ -98,6 +99,7 @@ <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.6.2</version> + <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> @@ -109,11 +111,5 @@ <version>1.9.5</version> <scope>test</scope> </dependency> - <dependency> - <groupId>rhino</groupId> - <artifactId>js</artifactId> - <version>1.6R6</version> - <scope>test</scope> - </dependency> </dependencies> </project> 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 deleted file mode 100644 index 4996e44..0000000 --- a/src/main/java/org/apache/sling/hc/healthchecks/impl/CompositeHealthCheck.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * 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.healthchecks.impl; - -import java.util.Arrays; -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; -import org.apache.sling.hc.api.Result; -import org.apache.sling.hc.api.ResultLog; -import org.apache.sling.hc.healthchecks.util.FormattingResultLog; -import org.apache.sling.hc.util.HealthCheckFilter; -import org.osgi.framework.BundleContext; -import org.osgi.service.component.ComponentContext; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** {@link HealthCheck} that executes a number of other HealthChecks, - * selected by their tags, and merges their Results. - */ -@Component( - name="org.apache.sling.hc.CompositeHealthCheck", - configurationFactory=true, - policy=ConfigurationPolicy.REQUIRE, - metatype=true) -@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(unbounded=PropertyUnbounded.ARRAY) - private static final String PROP_FILTER_TAGS = "filter.tags"; - private String [] filterTags; - - @Activate - public void activate(ComponentContext ctx) { - bundleContext = ctx.getBundleContext(); - filterTags = PropertiesUtil.toStringArray(ctx.getProperties().get(PROP_FILTER_TAGS), new String[] {}); - log.info("Activated, will select HealthCheck having tags {}", Arrays.asList(filterTags)); - } - - @Override - public Result execute() { - final FormattingResultLog resultLog = new FormattingResultLog(); - final HealthCheckFilter filter = new HealthCheckFilter(bundleContext); - try { - final List<HealthCheck> checks = filter.getTaggedHealthChecks(filterTags); - if(checks.size() == 0) { - resultLog.warn("HealthCheckFilter returns no HealthCheck for tags {}", Arrays.asList(filterTags)); - return new Result(resultLog); - } - - int executed = 0; - resultLog.debug("Executing {} HealthCheck selected by the {} tags", checks.size(), Arrays.asList(filterTags)); - int failures = 0; - for(HealthCheck hc : checks) { - if(hc == this) { - resultLog.info("Cowardly forfeiting execution of this HealthCheck in an infinite loop, ignoring it"); - continue; - } - resultLog.debug("Executing {}", hc); - executed++; - final Result sub = hc.execute(); - if(!sub.isOk()) { - failures++; - } - for(ResultLog.Entry e : sub) { - resultLog.add(e); - } - } - - if(failures == 0) { - resultLog.debug("{} HealthCheck executed, all ok", executed); - } else { - resultLog.warn("{} HealthCheck executed, {} failures", executed, failures); - } - } finally { - filter.dispose(); - } - return new Result(resultLog); - } -} \ No newline at end of file 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 72dfbcf..e882da5 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 @@ -36,7 +36,7 @@ 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.util.FormattingResultLog; import org.apache.sling.jcr.api.SlingRepository; import org.osgi.service.component.ComponentContext; import org.slf4j.Logger; 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 deleted file mode 100644 index 305b305..0000000 --- a/src/main/java/org/apache/sling/hc/healthchecks/impl/JmxAttributeHealthCheck.java +++ /dev/null @@ -1,99 +0,0 @@ -/* - * 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.healthchecks.impl; - -import java.lang.management.ManagementFactory; -import java.util.Map; - -import javax.management.MBeanServer; -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.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** {@link HealthCheck} that checks a single JMX attribute */ -@Component( - name="org.apache.sling.hc.JmxAttributeHealthCheck", - configurationFactory=true, - policy=ConfigurationPolicy.REQUIRE, - metatype=true) -@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()); - private String mbeanName; - private String attributeName; - private String constraint; - - @Property - public static final String PROP_OBJECT_NAME = "mbean.name"; - - @Property - public static final String PROP_ATTRIBUTE_NAME = "attribute.name"; - - @Property - public static final String PROP_CONSTRAINT = "attribute.value.constraint"; - - @Activate - 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[] { properties.get(HealthCheck.NAME), mbeanName, attributeName, constraint }); - } - - @Override - public Result execute() { - final FormattingResultLog resultLog = new FormattingResultLog(); - resultLog.debug("Checking {} / {} with constraint {}", mbeanName, attributeName, constraint); - try { - final MBeanServer jmxServer = ManagementFactory.getPlatformMBeanServer(); - final ObjectName objectName = new ObjectName(mbeanName); - if(jmxServer.queryNames(objectName, null).size() == 0) { - resultLog.warn("MBean not found: {}", objectName); - } else { - final Object value = jmxServer.getAttribute(objectName, attributeName); - resultLog.debug("{} {} returns {}", mbeanName, attributeName, value); - new SimpleConstraintChecker().check(value, constraint, resultLog); - } - } catch(Exception e) { - log.warn("JMX attribute {}/{} check failed: {}", new Object []{ mbeanName, attributeName, e}); - resultLog.healthCheckError("JMX attribute check failed: {}", e); - } - return new Result(resultLog); - } -} diff --git a/src/main/java/org/apache/sling/hc/healthchecks/impl/JmxScriptBinding.java b/src/main/java/org/apache/sling/hc/healthchecks/impl/JmxScriptBinding.java deleted file mode 100644 index 463a38e..0000000 --- a/src/main/java/org/apache/sling/hc/healthchecks/impl/JmxScriptBinding.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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.healthchecks.impl; - -import java.lang.management.ManagementFactory; - -import javax.management.AttributeNotFoundException; -import javax.management.InstanceNotFoundException; -import javax.management.MBeanException; -import javax.management.MBeanServer; -import javax.management.MalformedObjectNameException; -import javax.management.ObjectName; -import javax.management.ReflectionException; - -import org.apache.sling.hc.healthchecks.util.FormattingResultLog; - -/** The JmxBinding is meant to be bound as "jmx" global variables - * in scripted rules, to allow for writing scripted expressions - * like jmx.attribute("java.lang:type=ClassLoading", "LoadedClassCount") > 100 - */ -public class JmxScriptBinding { - private MBeanServer jmxServer = ManagementFactory.getPlatformMBeanServer(); - private final FormattingResultLog resultLog; - - public JmxScriptBinding(FormattingResultLog resultLog) { - this.resultLog = resultLog; - } - - public Object attribute(String objectNameString, String attributeName) - throws MalformedObjectNameException, AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException { - final ObjectName name = new ObjectName(objectNameString); - if(jmxServer.queryNames(name, null).size() == 0) { - final String msg = "JMX object name not found: [" + objectNameString + "]"; - resultLog.warn(msg); - throw new IllegalStateException(msg); - } - resultLog.debug("Got JMX Object [{}]", name); - final Object value = jmxServer.getAttribute(name, attributeName); - resultLog.debug("JMX Object [{}] Attribute [{}] = [{}]", name, attributeName, value); - return value; - } -} \ No newline at end of file diff --git a/src/main/java/org/apache/sling/hc/healthchecks/impl/OsgiScriptBinding.java b/src/main/java/org/apache/sling/hc/healthchecks/impl/OsgiScriptBinding.java deleted file mode 100644 index a34f8c9..0000000 --- a/src/main/java/org/apache/sling/hc/healthchecks/impl/OsgiScriptBinding.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * 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.healthchecks.impl; - -import org.apache.sling.hc.healthchecks.util.FormattingResultLog; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; - -/** 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 FormattingResultLog resultLog; - private final BundleContext bundleContext; - - public OsgiScriptBinding(BundleContext ctx, FormattingResultLog resultLog) { - this.resultLog = resultLog; - this.bundleContext = ctx; - } - - public int inactiveBundlesCount() { - int count = 0; - for(Bundle b : bundleContext.getBundles()) { - if(!isActive(b)) { - count++; - } - } - resultLog.debug("inactiveBundlesCount={}", count); - return count; - } - - private boolean isActive(Bundle b) { - boolean active = true; - if(!isFragment(b) && Bundle.ACTIVE != b.getState()) { - active = false; - resultLog.info("Bundle {} is not active, state={} ({})", b.getSymbolicName(), b.getState(), b.getState()); - } - return active; - } - - private boolean isFragment(Bundle b) { - final String header = (String) b.getHeaders().get( Constants.FRAGMENT_HOST ); - if(header!= null && header.trim().length() > 0) { - resultLog.debug("{} is a fragment bundle, state won't be checked", b); - return true; - } else { - return false; - } - } -} \ No newline at end of file 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 deleted file mode 100644 index a53c5d3..0000000 --- a/src/main/java/org/apache/sling/hc/healthchecks/impl/ScriptableHealthCheck.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * 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.healthchecks.impl; -import javax.script.Bindings; -import javax.script.ScriptEngine; -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; -import org.apache.sling.hc.api.HealthCheck; -import org.apache.sling.hc.api.Result; -import org.apache.sling.hc.healthchecks.util.FormattingResultLog; -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( - name="org.apache.sling.hc.ScriptableHealthCheck", - configurationFactory=true, - policy=ConfigurationPolicy.REQUIRE, - metatype=true) -@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()); - private String expression; - private String languageExtension; - private BundleContext bundleContext; - - private static final String DEFAULT_LANGUAGE_EXTENSION = "ecma"; - - @Property - public static final String PROP_EXPRESSION = "expression"; - - @Property(value=DEFAULT_LANGUAGE_EXTENSION) - public static final String PROP_LANGUAGE_EXTENSION = "language.extension"; - - @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); - - log.debug("Activated scriptable health check name={}, languageExtension={}, expression={}", - new Object[] {ctx.getProperties().get(HealthCheck.NAME), - languageExtension, expression}); - } - - @Override - public Result execute() { - final FormattingResultLog resultLog = new FormattingResultLog(); - resultLog.debug("Checking expression [{}], language extension=[{}]", expression, languageExtension); - try { - final ScriptEngine engine = scriptEngineManager.getEngineByExtension(languageExtension); - if (engine == null) { - resultLog.healthCheckError("No ScriptEngine available for extension {}", languageExtension); - } else { - // TODO pluggable Bindings? Reuse the Sling bindings providers? - final Bindings b = engine.createBindings(); - 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().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 (final Exception e) { - resultLog.healthCheckError( - "Exception while evaluating expression [{}] with language extension [{}]: {}", - expression, languageExtension, e); - } - return new Result(resultLog); - } -} \ No newline at end of file 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 f27c524..682f412 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 @@ -35,7 +35,7 @@ import org.apache.sling.commons.osgi.PropertiesUtil; import org.apache.sling.engine.SlingRequestProcessor; 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.util.FormattingResultLog; import org.osgi.service.component.ComponentContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/src/main/java/org/apache/sling/hc/healthchecks/util/FormattingResultLog.java b/src/main/java/org/apache/sling/hc/healthchecks/util/FormattingResultLog.java deleted file mode 100644 index 6933f08..0000000 --- a/src/main/java/org/apache/sling/hc/healthchecks/util/FormattingResultLog.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * 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.healthchecks.util; -import org.apache.sling.hc.api.Result; -import org.apache.sling.hc.api.ResultLog; -import org.slf4j.helpers.MessageFormatter; - -/** Utility that provides a logging-like facade on a ResultLog */ -public class FormattingResultLog extends ResultLog { - - private ResultLog.Entry createEntry(Result.Status status, String format, Object ... args) { - return new ResultLog.Entry(status, MessageFormatter.arrayFormat(format, args).getMessage()); - } - - public void debug(String format, Object ... args) { - add(createEntry(Result.Status.DEBUG, format, args)); - } - - public void info(String format, Object ... args) { - add(createEntry(Result.Status.INFO, format, args)); - } - - public void warn(String format, Object ... args) { - add(createEntry(Result.Status.WARN, format, args)); - } - - public void critical(String format, Object ... args) { - add(createEntry(Result.Status.CRITICAL, format, args)); - } - - public void healthCheckError(String format, Object ... args) { - add(createEntry(Result.Status.HEALTH_CHECK_ERROR, format, args)); - } -} \ No newline at end of file diff --git a/src/main/java/org/apache/sling/hc/healthchecks/util/SimpleConstraintChecker.java b/src/main/java/org/apache/sling/hc/healthchecks/util/SimpleConstraintChecker.java deleted file mode 100644 index 13c3b23..0000000 --- a/src/main/java/org/apache/sling/hc/healthchecks/util/SimpleConstraintChecker.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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.healthchecks.util; - -import org.apache.sling.hc.api.Result; -import org.apache.sling.hc.api.ResultLog; - -/** Simple check of numeric values against expressions - * like < N, > N, between two values etc. - * See {@link SimpleConstraintCheckerTest} for examples. - */ -public class SimpleConstraintChecker { - - public static final String CONTAINS = "contains"; - - /** Check value against expression and report to result */ - public void check(Object inputValue, String constraint, ResultLog resultLog) { - - final String stringValue = inputValue == null ? "" : inputValue.toString(); - - if(constraint == null || constraint.trim().length() == 0) { - throw new IllegalArgumentException("Empty constraint, cannot evaluate"); - } - - final String [] parts = constraint.split(" "); - boolean matches = false; - try { - if(constraint.startsWith(">") && parts.length == 2) { - final int value = Integer.valueOf(stringValue).intValue(); - matches = value > Integer.valueOf(parts[1]); - - } else if(constraint.startsWith("<") && parts.length == 2) { - final int value = Integer.valueOf(stringValue).intValue(); - matches = value < Integer.valueOf(parts[1]); - - } else if(parts.length == 4 && "between".equalsIgnoreCase(parts[0]) && "and".equalsIgnoreCase(parts[2]) ) { - final int value = Integer.valueOf(stringValue).intValue(); - final int lowerBound = Integer.valueOf(parts[1]); - 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); - } - } catch(NumberFormatException nfe) { - resultLog.add(new ResultLog.Entry( - Result.Status.WARN, - "Invalid numeric value [" + inputValue + "] while evaluating " + constraint)); - } - - if(matches) { - resultLog.add(new ResultLog.Entry( - Result.Status.DEBUG, - "Value [" + inputValue + "] matches constraint [" + constraint + "]")); - } else { - resultLog.add(new ResultLog.Entry( - Result.Status.WARN, - "Value [" + inputValue + "] does not match constraint [" + constraint + "]")); - } - } -} \ No newline at end of file diff --git a/src/main/java/org/apache/sling/hc/healthchecks/util/package-info.java b/src/main/java/org/apache/sling/hc/healthchecks/util/package-info.java deleted file mode 100644 index 73b5889..0000000 --- a/src/main/java/org/apache/sling/hc/healthchecks/util/package-info.java +++ /dev/null @@ -1,24 +0,0 @@ -/* - * 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. - */ - -@Version("1.0.0") -package org.apache.sling.hc.healthchecks.util; - -import aQute.bnd.annotation.Version; - diff --git a/src/test/java/org/apache/sling/hc/healthchecks/JmxAttributeHealthCheckTest.java b/src/test/java/org/apache/sling/hc/healthchecks/JmxAttributeHealthCheckTest.java deleted file mode 100644 index a0d93ff..0000000 --- a/src/test/java/org/apache/sling/hc/healthchecks/JmxAttributeHealthCheckTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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.healthchecks; - -import static org.junit.Assert.assertEquals; - -import java.util.HashMap; -import java.util.Map; - -import org.apache.sling.hc.api.Result; -import org.apache.sling.hc.healthchecks.impl.JmxAttributeHealthCheck; -import org.junit.Test; -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 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); - 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); - } -} diff --git a/src/test/java/org/apache/sling/hc/healthchecks/JmxScriptBindingTest.java b/src/test/java/org/apache/sling/hc/healthchecks/JmxScriptBindingTest.java deleted file mode 100644 index 8886c11..0000000 --- a/src/test/java/org/apache/sling/hc/healthchecks/JmxScriptBindingTest.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * 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.healthchecks; - -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; - -import org.apache.sling.hc.healthchecks.impl.JmxScriptBinding; -import org.apache.sling.hc.healthchecks.util.FormattingResultLog; -import org.junit.Test; - -public class JmxScriptBindingTest { - - @Test - public void testJmxAttribute() throws Exception { - final FormattingResultLog resultLog = new FormattingResultLog(); - final JmxScriptBinding b = new JmxScriptBinding(resultLog); - final Object value= b.attribute("java.lang:type=ClassLoading", "LoadedClassCount"); - assertNotNull("Expecting non-null attribute value", value); - assertTrue("Expecting non-empty value", value.toString().length() > 0); - } -} \ No newline at end of file diff --git a/src/test/java/org/apache/sling/hc/healthchecks/OsgiScriptBindingTest.java b/src/test/java/org/apache/sling/hc/healthchecks/OsgiScriptBindingTest.java deleted file mode 100644 index 731c7d9..0000000 --- a/src/test/java/org/apache/sling/hc/healthchecks/OsgiScriptBindingTest.java +++ /dev/null @@ -1,63 +0,0 @@ -/* - * 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.healthchecks; - -import static org.junit.Assert.assertEquals; - -import java.util.Dictionary; -import java.util.Hashtable; - -import org.apache.sling.hc.healthchecks.impl.OsgiScriptBinding; -import org.apache.sling.hc.healthchecks.util.FormattingResultLog; -import org.junit.Test; -import org.mockito.Mockito; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; - -public class OsgiScriptBindingTest { - - private Bundle mockBundle(boolean isFragment, boolean isActive) { - final Bundle b = Mockito.mock(Bundle.class); - Mockito.when(b.getState()).thenReturn(isActive ? Bundle.ACTIVE : Bundle.RESOLVED); - - final Dictionary<String, String> headers = new Hashtable<String, String>(); - if(isFragment) { - headers.put(Constants.FRAGMENT_HOST, "FOO"); - } - Mockito.when(b.getHeaders()).thenReturn(headers); - - return b; - } - - @Test - public void testInactiveBundles() throws Exception { - final BundleContext ctx = Mockito.mock(BundleContext.class); - final Bundle [] bundles = { - mockBundle(false, true), - mockBundle(false, false), - mockBundle(false, true), - mockBundle(true, false) - }; - Mockito.when(ctx.getBundles()).thenReturn(bundles); - - final FormattingResultLog resultLog = new FormattingResultLog(); - final OsgiScriptBinding b = new OsgiScriptBinding(ctx, resultLog); - assertEquals(1, b.inactiveBundlesCount()); - } -} \ No newline at end of file diff --git a/src/test/java/org/apache/sling/hc/healthchecks/ScriptableHealthCheckTest.java b/src/test/java/org/apache/sling/hc/healthchecks/ScriptableHealthCheckTest.java deleted file mode 100644 index 351b88f..0000000 --- a/src/test/java/org/apache/sling/hc/healthchecks/ScriptableHealthCheckTest.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * 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.healthchecks; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import java.lang.reflect.Field; -import java.util.Dictionary; -import java.util.Hashtable; - -import javax.script.ScriptEngine; -import javax.script.ScriptEngineManager; - -import org.apache.sling.hc.api.Result; -import org.apache.sling.hc.healthchecks.impl.ScriptableHealthCheck; -import org.junit.Test; -import org.mockito.Matchers; -import org.mockito.Mockito; -import org.osgi.service.component.ComponentContext; - -public class ScriptableHealthCheckTest { - - private ScriptableHealthCheck hc; - private Dictionary<String, String> props; - private ComponentContext ctx; - - private void assertExpression(String expression, String languageExtension, boolean expected) throws Exception { - hc = new ScriptableHealthCheck(); - ctx = Mockito.mock(ComponentContext.class); - props = new Hashtable<String, String>(); - - final ScriptEngine rhino = new ScriptEngineManager().getEngineByExtension("js"); - assertNotNull("With the rhino jar in our classpath, we should get a js script engine", rhino); - final ScriptEngineManager manager = Mockito.mock(ScriptEngineManager.class); - Mockito.when(manager.getEngineByExtension(Matchers.same("ecma"))).thenReturn(rhino); - final Field f = hc.getClass().getDeclaredField("scriptEngineManager"); - f.setAccessible(true); - f.set(hc, manager); - - props.put(ScriptableHealthCheck.PROP_EXPRESSION, expression); - if(languageExtension != null) { - props.put(ScriptableHealthCheck.PROP_LANGUAGE_EXTENSION, languageExtension); - } - Mockito.when(ctx.getProperties()).thenReturn(props); - hc.activate(ctx); - final Result r = hc.execute(); - assertEquals("Expecting result " + expected, expected, r.isOk()); - } - - @Test - public void testSimpleExpression() throws Exception { - assertExpression("2 + 3 == 5", null, true); - } - - @Test - public void testJmxExpression() throws Exception { - assertExpression( - "jmx.attribute('java.lang:type=ClassLoading', 'LoadedClassCount') > 10" - + " && jmx.attribute('java.lang:type=Runtime', 'ManagementSpecVersion') > 1", - "ecma", true); - } - - @Test - public void testFalseExpression() throws Exception { - assertExpression("2 + 3 == 15", null, false); - } - - @Test - public void testSyntaxError() throws Exception { - assertExpression("{not [valid ok?", null, false); - } - - @Test - public void testNoEngine() throws Exception { - assertExpression("2 + 3 == 5", null, true); - assertExpression("2 + 3 == 5", "groovy", false); - } -} diff --git a/src/test/java/org/apache/sling/hc/healthchecks/util/SimpleConstraintCheckerTest.java b/src/test/java/org/apache/sling/hc/healthchecks/util/SimpleConstraintCheckerTest.java deleted file mode 100644 index d981876..0000000 --- a/src/test/java/org/apache/sling/hc/healthchecks/util/SimpleConstraintCheckerTest.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * 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.healthchecks.util; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import org.apache.sling.hc.api.Result; -import org.apache.sling.hc.api.ResultLog; -import org.apache.sling.hc.healthchecks.util.SimpleConstraintChecker; -import org.junit.Before; -import org.junit.Test; - -public class SimpleConstraintCheckerTest { - private ResultLog resultLog; - - private final SimpleConstraintChecker checker = new SimpleConstraintChecker(); - - @Before - public void setup() { - resultLog = new ResultLog(); - } - - @Test - public void testStringEquals() { - final String s = "test_" + System.currentTimeMillis(); - checker.check(s, s, resultLog); - assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); - } - - @Test - public void testStringNotEquals() { - final String s = "test_" + System.currentTimeMillis(); - checker.check(s, "something else", resultLog); - assertFalse(resultLog.getAggregateStatus().equals(Result.Status.OK)); - } - - @Test - public void testFiveEquals() { - final String s = "5"; - checker.check(s, s, resultLog); - assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); - } - - @Test - public void testIntTwelveEquals() { - checker.check(12, "12", resultLog); - assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); - } - - @Test - public void testIntTwelveGreaterThan() { - checker.check(12, "> 11", resultLog); - assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); - } - - @Test - public void testFiveNotEquals() { - checker.check("5", "foo", resultLog); - assertFalse(resultLog.getAggregateStatus().equals(Result.Status.OK)); - } - - @Test - public void testNullNotEquals() { - checker.check(null, "foo", resultLog); - assertFalse(resultLog.getAggregateStatus().equals(Result.Status.OK)); - } - - @Test - public void testNullNotGreater() { - checker.check(null, "> 2", resultLog); - assertFalse(resultLog.getAggregateStatus().equals(Result.Status.OK)); - } - - @Test - public void testGreaterThanTrue() { - checker.check("5", "> 2", resultLog); - assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); - } - - @Test - public void testGreaterThanFalse() { - checker.check("5", "> 12", resultLog); - assertFalse(resultLog.getAggregateStatus().equals(Result.Status.OK)); - } - - @Test - public void testLessThanTrue() { - checker.check("5", "< 12", resultLog); - assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); - } - - @Test - public void testLessThanFalse() { - checker.check("5", "< 2", resultLog); - assertFalse(resultLog.getAggregateStatus().equals(Result.Status.OK)); - } - - @Test - public void testBetweenA() { - checker.check("5", "between 2 and 6", resultLog); - assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); - } - - @Test - public void testBetweenB() { - checker.check("5", "between 12 and 16", resultLog); - assertFalse(resultLog.getAggregateStatus().equals(Result.Status.OK)); - } - - @Test - public void testBetweenC() { - checker.check(5L, "between 12 and 16", resultLog); - assertFalse(resultLog.getAggregateStatus().equals(Result.Status.OK)); - } - - @Test - public void testBetweenD() { - checker.check(5L, "between 4 and 16", resultLog); - assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); - } - - @Test - public void testBetweenE() { - checker.check(5L, "betWEEN 4 aND 16", resultLog); - assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); - } - - @Test - public void testNotAnInteger() { - checker.check("foo", "between 12 and 16", resultLog); - assertFalse(resultLog.getAggregateStatus().equals(Result.Status.OK)); - } - - @Test - public void testContainsA() { - checker.check("This is a NICE STRING ok?", "contains NICE STRING", resultLog); - assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); - } - - @Test - public void testContainsB() { - checker.check("This is a NICE TOUCH ok?", "contains NICE STRING", resultLog); - assertFalse(resultLog.getAggregateStatus().equals(Result.Status.OK)); - } - - @Test - public void testContainsC() { - checker.check("This is a NICE TOUCH ok?", "contains NICE", resultLog); - assertTrue(resultLog.getAggregateStatus().equals(Result.Status.OK)); - } -} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
