You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bd...@apache.org on 2018/05/23 16:23:59 UTC

[sling-whiteboard] 02/02: Capabilities module, work in progress

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

bdelacretaz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-whiteboard.git

commit 53f9642f04474888584a8c389638c4468e7a2bf5
Author: Bertrand Delacretaz <bd...@apache.org>
AuthorDate: Wed May 23 18:23:48 2018 +0200

    Capabilities module, work in progress
---
 capabilities/README.txt                            |  12 +++
 capabilities/pom.xml                               | 105 +++++++++++++++++++++
 .../capabilities/internal/CapabilitiesServlet.java |  85 +++++++++++++++++
 .../capabilities/internal/HealthCheckProbe.java    |  47 +++++++++
 .../apache/sling/capabilities/internal/Probe.java  |  24 +++++
 .../sling/capabilities/internal/ProbeFactory.java  |  33 +++++++
 .../sling/capabilities/internal/ScriptProbe.java   |  50 ++++++++++
 7 files changed, 356 insertions(+)

diff --git a/capabilities/README.txt b/capabilities/README.txt
new file mode 100644
index 0000000..35d07ba
--- /dev/null
+++ b/capabilities/README.txt
@@ -0,0 +1,12 @@
+Sling Capabilities Module
+=========================
+
+This is a work in progress for a service that provides information on the capabilities
+of a Sling instance: what is installed, version levels etc.
+
+Capabilities are defined either by executing sets of Health Checks, selected by
+tags, or by executing small scripts.
+
+Each capability has a name and a value.
+
+TODO: complete this, examples etc.
diff --git a/capabilities/pom.xml b/capabilities/pom.xml
new file mode 100644
index 0000000..6e2cad3
--- /dev/null
+++ b/capabilities/pom.xml
@@ -0,0 +1,105 @@
+<?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">
+  <parent>
+    <artifactId>sling</artifactId>
+    <groupId>org.apache.sling</groupId>
+    <version>29</version>
+    <relativePath/>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <artifactId>org.apache.sling.capabilities</artifactId>
+  <version>0.0.1-SNAPSHOT</version>
+  <packaging>bundle</packaging>
+  <properties>
+    <sling.java.version>8</sling.java.version>
+  </properties>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-scr-plugin</artifactId>
+      </plugin>  
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <extensions>true</extensions>
+        <configuration>
+          <instructions>
+            <Embed-Dependency>
+              org.apache.felix.utils;inline=org/apache/felix/utils/json/JSONWriter.class
+            </Embed-Dependency>
+          </instructions>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+  <dependencies>
+    <dependency>
+      <groupId>biz.aQute.bnd</groupId>
+      <artifactId>biz.aQute.bndlib</artifactId>
+      <version>3.5.0</version>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.sling</groupId>
+      <artifactId>org.apache.sling.api</artifactId>
+      <version>2.16.4</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>javax.servlet</groupId>
+      <artifactId>javax.servlet-api</artifactId>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.felix</groupId>
+      <artifactId>org.apache.felix.scr.annotations</artifactId>
+      <version>1.12.0</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>org.apache.felix</groupId>
+      <artifactId>org.apache.felix.utils</artifactId>
+      <version>1.9.0</version>
+      <scope>provided</scope>
+    </dependency>
+    <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.slf4j</groupId>
+      <artifactId>slf4j-api</artifactId>
+      <scope>provided</scope>
+    </dependency>
+    <!-- TESTING -->
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <scope>test</scope>
+    </dependency>
+  </dependencies>
+</project>
diff --git a/capabilities/src/main/java/org/apache/sling/capabilities/internal/CapabilitiesServlet.java b/capabilities/src/main/java/org/apache/sling/capabilities/internal/CapabilitiesServlet.java
new file mode 100644
index 0000000..4c39abf
--- /dev/null
+++ b/capabilities/src/main/java/org/apache/sling/capabilities/internal/CapabilitiesServlet.java
@@ -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 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.capabilities.internal;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import javax.servlet.ServletException;
+import org.apache.felix.scr.annotations.sling.SlingServlet;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.SlingHttpServletResponse;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.ValueMap;
+import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
+import org.apache.felix.utils.json.JSONWriter;
+
+@SlingServlet(
+        resourceTypes = {"sling/capabilities"},
+        selectors = {"capabilities"},
+        methods = "GET",
+        extensions = "json"
+)
+public class CapabilitiesServlet extends SlingSafeMethodsServlet {
+    
+    public final static String PROBE_PROP_SUFFIX = "_probe";
+    public final static String CAPS_KEY = "org.apache.sling.capabilities";
+    private final ProbeFactory factory = new ProbeFactory();
+    
+    @Override
+    protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
+        response.setContentType("application/json");
+        response.setCharacterEncoding("UTF-8");
+        
+        final JSONWriter jw = new JSONWriter(response.getWriter());
+        jw.object();
+        jw.key(CAPS_KEY);
+        jw.object();
+        
+        for(String def : getProbeDefinitions(request.getResource())) {
+            final Probe p = factory.buildProbe(def);
+            String value = null;
+            try {
+                value = p.getValue();
+            } catch(Exception e) {
+                value = "EXCEPTION:" + e.getClass().getName() + ":" + e.getMessage();
+            }
+            jw.key(p.getName());
+            jw.value(value);
+        }
+        
+        jw.endObject();
+        jw.endObject();
+        response.getWriter().flush();
+    }
+    
+    Collection<String> getProbeDefinitions(Resource r) {
+        final ArrayList<String> result = new ArrayList();
+        final ValueMap vm = r.adaptTo(ValueMap.class);
+        if(vm != null) {
+            for(String key : vm.keySet()) {
+                if(key.endsWith(PROBE_PROP_SUFFIX)) {
+                    result.add(vm.get(key, String.class));
+                }
+            }
+        }
+        return result;
+    }
+}
\ No newline at end of file
diff --git a/capabilities/src/main/java/org/apache/sling/capabilities/internal/HealthCheckProbe.java b/capabilities/src/main/java/org/apache/sling/capabilities/internal/HealthCheckProbe.java
new file mode 100644
index 0000000..7b50d50
--- /dev/null
+++ b/capabilities/src/main/java/org/apache/sling/capabilities/internal/HealthCheckProbe.java
@@ -0,0 +1,47 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.capabilities.internal;
+
+/** A Probe that executes Health Checks to find out if 
+ *  specific capabilities are present.
+ *  The capability value is "true" if all HCs are OK,
+ *  "false" otherwise.
+ */
+class HealthCheckProbe implements Probe {
+    public static final String ID = "hc";
+    
+    private final String name;
+    private final String tags;
+    
+    HealthCheckProbe(String name, String tags) {
+        this.name = name;
+        this.tags = tags;
+    }
+
+    @Override
+    public String getName() {
+        return name;
+    }
+    
+    @Override
+    public String getValue() {
+        // TODO implement this
+        return tags;
+    }
+}
diff --git a/capabilities/src/main/java/org/apache/sling/capabilities/internal/Probe.java b/capabilities/src/main/java/org/apache/sling/capabilities/internal/Probe.java
new file mode 100644
index 0000000..8446667
--- /dev/null
+++ b/capabilities/src/main/java/org/apache/sling/capabilities/internal/Probe.java
@@ -0,0 +1,24 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.capabilities.internal;
+
+interface Probe {
+    String getName();
+    String getValue() throws Exception;
+}
diff --git a/capabilities/src/main/java/org/apache/sling/capabilities/internal/ProbeFactory.java b/capabilities/src/main/java/org/apache/sling/capabilities/internal/ProbeFactory.java
new file mode 100644
index 0000000..11d2240
--- /dev/null
+++ b/capabilities/src/main/java/org/apache/sling/capabilities/internal/ProbeFactory.java
@@ -0,0 +1,33 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.capabilities.internal;
+
+class ProbeFactory {
+    Probe buildProbe(String definition) {
+        final String [] parts = definition.split(":");
+        
+        if(ScriptProbe.ID.equals(parts[0])) {
+            return new ScriptProbe(parts[1], parts[2]);
+        } else if(HealthCheckProbe.ID.equals(parts[0])) {
+            return new HealthCheckProbe(parts[1], parts[2]);
+        } else {
+            throw new IllegalArgumentException("Invalid probe type " + parts[0] + " in " + definition);
+        }
+    }
+}
diff --git a/capabilities/src/main/java/org/apache/sling/capabilities/internal/ScriptProbe.java b/capabilities/src/main/java/org/apache/sling/capabilities/internal/ScriptProbe.java
new file mode 100644
index 0000000..4621db7
--- /dev/null
+++ b/capabilities/src/main/java/org/apache/sling/capabilities/internal/ScriptProbe.java
@@ -0,0 +1,50 @@
+/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ ~ 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.capabilities.internal;
+
+/** A Probe that executes a script to find out if 
+ *  specific capabilities are present.
+ *  The capability value is the output of the script.
+ */
+class ScriptProbe implements Probe {
+    
+    public static final String ID = "script";
+    
+    private final String name;
+    private final String script;
+    
+    ScriptProbe(String name, String script) {
+        this.name = name;
+        this.script = script;
+    }
+
+    @Override
+    public String getName() {
+        return name;
+    }
+    
+    @Override
+    public String getValue() {
+        // TODO implement this
+        if(script.contains("EX")) {
+            throw new IllegalArgumentException("foobar");
+        }
+        return script;
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
bdelacretaz@apache.org.