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 2013/08/05 18:02:54 UTC

svn commit: r1510580 - in /sling/branches/SLING-2987-healthcheck-redesign: hc-core/ hc-core/src/main/java/org/apache/sling/hc/impl/ hc-core/src/main/resources/ hc-core/src/main/resources/res/ hc-core/src/main/resources/res/ui/ it/src/test/java/org/apac...

Author: bdelacretaz
Date: Mon Aug  5 16:02:53 2013
New Revision: 1510580

URL: http://svn.apache.org/r1510580
Log:
SLING-2987 - webconsole plugin, work in progress

Added:
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckWebconsolePlugin.java
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/WebConsoleHelper.java
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/resources/
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/resources/res/
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/resources/res/ui/
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/resources/res/ui/healthcheck.css
Modified:
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/pom.xml
    sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckSelectorImpl.java
    sling/branches/SLING-2987-healthcheck-redesign/it/src/test/java/org/apache/sling/hc/it/core/HealthCheckSelectorTest.java

Modified: sling/branches/SLING-2987-healthcheck-redesign/hc-core/pom.xml
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/pom.xml?rev=1510580&r1=1510579&r2=1510580&view=diff
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/pom.xml (original)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/pom.xml Mon Aug  5 16:02:53 2013
@@ -40,6 +40,11 @@
                         <Export-Package>
                             org.apache.sling.hc.api,
                         </Export-Package>
+                        <Import-Package>
+                            javax.servlet.*;resolution:=optional,
+                            org.apache.sling.api.request;resolution:=optional,
+                            *
+                        </Import-Package>
                         <Private-Package>
                             org.apache.sling.hc.impl.*,
                             org.apache.sling.hc.util.*
@@ -91,12 +96,23 @@
             <scope>provided</scope>
         </dependency>
         <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>org.apache.sling.api</artifactId>
+            <version>2.1.0</version>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-api</artifactId>
             <version>1.6.2</version>
             <scope>provided</scope>
         </dependency>
         <dependency>
+            <groupId>javax.servlet</groupId>
+            <artifactId>servlet-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
+        <dependency>
             <groupId>org.slf4j</groupId>
             <artifactId>slf4j-simple</artifactId>
             <version>1.6.2</version>

Modified: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckSelectorImpl.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckSelectorImpl.java?rev=1510580&r1=1510579&r2=1510580&view=diff
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckSelectorImpl.java (original)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckSelectorImpl.java Mon Aug  5 16:02:53 2013
@@ -52,15 +52,16 @@ public class HealthCheckSelectorImpl imp
         // Build service filter
         final StringBuilder filterBuilder = new StringBuilder();
         filterBuilder.append("(&(objectClass=").append(HealthCheck.class.getName()).append(")");
