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 2016/07/10 19:21:05 UTC

karaf-cellar git commit: [KARAF-4316] Add cluster:feature-repo-refresh command (and corresponding operation in MBean)

Repository: karaf-cellar
Updated Branches:
  refs/heads/master 0ada60853 -> 8e98de8ab


[KARAF-4316] Add cluster:feature-repo-refresh command (and corresponding operation in MBean)


Project: http://git-wip-us.apache.org/repos/asf/karaf-cellar/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf-cellar/commit/8e98de8a
Tree: http://git-wip-us.apache.org/repos/asf/karaf-cellar/tree/8e98de8a
Diff: http://git-wip-us.apache.org/repos/asf/karaf-cellar/diff/8e98de8a

Branch: refs/heads/master
Commit: 8e98de8ab968ce1f5382cfe07b53d74c6dd243fd
Parents: 0ada608
Author: Jean-Baptiste Onofr� <jb...@apache.org>
Authored: Sun Jul 10 21:20:32 2016 +0200
Committer: Jean-Baptiste Onofr� <jb...@apache.org>
Committed: Sun Jul 10 21:20:32 2016 +0200

----------------------------------------------------------------------
 .../cellar/features/ClusterRepositoryEvent.java |   9 ++
 .../cellar/features/RepositoryEventHandler.java |  13 ++-
 .../management/CellarFeaturesMBean.java         |  10 ++
 .../internal/CellarFeaturesMBeanImpl.java       |  38 +++++++
 .../features/shell/RepoRefreshCommand.java      | 100 +++++++++++++++++++
 manual/src/main/asciidoc/user-guide/groups.adoc |   6 ++
 6 files changed, 172 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/8e98de8a/features/src/main/java/org/apache/karaf/cellar/features/ClusterRepositoryEvent.java
