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/10 11:43:58 UTC

svn commit: r1336565 - in /karaf/cellar/trunk/management/src/main: java/org/apache/karaf/cellar/management/CellarFeaturesMBean.java java/org/apache/karaf/cellar/management/internal/CellarFeaturesMBeanImpl.java resources/OSGI-INF/blueprint/blueprint.xml

Author: jbonofre
Date: Thu May 10 09:43:58 2012
New Revision: 1336565

URL: http://svn.apache.org/viewvc?rev=1336565&view=rev
Log:
[KARAF-1460] CellarFeatureMBean now provides repository operations

Modified:
    karaf/cellar/trunk/management/src/main/java/org/apache/karaf/cellar/management/CellarFeaturesMBean.java
    karaf/cellar/trunk/management/src/main/java/org/apache/karaf/cellar/management/internal/CellarFeaturesMBeanImpl.java
    karaf/cellar/trunk/management/src/main/resources/OSGI-INF/blueprint/blueprint.xml

Modified: karaf/cellar/trunk/management/src/main/java/org/apache/karaf/cellar/management/CellarFeaturesMBean.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/management/src/main/java/org/apache/karaf/cellar/management/CellarFeaturesMBean.java?rev=1336565&r1=1336564&r2=1336565&view=diff
==============================================================================
--- karaf/cellar/trunk/management/src/main/java/org/apache/karaf/cellar/management/CellarFeaturesMBean.java (original)
+++ karaf/cellar/trunk/management/src/main/java/org/apache/karaf/cellar/management/CellarFeaturesMBean.java Thu May 10 09:43:58 2012
@@ -14,17 +14,21 @@
 package org.apache.karaf.cellar.management;
 
 import javax.management.openmbean.TabularData;
+import java.util.List;
 
 /**
  * MBean interface describing the operations and attributes on a Cellar feature.
  */
 public interface CellarFeaturesMBean {
 
-    // operations
+    void addUrl(String group, String url) throws Exception;
+    void removeUrl(String group, String url) throws Exception;
     void install(String group, String name) throws Exception;
     void install(String group, String name, String version) throws Exception;
     void uninstall(String group, String name) throws Exception;
     void uninstall(String group, String name, String version) throws Exception;
+
+    List<String> getUrls(String group) throws Exception;
     TabularData getFeatures(String group) throws Exception;
 
 }

Modified: karaf/cellar/trunk/management/src/main/java/org/apache/karaf/cellar/management/internal/CellarFeaturesMBeanImpl.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/management/src/main/java/org/apache/karaf/cellar/management/internal/CellarFeaturesMBeanImpl.java?rev=1336565&r1=1336564&r2=1336565&view=diff
==============================================================================
--- karaf/cellar/trunk/management/src/main/java/org/apache/karaf/cellar/management/internal/CellarFeaturesMBeanImpl.java (original)
+++ karaf/cellar/trunk/management/src/main/java/org/apache/karaf/cellar/management/internal/CellarFeaturesMBeanImpl.java Thu May 10 09:43:58 2012
@@ -16,18 +16,21 @@ package org.apache.karaf.cellar.manageme
 import org.apache.karaf.cellar.core.*;
 import org.apache.karaf.cellar.core.control.SwitchStatus;
 import org.apache.karaf.cellar.core.event.EventProducer;
-import org.apache.karaf.cellar.core.event.EventTransportFactory;
 import org.apache.karaf.cellar.core.event.EventType;
 import org.apache.karaf.cellar.features.Constants;
 import org.apache.karaf.cellar.features.FeatureInfo;
 import org.apache.karaf.cellar.features.RemoteFeaturesEvent;
+import org.apache.karaf.cellar.features.RemoteRepositoryEvent;
 import org.apache.karaf.cellar.management.CellarFeaturesMBean;
