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 2012/05/07 13:47:14 UTC

svn commit: r1334961 - in /karaf/cellar/trunk/features/src/main: java/org/apache/karaf/cellar/features/ java/org/apache/karaf/cellar/features/shell/ resources/OSGI-INF/blueprint/

Author: jbonofre
Date: Mon May  7 11:47:14 2012
New Revision: 1334961

URL: http://svn.apache.org/viewvc?rev=1334961&view=rev
Log:
[KARAF-1294] Improve features/repositories management and add related commands

Added:
    karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/shell/UrlAddCommand.java
    karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/shell/UrlListCommand.java
    karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/shell/UrlRemoveCommand.java
Modified:
    karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSupport.java
    karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/LocalFeaturesListener.java
    karaf/cellar/trunk/features/src/main/resources/OSGI-INF/blueprint/shell-features.xml

Modified: karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSupport.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSupport.java?rev=1334961&r1=1334960&r2=1334961&view=diff
==============================================================================
--- karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSupport.java (original)
+++ karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/FeaturesSupport.java Mon May  7 11:47:14 2012
@@ -119,9 +119,16 @@ public class FeaturesSupport extends Cel
         String groupName = group.getName();
         List<String> repositories = clusterManager.getList(Constants.REPOSITORIES + Configurations.SEPARATOR + groupName);
 
-        if (featuresService != null && repositories != null) {
-            URI uri = repository.getURI();
-            repositories.add(uri.toString());
+        boolean found = false;
+        for (String registeredRepository : repositories) {
+            if (registeredRepository.equals(repository.getURI().toString())) {
+                found = true;
+                break;
+            }
+        }
+
+        if (!found) {
+            repositories.add(repository.getURI().toString());
         }
     }
 

Modified: karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/LocalFeaturesListener.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/LocalFeaturesListener.java?rev=1334961&r1=1334960&r2=1334961&view=diff
==============================================================================
--- karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/LocalFeaturesListener.java (original)
+++ karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/LocalFeaturesListener.java Mon May  7 11:47:14 2012
@@ -13,6 +13,7 @@
  */
 package org.apache.karaf.cellar.features;
 
