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 18:48:38 UTC

svn commit: r1335118 - in /karaf/cellar/trunk/obr: ./ src/main/java/org/apache/karaf/cellar/obr/management/ src/main/java/org/apache/karaf/cellar/obr/management/internal/ src/main/java/org/apache/karaf/cellar/obr/shell/ src/main/resources/OSGI-INF/blue...

Author: jbonofre
Date: Mon May  7 16:48:38 2012
New Revision: 1335118

URL: http://svn.apache.org/viewvc?rev=1335118&view=rev
Log:
[KARAF-1424] Add CellarOBRMBean and fix issues in cluster:obr* commands

Added:
    karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/management/
    karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/management/CellarOBRMBean.java
    karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/management/internal/
    karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/management/internal/CellarOBRMBeanImpl.java
    karaf/cellar/trunk/obr/src/main/resources/OSGI-INF/blueprint/management.xml
Modified:
    karaf/cellar/trunk/obr/pom.xml
    karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/shell/ObrListCommand.java
    karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/shell/ObrListUrlCommand.java
    karaf/cellar/trunk/obr/src/main/resources/OSGI-INF/blueprint/shell-commands.xml

Modified: karaf/cellar/trunk/obr/pom.xml
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/obr/pom.xml?rev=1335118&r1=1335117&r2=1335118&view=diff
==============================================================================
--- karaf/cellar/trunk/obr/pom.xml (original)
+++ karaf/cellar/trunk/obr/pom.xml Mon May  7 16:48:38 2012
@@ -43,6 +43,8 @@
             org.apache.karaf.shell.console.commands;version="[3,4)",
             org.apache.karaf.shell.console.completer;version="[3,4)",
             org.apache.karaf.shell.commands;version="[3,4)",
+            org.apache.karaf.management;version="[3,4)",
+            javax.management*,
             org.osgi*,
             org.slf4j;resolution:=optional
         </osgi.import>
@@ -53,6 +55,9 @@
         <osgi.export>
             org.apache.karaf.cellar.obr*;version="${project.version}"
         </osgi.export>
+        <osgi.private>
+            org.apache.karaf.cellar.obr.management.internal
+        </osgi.private>
     </properties>
 
     <dependencies>

Added: karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/management/CellarOBRMBean.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/management/CellarOBRMBean.java?rev=1335118&view=auto
==============================================================================
--- karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/management/CellarOBRMBean.java (added)
+++ karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/management/CellarOBRMBean.java Mon May  7 16:48:38 2012
@@ -0,0 +1,31 @@
+/*
+ * 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.obr.management;
+
+import javax.management.openmbean.TabularData;
+import java.util.List;
+
+/**
+ * MBean interface describing the operations and attributes on OBR service..
+ */
+public interface CellarOBRMBean {
+
+    List<String> listUrls(String groupName) throws Exception;
+    TabularData listBundles(String groupName) throws Exception;
+
+    void addUrl(String groupName, String url) throws Exception;
+    void removeUrl(String groupName, String url) throws Exception;
+    void deploy(String groupName, String bundleId) throws Exception;
+
+}