-import org.apache.karaf.features.FeatureEvent;
+import org.apache.karaf.features.*;
 import org.osgi.service.cm.ConfigurationAdmin;
 
 import javax.management.NotCompliantMBeanException;
 import javax.management.StandardMBean;
 import javax.management.openmbean.*;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 
 /**
@@ -38,6 +41,7 @@ public class CellarFeaturesMBeanImpl ext
     private ClusterManager clusterManager;
     private GroupManager groupManager;
     private EventProducer eventProducer;
+    private FeaturesService featuresService;
     private ConfigurationAdmin configurationAdmin;
 
     public CellarFeaturesMBeanImpl() throws NotCompliantMBeanException {
@@ -68,6 +72,14 @@ public class CellarFeaturesMBeanImpl ext
         this.eventProducer = eventProducer;
     }
 
+    public FeaturesService getFeaturesService() {
+        return featuresService;
+    }
+
+    public void setFeaturesService(FeaturesService featuresService) {
+        this.featuresService = featuresService;
+    }
+
     public ConfigurationAdmin getConfigurationAdmin() {
         return configurationAdmin;
     }
@@ -123,7 +135,8 @@ public class CellarFeaturesMBeanImpl ext
             if (feature == null) {
                 if (version == null)
                     throw new IllegalArgumentException("Feature " + name + " doesn't exist for cluster group " + groupName);
-                else throw new IllegalArgumentException("Feature " + name + "/" + version + " doesn't exist for cluster group " + groupName);
+                else
+                    throw new IllegalArgumentException("Feature " + name + "/" + version + " doesn't exist for cluster group " + groupName);
             }
 
             // update the distributed map
@@ -189,7 +202,8 @@ public class CellarFeaturesMBeanImpl ext
             if (feature == null) {
                 if (version == null)
                     throw new IllegalArgumentException("Feature " + name + " doesn't exist for cluster group " + groupName);
-                else throw new IllegalArgumentException("Feature " + name + "/" + version + " doesn't exist for cluster group " + groupName);
+                else
+                    throw new IllegalArgumentException("Feature " + name + "/" + version + " doesn't exist for cluster group " + groupName);
             }
 
             // update the distributed map
@@ -215,7 +229,6 @@ public class CellarFeaturesMBeanImpl ext
 
         TabularType tabularType = new TabularType("Features", "Table of all Karaf Cellar features",
                 featuresType, new String[]{"name", "version"});
-
         TabularData table = new TabularDataSupport(tabularType);
 
         ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
@@ -238,4 +251,180 @@ public class CellarFeaturesMBeanImpl ext
         return table;
     }
 
+    public List<String> getUrls(String groupName) throws Exception {
+        // check if the group exists
+        Group group = groupManager.findGroupByName(groupName);
+        if (group == null) {
+            throw new IllegalArgumentException("Cluster group " + groupName + " doesn't exist");
+        }
+
+        // get the distributed URLs list
+        List<String> urls = clusterManager.getList(Constants.REPOSITORIES + Configurations.SEPARATOR + groupName);
+
+        List<String> result = new ArrayList<String>();
+        for (String url : urls) {
+            result.add(url);
+        }
+
+        return result;
+    }
+
+    public void addUrl(String groupName, String url) 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();
+        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);
+
+            // 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) {
+                        throw new IllegalArgumentException("Repository URL " + url + " is not valid: " + e.getMessage());
+                    }
+                    // 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 {
+                throw new IllegalArgumentException("Repository URL " + url + " already registered");
+            }
+        } finally {
+            Thread.currentThread().setContextClassLoader(originalClassLoader);
+        }
+    }
+
+    public void removeUrl(String groupName, String url) 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");
+        }
+
+        // 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);
+
+        // 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) {
+                    throw new IllegalArgumentException("Repository URL " + url + " is not valid: " + e.getMessage());
+                }
+                // 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 {
+            throw new IllegalArgumentException("Repository URL " + url + " not found");
+        }
+    }
+
 }

Modified: karaf/cellar/trunk/management/src/main/resources/OSGI-INF/blueprint/blueprint.xml
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/management/src/main/resources/OSGI-INF/blueprint/blueprint.xml?rev=1336565&r1=1336564&r2=1336565&view=diff
==============================================================================
--- karaf/cellar/trunk/management/src/main/resources/OSGI-INF/blueprint/blueprint.xml (original)
+++ karaf/cellar/trunk/management/src/main/resources/OSGI-INF/blueprint/blueprint.xml Thu May 10 09:43:58 2012
@@ -20,6 +20,7 @@
     <reference id="executionContext" interface="org.apache.karaf.cellar.core.command.ExecutionContext"/>
     <reference id="eventProducer" interface="org.apache.karaf.cellar.core.event.EventProducer"/>
     <reference id="configurationAdmin" interface="org.osgi.service.cm.ConfigurationAdmin"/>
+    <reference id="featuresService" interface="org.apache.karaf.features.FeaturesService"/>
 
     <reference id="mbeanServer" interface="javax.management.MBeanServer">
         <reference-listener ref="mbeanRegister" bind-method="registerMBeanServer" unbind-method="unregisterMBeanServer"/>
@@ -47,6 +48,7 @@
         <property name="eventProducer" ref="eventProducer"/>
         <property name="groupManager" ref="groupManager"/>
         <property name="configurationAdmin" ref="configurationAdmin"/>
+        <property name="featuresService" ref="featuresService"/>
     </bean>
 
     <bean id="cellarGroupMBean" class="org.apache.karaf.cellar.management.internal.CellarGroupMBeanImpl">