+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;
@@ -24,6 +25,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 
 /**
@@ -113,8 +115,28 @@ public class LocalFeaturesListener exten
                     // update the distributed map
                     if (RepositoryEvent.EventType.RepositoryAdded.equals(type)){
                         pushRepository(event.getRepository(), group);
+                        // update the feature map
+                        Map<FeatureInfo, Boolean> distributedFeatures = clusterManager.getMap(Constants.FEATURES + Configurations.SEPARATOR + group.getName());
+                        try {
+                            for (Feature feature : event.getRepository().getFeatures()) {
+                                FeatureInfo info = new FeatureInfo(feature.getName(), feature.getVersion());
+                                distributedFeatures.put(info, false);
+                            }
+                        } catch (Exception e) {
+                            LOGGER.warn("CELLAR FEATURES: can't update the distributed features map", e);
+                        }
                     } else {
                         removeRepository(event.getRepository(),group);
+                        // update the feature map
+                        Map<FeatureInfo, Boolean> distributedFeatures = clusterManager.getMap(Constants.FEATURES + Configurations.SEPARATOR + group.getName());
+                        try {
+                            for (Feature feature : event.getRepository().getFeatures()) {
+                                FeatureInfo info = new FeatureInfo(feature.getName(), feature.getVersion());
+                                distributedFeatures.remove(info);
+                            }
+                        } catch (Exception e) {
+                            LOGGER.warn("CELLAR FEATURES: can't update the distributed features map", e);
+                        }
                     }
                     eventProducer.produce(repositoryEvent);
                 }

Added: karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/shell/UrlAddCommand.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/shell/UrlAddCommand.java?rev=1334961&view=auto
==============================================================================
--- karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/shell/UrlAddCommand.java (added)
+++ karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/shell/UrlAddCommand.java Mon May  7 11:47:14 2012
@@ -0,0 +1,142 @@
+/*
+ * 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.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Command;
+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.features.Constants;
+import org.apache.karaf.cellar.features.FeatureInfo;
+import org.apache.karaf.cellar.features.RemoteRepositoryEvent;
+import org.apache.karaf.features.Feature;
+import org.apache.karaf.features.Repository;
+import org.apache.karaf.features.RepositoryEvent;
+
+import java.net.URI;
+import java.util.List;
+import java.util.Map;
+
+@Command(scope = "cluster", name = "feature-url-add", description = "Adds a list of repository URLs to the features service in the given cluster group")
+public class UrlAddCommand extends FeatureCommandSupport {
+
+    @Argument(index = 0, name = "group", description = "The cluster group name", required = true, multiValued = false)
+    String groupName;
+
+    @Argument(index = 1, name = "urls", description = "One or more repository URLs separated by whitespaces", required = true, multiValued = true)
+    List<String> urls;
+
+    private EventProducer eventProducer;
+
+    @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();
+        try {
+            Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
+            // get the repositories distributed list
+            List<String> distributedRepositories = clusterManager.getList(Constants.REPOSITORIES + Configurations.SEPARATOR + groupName);
+            // get the features distributed map
+            Map<FeatureInfo, Boolean> distributedFeatures = clusterManager.getMap(Constants.FEATURES + Configurations.SEPARATOR + groupName);
+
+            for (String url : urls) {
+                // check if the URL is already registered
+                boolean found = false;
+                for (String repository : distributedRepositories) {
+                    if (repository.equals(url)) {
+                        found = true;
+                        break;
+                    }
+                }
+                if (!found) {
+                    // update the repository temporary locally
+                    Repository repository  = null;
+                    boolean localRegistered = false;
+                    // local lookup
+                    for (Repository registeredRepository : featuresService.listRepositories()) {
+                        if (registeredRepository.getURI().equals(new URI(url))) {
+                            repository = registeredRepository;
+                            break;
+                        }
+                    }
+                    if (repository == null) {
+                        // registered locally
+                        try {
+                            featuresService.addRepository(new URI(url));
+                        } catch (Exception e) {
+                            System.err.println("Repository URL " + url + " is not valid: " + e.getMessage());
+                            continue;
+                        }
+                        // get the repository
+                        for (Repository registeredRepository : featuresService.listRepositories()) {
+                            if (registeredRepository.getURI().equals(new URI(url))) {
+                                repository = registeredRepository;
+                                break;
+                            }
+                        }
+                    } else {
+                        localRegistered = true;
+                    }
+
+                    // update the distributed list
+                    distributedRepositories.add(url);
+
+                    // update the distributed feature map
+                    for (Feature feature : repository.getFeatures()) {
+                        FeatureInfo info = new FeatureInfo(feature.getName(), feature.getVersion());
+                        distributedFeatures.put(info, false);
+                    }
+
+                    // unregister the repository if it's not local registered
+                    if (!localRegistered)
+                        featuresService.removeRepository(new URI(url));
+
+                    // broadcast the cluster event
+                    RemoteRepositoryEvent event = new RemoteRepositoryEvent(url, RepositoryEvent.EventType.RepositoryAdded);
+                    event.setSourceGroup(group);
+                    eventProducer.produce(event);
+                } else {
+                    System.err.println("Repository URL " + url + " already registered");
+                }
+            }
+        } finally {
+            Thread.currentThread().setContextClassLoader(originalClassLoader);
+        }
+
+        return null;
+    }
+
+    public EventProducer getEventProducer() {
+        return eventProducer;
+    }
+
+    public void setEventProducer(EventProducer eventProducer) {
+        this.eventProducer = eventProducer;
+    }
+
+}

Added: karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/shell/UrlListCommand.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/shell/UrlListCommand.java?rev=1334961&view=auto
==============================================================================
--- karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/shell/UrlListCommand.java (added)
+++ karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/shell/UrlListCommand.java Mon May  7 11:47:14 2012
@@ -0,0 +1,49 @@
+/*
+ * 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.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Command;
+import org.apache.karaf.cellar.core.Configurations;
+import org.apache.karaf.cellar.core.Group;
+import org.apache.karaf.cellar.features.Constants;
+
+import java.util.List;
+
+@Command(scope = "cluster", name = "feature-url-list", description = "Displays a list of all defined repository URLs in the given cluster group")
+public class UrlListCommand extends FeatureCommandSupport {
+
+    @Argument(index = 0, name = "group", description = "The cluster group name", required = true, multiValued = false)
+    String groupName;
+
+    @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;
+        }
+
+        // get the distributed list
+        List<String> repositories = clusterManager.getList(Constants.REPOSITORIES + Configurations.SEPARATOR + groupName);
+
+        for (String repository : repositories) {
+            System.out.println(repository);
+        }
+
+        return null;
+    }
+
+}

Added: karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/shell/UrlRemoveCommand.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/shell/UrlRemoveCommand.java?rev=1334961&view=auto
==============================================================================
--- karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/shell/UrlRemoveCommand.java (added)
+++ karaf/cellar/trunk/features/src/main/java/org/apache/karaf/cellar/features/shell/UrlRemoveCommand.java Mon May  7 11:47:14 2012
@@ -0,0 +1,136 @@
+/*
+ * 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.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Command;
+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.features.Constants;
+import org.apache.karaf.cellar.features.FeatureInfo;
+import org.apache.karaf.cellar.features.RemoteRepositoryEvent;
+import org.apache.karaf.features.Feature;
+import org.apache.karaf.features.Repository;
+import org.apache.karaf.features.RepositoryEvent;
+
+import java.net.URI;
+import java.util.List;
+import java.util.Map;
+
+@Command(scope = "cluster", name = "feature-url-remove", description = "Removes the given list of repository URLs for the given cluster group")
+public class UrlRemoveCommand extends FeatureCommandSupport {
+
+    @Argument(index = 0, name = "group", description = "The cluster group name", required = true, multiValued = false)
+    String groupName;
+
+    @Argument(index = 1, name = "urls", description = "One or more repository URLs separated by whitespaces", required = true, multiValued = true)
+    List<String> urls;
+
+    private EventProducer eventProducer;
+
+    @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;
+        }
+
+        // get the distributed list
+        List<String> distributedRepositories = clusterManager.getList(Constants.REPOSITORIES + Configurations.SEPARATOR + groupName);
+        // get the features distributed map
+        Map<FeatureInfo, Boolean> distributedFeatures = clusterManager.getMap(Constants.FEATURES + Configurations.SEPARATOR + groupName);
+
+        for (String url : urls) {
+            // looking for the URL in the list
+            boolean found = false;
+            for (String repository : distributedRepositories) {
+                if (repository.equals(url)) {
+                    found = true;
+                    break;
+                }
+            }
+            if (found) {
+                // update the repository temporary locally
+                Repository repository  = null;
+                boolean localRegistered = false;
+                // local lookup
+                for (Repository registeredRepository : featuresService.listRepositories()) {
+                    if (registeredRepository.getURI().equals(new URI(url))) {
+                        repository = registeredRepository;
+                        break;
+                    }
+                }
+                if (repository == null) {
+                    // registered locally
+                    try {
+                        featuresService.addRepository(new URI(url));
+                    } catch (Exception e) {
+                        System.err.println("Repository URL " + url + " is not valid: " + e.getMessage());
+                        continue;
+                    }
+                    // get the repository
+                    for (Repository registeredRepository : featuresService.listRepositories()) {
+                        if (registeredRepository.getURI().equals(new URI(url))) {
+                            repository = registeredRepository;
+                            break;
+                        }
+                    }
+                } else {
+                    localRegistered = true;
+                }
+
+                // update the list
+                distributedRepositories.remove(url);
+
+                // update the distributed feature map
+                for (Feature feature : repository.getFeatures()) {
+                    FeatureInfo info = new FeatureInfo(feature.getName(), feature.getVersion());
+                    distributedFeatures.remove(info);
+                }
+
+                // unregister the repository if it's not local registered
+                if (!localRegistered)
+                    featuresService.removeRepository(new URI(url));
+
+                // broadcast a cluster event
+                RemoteRepositoryEvent event = new RemoteRepositoryEvent(url, RepositoryEvent.EventType.RepositoryRemoved);
+                event.setSourceGroup(group);
+                eventProducer.produce(event);
+            } else {
+                System.err.println("Repository URL " + url + " not found");
+            }
+        }
+
+        return null;
+    }
+
+    public EventProducer getEventProducer() {
+        return eventProducer;
+    }
+
+    public void setEventProducer(EventProducer eventProducer) {
+        this.eventProducer = eventProducer;
+    }
+
+}

Modified: karaf/cellar/trunk/features/src/main/resources/OSGI-INF/blueprint/shell-features.xml
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/features/src/main/resources/OSGI-INF/blueprint/shell-features.xml?rev=1334961&r1=1334960&r2=1334961&view=diff
==============================================================================
--- karaf/cellar/trunk/features/src/main/resources/OSGI-INF/blueprint/shell-features.xml (original)
+++ karaf/cellar/trunk/features/src/main/resources/OSGI-INF/blueprint/shell-features.xml Mon May  7 11:47:14 2012
@@ -50,6 +50,37 @@
                 <ref component-id="allGroupCompleter"/>
             </completers>
         </command>
+        <command name="cluster/feature-url-list">
+            <action class="org.apache.karaf.cellar.features.shell.UrlListCommand">
+                <property name="clusterManager" ref="clusterManager"/>
+                <property name="groupManager" ref="groupManager"/>
+            </action>
+            <completers>
+                <ref component-id="allGroupCompleter"/>
+            </completers>
+        </command>
+        <command name="cluster/feature-url-add">
+            <action class="org.apache.karaf.cellar.features.shell.UrlAddCommand">
+                <property name="clusterManager" ref="clusterManager"/>
+                <property name="groupManager" ref="groupManager"/>
+                <property name="eventProducer" ref="eventProducer"/>
+                <property name="featuresService" ref="featuresService"/>
+            </action>
+            <completers>
+                <ref component-id="allGroupCompleter"/>
+            </completers>
+        </command>
+        <command name="cluster/feature-url-remove">
+            <action class="org.apache.karaf.cellar.features.shell.UrlRemoveCommand">
+                <property name="clusterManager" ref="clusterManager"/>
+                <property name="groupManager" ref="groupManager"/>
+                <property name="eventProducer" ref="eventProducer"/>
+                <property name="featuresService" ref="featuresService"/>
+            </action>
+            <completers>
+                <ref component-id="allGroupCompleter"/>
+            </completers>
+        </command>
     </command-bundle>
 
     <bean id="allGroupCompleter" class="org.apache.karaf.cellar.core.shell.completer.AllGroupsCompleter">