Added: karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/management/internal/CellarOBRMBeanImpl.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/management/internal/CellarOBRMBeanImpl.java?rev=1335118&view=auto
==============================================================================
--- karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/management/internal/CellarOBRMBeanImpl.java (added)
+++ karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/management/internal/CellarOBRMBeanImpl.java Mon May  7 16:48:38 2012
@@ -0,0 +1,218 @@
+/*
+ * 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.obr.management.internal;
+
+import org.apache.felix.bundlerepository.Repository;
+import org.apache.felix.bundlerepository.RepositoryAdmin;
+import org.apache.felix.bundlerepository.Resource;
+import org.apache.karaf.cellar.core.ClusterManager;
+import org.apache.karaf.cellar.core.Configurations;
+import org.apache.karaf.cellar.core.Group;
+import org.apache.karaf.cellar.core.GroupManager;
+import org.apache.karaf.cellar.core.control.SwitchStatus;
+import org.apache.karaf.cellar.core.event.EventProducer;
+import org.apache.karaf.cellar.obr.Constants;
+import org.apache.karaf.cellar.obr.ObrBundleEvent;
+import org.apache.karaf.cellar.obr.ObrBundleInfo;
+import org.apache.karaf.cellar.obr.ObrUrlEvent;
+import org.apache.karaf.cellar.obr.management.CellarOBRMBean;
+
+import javax.management.NotCompliantMBeanException;
+import javax.management.StandardMBean;
+import javax.management.openmbean.*;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Implementation of the Cellar OBR MBean.
+ */
+public class CellarOBRMBeanImpl extends StandardMBean implements CellarOBRMBean {
+
+    private ClusterManager clusterManager;
+    private GroupManager groupManager;
+    private EventProducer eventProducer;
+    private RepositoryAdmin obrService;
+
+    public CellarOBRMBeanImpl() throws NotCompliantMBeanException {
+        super(CellarOBRMBean.class);
+    }
+
+    public List<String> listUrls(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");
+        }
+
+        List<String> result = new ArrayList<String>();
+        Set<String> urls = clusterManager.getSet(Constants.URLS_DISTRIBUTED_SET_NAME + Configurations.SEPARATOR + groupName);
+        for (String url : urls) {
+            result.add(url);
+        }
+        return result;
+    }
+
+    public TabularData listBundles(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");
+        }
+
+        CompositeType compositeType = new CompositeType("OBR Bundle", "Bundles available in the OBR service",
+                new String[]{ "name", "symbolic", "version"},
+                new String[]{ "Name of the bundle", "Symbolic name of the bundle", "Version of the bundle" },
+                new OpenType[]{ SimpleType.STRING, SimpleType.STRING, SimpleType.STRING });
+        TabularType tableType = new TabularType("OBR Bundles", "Table of all bundles available in the OBR service", compositeType,
+                new String[]{"name", "version"});
+        TabularData table = new TabularDataSupport(tableType);
+
+        ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
+        try {
+            Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
+            Set<ObrBundleInfo> bundles = clusterManager.getSet(Constants.BUNDLES_DISTRIBUTED_SET_NAME + Configurations.SEPARATOR + groupName);
+            for (ObrBundleInfo info : bundles) {
+                CompositeData data = new CompositeDataSupport(compositeType,
+                        new String[]{ "name", "symbolic", "version" },
+                        new Object[]{ info.getPresentationName(), info.getSymbolicName(), info.getVersion() });
+                table.put(data);
+            }
+        } finally {
+            Thread.currentThread().setContextClassLoader(originalClassLoader);
+        }
+
+        return table;
+    }
+
+    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 producer is ON
+        if (eventProducer.getSwitch().getStatus().equals(SwitchStatus.OFF)) {
+            throw new IllegalStateException("Cluster event producer is OFF");
+        }
+
+        // push the OBR URL in the distributed set
+        Set<String> urls = clusterManager.getSet(Constants.URLS_DISTRIBUTED_SET_NAME + Configurations.SEPARATOR + groupName);
+        urls.add(url);
+        // push the bundles in the OBR distributed set
+        Set<ObrBundleInfo> bundles = clusterManager.getSet(Constants.BUNDLES_DISTRIBUTED_SET_NAME + Configurations.SEPARATOR + groupName);
+        synchronized (obrService) {
+            Repository repository = obrService.addRepository(url);
+            Resource[] resources = repository.getResources();
+            for (Resource resource : resources) {
+                ObrBundleInfo info = new ObrBundleInfo(resource.getPresentationName(), resource.getSymbolicName(), resource.getVersion().toString());
+                bundles.add(info);
+            }
+            obrService.removeRepository(url);
+        }
+
+        // create an cluster event and produce it
+        ObrUrlEvent event = new ObrUrlEvent(url, Constants.URL_ADD_EVENT_TYPE);
+        event.setForce(true);
+        event.setSourceGroup(group);
+        eventProducer.produce(event);
+    }
+
+    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 producer is ON
+        if (eventProducer.getSwitch().getStatus().equals(SwitchStatus.OFF)) {
+            throw new IllegalStateException("Cluster event producer is OFF");
+        }
+
+        // remove URL from the distributed map
+        Set<String> urls = clusterManager.getSet(Constants.URLS_DISTRIBUTED_SET_NAME + Configurations.SEPARATOR + groupName);
+        urls.remove(url);
+        // remove bundles from the distributed map
+        Set<ObrBundleInfo> bundles = clusterManager.getSet(Constants.BUNDLES_DISTRIBUTED_SET_NAME + Configurations.SEPARATOR + groupName);
+        synchronized (obrService) {
+            Repository repository = obrService.addRepository(url);
+            Resource[] resources = repository.getResources();
+            for (Resource resource : resources) {
+                ObrBundleInfo info = new ObrBundleInfo(resource.getPresentationName(), resource.getSymbolicName(), resource.getVersion().toString());
+                bundles.remove(info);
+            }
+            obrService.removeRepository(url);
+        }
+
+        // create an event and produce it
+        ObrUrlEvent event = new ObrUrlEvent(url, Constants.URL_REMOVE_EVENT_TYPE);
+        event.setSourceGroup(group);
+        eventProducer.produce(event);
+    }
+
+    public void deploy(String groupName, String bundleId) 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 producer is ON
+        if (eventProducer.getSwitch().getStatus().equals(SwitchStatus.OFF)) {
+            throw new IllegalStateException("Cluster event producer is OFF");
+        }
+
+        // create an event and produce it
+        int type = 0;
+        ObrBundleEvent event = new ObrBundleEvent(bundleId, type);
+        event.setForce(true);
+        event.setSourceGroup(group);
+        eventProducer.produce(event);
+    }
+
+    public ClusterManager getClusterManager() {
+        return clusterManager;
+    }
+
+    public void setClusterManager(ClusterManager clusterManager) {
+        this.clusterManager = clusterManager;
+    }
+
+    public GroupManager getGroupManager() {
+        return groupManager;
+    }
+
+    public void setGroupManager(GroupManager groupManager) {
+        this.groupManager = groupManager;
+    }
+
+    public EventProducer getEventProducer() {
+        return eventProducer;
+    }
+
+    public void setEventProducer(EventProducer eventProducer) {
+        this.eventProducer = eventProducer;
+    }
+
+    public RepositoryAdmin getObrService() {
+        return obrService;
+    }
+
+    public void setObrService(RepositoryAdmin obrService) {
+        this.obrService = obrService;
+    }
+
+}

