You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by jb...@apache.org on 2022/09/30 12:30:22 UTC

[karaf] branch karaf-4.3.x updated: KARAF-7554 - add feature:status command

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

jbonofre pushed a commit to branch karaf-4.3.x
in repository https://gitbox.apache.org/repos/asf/karaf.git


The following commit(s) were added to refs/heads/karaf-4.3.x by this push:
     new 0e03d41cd1 KARAF-7554 - add feature:status command
0e03d41cd1 is described below

commit 0e03d41cd1414bf17ca336c30dccd824a1b63dbd
Author: Aleksy Wróblewski <al...@bbbit.io>
AuthorDate: Wed Sep 21 19:43:58 2022 +0200

    KARAF-7554 - add feature:status command
    
    (cherry picked from commit aff059526139059bc181a23907fe50b518f5855d)
---
 .../karaf/features/command/StatusCommand.java      | 54 +++++++++++++++++
 .../completers/FeatureVersionCompleter.java        | 70 ++++++++++++++++++++++
 .../java/org/apache/karaf/itests/FeatureTest.java  | 12 ++++
 3 files changed, 136 insertions(+)

diff --git a/features/command/src/main/java/org/apache/karaf/features/command/StatusCommand.java b/features/command/src/main/java/org/apache/karaf/features/command/StatusCommand.java
new file mode 100644
index 0000000000..b494012833
--- /dev/null
+++ b/features/command/src/main/java/org/apache/karaf/features/command/StatusCommand.java
@@ -0,0 +1,54 @@
+/**
+ *
+ * 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.karaf.features.command;
+
+import org.apache.karaf.features.FeaturesService;
+import org.apache.karaf.features.command.completers.AllFeatureCompleter;
+import org.apache.karaf.features.command.completers.FeatureVersionCompleter;
+import org.apache.karaf.shell.api.action.Argument;
+import org.apache.karaf.shell.api.action.Command;
+import org.apache.karaf.shell.api.action.Completion;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+
+@Command(scope = "feature", name = "status", description = "Get the feature's current status")
+@Service
+public class StatusCommand extends FeaturesCommandSupport {
+
+    @Argument(index = 0, name = "feature", description = "Name of the feature.", required = true)
+    @Completion(AllFeatureCompleter.class)
+    String feature;
+
+    @Argument(index = 1, name = "version", description = "Optional version of the feature.")
+    @Completion(value = FeatureVersionCompleter.class)
+    String version;
+
+    @Override
+    protected void doExecute(FeaturesService admin) throws Exception {
+        System.out.println(getState(admin));
+    }
+
+    private String getState(FeaturesService featuresService) throws Exception {
+        String id;
+        if (version == null) {
+            id = featuresService.getFeature(feature).getId();
+        } else {
+            id = featuresService.getFeature(feature, version).getId();
+        }
+        return featuresService.getState(id).name();
+    }
+}
diff --git a/features/command/src/main/java/org/apache/karaf/features/command/completers/FeatureVersionCompleter.java b/features/command/src/main/java/org/apache/karaf/features/command/completers/FeatureVersionCompleter.java
new file mode 100644
index 0000000000..959dfe65d1
--- /dev/null
+++ b/features/command/src/main/java/org/apache/karaf/features/command/completers/FeatureVersionCompleter.java
@@ -0,0 +1,70 @@
+/**
+ *
+ * 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.karaf.features.command.completers;
+
+import org.apache.karaf.features.Feature;
+import org.apache.karaf.features.FeaturesService;
+import org.apache.karaf.features.Repository;
+import org.apache.karaf.shell.api.action.lifecycle.Reference;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+import org.apache.karaf.shell.api.console.CommandLine;
+import org.apache.karaf.shell.api.console.Completer;
+import org.apache.karaf.shell.api.console.Session;
+import org.apache.karaf.shell.support.completers.StringsCompleter;
+
+import java.util.ArrayList;
+import java.util.List;
+
+@Service
+public class FeatureVersionCompleter implements Completer {
+
+    @Reference
+    private FeaturesService featuresService;
+
+    @Override
+    public int complete(Session session, CommandLine commandLine, List<String> candidates) {
+        StringsCompleter delegate = new StringsCompleter();
+        String[] args = commandLine.getArguments();
+        // args look like this at this point: [feature:status, wrapper, '']
+        if (args.length >= 3) {
+            String featureArg = args[1];
+            try {
+                List<String> versions = getAllVersionsOfFeature(featureArg, featuresService);
+                delegate.getStrings().addAll(versions);
+                return delegate.complete(session, commandLine, candidates);
+            } catch (Exception e) {
+                // Ignore
+            }
+        }
+
+        return delegate.complete(session, commandLine, candidates);
+    }
+
+    private List<String> getAllVersionsOfFeature(String feature, FeaturesService featuresService) throws Exception {
+        List<String> versions = new ArrayList<>();
+        for (Repository repo : featuresService.listRepositories()) {
+            for (Feature f : repo.getFeatures()) {
+                if (f.getName().equals(feature)) {
+                    versions.add(f.getVersion());
+                }
+            }
+        }
+
+        return versions;
+    }
+}
diff --git a/itests/test/src/test/java/org/apache/karaf/itests/FeatureTest.java b/itests/test/src/test/java/org/apache/karaf/itests/FeatureTest.java
index 977433c8c0..8c6d6f3796 100644
--- a/itests/test/src/test/java/org/apache/karaf/itests/FeatureTest.java
+++ b/itests/test/src/test/java/org/apache/karaf/itests/FeatureTest.java
@@ -21,6 +21,7 @@ import javax.management.MBeanServer;
 import javax.management.ObjectName;
 import javax.management.openmbean.TabularData;
 
+import org.apache.karaf.features.FeatureState;
 import org.apache.karaf.jaas.boot.principal.RolePrincipal;
 
 import org.junit.Test;
@@ -153,6 +154,17 @@ public class FeatureTest extends BaseTest {
         mbeanServer.invoke(name, "refreshRepository", new Object[] { ".*pax-web.*" }, new String[]{ "java.lang.String" });
     }
 
+    @Test
+    public void statusCommand() throws Exception {
+        executeCommand("feature:install -v -r wrapper", new RolePrincipal("admin"));
+        String featureStatus = executeCommand("feature:status wrapper");
+        assertContains(FeatureState.Started.name(), featureStatus);
+
+        executeCommand("feature:uninstall wrapper", new RolePrincipal("admin"));
+        featureStatus = executeCommand("feature:status wrapper");
+        assertContains(FeatureState.Uninstalled.name(), featureStatus);
+    }
+
     @Test
     public void configRegularLifecycle() throws Exception {
         System.out.println(executeCommand("feature:install http", new RolePrincipal("admin")));