You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ro...@apache.org on 2020/09/04 13:29:17 UTC

[sling-whiteboard] 01/04: Add the hc-resource module

This is an automated email from the ASF dual-hosted git repository.

rombert pushed a commit to branch feature/new-modules
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit ba32190bfdff2837876f7fc839ca8bb20507cc84
Author: Robert Munteanu <ro...@apache.org>
AuthorDate: Fri Sep 4 15:27:30 2020 +0200

    Add the hc-resource module
---
 hc-resource/README.md                              | 20 +++++
 hc-resource/pom.xml                                | 64 ++++++++++++++++
 .../hc/resource/impl/ResourceHealthCheck.java      | 89 ++++++++++++++++++++++
 3 files changed, 173 insertions(+)

diff --git a/hc-resource/README.md b/hc-resource/README.md
new file mode 100644
index 0000000..547a5d9
--- /dev/null
+++ b/hc-resource/README.md
@@ -0,0 +1,20 @@
+# Apache Sling Markdown Resource Provider
+
+This module is part of the [Apache Sling](https://sling.apache.org) project.
+
+This module contains [Health Checks](https://github.com/apache/felix-dev/tree/master/healthcheck) that use
+the Sling Resource API.
+
+## Usage
+
+```
+"org.apache.sling.hc.resource.impl.ResourceHealthCheck~slingshot": {
+    "hc.tags":[
+        "systemalive"
+    ],
+    "resources": [
+        "/content/slingshot",
+        "/libs/slingshot/Home/html.jsp"
+    ]
+}
+```
diff --git a/hc-resource/pom.xml b/hc-resource/pom.xml
new file mode 100644
index 0000000..630672a
--- /dev/null
+++ b/hc-resource/pom.xml
@@ -0,0 +1,64 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!-- 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/maven-v4_0_0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <parent>
+        <groupId>org.apache.sling</groupId>
+        <artifactId>sling-bundle-parent</artifactId>
+        <version>39</version>
+        <relativePath />
+    </parent>
+    <artifactId>org.apache.sling.hc.resource</artifactId>
+    <version>1.0-SNAPSHOT</version>
+    <name>Apache Sling Health Check Resource</name>
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>biz.aQute.bnd</groupId>
+                <artifactId>bnd-maven-plugin</artifactId>
+            </plugin>
+            <plugin>
+                <groupId>biz.aQute.bnd</groupId>
+                <artifactId>bnd-baseline-maven-plugin</artifactId>
+                <configuration>
+                    <failOnMissing>false</failOnMissing>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+    <dependencies>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>osgi.core</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.osgi</groupId>
+            <artifactId>osgi.cmpn</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.healthcheck.api</artifactId>
+            <version>2.0.4</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.api</artifactId>
+            <version>2.20.0</version>
+            <scope>provided</scope>
+        </dependency>
+    </dependencies>
+</project>
diff --git a/hc-resource/src/main/java/org/apache/sling/hc/resource/impl/ResourceHealthCheck.java b/hc-resource/src/main/java/org/apache/sling/hc/resource/impl/ResourceHealthCheck.java
new file mode 100644
index 0000000..d76fe10
--- /dev/null
+++ b/hc-resource/src/main/java/org/apache/sling/hc/resource/impl/ResourceHealthCheck.java
@@ -0,0 +1,89 @@
+/*
+ * 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.sling.hc.resource.impl;
+
+import org.apache.felix.hc.api.FormattingResultLog;
+import org.apache.felix.hc.api.HealthCheck;
+import org.apache.felix.hc.api.Result;
+import org.apache.sling.api.resource.LoginException;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ResourceResolver;
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.ConfigurationPolicy;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.Designate;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+
+@Component(configurationPolicy = ConfigurationPolicy.REQUIRE)
+@Designate(ocd = ResourceHealthCheck.Config.class, factory = true)
+public class ResourceHealthCheck implements HealthCheck {
+    
+    @ObjectClassDefinition(name = "Health Check: Resources Present", description = "Checks the configured path(s) against the given thresholds")
+    public @interface Config {
+        @AttributeDefinition(name = "Name", description = "Name of this health check")
+        String hc_name() default "Resources Present";
+
+        @AttributeDefinition(name = "Tags", description = "List of tags for this health check, used to select subsets of health checks for execution e.g. by a composite health check.")
+        String[] hc_tags() default {};
+        
+        @AttributeDefinition(name = "Resources", description = "List of resources to check for existence")
+        String[] resources() default {};
+
+        @AttributeDefinition
+        String webconsole_configurationFactory_nameHint() default "Resources present: {resources}";
+    }
+    
+    @Reference
+    private ResourceResolverFactory resolverFactory;
+    private Config cfg;
+    
+    @Activate
+    protected void activate(Config cfg) {
+        this.cfg = cfg;
+    }
+    
+    @Override
+    public Result execute() {
+        FormattingResultLog result = new FormattingResultLog();
+        
+        try ( ResourceResolver resolver = resolverFactory.getServiceResourceResolver(null) ) {
+            for (String resource : cfg.resources() )
+                checkResource(result, resolver, resource);
+        } catch (LoginException e) {
+            result.critical("Error obtaining a resource resolver", e);
+        }
+        
+        return new Result(result);
+    }
+
+    private void checkResource(FormattingResultLog result, ResourceResolver resolver, String resourcePath) {
+        
+        result.debug("Checking for resource at {}", resourcePath);
+        Resource resource = resolver.getResource(resourcePath);
+        
+        if ( resource == null )
+            result.critical("Resource at {} does not exist", resourcePath);
+        else
+            result.info("Resource {} exists", resourcePath);
+    }
+
+}