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 2014/06/17 21:40:15 UTC
git commit: [KARAF-3051] Refactoring of dosgi module to use shell
table and add ServiceMBean
Repository: karaf-cellar
Updated Branches:
refs/heads/master 5e97cb14a -> 38af3d0c4
[KARAF-3051] Refactoring of dosgi module to use shell table and add ServiceMBean
Project: http://git-wip-us.apache.org/repos/asf/karaf-cellar/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf-cellar/commit/38af3d0c
Tree: http://git-wip-us.apache.org/repos/asf/karaf-cellar/tree/38af3d0c
Diff: http://git-wip-us.apache.org/repos/asf/karaf-cellar/diff/38af3d0c
Branch: refs/heads/master
Commit: 38af3d0c4e03bccb52f446c4e0c1373f98592425
Parents: 5e97cb1
Author: Jean-Baptiste Onofré <jb...@apache.org>
Authored: Tue Jun 17 21:39:16 2014 +0200
Committer: Jean-Baptiste Onofré <jb...@apache.org>
Committed: Tue Jun 17 21:39:16 2014 +0200
----------------------------------------------------------------------
dosgi/pom.xml | 4 ++
.../cellar/dosgi/management/ServiceMBean.java | 26 ++++++++
.../management/internal/ServiceMBeanImpl.java | 66 ++++++++++++++++++++
.../shell/ListDistributedServicesCommand.java | 11 ++--
.../resources/OSGI-INF/blueprint/management.xml | 32 ++++++++++
5 files changed, 134 insertions(+), 5 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/38af3d0c/dosgi/pom.xml
----------------------------------------------------------------------
diff --git a/dosgi/pom.xml b/dosgi/pom.xml
index d22efde..982deda 100644
--- a/dosgi/pom.xml
+++ b/dosgi/pom.xml
@@ -58,6 +58,10 @@
<groupId>org.apache.karaf.cellar</groupId>
<artifactId>org.apache.karaf.cellar.core</artifactId>
</dependency>
+ <dependency>
+ <groupId>org.apache.karaf.shell</groupId>
+ <artifactId>org.apache.karaf.shell.table</artifactId>
+ </dependency>
<!-- Logging Dependencies -->
<dependency>
http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/38af3d0c/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/management/ServiceMBean.java
----------------------------------------------------------------------
diff --git a/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/management/ServiceMBean.java b/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/management/ServiceMBean.java
new file mode 100644
index 0000000..eb4bc5f
--- /dev/null
+++ b/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/management/ServiceMBean.java
@@ -0,0 +1,26 @@
+/*
+ * 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.dosgi.management;
+
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Describe the attributes and operations on the Cellar service MBean.
+ */
+public interface ServiceMBean {
+
+ public Map<String, List<String>> getServices();
+
+}
http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/38af3d0c/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/management/internal/ServiceMBeanImpl.java
----------------------------------------------------------------------
diff --git a/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/management/internal/ServiceMBeanImpl.java b/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/management/internal/ServiceMBeanImpl.java
new file mode 100644
index 0000000..a5272f4
--- /dev/null
+++ b/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/management/internal/ServiceMBeanImpl.java
@@ -0,0 +1,66 @@
+/*
+ * 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.dosgi.management.internal;
+
+import org.apache.karaf.cellar.core.ClusterManager;
+import org.apache.karaf.cellar.core.Node;
+import org.apache.karaf.cellar.dosgi.Constants;
+import org.apache.karaf.cellar.dosgi.EndpointDescription;
+import org.apache.karaf.cellar.dosgi.management.ServiceMBean;
+
+import javax.management.NotCompliantMBeanException;
+import javax.management.StandardMBean;
+import java.util.*;
+
+public class ServiceMBeanImpl extends StandardMBean implements ServiceMBean {
+
+ private ClusterManager clusterManager;
+
+ public ServiceMBeanImpl() throws NotCompliantMBeanException {
+ super(ServiceMBean.class);
+ }
+
+ public Map<String, List<String>> getServices() {
+ Map<String, List<String>> services = new HashMap<String, List<String>>();
+ ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
+ try {
+ Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
+ Map<String, EndpointDescription> remoteEndpoints = clusterManager.getMap(Constants.REMOTE_ENDPOINTS);
+ if (remoteEndpoints != null && !remoteEndpoints.isEmpty()) {
+ for (Map.Entry<String, EndpointDescription> entry : remoteEndpoints.entrySet()) {
+ EndpointDescription endpointDescription = entry.getValue();
+ String serviceClass = endpointDescription.getServiceClass();
+ Set<Node> nodes = endpointDescription.getNodes();
+ LinkedList<String> providers = new LinkedList<String>();
+ for (Node node : nodes) {
+ providers.add(node.getId());
+ }
+ services.put(serviceClass, providers);
+ }
+ }
+ } finally {
+ Thread.currentThread().setContextClassLoader(originalClassLoader);
+ }
+ return services;
+ }
+
+ public ClusterManager getClusterManager() {
+ return this.clusterManager;
+ }
+
+ public void setClusterManager(ClusterManager clusterManager) {
+ this.clusterManager = clusterManager;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/38af3d0c/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/shell/ListDistributedServicesCommand.java
----------------------------------------------------------------------
diff --git a/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/shell/ListDistributedServicesCommand.java b/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/shell/ListDistributedServicesCommand.java
index f22001b..c3bf6e4 100644
--- a/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/shell/ListDistributedServicesCommand.java
+++ b/dosgi/src/main/java/org/apache/karaf/cellar/dosgi/shell/ListDistributedServicesCommand.java
@@ -18,6 +18,7 @@ import org.apache.karaf.cellar.core.shell.CellarCommandSupport;
import org.apache.karaf.cellar.dosgi.Constants;
import org.apache.karaf.cellar.dosgi.EndpointDescription;
import org.apache.karaf.shell.commands.Command;
+import org.apache.karaf.shell.table.ShellTable;
import java.util.Map;
import java.util.Set;
@@ -25,8 +26,6 @@ import java.util.Set;
@Command(scope = "cluster", name = "service-list", description = "List the services available on the cluster")
public class ListDistributedServicesCommand extends CellarCommandSupport {
- private static final String LIST_FORMAT = "%-80s %-20s";
-
@Override
protected Object doExecute() throws Exception {
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
@@ -34,17 +33,19 @@ public class ListDistributedServicesCommand extends CellarCommandSupport {
Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
Map<String, EndpointDescription> remoteEndpoints = clusterManager.getMap(Constants.REMOTE_ENDPOINTS);
if (remoteEndpoints != null && !remoteEndpoints.isEmpty()) {
- System.out.println(String.format(LIST_FORMAT, "Service Class", "Provider Node"));
+ ShellTable table = new ShellTable();
+ table.column("Service Class");
+ table.column("Provider Node");
for (Map.Entry<String, EndpointDescription> entry : remoteEndpoints.entrySet()) {
EndpointDescription endpointDescription = entry.getValue();
String serviceClass = endpointDescription.getServiceClass();
Set<Node> nodes = endpointDescription.getNodes();
for (Node node : nodes) {
- System.out.println(String.format(LIST_FORMAT, serviceClass, node.getId()));
+ table.addRow().addContent(serviceClass, node.getId());
serviceClass = "";
}
}
-
+ table.print(System.out);
} else {
System.out.println("No service available on the cluster");
}
http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/38af3d0c/dosgi/src/main/resources/OSGI-INF/blueprint/management.xml
----------------------------------------------------------------------
diff --git a/dosgi/src/main/resources/OSGI-INF/blueprint/management.xml b/dosgi/src/main/resources/OSGI-INF/blueprint/management.xml
new file mode 100644
index 0000000..3b52c9a
--- /dev/null
+++ b/dosgi/src/main/resources/OSGI-INF/blueprint/management.xml
@@ -0,0 +1,32 @@
+<?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 default-availability="mandatory"
+ xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
+ xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.0.0">
+
+ <!-- system properties -->
+ <ext:property-placeholder placeholder-prefix="$[" placeholder-suffix="]" />
+
+ <!-- Cellar Service MBean -->
+ <bean id="cellarServiceMBean" class="org.apache.karaf.cellar.dosgi.management.internal.ServiceMBeanImpl">
+ <property name="clusterManager" ref="clusterManager"/>
+ </bean>
+ <service ref="cellarServiceMBean" auto-export="interfaces">
+ <service-properties>
+ <entry key="jmx.objectname" value="org.apache.karaf.cellar:type=service,name=$[karaf.name]"/>
+ </service-properties>
+ </service>
+
+</blueprint>
\ No newline at end of file