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 2013/11/25 11:19:32 UTC

svn commit: r1545208 - in /karaf/trunk/service/core/src/main: java/org/apache/karaf/service/core/ java/org/apache/karaf/service/core/internal/ resources/OSGI-INF/blueprint/

Author: jbonofre
Date: Mon Nov 25 10:19:31 2013
New Revision: 1545208

URL: http://svn.apache.org/r1545208
Log:
[KARAF-2264] Rename Services to ServicesMBeanImpl and wrap exceptions as MBeanException

Added:
    karaf/trunk/service/core/src/main/java/org/apache/karaf/service/core/internal/ServicesMBeanImpl.java
Removed:
    karaf/trunk/service/core/src/main/java/org/apache/karaf/service/core/internal/Services.java
Modified:
    karaf/trunk/service/core/src/main/java/org/apache/karaf/service/core/ServicesMBean.java
    karaf/trunk/service/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml

Modified: karaf/trunk/service/core/src/main/java/org/apache/karaf/service/core/ServicesMBean.java
URL: http://svn.apache.org/viewvc/karaf/trunk/service/core/src/main/java/org/apache/karaf/service/core/ServicesMBean.java?rev=1545208&r1=1545207&r2=1545208&view=diff
==============================================================================
--- karaf/trunk/service/core/src/main/java/org/apache/karaf/service/core/ServicesMBean.java (original)
+++ karaf/trunk/service/core/src/main/java/org/apache/karaf/service/core/ServicesMBean.java Mon Nov 25 10:19:31 2013
@@ -16,6 +16,7 @@
  */
 package org.apache.karaf.service.core;
 
+import javax.management.MBeanException;
 import javax.management.openmbean.TabularData;
 
 /**
@@ -23,10 +24,10 @@ import javax.management.openmbean.Tabula
  */
 public interface ServicesMBean {
 
-    TabularData getServices() throws Exception;
+    TabularData getServices() throws MBeanException;
 
-    TabularData getServices(boolean inUse) throws Exception;
-    TabularData getServices(long bundleId) throws Exception;
-    TabularData getServices(long bundleId, boolean inUse) throws Exception;
+    TabularData getServices(boolean inUse) throws MBeanException;
+    TabularData getServices(long bundleId) throws MBeanException;
+    TabularData getServices(long bundleId, boolean inUse) throws MBeanException;
 
 }

Added: karaf/trunk/service/core/src/main/java/org/apache/karaf/service/core/internal/ServicesMBeanImpl.java
URL: http://svn.apache.org/viewvc/karaf/trunk/service/core/src/main/java/org/apache/karaf/service/core/internal/ServicesMBeanImpl.java?rev=1545208&view=auto
==============================================================================
--- karaf/trunk/service/core/src/main/java/org/apache/karaf/service/core/internal/ServicesMBeanImpl.java (added)
+++ karaf/trunk/service/core/src/main/java/org/apache/karaf/service/core/internal/ServicesMBeanImpl.java Mon Nov 25 10:19:31 2013
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.service.core.internal;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.management.MBeanException;
+import javax.management.NotCompliantMBeanException;
+import javax.management.StandardMBean;
+import javax.management.openmbean.ArrayType;
+import javax.management.openmbean.CompositeData;
+import javax.management.openmbean.CompositeDataSupport;
+import javax.management.openmbean.CompositeType;
+import javax.management.openmbean.OpenType;
+import javax.management.openmbean.SimpleType;
+import javax.management.openmbean.TabularData;
+import javax.management.openmbean.TabularDataSupport;
+import javax.management.openmbean.TabularType;
+
+import org.apache.karaf.service.core.ServicesMBean;
+import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+
+/**
+ * Implementation of the Services MBean.
+ */
+public class ServicesMBeanImpl extends StandardMBean implements ServicesMBean {
+
+    private BundleContext bundleContext;
+
+    public ServicesMBeanImpl() throws NotCompliantMBeanException {
+        super(ServicesMBean.class);
+    }
+
+    public TabularData getServices() throws MBeanException {
+        try {
+            return getServices(-1, false);
+        } catch (Exception e) {
+            throw new MBeanException(null, e.getMessage());
+        }
+    }
+
+    public TabularData getServices(boolean inUse) throws MBeanException {
+        try {
+            return getServices(-1, inUse);
+        } catch (Exception e) {
+            throw new MBeanException(null, e.getMessage());
+        }
+    }
+
+    public TabularData getServices(long bundleId) throws MBeanException {
+        try {
+            return getServices(bundleId, false);
+        } catch (Exception e) {
+            throw new MBeanException(null, e.getMessage());
+        }
+    }
+
+    public TabularData getServices(long bundleId, boolean inUse) throws MBeanException {
+        try {
+            CompositeType serviceType = new CompositeType("Service", "OSGi Service",
+                    new String[]{"Interfaces", "Properties"},
+                    new String[]{"Interfaces class name of the service", "Properties of the service"},
+                    new OpenType[]{new ArrayType(1, SimpleType.STRING), new ArrayType(1, SimpleType.STRING)});
+            TabularType tableType = new TabularType("Services", "Table of OSGi Services", serviceType,
+                    new String[]{"Interfaces", "Properties"});
+            TabularData table = new TabularDataSupport(tableType);
+
+            Bundle[] bundles;
+            if (bundleId >= 0) {
+                bundles = new Bundle[]{bundleContext.getBundle(bundleId)};
+            } else {
+                bundles = bundleContext.getBundles();
+            }
+            for (Bundle bundle : bundles) {
+                try {
+                    ServiceReference[] serviceReferences;
+                    if (inUse) {
+                        serviceReferences = bundle.getServicesInUse();
+                    } else {
+                        serviceReferences = bundle.getRegisteredServices();
+                    }
+                    if (serviceReferences != null) {
+                        for (ServiceReference reference : serviceReferences) {
+                            String[] interfaces = (String[]) reference.getProperty("objectClass");
+                            List<String> properties = new ArrayList<String>();
+                            for (String key : reference.getPropertyKeys()) {
+                                properties.add(key + " = " + reference.getProperty(key));
+                            }
+                            CompositeData data = new CompositeDataSupport(serviceType,
+                                    new String[]{"Interfaces", "Properties"},
+                                    new Object[]{interfaces, properties.toArray(new String[0])});
+                            table.put(data);
+                        }
+                    }
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+            }
+            return table;
+        } catch (Exception e) {
+            throw new MBeanException(null, e.getMessage());
+        }
+    }
+
+    public BundleContext getBundleContext() {
+        return this.bundleContext;
+    }
+
+    public void setBundleContext(BundleContext bundleContext) {
+        this.bundleContext = bundleContext;
+    }
+
+}

Modified: karaf/trunk/service/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml
URL: http://svn.apache.org/viewvc/karaf/trunk/service/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml?rev=1545208&r1=1545207&r2=1545208&view=diff
==============================================================================
--- karaf/trunk/service/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml (original)
+++ karaf/trunk/service/core/src/main/resources/OSGI-INF/blueprint/blueprint.xml Mon Nov 25 10:19:31 2013
@@ -17,7 +17,7 @@
 
     <ext:property-placeholder/>
 
-    <bean id="servicesMBean" class="org.apache.karaf.service.core.internal.Services">
+    <bean id="servicesMBean" class="org.apache.karaf.service.core.internal.ServicesMBeanImpl">
         <property name="bundleContext" ref="blueprintBundleContext"/>
     </bean>