Modified: karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/shell/ObrListCommand.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/shell/ObrListCommand.java?rev=1335118&r1=1335117&r2=1335118&view=diff
==============================================================================
--- karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/shell/ObrListCommand.java (original)
+++ karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/shell/ObrListCommand.java Mon May  7 16:48:38 2012
@@ -23,10 +23,13 @@ import org.apache.karaf.shell.commands.C
 
 import java.util.Set;
 
-@Command(scope = "cluster", name = "obr-list", description = "List available bundles in the OBR of all nodes assigned to a cluster group.")
+/**
+ * cluster:obr-list command.
+ */
+@Command(scope = "cluster", name = "obr-list", description = "List available bundles in the OBR of all nodes assigned to a cluster group")
 public class ObrListCommand extends CellarCommandSupport {
 
-    @Argument(index = 0, name = "group", description = "The cluster group name.", required = true, multiValued = false)
+    @Argument(index = 0, name = "group", description = "The cluster group name", required = true, multiValued = false)
     String groupName;
 
     public Object doExecute() {

Modified: karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/shell/ObrListUrlCommand.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/shell/ObrListUrlCommand.java?rev=1335118&r1=1335117&r2=1335118&view=diff
==============================================================================
--- karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/shell/ObrListUrlCommand.java (original)
+++ karaf/cellar/trunk/obr/src/main/java/org/apache/karaf/cellar/obr/shell/ObrListUrlCommand.java Mon May  7 16:48:38 2012
@@ -22,10 +22,13 @@ import org.apache.karaf.shell.commands.C
 
 import java.util.Set;
 
-@Command(scope = "cluster", name = "obr-list-url", description = "List repository URLs defined in the distributed OBR service for the given cluster group.")
+/**
+ * cluster:obr-list command
+ */
+@Command(scope = "cluster", name = "obr-list-url", description = "List repository URLs defined in the distributed OBR service assigned to a given group")
 public class ObrListUrlCommand extends CellarCommandSupport {
 
-    @Argument(index = 0, name = "group", description = "The cluster group name.", required = true, multiValued = false)
+    @Argument(index = 0, name = "group", description = "The cluster group name", required = true, multiValued = false)
     String groupName;
 
     public Object doExecute() throws Exception {

Added: karaf/cellar/trunk/obr/src/main/resources/OSGI-INF/blueprint/management.xml
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/obr/src/main/resources/OSGI-INF/blueprint/management.xml?rev=1335118&view=auto
==============================================================================
--- karaf/cellar/trunk/obr/src/main/resources/OSGI-INF/blueprint/management.xml (added)
+++ karaf/cellar/trunk/obr/src/main/resources/OSGI-INF/blueprint/management.xml Mon May  7 16:48:38 2012
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+   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.
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0">
+
+    <reference id="mbeanServer" interface="javax.management.MBeanServer">
+        <reference-listener ref="mbeanRegister" bind-method="registerMBeanServer" unbind-method="unregisterMBeanServer"/>
+    </reference>
+
+    <bean id="cellarOBRMBean" class="org.apache.karaf.cellar.obr.management.internal.CellarOBRMBeanImpl">
+        <property name="clusterManager" ref="clusterManager"/>
+        <property name="groupManager" ref="groupManager"/>
+        <property name="eventProducer" ref="eventProducer"/>
+    </bean>
+
+    <bean id="mbeanRegister" class="org.apache.karaf.management.MBeanRegistrer">
+        <property name="mbeans">
+            <map>
+                <entry value="org.apache.karaf.cellar:type=obr,name=${karaf.name}" key-ref="cellarOBRMBean"/>
+            </map>
+        </property>
+    </bean>
+
+</blueprint>
\ No newline at end of file

Modified: karaf/cellar/trunk/obr/src/main/resources/OSGI-INF/blueprint/shell-commands.xml
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/obr/src/main/resources/OSGI-INF/blueprint/shell-commands.xml?rev=1335118&r1=1335117&r2=1335118&view=diff
==============================================================================
--- karaf/cellar/trunk/obr/src/main/resources/OSGI-INF/blueprint/shell-commands.xml (original)
+++ karaf/cellar/trunk/obr/src/main/resources/OSGI-INF/blueprint/shell-commands.xml Mon May  7 16:48:38 2012
@@ -41,7 +41,6 @@
             <action class="org.apache.karaf.cellar.obr.shell.ObrListUrlCommand">
                 <property name="clusterManager" ref="clusterManager"/>
                 <property name="groupManager" ref="groupManager"/>
-                <property name="eventProducer" ref="eventProducer"/>
             </action>
             <completers>
                 <ref component-id="allGroupCompleter"/>