-        if(tags.length > 0) {
-            final int prefixLen = OMIT_PREFIX.length();
-            for(String tag : tags) {
-                tag = tag.trim();
-                if(tag.startsWith(OMIT_PREFIX)) {
-                    filterBuilder.append("(!(").append(Constants.HC_TAGS).append("=").append(tag.substring(prefixLen)).append("))");
-                } else {
-                    filterBuilder.append("(").append(Constants.HC_TAGS).append("=").append(tag).append(")");
-                }
+        final int prefixLen = OMIT_PREFIX.length();
+        for(String tag : tags) {
+            tag = tag.trim();
+            if(tag.length() == 0) {
+                continue;
+            }
+            if(tag.startsWith(OMIT_PREFIX)) {
+                filterBuilder.append("(!(").append(Constants.HC_TAGS).append("=").append(tag.substring(prefixLen)).append("))");
+            } else {
+                filterBuilder.append("(").append(Constants.HC_TAGS).append("=").append(tag).append(")");
             }
         }
         filterBuilder.append(")");

Added: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckWebconsolePlugin.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckWebconsolePlugin.java?rev=1510580&view=auto
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckWebconsolePlugin.java (added)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/HealthCheckWebconsolePlugin.java Mon Aug  5 16:02:53 2013
@@ -0,0 +1,156 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The SF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package org.apache.sling.hc.impl;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.PrintWriter;
+import java.util.List;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.felix.scr.annotations.Component;
+import org.apache.felix.scr.annotations.Properties;
+import org.apache.felix.scr.annotations.Property;
+import org.apache.felix.scr.annotations.Reference;
+import org.apache.felix.scr.annotations.Service;
+import org.apache.sling.api.request.ResponseUtil;
+import org.apache.sling.hc.api.HealthCheck;
+import org.apache.sling.hc.api.HealthCheckSelector;
+import org.apache.sling.hc.api.Result;
+import org.apache.sling.hc.api.ResultLog;
+import org.osgi.framework.Constants;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/** Webconsole plugin to execute health check services */ 
+@Component(immediate=true)
+@Service(Servlet.class)
+@SuppressWarnings("serial")
+@Properties({
+    @Property(name=Constants.SERVICE_DESCRIPTION, value="Sling Health Check Web Console Plugin"),
+    @Property(name=Constants.SERVICE_VENDOR, value="The Apache Software Foundation"),
+    @Property(name="felix.webconsole.label", value=HealthCheckWebconsolePlugin.LABEL),
+    @Property(name="felix.webconsole.title", value=HealthCheckWebconsolePlugin.TITLE),
+    @Property(name="felix.webconsole.category", value=HealthCheckWebconsolePlugin.CATEGORY),
+    @Property(name="felix.webconsole.css", value="/healthcheck/res/ui/healthcheck.css")
+})
+public class HealthCheckWebconsolePlugin extends HttpServlet {
+
+    private final Logger log = LoggerFactory.getLogger(getClass());
+    
+    public static final String TITLE = "Sling Health Check";
+    public static final String LABEL = "healthcheck";
+    public static final String CATEGORY = "Sling";
+    public static final String PARAM_TAGS = "tags";
+    
+    @Reference
+    private HealthCheckSelector selector;
+    
+    @Override
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+        // Static resource?
+        final String pathInfo = req.getPathInfo();
+        if(pathInfo!= null && pathInfo.contains("res/ui")) {
+            final String prefix = "/" + LABEL;
+            final InputStream is = getClass().getResourceAsStream(pathInfo.substring(prefix.length()));
+            if(is == null) {
+                resp.sendError(HttpServletResponse.SC_NOT_FOUND, pathInfo);
+            }
+            final byte [] buffer = new byte[16384];
+            int n=0;
+            while( (n = is.read(buffer, 0, buffer.length)) > 0) {
+                resp.getOutputStream().write(buffer, 0, n); 
+            }
+            resp.getOutputStream().flush();
+            return;
+        }
+        
+        final String tags = getParam(req, PARAM_TAGS, "");
+        
+        doForm(req, resp, tags);
+        
+        final List<HealthCheck> checks = selector.getTaggedHealthCheck(tags.split(","));
+        final PrintWriter pw = resp.getWriter();
+        pw.println("<table class='content healthcheck' cellpadding='0' cellspacing='0' width='100%'>");
+        for(HealthCheck hc : checks) {
+            final ResultLog rl = new ResultLog(log);
+            renderResult(resp, hc.execute(rl));
+        }
+        pw.println("</table>");
+    }
+    
+    private void renderResult(HttpServletResponse resp, Result result) throws IOException {
+        final WebConsoleHelper c = new WebConsoleHelper(resp.getWriter());
+
+        c.tr();
+        c.tdLabel(ResponseUtil.escapeXml(result.getHealthCheck().toString()));
+        c.closeTd();
+        
+        // TODO tags and info
+        // dataRow(c, "Tags", ResponseUtil.escapeXml(r.getRule().getTags().toString()));
+            
+        c.tdContent();
+        for(ResultLog.Entry e : result.getLogEntries()) {
+            final StringBuilder sb = new StringBuilder();
+            sb.append("<div class='log").append(e.getLevel().toString()).append("'>");
+            sb.append(e.getLevel().toString())
+                .append(" ")
+                .append(ResponseUtil.escapeXml(e.getMessage()))
+                .append("</div>");
+            c.writer().println(sb.toString());
+        }
+        c.closeTd();
+    }
+    
+    private void doForm(HttpServletRequest req, HttpServletResponse resp, String tags) throws IOException {
+        final PrintWriter pw = resp.getWriter();
+        final WebConsoleHelper c = new WebConsoleHelper(pw);
+        pw.print("<form method='get'>");
+        pw.println("<table class='content' cellpadding='0' cellspacing='0' width='100%'>");
+        c.titleHtml(TITLE, "To execute health check services, enter "
+                + " an optional list of tags, to select specific health checks, or no tags for all checks.");
+        
+        c.tr(); 
+        c.tdLabel("Rule tags (comma-separated)");
+        c.tdContent();
+        pw.println("<input type='text' name='" + PARAM_TAGS + "' value='" + tags + "' class='input' size='80'>");
+        c.closeTd(); 
+        c.closeTr();
+        
+        c.tr(); 
+        c.tdContent();
+        pw.println("<input type='submit' value='Execute selected health checks'/>");
+        c.closeTd(); 
+        c.closeTr();
+        
+        pw.println("</table></form>");
+    }
+
+    private String getParam(HttpServletRequest req, String name, String defaultValue) {
+        String result = req.getParameter(name);
+        if(result == null) {
+            result = defaultValue;
+        }
+        return result;
+    }
+}

Added: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/WebConsoleHelper.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/WebConsoleHelper.java?rev=1510580&view=auto
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/WebConsoleHelper.java (added)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/java/org/apache/sling/hc/impl/WebConsoleHelper.java Mon Aug  5 16:02:53 2013
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The SF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations under the License.
+ */
+package org.apache.sling.hc.impl;
+
+import java.io.PrintWriter;
+
+import org.apache.sling.api.request.ResponseUtil;
+
+/** Webconsole plugin to execute health check rules */ 
+class WebConsoleHelper {
+    
+    final PrintWriter pw;
+    
+    WebConsoleHelper(PrintWriter w) {
+        pw = w;
+    }
+
+    PrintWriter writer() {
+        return pw;
+    }
+    
+    void tdContent() {
+        pw.print("<td class='content' colspan='2'>");
+    }
+
+    void closeTd() {
+        pw.print("</td>");
+    }
+
+    void closeTr() {
+        pw.println("</tr>");
+    }
+
+    void tdLabel(final String label) {
+        pw.println("<td class='content'>" + ResponseUtil.escapeXml(label) + "</td>");
+    }
+
+    void tr() {
+        pw.println("<tr class='content'>");
+    }
+
+    void titleHtml(String title, String description) {
+        tr();
+        pw.println("<th colspan='3' class='content container'>" + ResponseUtil.escapeXml(title) + "</th>");
+        closeTr();
+
+        if (description != null) {
+            tr();
+            pw.println("<td colspan='3' class='content'>" +ResponseUtil.escapeXml(description) + "</th>");
+            closeTr();
+        }
+    }
+}

Added: sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/resources/res/ui/healthcheck.css
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/resources/res/ui/healthcheck.css?rev=1510580&view=auto
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/resources/res/ui/healthcheck.css (added)
+++ sling/branches/SLING-2987-healthcheck-redesign/hc-core/src/main/resources/res/ui/healthcheck.css Mon Aug  5 16:02:53 2013
@@ -0,0 +1,38 @@
+/*
+ * 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.
+ */
+
+.healthcheck .logDEBUG {
+    color:grey;
+}
+
+.healthcheck .logINFO {
+    color:blue;
+}
+
+.healthcheck .logWARN {
+    color:red;
+}
+
+.healthcheck .logERROR {
+    color:red;
+    font-weight:bold;
+}
+
+.healthcheck .nothingToReport {
+	color:green;
+    font-weight:bold;
+}

Modified: sling/branches/SLING-2987-healthcheck-redesign/it/src/test/java/org/apache/sling/hc/it/core/HealthCheckSelectorTest.java
URL: http://svn.apache.org/viewvc/sling/branches/SLING-2987-healthcheck-redesign/it/src/test/java/org/apache/sling/hc/it/core/HealthCheckSelectorTest.java?rev=1510580&r1=1510579&r2=1510580&view=diff
==============================================================================
--- sling/branches/SLING-2987-healthcheck-redesign/it/src/test/java/org/apache/sling/hc/it/core/HealthCheckSelectorTest.java (original)
+++ sling/branches/SLING-2987-healthcheck-redesign/it/src/test/java/org/apache/sling/hc/it/core/HealthCheckSelectorTest.java Mon Aug  5 16:02:53 2013
@@ -156,6 +156,12 @@ public class HealthCheckSelectorTest {
     }
     
     @Test
+    public void testEmptyTags() {
+        final List<HealthCheck> s = selector.getTaggedHealthCheck("","","");
+        assertServices(s, true, true, true, true, true);
+    }
+    
+    @Test
     public void testFooTag() {
         final List<HealthCheck> s = selector.getTaggedHealthCheck("foo");
         assertServices(s, true, false, true, false, false);