You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by gh...@apache.org on 2018/12/18 22:58:17 UTC
svn commit: r1849246 [1/6] - in /felix/trunk/healthcheck: ./ api/ api/src/
api/src/main/ api/src/main/java/ api/src/main/java/org/
api/src/main/java/org/apache/ api/src/main/java/org/apache/felix/
api/src/main/java/org/apache/felix/hc/ api/src/main/jav...
Author: ghenzler
Date: Tue Dec 18 22:58:15 2018
New Revision: 1849246
URL: http://svn.apache.org/viewvc?rev=1849246&view=rev
Log:
FELIX-5952 Initial commit of Felix Health Checks (as contributed by sister project Apache Sling)
Added:
felix/trunk/healthcheck/
felix/trunk/healthcheck/api/
felix/trunk/healthcheck/api/bnd.bnd
felix/trunk/healthcheck/api/pom.xml
felix/trunk/healthcheck/api/src/
felix/trunk/healthcheck/api/src/main/
felix/trunk/healthcheck/api/src/main/java/
felix/trunk/healthcheck/api/src/main/java/org/
felix/trunk/healthcheck/api/src/main/java/org/apache/
felix/trunk/healthcheck/api/src/main/java/org/apache/felix/
felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/
felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/
felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/HealthCheck.java
felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/Result.java
felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/ResultLog.java
felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/
felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/HealthCheckExecutionOptions.java
felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/HealthCheckExecutionResult.java
felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/HealthCheckExecutor.java
felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/HealthCheckSelector.java
felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/package-info.java
felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/package-info.java
felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/
felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/FormattingResultLog.java
felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/HealthCheckFilter.java
felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/HealthCheckMetadata.java
felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/SimpleConstraintChecker.java
felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/package-info.java
felix/trunk/healthcheck/api/src/test/
felix/trunk/healthcheck/api/src/test/java/
felix/trunk/healthcheck/api/src/test/java/org/
felix/trunk/healthcheck/api/src/test/java/org/apache/
felix/trunk/healthcheck/api/src/test/java/org/apache/felix/
felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/
felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/api/
felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/api/ResultLogTest.java
felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/api/ResultTest.java
felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/util/
felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/util/HealthCheckFilterTest.java
felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/util/SimpleConstraintCheckerTest.java
felix/trunk/healthcheck/core/
felix/trunk/healthcheck/core/bnd.bnd
felix/trunk/healthcheck/core/pom.xml
felix/trunk/healthcheck/core/src/
felix/trunk/healthcheck/core/src/main/
felix/trunk/healthcheck/core/src/main/java/
felix/trunk/healthcheck/core/src/main/java/org/
felix/trunk/healthcheck/core/src/main/java/org/apache/
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/CompositeHealthCheck.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/CompositeHealthCheckConfiguration.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/CompositeResult.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/JmxAdjustableStatusHealthCheck.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/JmxAttributeHealthCheck.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/JmxAttributeHealthCheckConfiguration.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/executor/
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/executor/ExecutionResult.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/executor/ExtendedHealthCheckExecutor.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/executor/HealthCheckExecutorImpl.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/executor/HealthCheckExecutorImplConfiguration.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/executor/HealthCheckExecutorThreadPool.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/executor/HealthCheckExecutorThreadPoolConfiguration.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/executor/HealthCheckFuture.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/executor/HealthCheckResultCache.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/executor/async/
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/executor/async/AsyncHealthCheckExecutor.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/executor/async/AsyncHealthCheckIntervalJob.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/executor/async/AsyncHealthCheckJob.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/executor/async/AsyncHealthCheckQuartzCronJob.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/executor/async/QuartzCronScheduler.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/servlet/
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/servlet/HealthCheckExecutorServlet.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/servlet/HealthCheckExecutorServletConfiguration.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/servlet/ResultHtmlSerializer.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/servlet/ResultHtmlSerializerConfiguration.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/servlet/ResultJsonSerializer.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/servlet/ResultTxtSerializer.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/servlet/ResultTxtVerboseSerializer.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/servlet/ResultTxtVerboseSerializerConfiguration.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/jmx/
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/jmx/impl/
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/jmx/impl/HealthCheckMBean.java
felix/trunk/healthcheck/core/src/main/java/org/apache/felix/hc/jmx/impl/HealthCheckMBeanCreator.java
felix/trunk/healthcheck/core/src/test/
felix/trunk/healthcheck/core/src/test/java/
felix/trunk/healthcheck/core/src/test/java/org/
felix/trunk/healthcheck/core/src/test/java/org/apache/
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/core/
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/core/impl/
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/core/impl/CompositeHealthCheckTest.java
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/core/impl/JmxAttributeHealthCheckTest.java
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/core/impl/executor/
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/core/impl/executor/HealthCheckExecutorImplTest.java
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/core/impl/executor/HealthCheckResultCacheTest.java
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/core/impl/servlet/
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/core/impl/servlet/HealthCheckExecutorServletTest.java
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/core/impl/servlet/ResultJsonSerializerTest.java
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/core/it/
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/core/it/AsyncHealthCheckIT.java
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/core/it/ExtendedHealthCheckExecutorIT.java
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/core/it/HealthCheckExecutorSelectionIT.java
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/core/it/HealthCheckFilterIT.java
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/core/it/HealthCheckServletIT.java
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/core/it/JmxAdjustableStatusHealthCheckIT.java
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/core/it/MockHttpService.java
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/core/it/U.java
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/jmx/
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/jmx/impl/
felix/trunk/healthcheck/core/src/test/java/org/apache/felix/hc/jmx/impl/HealthCheckMBeanTest.java
felix/trunk/healthcheck/docs/
felix/trunk/healthcheck/docs/felix-health-checks.md
felix/trunk/healthcheck/webconsoleplugin/
felix/trunk/healthcheck/webconsoleplugin/pom.xml
felix/trunk/healthcheck/webconsoleplugin/src/
felix/trunk/healthcheck/webconsoleplugin/src/main/
felix/trunk/healthcheck/webconsoleplugin/src/main/java/
felix/trunk/healthcheck/webconsoleplugin/src/main/java/org/
felix/trunk/healthcheck/webconsoleplugin/src/main/java/org/apache/
felix/trunk/healthcheck/webconsoleplugin/src/main/java/org/apache/felix/
felix/trunk/healthcheck/webconsoleplugin/src/main/java/org/apache/felix/hc/
felix/trunk/healthcheck/webconsoleplugin/src/main/java/org/apache/felix/hc/webconsole/
felix/trunk/healthcheck/webconsoleplugin/src/main/java/org/apache/felix/hc/webconsole/impl/
felix/trunk/healthcheck/webconsoleplugin/src/main/java/org/apache/felix/hc/webconsole/impl/HealthCheckWebconsolePlugin.java
felix/trunk/healthcheck/webconsoleplugin/src/main/java/org/apache/felix/hc/webconsole/impl/WebConsoleHelper.java
felix/trunk/healthcheck/webconsoleplugin/src/main/resources/
felix/trunk/healthcheck/webconsoleplugin/src/main/resources/res/
felix/trunk/healthcheck/webconsoleplugin/src/main/resources/res/ui/
felix/trunk/healthcheck/webconsoleplugin/src/main/resources/res/ui/healthcheck.css
felix/trunk/healthcheck/webconsoleplugin/src/test/
felix/trunk/healthcheck/webconsoleplugin/src/test/java/
felix/trunk/healthcheck/webconsoleplugin/src/test/java/org/
felix/trunk/healthcheck/webconsoleplugin/src/test/java/org/apache/
felix/trunk/healthcheck/webconsoleplugin/src/test/java/org/apache/felix/
felix/trunk/healthcheck/webconsoleplugin/src/test/java/org/apache/felix/hc/
felix/trunk/healthcheck/webconsoleplugin/src/test/java/org/apache/felix/hc/webconsole/
felix/trunk/healthcheck/webconsoleplugin/src/test/java/org/apache/felix/hc/webconsole/impl/
felix/trunk/healthcheck/webconsoleplugin/src/test/java/org/apache/felix/hc/webconsole/impl/HealthCheckWebconsolePluginTest.java
Added: felix/trunk/healthcheck/api/bnd.bnd
URL: http://svn.apache.org/viewvc/felix/trunk/healthcheck/api/bnd.bnd?rev=1849246&view=auto
==============================================================================
--- felix/trunk/healthcheck/api/bnd.bnd (added)
+++ felix/trunk/healthcheck/api/bnd.bnd Tue Dec 18 22:58:15 2018
@@ -0,0 +1,12 @@
+Bundle-Category: felix
+
+Bundle-Description: ${project.description}
+
+Bundle-DocURL: https://felix.apache.org
+
+Bundle-License: Apache License, Version 2.0
+
+Bundle-Vendor: The Apache Software Foundation
+
+Export-Package: org.apache.felix.hc.*
+
Added: felix/trunk/healthcheck/api/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/healthcheck/api/pom.xml?rev=1849246&view=auto
==============================================================================
--- felix/trunk/healthcheck/api/pom.xml (added)
+++ felix/trunk/healthcheck/api/pom.xml Tue Dec 18 22:58:15 2018
@@ -0,0 +1,101 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+-->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <modelVersion>4.0.0</modelVersion>
+
+ <parent>
+ <groupId>org.apache.felix</groupId>
+ <artifactId>felix-parent</artifactId>
+ <version>6</version>
+ <relativePath/>
+ </parent>
+
+ <artifactId>org.apache.felix.healthcheck.api</artifactId>
+ <version>2.0.0-SNAPSHOT</version>
+
+ <name>Apache Felix Health Check API</name>
+ <inceptionYear>2013</inceptionYear>
+
+ <description>
+ The Felix Health Check API
+ </description>
+
+ <scm>
+ <connection>scm:svn:http://svn.apache.org/repos/asf/felix/trunk/healthcheck/api</connection>
+ <developerConnection>scm:svn:https://svn.apache.org/repos/asf/felix/trunk/healthcheck/api</developerConnection>
+ <url>http://svn.apache.org/viewvc/felix/trunk/http/api/</url>
+ </scm>
+
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>biz.aQute.bnd</groupId>
+ <artifactId>bnd-maven-plugin</artifactId>
+ <version>4.0.0</version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>bnd-process</goal>
+ </goals>
+ </execution>
+ </executions>
+ </plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-jar-plugin</artifactId>
+ <configuration>
+ <archive>
+ <manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
+ </archive>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+
+ <dependencies>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>osgi.core</artifactId>
+ <version>6.0.0</version>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>osgi.annotation</artifactId>
+ <version>6.0.1</version>
+ <scope>provided</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>1.7.6</version>
+ <scope>provided</scope>
+ </dependency>
+
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <version>4.12</version>
+ <scope>test</scope>
+ </dependency>
+ </dependencies>
+
+</project>
\ No newline at end of file
Added: felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/HealthCheck.java
URL: http://svn.apache.org/viewvc/felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/HealthCheck.java?rev=1849246&view=auto
==============================================================================
--- felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/HealthCheck.java (added)
+++ felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/HealthCheck.java Tue Dec 18 22:58:15 2018
@@ -0,0 +1,64 @@
+/*
+ * 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.felix.hc.api;
+
+import org.osgi.annotation.versioning.ConsumerType;
+
+/** Health Check services can be executed and return an execution {@link Result}.
+ *
+ * Clients should not look up health checks directly but rather use the {@link org.apache.felix.hc.api.execution.HealthCheckExecutor}
+ * service and executed checks based on tags.
+ *
+ * If the {@link #MBEAN_NAME} service registration property is set, the health check is registered as an mbean and can be invoked by getting
+ * the MBean from the JMX registry. */
+@ConsumerType
+public interface HealthCheck {
+
+ /** Optional service property: the name of a health check. This name should be unique, however there might be more than one health check
+ * service with the same value for this property. The value of this property must be of type String. */
+ String NAME = "hc.name";
+
+ /** Optional service property: the name of the MBean for registering the health check as an MBean. If this property is missing the
+ * health check is not registered as a JMX MBean. If there is more than one service with the same value for this property, the one with
+ * the highest service ranking is registered only. The value of this property must be of type String. */
+ String MBEAN_NAME = "hc.mbean.name";
+
+ /** Optional service property: tags for categorizing the health check services. The value of this property must be of type String or
+ * String array. */
+ String TAGS = "hc.tags";
+
+ /** Optional service property: If this property is set the health check will be executed asynchronously using the cron expression
+ * provided. */
+ String ASYNC_CRON_EXPRESSION = "hc.async.cronExpression";
+
+ /** Optional service property: If this property is set the health check will be executed asynchronously every n seconds */
+ String ASYNC_INTERVAL_IN_SEC = "hc.async.intervalInSec";
+
+ /** Optional service property: TTL for health check {@link Result}. The value of this property must be of type {@link Long} and is
+ * specified in ms. */
+ String RESULT_CACHE_TTL_IN_MS = "hc.resultCacheTtlInMs";
+
+ /** Optional service property: If given, warning results (that is WARN, CRITICAL or HEALTH_CHECK_ERROR) from the past executions will be
+ * taken into account as well for the given minutes (use Integer.MAX_VALUE for indefinitely). Useful for unhealthy system states that
+ * disappear but might leave the system at an inconsistent state (e.g. an event queue overflow). */
+ String WARNINGS_STICK_FOR_MINUTES = "hc.warningsStickForMinutes";
+
+ /** Execute this health check and return a {@link Result} This is meant to execute quickly, access to external systems, for example,
+ * should be managed asynchronously. */
+ Result execute();
+}
Added: felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/Result.java
URL: http://svn.apache.org/viewvc/felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/Result.java?rev=1849246&view=auto
==============================================================================
--- felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/Result.java (added)
+++ felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/Result.java Tue Dec 18 22:58:15 2018
@@ -0,0 +1,74 @@
+/*
+ * 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.felix.hc.api;
+
+import java.util.Iterator;
+
+/** The result of executing a {@link HealthCheck} */
+public class Result implements Iterable<ResultLog.Entry> {
+
+ protected final ResultLog resultLog;
+
+ public enum Status {
+ OK, // no problem
+ WARN, // health check detected something wrong but not critical
+ CRITICAL, // health check detected a critical problem
+ HEALTH_CHECK_ERROR // health check did not execute properly
+ }
+
+ /** Build a single-value Result
+ *
+ * @param s if lower than OK, our status is set to OK */
+ public Result(final Status s, final String explanation) {
+ resultLog = new ResultLog().add(new ResultLog.Entry(s, explanation));
+ }
+
+ /** Build a single-value Result with exception
+ *
+ * @param s if lower than OK, our status is set to OK */
+ public Result(final Status s, final String explanation, final Exception e) {
+ resultLog = new ResultLog().add(new ResultLog.Entry(s, explanation, e));
+ }
+
+ /** Build a a Result based on a ResultLog, which can provide more details than a single-value Result. */
+ public Result(final ResultLog log) {
+ resultLog = new ResultLog(log);
+ }
+
+ /** True if our status is OK - provides a convenient way of checking that. */
+ public boolean isOk() {
+ return getStatus().equals(Status.OK);
+ }
+
+ /** Return our Status */
+ public Status getStatus() {
+ return resultLog.getAggregateStatus();
+ }
+
+ /** Return an Iterator on the entries of our ResultLog */
+ @Override
+ public Iterator<ResultLog.Entry> iterator() {
+ return resultLog.iterator();
+ }
+
+ @Override
+ public String toString() {
+ return "Result [status=" + getStatus() + ", resultLog=" + resultLog + "]";
+ }
+
+}
\ No newline at end of file
Added: felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/ResultLog.java
URL: http://svn.apache.org/viewvc/felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/ResultLog.java?rev=1849246&view=auto
==============================================================================
--- felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/ResultLog.java (added)
+++ felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/ResultLog.java Tue Dec 18 22:58:15 2018
@@ -0,0 +1,132 @@
+/*
+ * 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.felix.hc.api;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.felix.hc.api.Result.Status;
+
+/** The log of a Result, allows for providing multiple lines of information which are aggregated as a single Result. */
+public class ResultLog implements Iterable<ResultLog.Entry> {
+
+ private List<Entry> entries = new LinkedList<Entry>();
+ private Status aggregateStatus;
+
+ /** An entry in this log */
+ public static class Entry {
+ private final Status status;
+ private final String message;
+ private final boolean isDebug;
+ private final Exception exception;
+
+ public Entry(Status s, String message) {
+ this(s, message, false, null);
+ }
+
+ public Entry(String message, boolean isDebug) {
+ this(Status.OK, message, isDebug, null);
+ }
+ public Entry(String message, boolean isDebug, Exception exception) {
+ this(Status.OK, message, isDebug, exception);
+ }
+
+ public Entry(Status s, String message, Exception exception) {
+ this(s, message, false, exception);
+ }
+
+ // private to not allow invalid combinations of isDebug=true and a status different than Status.OK
+ private Entry(Status s, String message, boolean isDebug, Exception exception) {
+ this.status = s;
+ this.message = message;
+ this.exception = exception;
+ this.isDebug = isDebug;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder builder = new StringBuilder(status.toString()).append(" ").append(message);
+ if (exception != null) {
+ builder.append(" Exception: " + exception.getMessage());
+ }
+ return builder.toString();
+ }
+
+ public Status getStatus() {
+ return status;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+
+ public Exception getException() {
+ return exception;
+ }
+
+ public boolean isDebug() {
+ return isDebug;
+ }
+ }
+
+ /** Build a log. Initial aggregate status is set to WARN, as an empty log is not considered ok. That's reset to OK before adding the
+ * first log entry, and then the status aggregation rules take over. */
+ public ResultLog() {
+ aggregateStatus = Result.Status.WARN;
+ }
+
+ /** Create a copy of the result log */
+ public ResultLog(final ResultLog log) {
+ this.aggregateStatus = log.aggregateStatus;
+ this.entries = new ArrayList<ResultLog.Entry>(log.entries);
+ }
+
+ /** Add an entry to this log. The aggregate status of this is set to the highest of the current aggregate status and the new Entry's
+ * status */
+ public ResultLog add(Entry e) {
+ if (entries.isEmpty()) {
+ aggregateStatus = Result.Status.OK;
+ }
+ entries.add(e);
+ if (e.getStatus().ordinal() > aggregateStatus.ordinal()) {
+ aggregateStatus = e.getStatus();
+ }
+ return this;
+ }
+
+ /** Return an Iterator on our entries */
+ @Override
+ public Iterator<ResultLog.Entry> iterator() {
+ return entries.iterator();
+ }
+
+ /** Return our aggregate status, i.e. the highest status of the entries added to this log. Starts at OK for an empty ResultLog, so
+ * cannot be lower than that. */
+ public Status getAggregateStatus() {
+ return aggregateStatus;
+ }
+
+ @Override
+ public String toString() {
+ final StringBuilder sb = new StringBuilder("ResultLog: ");
+ sb.append(this.entries.toString());
+ return sb.toString();
+ }
+}
\ No newline at end of file
Added: felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/HealthCheckExecutionOptions.java
URL: http://svn.apache.org/viewvc/felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/HealthCheckExecutionOptions.java?rev=1849246&view=auto
==============================================================================
--- felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/HealthCheckExecutionOptions.java (added)
+++ felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/HealthCheckExecutionOptions.java Tue Dec 18 22:58:15 2018
@@ -0,0 +1,97 @@
+/*
+ * 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.felix.hc.api.execution;
+
+/** Options for behavior of health check execution. */
+public class HealthCheckExecutionOptions {
+
+ private boolean forceInstantExecution = false;
+ private boolean combineTagsWithOr = false;
+ private int overrideGlobalTimeout = 0;
+
+ @Override
+ public String toString() {
+ return "[HealthCheckExecutionOptions forceInstantExecution=" + forceInstantExecution + ", combineTagsWithOr=" + combineTagsWithOr
+ + ", overrideGlobalTimeout=" + overrideGlobalTimeout + "]";
+ }
+
+ /** If activated, this will ensure that asynchronous checks will be executed immediately.
+ *
+ * @param forceInstantExecution boolean flag */
+ public void setForceInstantExecution(boolean forceInstantExecution) {
+ this.forceInstantExecution = forceInstantExecution;
+ }
+
+ /** If activated, the given tags will be combined with a logical "or" instead of "and".
+ *
+ * @param combineTagsWithOr boolean flag */
+ public void setCombineTagsWithOr(boolean combineTagsWithOr) {
+ this.combineTagsWithOr = combineTagsWithOr;
+ }
+
+ /** Allows to override the global timeout for this particular execution of the health check.
+ *
+ * @param overrideGlobalTimeout timeout in ms to be used for this execution of the execution */
+ public void setOverrideGlobalTimeout(int overrideGlobalTimeout) {
+ this.overrideGlobalTimeout = overrideGlobalTimeout;
+ }
+
+ /** @return true if instant execution is turned on */
+ public boolean isForceInstantExecution() {
+ return forceInstantExecution;
+ }
+
+ /** @return true if combining tags with or is turned on */
+ public boolean isCombineTagsWithOr() {
+ return combineTagsWithOr;
+ }
+
+ /** @return the timeout to be used for this execution (overriding the global timeout) */
+ public int getOverrideGlobalTimeout() {
+ return overrideGlobalTimeout;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + (combineTagsWithOr ? 1231 : 1237);
+ result = prime * result + (forceInstantExecution ? 1231 : 1237);
+ result = prime * result + overrideGlobalTimeout;
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ HealthCheckExecutionOptions other = (HealthCheckExecutionOptions) obj;
+ if (combineTagsWithOr != other.combineTagsWithOr)
+ return false;
+ if (forceInstantExecution != other.forceInstantExecution)
+ return false;
+ if (overrideGlobalTimeout != other.overrideGlobalTimeout)
+ return false;
+ return true;
+ }
+
+}
\ No newline at end of file
Added: felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/HealthCheckExecutionResult.java
URL: http://svn.apache.org/viewvc/felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/HealthCheckExecutionResult.java?rev=1849246&view=auto
==============================================================================
--- felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/HealthCheckExecutionResult.java (added)
+++ felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/HealthCheckExecutionResult.java Tue Dec 18 22:58:15 2018
@@ -0,0 +1,49 @@
+/*
+ * 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.felix.hc.api.execution;
+
+import java.util.Date;
+
+import org.apache.felix.hc.api.Result;
+import org.apache.felix.hc.util.HealthCheckMetadata;
+import org.osgi.annotation.versioning.ProviderType;
+
+/** Interface for health check executions via the {@link HealthCheckExecutor}. */
+@ProviderType
+public interface HealthCheckExecutionResult {
+
+ /** Get the result of the health check run. */
+ Result getHealthCheckResult();
+
+ /** Get the elapsed time in ms */
+ long getElapsedTimeInMs();
+
+ /** Get the date, the health check finished or if the execution timed out, the execution was aborted.
+ *
+ * @return The finished date of the execution. */
+ Date getFinishedAt();
+
+ /** Returns true if the execution has timed out. In this case the result does not reflect the real result of the underlying check, but a
+ * result indicating the timeout.
+ *
+ * @return <code>true</code> if execution timed out. */
+ boolean hasTimedOut();
+
+ /** Get the meta data about the health check service */
+ HealthCheckMetadata getHealthCheckMetadata();
+}
\ No newline at end of file
Added: felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/HealthCheckExecutor.java
URL: http://svn.apache.org/viewvc/felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/HealthCheckExecutor.java?rev=1849246&view=auto
==============================================================================
--- felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/HealthCheckExecutor.java (added)
+++ felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/HealthCheckExecutor.java Tue Dec 18 22:58:15 2018
@@ -0,0 +1,43 @@
+/*
+ * 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.felix.hc.api.execution;
+
+import java.util.List;
+
+import org.apache.felix.hc.api.HealthCheck;
+import org.osgi.annotation.versioning.ProviderType;
+
+/** Executes health checks registered as OSGi services and implementing the interface {@link HealthCheck}. */
+@ProviderType
+public interface HealthCheckExecutor {
+
+ /** Executes all health checks matching the supplied filter options. If no options are supplied, all health checks are executed.
+ *
+ * @param selector filter selector
+ * @return List of results. The list might be empty. */
+ List<HealthCheckExecutionResult> execute(HealthCheckSelector selector);
+
+ /** Executes all health checks with the supplied filter options. If no options are supplied, all health checks are executed.
+ *
+ * @param selector filter selector
+ * @param options options for controlling execution behavior
+ *
+ * @return List of results. The list might be empty. */
+ List<HealthCheckExecutionResult> execute(HealthCheckSelector selector, HealthCheckExecutionOptions options);
+
+}
\ No newline at end of file
Added: felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/HealthCheckSelector.java
URL: http://svn.apache.org/viewvc/felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/HealthCheckSelector.java?rev=1849246&view=auto
==============================================================================
--- felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/HealthCheckSelector.java (added)
+++ felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/HealthCheckSelector.java Tue Dec 18 22:58:15 2018
@@ -0,0 +1,98 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.felix.hc.api.execution;
+
+import java.util.Arrays;
+
+import org.osgi.annotation.versioning.ProviderType;
+
+/** Parameter class to pass a set of tags and names to the filter. */
+@ProviderType
+public final class HealthCheckSelector {
+
+ private String[] tags;
+ private String[] names;
+
+ public String[] tags() {
+ return tags;
+ }
+
+ public String[] names() {
+ return names;
+ }
+
+ private HealthCheckSelector() {
+ }
+
+ /** Copy the specified names into the current tags array.
+ *
+ * @param tags the new tags. Specify null to clear the current tag array
+ * @return this */
+ public HealthCheckSelector withTags(String... tags) {
+ if (this.tags == null) {
+ this.tags = tags;
+ } else if (tags != null) {
+ String[] copy = Arrays.copyOf(this.tags, this.tags.length + tags.length);
+ System.arraycopy(tags, 0, copy, this.tags.length, tags.length);
+ this.tags = copy;
+ } else {
+ this.tags = null;
+ }
+ return this;
+ }
+
+ /** Copy the specified names into the current names array.
+ *
+ * @param names the new names. Specify null to clear the current name array
+ * @return this */
+ public HealthCheckSelector withNames(String... names) {
+ if (this.names == null) {
+ this.names = names;
+ } else if (names != null) {
+ String[] copy = Arrays.copyOf(this.names, this.names.length + names.length);
+ System.arraycopy(names, 0, copy, this.names.length, names.length);
+ this.names = copy;
+ } else {
+ this.names = null;
+ }
+ return this;
+ }
+
+ public static HealthCheckSelector empty() {
+ return new HealthCheckSelector();
+ }
+
+ public static HealthCheckSelector tags(String... tags) {
+ HealthCheckSelector selector = new HealthCheckSelector();
+ selector.tags = tags;
+ return selector;
+ }
+
+ public static HealthCheckSelector names(String... names) {
+ HealthCheckSelector selector = new HealthCheckSelector();
+ selector.names = names;
+ return selector;
+ }
+
+ @Override
+ public String toString() {
+ return "HealthCheckSelector{" +
+ "tags=" + (tags == null ? "*" : Arrays.toString(tags)) +
+ ", names=" + (names == null ? "*" : Arrays.toString(names)) +
+ '}';
+ }
+}
Added: felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/package-info.java
URL: http://svn.apache.org/viewvc/felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/package-info.java?rev=1849246&view=auto
==============================================================================
--- felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/package-info.java (added)
+++ felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/execution/package-info.java Tue Dec 18 22:58:15 2018
@@ -0,0 +1,23 @@
+/*
+ * 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("2.0.0")
+package org.apache.felix.hc.api.execution;
+
+import org.osgi.annotation.versioning.Version;
Added: felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/package-info.java
URL: http://svn.apache.org/viewvc/felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/package-info.java?rev=1849246&view=auto
==============================================================================
--- felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/package-info.java (added)
+++ felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/api/package-info.java Tue Dec 18 22:58:15 2018
@@ -0,0 +1,23 @@
+/*
+ * 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("2.0.0")
+package org.apache.felix.hc.api;
+
+import org.osgi.annotation.versioning.Version;
Added: felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/FormattingResultLog.java
URL: http://svn.apache.org/viewvc/felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/FormattingResultLog.java?rev=1849246&view=auto
==============================================================================
--- felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/FormattingResultLog.java (added)
+++ felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/FormattingResultLog.java Tue Dec 18 22:58:15 2018
@@ -0,0 +1,85 @@
+/*
+ * 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.felix.hc.util;
+
+import java.text.NumberFormat;
+import java.util.Locale;
+
+import org.apache.felix.hc.api.Result;
+import org.apache.felix.hc.api.ResultLog;
+import org.osgi.annotation.versioning.ProviderType;
+import org.slf4j.helpers.MessageFormatter;
+
+/** Utility that provides a logging-like facade on a ResultLog */
+@ProviderType
+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());
+ }
+ private ResultLog.Entry createEntry(boolean isDebug, String format, Object... args) {
+ return new ResultLog.Entry(MessageFormatter.arrayFormat(format, args).getMessage(), isDebug);
+ }
+
+ public void debug(String format, Object... args) {
+ add(createEntry(true, format, args));
+ }
+
+ public void info(String format, Object... args) {
+ add(createEntry(false, 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));
+ }
+
+ /** Utility method to return any magnitude of milliseconds in a human readable format using the appropriate time unit (ms, sec, min)
+ * depending on the magnitude of the input.
+ *
+ * @param millis milliseconds
+ * @return a string with a number and a unit */
+ public static String msHumanReadable(final long millis) {
+
+ double number = millis;
+ final String[] units = new String[] { "ms", "sec", "min", "h", "days" };
+ final double[] divisors = new double[] { 1000, 60, 60, 24 };
+
+ int magnitude = 0;
+ do {
+ double currentDivisor = divisors[Math.min(magnitude, divisors.length - 1)];
+ if (number < currentDivisor) {
+ break;
+ }
+ number /= currentDivisor;
+ magnitude++;
+ } while (magnitude < units.length - 1);
+ NumberFormat format = NumberFormat.getNumberInstance(Locale.UK);
+ format.setMinimumFractionDigits(0);
+ format.setMaximumFractionDigits(1);
+ String result = format.format(number) + units[magnitude];
+ return result;
+ }
+}
\ No newline at end of file
Added: felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/HealthCheckFilter.java
URL: http://svn.apache.org/viewvc/felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/HealthCheckFilter.java?rev=1849246&view=auto
==============================================================================
--- felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/HealthCheckFilter.java (added)
+++ felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/HealthCheckFilter.java Tue Dec 18 22:58:15 2018
@@ -0,0 +1,178 @@
+/*
+ * 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.felix.hc.util;
+
+import static org.apache.felix.hc.api.execution.HealthCheckSelector.empty;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.felix.hc.api.HealthCheck;
+import org.apache.felix.hc.api.execution.HealthCheckSelector;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Select from available {@link HealthCheck} services. Once this filter object and the returned health check services are no longer be used
+ * {@link #dispose()} should be called, to free the service references.
+ *
+ * This class is not thread safe and instances shouldn't be used concurrently from different threads. */
+public class HealthCheckFilter {
+ private final Logger log = LoggerFactory.getLogger(getClass());
+
+ private final BundleContext bundleContext;
+
+ public static final String OMIT_PREFIX = "-";
+
+ private final Set<ServiceReference> usedReferences = new HashSet<ServiceReference>();
+
+ /** Create a new filter object */
+ public HealthCheckFilter(final BundleContext bc) {
+ bundleContext = bc;
+ }
+
+ public List<HealthCheck> getHealthChecks(final HealthCheckSelector selector) {
+ final ServiceReference[] refs = this.getHealthCheckServiceReferences(selector);
+ final List<HealthCheck> result = new ArrayList<HealthCheck>();
+
+ if (refs != null) {
+ final List<ServiceReference> sortedRefs = Arrays.asList(refs);
+ Collections.sort(sortedRefs);
+
+ for (final ServiceReference ref : sortedRefs) {
+ final HealthCheck hc = (HealthCheck) bundleContext.getService(ref);
+ log.debug("Selected HealthCheck service {}", hc);
+ if (hc != null) {
+ this.usedReferences.add(ref);
+ result.add(hc);
+ }
+ }
+ }
+
+ return result;
+ }
+
+ public ServiceReference<HealthCheck>[] getHealthCheckServiceReferences(final HealthCheckSelector selector) {
+ return getHealthCheckServiceReferences(selector, false);
+ }
+
+ public ServiceReference<HealthCheck>[] getHealthCheckServiceReferences(final HealthCheckSelector selector, boolean combineTagsWithOr) {
+ final CharSequence filterBuilder = selector != null ? getServiceFilter(selector, combineTagsWithOr)
+ : getServiceFilter(empty(), combineTagsWithOr);
+
+ log.trace("OSGi service filter in getHealthCheckServiceReferences(): {}", filterBuilder);
+
+ try {
+ final String filterString = filterBuilder.length() == 0 ? null : filterBuilder.toString();
+ bundleContext.createFilter(filterString); // check syntax early
+ final ServiceReference[] refs = bundleContext.getServiceReferences((String) null, filterString);
+ if (refs == null) {
+ log.debug("Found no HealthCheck services for filter: {}", filterString);
+ return new ServiceReference[0];
+ } else {
+ log.debug("Found {} HealthCheck services for filter: {}", refs.length, filterString);
+ }
+ return refs;
+ } catch (final InvalidSyntaxException ise) {
+ // this should not happen, but we fail gracefully
+ log.error("Invalid OSGi filter syntax in '" + filterBuilder + "'", ise);
+ return new ServiceReference[0];
+ }
+ }
+
+ /** Dispose all used service references */
+ public void dispose() {
+ for (final ServiceReference ref : this.usedReferences) {
+ this.bundleContext.ungetService(ref);
+ }
+ this.usedReferences.clear();
+ }
+
+ CharSequence getServiceFilter(HealthCheckSelector selector, boolean combineTagsWithOr) {
+ // Build service filter
+ final StringBuilder filterBuilder = new StringBuilder();
+ filterBuilder.append("(&"); // overall and
+
+ // object class (supporting current interface and legacy)
+ filterBuilder.append("(|(objectClass=").append(HealthCheck.class.getName())
+ .append(")(objectClass=org.apache.sling.hc.api.HealthCheck))");
+
+ final int prefixLen = HealthCheckFilter.OMIT_PREFIX.length();
+ final StringBuilder filterBuilderForOrOperator = new StringBuilder(); // or filters
+ final StringBuilder tagsBuilder = new StringBuilder();
+ int tagsAndClauses = 0;
+ if (selector.tags() != null) {
+ for (String tag : selector.tags()) {
+ tag = tag.trim();
+ if (tag.length() == 0) {
+ continue;
+ }
+ if (tag.startsWith(HealthCheckFilter.OMIT_PREFIX)) {
+ // ommit tags always have to be added as and-clause
+ filterBuilder.append("(!(").append(HealthCheck.TAGS).append("=").append(tag.substring(prefixLen)).append("))");
+ } else {
+ // add regular tags in the list either to outer and-clause or inner or-clause
+ if (combineTagsWithOr) {
+ filterBuilderForOrOperator.append("(").append(HealthCheck.TAGS).append("=").append(tag).append(")");
+ } else {
+ tagsBuilder.append("(").append(HealthCheck.TAGS).append("=").append(tag).append(")");
+ tagsAndClauses++;
+ }
+ }
+ }
+ }
+ boolean addedNameToOrBuilder = false;
+ if (selector.names() != null) {
+ for (String name : selector.names()) {
+ name = name.trim();
+ if (name.length() == 0) {
+ continue;
+ }
+ if (name.startsWith(HealthCheckFilter.OMIT_PREFIX)) {
+ // ommit tags always have to be added as and-clause
+ filterBuilder.append("(!(").append(HealthCheck.NAME).append("=").append(name.substring(prefixLen)).append("))");
+ } else {
+ // names are always ORd
+ filterBuilderForOrOperator.append("(").append(HealthCheck.NAME).append("=").append(name).append(")");
+ addedNameToOrBuilder = true;
+ }
+ }
+ }
+ if (addedNameToOrBuilder) {
+ if (tagsAndClauses > 1) {
+ filterBuilderForOrOperator.append("(&").append(tagsBuilder).append(")");
+ } else {
+ filterBuilderForOrOperator.append(tagsBuilder);
+ }
+ } else {
+ filterBuilder.append(tagsBuilder);
+ }
+ // add "or" clause if we have accumulated any
+ if (filterBuilderForOrOperator.length() > 0) {
+ filterBuilder.append("(|").append(filterBuilderForOrOperator).append(")");
+ }
+ filterBuilder.append(")");
+ return filterBuilder;
+ }
+}
Added: felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/HealthCheckMetadata.java
URL: http://svn.apache.org/viewvc/felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/HealthCheckMetadata.java?rev=1849246&view=auto
==============================================================================
--- felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/HealthCheckMetadata.java (added)
+++ felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/HealthCheckMetadata.java Tue Dec 18 22:58:15 2018
@@ -0,0 +1,195 @@
+/*
+ * 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.felix.hc.util;
+
+import java.util.Arrays;
+import java.util.LinkedList;
+import java.util.List;
+
+import org.apache.felix.hc.api.HealthCheck;
+import org.osgi.annotation.versioning.ProviderType;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+
+/** This class helps retrieving meta data information about a health check service.
+ *
+ * @since 1.1 */
+@ProviderType
+public class HealthCheckMetadata {
+
+ private final String name;
+
+ private final String mbeanName;
+
+ private final String title;
+
+ private final long serviceId;
+
+ private final List<String> tags;
+
+ private final String asyncCronExpression;
+ private final Long asyncIntervalInSec;
+
+ private final ServiceReference serviceReference;
+
+ private final Long resultCacheTtlInMs;
+
+ private final Long warningsStickForMinutes;
+
+ public HealthCheckMetadata(final ServiceReference ref) {
+ this.serviceId = (Long) ref.getProperty(Constants.SERVICE_ID);
+ this.name = (String) ref.getProperty(HealthCheck.NAME);
+ this.mbeanName = (String) ref.getProperty(HealthCheck.MBEAN_NAME);
+ this.title = getHealthCheckTitle(ref);
+ this.tags = arrayPropertyToListOfStr(ref.getProperty(HealthCheck.TAGS));
+
+ this.asyncCronExpression = (String) ref.getProperty(HealthCheck.ASYNC_CRON_EXPRESSION);
+ this.asyncIntervalInSec = toLong(ref.getProperty(HealthCheck.ASYNC_INTERVAL_IN_SEC));
+
+ this.resultCacheTtlInMs = (Long) ref.getProperty(HealthCheck.RESULT_CACHE_TTL_IN_MS);
+ this.warningsStickForMinutes = toLong(ref.getProperty(HealthCheck.WARNINGS_STICK_FOR_MINUTES));
+ this.serviceReference = ref;
+ }
+
+ private Long toLong(Object configValue) {
+ if (configValue == null) {
+ return null;
+ }
+ if (configValue instanceof Long) {
+ return (Long) configValue;
+ }
+ return Long.valueOf(configValue.toString());
+ }
+
+ /** The name of the health check as defined through the {@link HealthCheck#NAME} property.
+ *
+ * @return The name or <code>null</code> */
+ public String getName() {
+ return name;
+ }
+
+ /** The mbean name of the health check as defined through the {@link HealthCheck#MBEAN_NAME} property.
+ *
+ * @return The mbean name or <code>null</code> */
+ public String getMBeanName() {
+ return mbeanName;
+ }
+
+ /** The title of the health check. If the health check has a name, this is used as the title. Otherwise the description, PID and service
+ * ID are checked for values. */
+ public String getTitle() {
+ return title;
+ }
+
+ /** Return the list of defined tags for this check as set through {@link HealthCheckMetadata#tags}
+ *
+ * @return */
+ public List<String> getTags() {
+ return tags;
+ }
+
+ /** Return the cron expression used for asynchronous execution. */
+ public String getAsyncCronExpression() {
+ return asyncCronExpression;
+ }
+
+ /** Return the interval in sec used for asynchronous execution. */
+ public Long getAsyncIntervalInSec() {
+ return asyncIntervalInSec;
+ }
+
+ /** Return the service id. */
+ public long getServiceId() {
+ return this.serviceId;
+ }
+
+ /** Get the service reference. */
+ public ServiceReference getServiceReference() {
+ return this.serviceReference;
+ }
+
+ /** TTL for the result cache in ms.
+ *
+ * @return TTL for the result cache or <code>null</code> if not configured. */
+ public Long getResultCacheTtlInMs() {
+ return resultCacheTtlInMs;
+ }
+
+ /** Make warnings stick for the given amount of time.
+ *
+ * @return Time to make warn results sticky in minutes. */
+ public Long getWarningsStickForMinutes() {
+ return warningsStickForMinutes;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + (int) (serviceId ^ (serviceId >>> 32));
+ return result;
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ if (!(obj instanceof HealthCheckMetadata)) {
+ return false;
+ }
+ final HealthCheckMetadata other = (HealthCheckMetadata) obj;
+ return serviceId == other.serviceId;
+ }
+
+ @Override
+ public String toString() {
+ return "HealthCheck '" + name + "'";
+ }
+
+ private String getHealthCheckTitle(final ServiceReference ref) {
+ String name = (String) ref.getProperty(HealthCheck.NAME);
+ if (name == null || name.isEmpty()) {
+ final Object val = ref.getProperty(Constants.SERVICE_DESCRIPTION);
+ if (val != null) {
+ name = val.toString();
+ }
+ }
+ if (name == null || name.isEmpty()) {
+ name = "HealthCheck:" + ref.getProperty(Constants.SERVICE_ID);
+ final Object val = ref.getProperty(Constants.SERVICE_PID);
+ String pid = null;
+ if (val instanceof String) {
+ pid = (String) val;
+ } else if (val instanceof String[]) {
+ pid = Arrays.toString((String[]) val);
+ }
+ if (pid != null && !pid.isEmpty()) {
+ name = name + " (" + pid + ")";
+ }
+ }
+ return name;
+ }
+
+ private List<String> arrayPropertyToListOfStr(final Object arrayProp) {
+ List<String> res = new LinkedList<>();
+ if (arrayProp instanceof String) {
+ res.add((String) arrayProp);
+ } else if (arrayProp instanceof String[]) {
+ res.addAll(Arrays.asList((String[]) arrayProp));
+ }
+ return res;
+ }
+}
Added: felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/SimpleConstraintChecker.java
URL: http://svn.apache.org/viewvc/felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/SimpleConstraintChecker.java?rev=1849246&view=auto
==============================================================================
--- felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/SimpleConstraintChecker.java (added)
+++ felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/SimpleConstraintChecker.java Tue Dec 18 22:58:15 2018
@@ -0,0 +1,77 @@
+/*
+ * 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.felix.hc.util;
+
+import org.apache.felix.hc.api.Result;
+import org.apache.felix.hc.api.ResultLog;
+
+/** Simple check of numeric values against expressions like < N, > N, between two values etc. See the 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(
+ "Value [" + inputValue + "] matches constraint [" + constraint + "]", true));
+ } else {
+ resultLog.add(new ResultLog.Entry(
+ Result.Status.WARN,
+ "Value [" + inputValue + "] does not match constraint [" + constraint + "]"));
+ }
+ }
+}
\ No newline at end of file
Added: felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/package-info.java
URL: http://svn.apache.org/viewvc/felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/package-info.java?rev=1849246&view=auto
==============================================================================
--- felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/package-info.java (added)
+++ felix/trunk/healthcheck/api/src/main/java/org/apache/felix/hc/util/package-info.java Tue Dec 18 22:58:15 2018
@@ -0,0 +1,23 @@
+/*
+ * 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("2.0.0")
+package org.apache.felix.hc.util;
+
+import org.osgi.annotation.versioning.Version;
Added: felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/api/ResultLogTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/api/ResultLogTest.java?rev=1849246&view=auto
==============================================================================
--- felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/api/ResultLogTest.java (added)
+++ felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/api/ResultLogTest.java Tue Dec 18 22:58:15 2018
@@ -0,0 +1,91 @@
+/*
+ * 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.felix.hc.api;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+
+import java.util.Iterator;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class ResultLogTest {
+
+ private ResultLog log;
+
+ @Before
+ public void setup() {
+ log = new ResultLog();
+ }
+
+ @Test
+ public void testEmptyLogIsNotOk() {
+ assertEquals(Result.Status.WARN, log.getAggregateStatus());
+ assertFalse(log.iterator().hasNext());
+ }
+
+ @Test
+ public void testSetStatusGoingUp() {
+ log.add(new ResultLog.Entry(Result.Status.OK, "argh"));
+ assertEquals(Result.Status.OK, log.getAggregateStatus());
+ log.add(new ResultLog.Entry(Result.Status.WARN, "argh"));
+ assertEquals(Result.Status.WARN, log.getAggregateStatus());
+ log.add(new ResultLog.Entry(Result.Status.CRITICAL, "argh"));
+ assertEquals(Result.Status.CRITICAL, log.getAggregateStatus());
+ log.add(new ResultLog.Entry(Result.Status.HEALTH_CHECK_ERROR, "argh"));
+ assertEquals(Result.Status.HEALTH_CHECK_ERROR, log.getAggregateStatus());
+ }
+
+ @Test
+ public void testSetStatusGoingDownHCE() {
+ log.add(new ResultLog.Entry(Result.Status.HEALTH_CHECK_ERROR, "argh"));
+ assertEquals(Result.Status.HEALTH_CHECK_ERROR, log.getAggregateStatus());
+ log.add(new ResultLog.Entry(Result.Status.CRITICAL, "argh"));
+ assertEquals(Result.Status.HEALTH_CHECK_ERROR, log.getAggregateStatus());
+ }
+
+ @Test
+ public void testSetStatusGoingDownCRIT() {
+ log.add(new ResultLog.Entry(Result.Status.CRITICAL, "argh"));
+ assertEquals(Result.Status.CRITICAL, log.getAggregateStatus());
+ log.add(new ResultLog.Entry(Result.Status.WARN, "argh"));
+ assertEquals(Result.Status.CRITICAL, log.getAggregateStatus());
+ }
+
+ @Test
+ public void testSetStatusGoingDownWARN() {
+ log.add(new ResultLog.Entry(Result.Status.WARN, "argh"));
+ assertEquals(Result.Status.WARN, log.getAggregateStatus());
+ log.add(new ResultLog.Entry(Result.Status.OK, "argh"));
+ assertEquals(Result.Status.WARN, log.getAggregateStatus());
+ }
+
+ @Test
+ public void testLogEntries() {
+ log.add(new ResultLog.Entry(Result.Status.OK, "ok 1"));
+ log.add(new ResultLog.Entry(Result.Status.WARN, "warn 3"));
+ log.add(new ResultLog.Entry(Result.Status.CRITICAL, "critical 4"));
+
+ final Iterator<ResultLog.Entry> it = log.iterator();
+ assertEquals("OK ok 1", it.next().toString());
+ assertEquals("WARN warn 3", it.next().toString());
+ assertEquals("CRITICAL critical 4", it.next().toString());
+ assertFalse(it.hasNext());
+ }
+}
Added: felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/api/ResultTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/api/ResultTest.java?rev=1849246&view=auto
==============================================================================
--- felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/api/ResultTest.java (added)
+++ felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/api/ResultTest.java Tue Dec 18 22:58:15 2018
@@ -0,0 +1,63 @@
+/*
+ * 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.felix.hc.api;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.junit.Test;
+
+public class ResultTest {
+
+ private final AtomicInteger counter = new AtomicInteger();
+
+ private void assertSingleResult(Result.Status toSet, Result.Status expected, boolean expectOk) {
+ final String msg = "test " + counter.incrementAndGet();
+ final Result r = new Result(toSet, msg);
+ assertEquals(expected, r.getStatus());
+ assertEquals(expectOk, r.isOk());
+ assertTrue(r.iterator().hasNext());
+ assertEquals(toSet.toString() + " " + msg, r.iterator().next().toString());
+ }
+
+ @Test
+ public void testSingleResult() {
+ assertSingleResult(Result.Status.OK, Result.Status.OK, true);
+ assertSingleResult(Result.Status.WARN, Result.Status.WARN, false);
+ assertSingleResult(Result.Status.CRITICAL, Result.Status.CRITICAL, false);
+ assertSingleResult(Result.Status.HEALTH_CHECK_ERROR, Result.Status.HEALTH_CHECK_ERROR, false);
+ }
+
+ @Test
+ public void testLog() {
+ final ResultLog log = new ResultLog();
+ log.add(new ResultLog.Entry(Result.Status.OK, "some msg"));
+ log.add(new ResultLog.Entry(Result.Status.WARN, "problematic condition"));
+
+ final Result result = new Result(log);
+ assertEquals(Result.Status.WARN, result.getStatus());
+
+ final StringBuilder sb = new StringBuilder();
+ for (ResultLog.Entry e : result) {
+ sb.append(e.toString()).append("#");
+ }
+ assertEquals("OK some msg#WARN problematic condition#", sb.toString());
+ }
+}
Added: felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/util/HealthCheckFilterTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/util/HealthCheckFilterTest.java?rev=1849246&view=auto
==============================================================================
--- felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/util/HealthCheckFilterTest.java (added)
+++ felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/util/HealthCheckFilterTest.java Tue Dec 18 22:58:15 2018
@@ -0,0 +1,108 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.felix.hc.util;
+
+import static org.apache.felix.hc.api.execution.HealthCheckSelector.empty;
+import static org.apache.felix.hc.api.execution.HealthCheckSelector.names;
+import static org.apache.felix.hc.api.execution.HealthCheckSelector.tags;
+import static org.junit.Assert.assertEquals;
+
+import org.apache.felix.hc.api.HealthCheck;
+import org.apache.felix.hc.api.execution.HealthCheckSelector;
+import org.junit.Test;
+
+public class HealthCheckFilterTest {
+
+ private static final String HC_OBJ_CLASS_QUERY = "(|(objectClass=" + HealthCheck.class.getName()
+ + ")(objectClass=org.apache.sling.hc.api.HealthCheck))";
+
+ private HealthCheckFilter filter = new HealthCheckFilter(null);
+
+ private static void assertStrEquals(String expected, CharSequence actual) {
+ assertEquals(expected, actual.toString());
+ }
+
+ @Test
+ public void testEmptyOptions() {
+ assertStrEquals("(&" + HC_OBJ_CLASS_QUERY + ")", filter.getServiceFilter(empty(), false));
+ }
+
+ @Test
+ public void testWithOneTag() {
+ HealthCheckSelector selector = tags("foo");
+ assertStrEquals("(&" + HC_OBJ_CLASS_QUERY + "(hc.tags=foo))", filter.getServiceFilter(selector, false));
+ }
+
+ @Test
+ public void testWithTwoTags() {
+ HealthCheckSelector selector = tags("foo", "bar");
+ assertStrEquals("(&" + HC_OBJ_CLASS_QUERY + "(hc.tags=foo)(hc.tags=bar))", filter.getServiceFilter(selector, false));
+ }
+
+ @Test
+ public void testWithTwoTagsOr() {
+ HealthCheckSelector selector = tags("foo", "bar");
+ assertStrEquals("(&" + HC_OBJ_CLASS_QUERY + "(|(hc.tags=foo)(hc.tags=bar)))", filter.getServiceFilter(selector, true));
+ }
+
+ @Test
+ public void testWithTwoTagsExcludeOne() {
+ HealthCheckSelector selector = tags("foo", "bar").withTags("-baz");
+ assertStrEquals("(&" + HC_OBJ_CLASS_QUERY + "(!(hc.tags=baz))(hc.tags=foo)(hc.tags=bar))",
+ filter.getServiceFilter(selector, false));
+ }
+
+ @Test
+ public void testWithOneName() {
+ HealthCheckSelector selector = names("foo");
+ assertStrEquals("(&" + HC_OBJ_CLASS_QUERY + "(|(hc.name=foo)))", filter.getServiceFilter(selector, false));
+ }
+
+ @Test
+ public void testWithTwoNames() {
+ HealthCheckSelector selector = names("foo").withNames("bar");
+ assertStrEquals("(&" + HC_OBJ_CLASS_QUERY + "(|(hc.name=foo)(hc.name=bar)))", filter.getServiceFilter(selector, false));
+ }
+
+ @Test
+ public void testWithTwoNamesExcludingOne() {
+ HealthCheckSelector selector = names("foo", "bar", "-baz");
+ assertStrEquals("(&" + HC_OBJ_CLASS_QUERY + "(!(hc.name=baz))(|(hc.name=foo)(hc.name=bar)))",
+ filter.getServiceFilter(selector, false));
+ }
+
+ @Test
+ public void testWithTagAndName() {
+ HealthCheckSelector selector = empty().withTags("t1").withNames("foo");
+ assertStrEquals("(&" + HC_OBJ_CLASS_QUERY + "(|(hc.name=foo)(hc.tags=t1)))", filter.getServiceFilter(selector, false));
+ }
+
+ @Test
+ public void testWithTwoOrTagsAndTwoNames() {
+ HealthCheckSelector selector = empty().withNames("foo", "bar").withTags("t1", "t2");
+ assertStrEquals("(&" + HC_OBJ_CLASS_QUERY + "(|(hc.tags=t1)(hc.tags=t2)(hc.name=foo)(hc.name=bar)))",
+ filter.getServiceFilter(selector, true));
+ }
+
+ @Test
+ public void testWithTwoAndTagsAndTwoNames() {
+ HealthCheckSelector selector = empty().withNames("foo", "bar").withTags("t1", "t2");
+ assertStrEquals("(&" + HC_OBJ_CLASS_QUERY + "(|(hc.name=foo)(hc.name=bar)(&(hc.tags=t1)(hc.tags=t2))))",
+ filter.getServiceFilter(selector, false));
+ }
+
+}
Added: felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/util/SimpleConstraintCheckerTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/util/SimpleConstraintCheckerTest.java?rev=1849246&view=auto
==============================================================================
--- felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/util/SimpleConstraintCheckerTest.java (added)
+++ felix/trunk/healthcheck/api/src/test/java/org/apache/felix/hc/util/SimpleConstraintCheckerTest.java Tue Dec 18 22:58:15 2018
@@ -0,0 +1,166 @@
+/*
+ * 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.felix.hc.util;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import org.apache.felix.hc.api.Result;
+import org.apache.felix.hc.api.ResultLog;
+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));
+ }
+}