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 2020/07/08 10:28:53 UTC

[felix-dev] branch master updated: FELIX-6291 Gogo command hc:list

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

ghenzler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/felix-dev.git


The following commit(s) were added to refs/heads/master by this push:
     new 9bd6681  FELIX-6291 Gogo command hc:list
9bd6681 is described below

commit 9bd66812f21944925268bfd5551218c436886d30
Author: georg.henzler <ge...@netcentric.biz>
AuthorDate: Wed Jul 8 12:28:17 2020 +0200

    FELIX-6291 Gogo command hc:list
---
 .../core/impl/commands/HealthCheckListCommand.java | 98 ++++++++++++++++++++++
 .../impl/servlet/ResultTxtVerboseSerializer.java   |  2 +-
 2 files changed, 99 insertions(+), 1 deletion(-)

diff --git a/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/commands/HealthCheckListCommand.java b/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/commands/HealthCheckListCommand.java
new file mode 100644
index 0000000..e0db165
--- /dev/null
+++ b/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/commands/HealthCheckListCommand.java
@@ -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.core.impl.commands;
+
+import static org.apache.felix.hc.core.impl.servlet.ResultTxtVerboseSerializer.rightPad;
+
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.apache.felix.hc.api.HealthCheck;
+import org.apache.felix.hc.api.execution.HealthCheckMetadata;
+import org.apache.felix.hc.api.execution.HealthCheckSelector;
+import org.apache.felix.hc.core.impl.util.HealthCheckFilter;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+
+@Component(
+        service = HealthCheckListCommand.class,
+        property = {
+                "osgi.command.scope=hc", 
+                "osgi.command.function=list"
+        }
+        )
+public class HealthCheckListCommand {
+    
+    private BundleContext bundleContext;
+
+    @Activate
+    protected final void activate(final BundleContext bundleContext) {
+        this.bundleContext = bundleContext;
+    }
+    public String list(String... params) {
+
+        boolean isVerbose = false;
+        for (String param : params) {
+            if("-v".equals(param)) {
+                isVerbose = true;
+            } else if("-h".equals(param)) {
+                return getHelpText();
+            } else {
+                System.out.println("unrecognized option: "+param);
+                return getHelpText();
+            }
+        }
+
+        HealthCheckFilter hcFilter = new HealthCheckFilter(bundleContext);
+        HealthCheckSelector selector = HealthCheckSelector.tags("*");
+
+        ServiceReference<HealthCheck>[] hcRefs = hcFilter.getHealthCheckServiceReferences(selector);
+        Stream<ServiceReference<HealthCheck>> hcRefsStream = Arrays.asList(hcRefs).stream();
+        
+        if(isVerbose) {
+
+            return hcRefsStream
+                    .map(ref -> { 
+                        HealthCheckMetadata metadata = new HealthCheckMetadata(ref);
+                        return String.join(" ", 
+                                rightPad(metadata.getTitle(), 60), 
+                                rightPad(String.join(",", metadata.getTags()), 50), 
+                                ref.getBundle().getSymbolicName());
+                    })
+                    .collect(Collectors.joining("\n"));
+            
+        } else {
+
+            return hcRefsStream
+                .flatMap(ref -> { return new HealthCheckMetadata(ref).getTags().stream(); })
+                .distinct()
+                .sorted(Comparator.naturalOrder())
+                .collect(Collectors.joining(","));
+
+        }
+    }
+
+    private String getHelpText() {
+        return "Usage: hc:list [-v]\n  -v verbose";
+    }
+}
diff --git a/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/servlet/ResultTxtVerboseSerializer.java b/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/servlet/ResultTxtVerboseSerializer.java
index 8192636..1148344 100644
--- a/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/servlet/ResultTxtVerboseSerializer.java
+++ b/healthcheck/core/src/main/java/org/apache/felix/hc/core/impl/servlet/ResultTxtVerboseSerializer.java
@@ -141,7 +141,7 @@ public class ResultTxtVerboseSerializer {
         return s.replaceAll("(.{1,"+maxWidth+"})(?: +|$)\\n?|(.{"+maxWidth+"})", "$1$2"+newlineDelimiter).trim();
     }
     
-    static String rightPad(String s, int size) {
+    public static String rightPad(String s, int size) {
         if(s.length() < size) {
             return s + StringUtils.repeat(" ", size - s.length());
         } else {