----------------------------------------------------------------------
diff --git a/features/src/main/java/org/apache/karaf/cellar/features/ClusterRepositoryEvent.java b/features/src/main/java/org/apache/karaf/cellar/features/ClusterRepositoryEvent.java
index 9480483..784c492 100644
--- a/features/src/main/java/org/apache/karaf/cellar/features/ClusterRepositoryEvent.java
+++ b/features/src/main/java/org/apache/karaf/cellar/features/ClusterRepositoryEvent.java
@@ -24,6 +24,7 @@ public class ClusterRepositoryEvent extends Event {
 	private EventType type;
     private Boolean install;
     private Boolean uninstall;
+    private Boolean refresh;
 
     public ClusterRepositoryEvent(String id, EventType type) {
         super(id);
@@ -50,6 +51,14 @@ public class ClusterRepositoryEvent extends Event {
         this.uninstall = uninstall;
     }
 
+    public Boolean getRefresh() {
+        return refresh;
+    }
+
+    public void setRefresh(Boolean refresh) {
+        this.refresh = refresh;
+    }
+
     @Override
 	public String toString() {
 		return "ClusterRepositoryEvent [type=" + type + ", id=" + id

http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/8e98de8a/features/src/main/java/org/apache/karaf/cellar/features/RepositoryEventHandler.java
----------------------------------------------------------------------
diff --git a/features/src/main/java/org/apache/karaf/cellar/features/RepositoryEventHandler.java b/features/src/main/java/org/apache/karaf/cellar/features/RepositoryEventHandler.java
index 494537c..d974b5b 100644
--- a/features/src/main/java/org/apache/karaf/cellar/features/RepositoryEventHandler.java
+++ b/features/src/main/java/org/apache/karaf/cellar/features/RepositoryEventHandler.java
@@ -75,11 +75,16 @@ public class RepositoryEventHandler extends FeaturesSupport implements EventHand
         try {
             // TODO check if isAllowed
             if (RepositoryEvent.EventType.RepositoryAdded.equals(type)) {
-                if (!isRepositoryRegisteredLocally(uri)) {
-                    LOGGER.debug("CELLAR FEATURE: adding repository URI {}", uri);
-                    featuresService.addRepository(new URI(uri), event.getInstall());
+                if (event.getRefresh()) {
+                    LOGGER.debug("CELLAR FEATURE: refresh repository {}", uri);
+                    featuresService.refreshRepository(new URI(uri));
                 } else {
-                    LOGGER.debug("CELLAR FEATURE: repository URI {} is already registered locally");
+                    if (!isRepositoryRegisteredLocally(uri)) {
+                        LOGGER.debug("CELLAR FEATURE: adding repository URI {}", uri);
+                        featuresService.addRepository(new URI(uri), event.getInstall());
+                    } else {
+                        LOGGER.debug("CELLAR FEATURE: repository URI {} is already registered locally", uri);
+                    }
                 }
             } else {
                 if (isRepositoryRegisteredLocally(uri)) {

http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/8e98de8a/features/src/main/java/org/apache/karaf/cellar/features/management/CellarFeaturesMBean.java
----------------------------------------------------------------------
diff --git a/features/src/main/java/org/apache/karaf/cellar/features/management/CellarFeaturesMBean.java b/features/src/main/java/org/apache/karaf/cellar/features/management/CellarFeaturesMBean.java
index faa6db1..ea63a29 100644
--- a/features/src/main/java/org/apache/karaf/cellar/features/management/CellarFeaturesMBean.java
+++ b/features/src/main/java/org/apache/karaf/cellar/features/management/CellarFeaturesMBean.java
@@ -71,6 +71,16 @@ public interface CellarFeaturesMBean {
     void removeRepository(String group, String repository, boolean uninstall) throws Exception;
 
     /**
+     * Refresh a features repository in a cluster group.
+     *
+     * @param group the cluster group name.
+     * @param repository the features repository name or URL.
+     * @param version the features repository version.
+     * @throws Exception
+     */
+    void refreshRepository(String group, String repository, String version) throws Exception;
+
+    /**
      * Install a feature in a cluster group.
      *
      * @param group the cluster group name.

http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/8e98de8a/features/src/main/java/org/apache/karaf/cellar/features/management/internal/CellarFeaturesMBeanImpl.java
----------------------------------------------------------------------
diff --git a/features/src/main/java/org/apache/karaf/cellar/features/management/internal/CellarFeaturesMBeanImpl.java b/features/src/main/java/org/apache/karaf/cellar/features/management/internal/CellarFeaturesMBeanImpl.java
index 1361107..696802c 100644
--- a/features/src/main/java/org/apache/karaf/cellar/features/management/internal/CellarFeaturesMBeanImpl.java
+++ b/features/src/main/java/org/apache/karaf/cellar/features/management/internal/CellarFeaturesMBeanImpl.java
@@ -469,6 +469,44 @@ public class CellarFeaturesMBeanImpl extends StandardMBean implements CellarFeat
     }
 
     @Override
+    public void refreshRepository(String groupName, String nameOrUrl, String version) throws Exception {
+        // check if the group exists
+        Group group = groupManager.findGroupByName(groupName);
+        if (group == null) {
+            throw new IllegalArgumentException("Cluster group " + groupName + " doesn't exist");
+        }
+
+        // check if the event producer is ON
+        if (eventProducer.getSwitch().getStatus().equals(SwitchStatus.OFF)) {
+            throw new IllegalStateException("Cluster event producer is OFF");
+        }
+
+        ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
+        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
+        try {
+            // get the cluster features repositories
+            Map<String, String> clusterFeaturesRepositories = clusterManager.getMap(Constants.REPOSITORIES_MAP + Configurations.SEPARATOR + groupName);
+
+            URI uri = featuresService.getRepositoryUriFor(nameOrUrl, version);
+            if (uri == null) {
+                uri = new URI(nameOrUrl);
+            }
+
+            if (clusterFeaturesRepositories.get(uri) == null) {
+                throw new IllegalArgumentException("Features repository " + nameOrUrl + " doesn't exist in cluster group " + groupName);
+            }
+
+            // broadcast the cluster event
+            ClusterRepositoryEvent event = new ClusterRepositoryEvent(uri.toString(), RepositoryEvent.EventType.RepositoryAdded);
+            event.setRefresh(true);
+            event.setSourceGroup(group);
+            eventProducer.produce(event);
+        } finally {
+            Thread.currentThread().setContextClassLoader(originalClassLoader);
+        }
+    }
+
+    @Override
     public void removeRepository(String groupName, String repository) throws Exception {
         this.removeRepository(groupName, repository, false);
     }

http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/8e98de8a/features/src/main/java/org/apache/karaf/cellar/features/shell/RepoRefreshCommand.java
----------------------------------------------------------------------
diff --git a/features/src/main/java/org/apache/karaf/cellar/features/shell/RepoRefreshCommand.java b/features/src/main/java/org/apache/karaf/cellar/features/shell/RepoRefreshCommand.java
new file mode 100644
index 0000000..9b183bf
--- /dev/null
+++ b/features/src/main/java/org/apache/karaf/cellar/features/shell/RepoRefreshCommand.java
@@ -0,0 +1,100 @@
+/*
+ * Licensed 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.cellar.features.shell;
+
+import org.apache.karaf.cellar.core.Configurations;
+import org.apache.karaf.cellar.core.Group;
+import org.apache.karaf.cellar.core.control.SwitchStatus;
+import org.apache.karaf.cellar.core.event.EventProducer;
+import org.apache.karaf.cellar.core.shell.CellarCommandSupport;
+import org.apache.karaf.cellar.core.shell.completer.AllGroupsCompleter;
+import org.apache.karaf.cellar.features.ClusterRepositoryEvent;
+import org.apache.karaf.cellar.features.Constants;
+import org.apache.karaf.features.FeaturesService;
+import org.apache.karaf.features.RepositoryEvent;
+import org.apache.karaf.features.command.completers.AvailableRepoNameCompleter;
+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.Reference;
+import org.apache.karaf.shell.api.action.lifecycle.Service;
+
+import java.net.URI;
+import java.util.Map;
+
+@Command(scope = "cluster", name = "feature-repo-refresh", description = "Refresh a features repository on the cluster")
+@Service
+public class RepoRefreshCommand extends CellarCommandSupport {
+
+    @Argument(index = 0, name = "group", description = "The cluster group name", required = true, multiValued = false)
+    @Completion(AllGroupsCompleter.class)
+    String groupName;
+
+    @Argument(index = 1, name = "name/url", description = "Shortcut name of the features repository or the full URL", required = true, multiValued = false)
+    @Completion(AvailableRepoNameCompleter.class)
+    String nameOrUrl;
+
+    @Argument(index = 2, name = "version", description = "The version of the features repository if using features repository name as first argument. It should be empty if using the URL.", required = false, multiValued = false)
+    String version;
+
+    @Reference
+    private EventProducer eventProducer;
+
+    @Reference
+    private FeaturesService featuresService;
+
+    @Override
+    protected Object doExecute() throws Exception {
+        // check if the group exists
+        Group group = groupManager.findGroupByName(groupName);
+        if (group == null) {
+            System.err.println("Cluster group " + groupName + " doesn't exist");
+            return null;
+        }
+
+        // check if the event producer is ON
+        if (eventProducer.getSwitch().getStatus().equals(SwitchStatus.OFF)) {
+            System.err.println("Cluster event producer is OFF");
+            return null;
+        }
+
+        ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
+        Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
+        try {
+            // get the cluster features repositories
+            Map<String, String> clusterFeaturesRepositories = clusterManager.getMap(Constants.REPOSITORIES_MAP + Configurations.SEPARATOR + groupName);
+
+            URI uri = featuresService.getRepositoryUriFor(nameOrUrl, version);
+            if (uri == null) {
+                uri = new URI(nameOrUrl);
+            }
+
+            if (clusterFeaturesRepositories.get(uri) == null) {
+                System.err.println("Features repository " + nameOrUrl + " doesn't exist in cluster group " + groupName);
+                return null;
+            }
+
+            // broadcast the cluster event
+            ClusterRepositoryEvent event = new ClusterRepositoryEvent(uri.toString(), RepositoryEvent.EventType.RepositoryAdded);
+            event.setRefresh(true);
+            event.setSourceGroup(group);
+            eventProducer.produce(event);
+        } finally {
+            Thread.currentThread().setContextClassLoader(originalClassLoader);
+        }
+
+        return null;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/8e98de8a/manual/src/main/asciidoc/user-guide/groups.adoc
----------------------------------------------------------------------
diff --git a/manual/src/main/asciidoc/user-guide/groups.adoc b/manual/src/main/asciidoc/user-guide/groups.adoc
index 6e49cb7..74ae35f 100644
--- a/manual/src/main/asciidoc/user-guide/groups.adoc
+++ b/manual/src/main/asciidoc/user-guide/groups.adoc
@@ -83,6 +83,12 @@ You can add a repository on a cluster group using the `cluster:feature-repo-add`
 karaf@node1()> cluster:feature-repo-add default mvn:org.apache.activemq/activemq-karaf/5.10.0/xml/features
 ----
 
+You can refresh a features repository in a cluster group using the `cluster:feature-repo-refresh` command:
+
+----
+karaf@node1()> cluster:feature-repo-refresh default foo
+----
+
 You can remove a repository from a cluster group using the `cluster:feature-repo-remove` command:
 
 ----