You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by da...@apache.org on 2012/01/03 15:23:54 UTC
svn commit: r1226784 - in /aries/trunk/sandbox/jmx-next:
jmx-api/src/main/java/org/osgi/jmx/framework/
jmx-core/src/main/java/org/apache/aries/jmx/codec/
jmx-core/src/test/java/org/apache/aries/jmx/codec/
jmx-itests/src/test/java/org/apache/aries/jmx/f...
Author: davidb
Date: Tue Jan 3 14:23:54 2012
New Revision: 1226784
URL: http://svn.apache.org/viewvc?rev=1226784&view=rev
Log:
Add support for service properties in ServiceStateMBean.listServices()
Modified:
aries/trunk/sandbox/jmx-next/jmx-api/src/main/java/org/osgi/jmx/framework/ServiceStateMBean.java
aries/trunk/sandbox/jmx-next/jmx-core/src/main/java/org/apache/aries/jmx/codec/ServiceData.java
aries/trunk/sandbox/jmx-next/jmx-core/src/test/java/org/apache/aries/jmx/codec/ServiceDataTest.java
aries/trunk/sandbox/jmx-next/jmx-itests/src/test/java/org/apache/aries/jmx/framework/ServiceStateMBeanTest.java
Modified: aries/trunk/sandbox/jmx-next/jmx-api/src/main/java/org/osgi/jmx/framework/ServiceStateMBean.java
URL: http://svn.apache.org/viewvc/aries/trunk/sandbox/jmx-next/jmx-api/src/main/java/org/osgi/jmx/framework/ServiceStateMBean.java?rev=1226784&r1=1226783&r2=1226784&view=diff
==============================================================================
--- aries/trunk/sandbox/jmx-next/jmx-api/src/main/java/org/osgi/jmx/framework/ServiceStateMBean.java (original)
+++ aries/trunk/sandbox/jmx-next/jmx-api/src/main/java/org/osgi/jmx/framework/ServiceStateMBean.java Tue Jan 3 14:23:54 2012
@@ -82,6 +82,18 @@ public interface ServiceStateMBean {
"The identifier of the service", SimpleType.LONG);
/**
+ * The key PROPERTIES, used in {@link #PROPERTIES_ITEM}.
+ */
+ String PROPERTIES = "Properties";
+
+ /**
+ * The item containing service properties in {@link #SERVICE_TYPE}. The key
+ * is {@link #PROPERTIES} and the type is {@link JmxConstants#PROPERTIES_TYPE}.
+ */
+ Item PROPERTIES_ITEM = new Item(PROPERTIES,
+ "The service properties", JmxConstants.PROPERTIES_TYPE);
+
+ /**
* The key USING_BUNDLES, used in {@link #USING_BUNDLES_ITEM}.
*/
String USING_BUNDLES = "UsingBundles";
@@ -106,7 +118,7 @@ public interface ServiceStateMBean {
*/
CompositeType SERVICE_TYPE = Item.compositeType("SERVICE",
"This type encapsulates an OSGi service", BUNDLE_IDENTIFIER_ITEM,
- IDENTIFIER_ITEM, OBJECT_CLASS_ITEM,
+ IDENTIFIER_ITEM, OBJECT_CLASS_ITEM, PROPERTIES_ITEM,
USING_BUNDLES_ITEM);
/**
Modified: aries/trunk/sandbox/jmx-next/jmx-core/src/main/java/org/apache/aries/jmx/codec/ServiceData.java
URL: http://svn.apache.org/viewvc/aries/trunk/sandbox/jmx-next/jmx-core/src/main/java/org/apache/aries/jmx/codec/ServiceData.java?rev=1226784&r1=1226783&r2=1226784&view=diff
==============================================================================
--- aries/trunk/sandbox/jmx-next/jmx-core/src/main/java/org/apache/aries/jmx/codec/ServiceData.java (original)
+++ aries/trunk/sandbox/jmx-next/jmx-core/src/main/java/org/apache/aries/jmx/codec/ServiceData.java Tue Jan 3 14:23:54 2012
@@ -22,19 +22,25 @@ import static org.apache.aries.jmx.util.
import static org.osgi.jmx.framework.ServiceStateMBean.BUNDLE_IDENTIFIER;
import static org.osgi.jmx.framework.ServiceStateMBean.IDENTIFIER;
import static org.osgi.jmx.framework.ServiceStateMBean.OBJECT_CLASS;
+import static org.osgi.jmx.framework.ServiceStateMBean.PROPERTIES;
import static org.osgi.jmx.framework.ServiceStateMBean.SERVICE_TYPE;
import static org.osgi.jmx.framework.ServiceStateMBean.USING_BUNDLES;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataSupport;
import javax.management.openmbean.OpenDataException;
+import javax.management.openmbean.TabularData;
+import javax.management.openmbean.TabularDataSupport;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
+import org.osgi.jmx.JmxConstants;
import org.osgi.jmx.framework.ServiceStateMBean;
/**
@@ -62,11 +68,10 @@ public class ServiceData {
*/
private String[] serviceInterfaces;
- // keep properties for next version of the spec
- ///**
- // * @see ServiceStateMBean#PROPERTIES_ITEM
- // */
- //private List<PropertyData<? extends Object>> properties = new ArrayList<PropertyData<? extends Object>>();
+ /**
+ * @see ServiceStateMBean#PROPERTIES_ITEM
+ */
+ private List<PropertyData<? extends Object>> properties = new ArrayList<PropertyData<? extends Object>>();
/**
* @see ServiceStateMBean#USING_BUNDLES_ITEM
@@ -77,7 +82,7 @@ public class ServiceData {
super();
}
- public ServiceData(ServiceReference serviceReference) throws IllegalArgumentException {
+ public ServiceData(ServiceReference<?> serviceReference) throws IllegalArgumentException {
if (serviceReference == null) {
throw new IllegalArgumentException("Argument serviceReference cannot be null");
}
@@ -85,9 +90,9 @@ public class ServiceData {
this.bundleId = serviceReference.getBundle().getBundleId();
this.serviceInterfaces = (String[]) serviceReference.getProperty(Constants.OBJECTCLASS);
this.usingBundles = getBundleIds(serviceReference.getUsingBundles());
- //for (String propertyKey: serviceReference.getPropertyKeys()) {
- // this.properties.add(PropertyData.newInstance(propertyKey, serviceReference.getProperty(propertyKey)));
- //}
+ for (String propertyKey: serviceReference.getPropertyKeys()) {
+ this.properties.add(PropertyData.newInstance(propertyKey, serviceReference.getProperty(propertyKey)));
+ }
}
/**
@@ -109,11 +114,12 @@ public class ServiceData {
if (itemNames.contains(OBJECT_CLASS))
items.put(OBJECT_CLASS, this.serviceInterfaces);
- //TabularData propertiesTable = new TabularDataSupport(PROPERTIES_TYPE);
- //for (PropertyData<? extends Object> propertyData : this.properties) {
- // propertiesTable.put(propertyData.toCompositeData());
- //}
- // items.put(PROPERTIES, propertiesTable);
+ TabularData propertiesTable = new TabularDataSupport(JmxConstants.PROPERTIES_TYPE);
+ for (PropertyData<? extends Object> propertyData : this.properties) {
+ propertiesTable.put(propertyData.toCompositeData());
+ }
+ items.put(PROPERTIES, propertiesTable);
+
if (itemNames.contains(USING_BUNDLES))
items.put(USING_BUNDLES, toLong(this.usingBundles));
@@ -152,11 +158,11 @@ public class ServiceData {
serviceData.bundleId = (Long) compositeData.get(BUNDLE_IDENTIFIER);
serviceData.serviceInterfaces = (String[]) compositeData.get(OBJECT_CLASS);
serviceData.usingBundles = toPrimitive((Long[]) compositeData.get(USING_BUNDLES));
- // TabularData propertiesTable = (TabularData) compositeData.get(PROPERTIES);
- // Collection<CompositeData> propertyData = (Collection<CompositeData>) propertiesTable.values();
- // for (CompositeData propertyRow: propertyData) {
- // serviceData.properties.add(PropertyData.from(propertyRow));
- // }
+ TabularData propertiesTable = (TabularData) compositeData.get(PROPERTIES);
+ Collection<CompositeData> propertyData = (Collection<CompositeData>) propertiesTable.values();
+ for (CompositeData propertyRow: propertyData) {
+ serviceData.properties.add(PropertyData.from(propertyRow));
+ }
return serviceData;
}
@@ -172,12 +178,11 @@ public class ServiceData {
return serviceInterfaces;
}
- //public List<PropertyData<? extends Object>> getProperties() {
- // return properties;
- //}
+ public List<PropertyData<? extends Object>> getProperties() {
+ return properties;
+ }
public long[] getUsingBundles() {
return usingBundles;
}
-
}
Modified: aries/trunk/sandbox/jmx-next/jmx-core/src/test/java/org/apache/aries/jmx/codec/ServiceDataTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/sandbox/jmx-next/jmx-core/src/test/java/org/apache/aries/jmx/codec/ServiceDataTest.java?rev=1226784&r1=1226783&r2=1226784&view=diff
==============================================================================
--- aries/trunk/sandbox/jmx-next/jmx-core/src/test/java/org/apache/aries/jmx/codec/ServiceDataTest.java (original)
+++ aries/trunk/sandbox/jmx-next/jmx-core/src/test/java/org/apache/aries/jmx/codec/ServiceDataTest.java Tue Jan 3 14:23:54 2012
@@ -18,46 +18,57 @@ package org.apache.aries.jmx.codec;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import static org.osgi.jmx.JmxConstants.BOOLEAN;
+import static org.osgi.jmx.JmxConstants.KEY;
+import static org.osgi.jmx.JmxConstants.LONG;
+import static org.osgi.jmx.JmxConstants.P_BOOLEAN;
+import static org.osgi.jmx.JmxConstants.STRING;
+import static org.osgi.jmx.JmxConstants.TYPE;
+import static org.osgi.jmx.JmxConstants.VALUE;
import static org.osgi.jmx.framework.BundleStateMBean.IDENTIFIER;
import static org.osgi.jmx.framework.ServiceStateMBean.BUNDLE_IDENTIFIER;
import static org.osgi.jmx.framework.ServiceStateMBean.OBJECT_CLASS;
+import static org.osgi.jmx.framework.ServiceStateMBean.PROPERTIES;
import static org.osgi.jmx.framework.ServiceStateMBean.SERVICE_TYPE;
import static org.osgi.jmx.framework.ServiceStateMBean.USING_BUNDLES;
+import java.util.Collection;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.CompositeDataSupport;
+import javax.management.openmbean.TabularData;
+import javax.management.openmbean.TabularDataSupport;
import org.junit.Test;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
+import org.osgi.jmx.JmxConstants;
+import org.osgi.jmx.framework.ServiceStateMBean;
/**
- *
- *
* @version $Rev$ $Date$
*/
public class ServiceDataTest {
-
-
@Test
public void testToCompositeData() throws Exception {
-
- ServiceReference reference = mock(ServiceReference.class);
+ ServiceReference<?> reference = mock(ServiceReference.class);
Bundle bundle = mock(Bundle.class);
String[] interfaces = new String[] { "org.apache.aries.jmx.Test", "org.apache.aries.jmx.Mock" };
-
+
Bundle b1 = mock(Bundle.class);
when(b1.getBundleId()).thenReturn(new Long(6));
Bundle b2 = mock(Bundle.class);
when(b2.getBundleId()).thenReturn(new Long(9));
-
-
+
+
when(reference.getProperty(Constants.SERVICE_ID)).thenReturn(new Long(98));
when(reference.getBundle()).thenReturn(bundle);
when(bundle.getBundleId()).thenReturn(new Long(34));
@@ -68,84 +79,84 @@ public class ServiceDataTest {
when(reference.getProperty("x.domain")).thenReturn("test");
when(reference.getProperty("x.index")).thenReturn(new Long(67));
when(reference.getProperty("x.optimized")).thenReturn(true);
-
-
+
+
ServiceData serviceData = new ServiceData(reference);
-
+
CompositeData compositeData = serviceData.toCompositeData();
assertEquals(new Long(98), compositeData.get(IDENTIFIER));
assertEquals(new Long(34), compositeData.get(BUNDLE_IDENTIFIER));
assertArrayEquals( new Long[] {new Long(6), new Long(9)}, (Long[]) compositeData.get(USING_BUNDLES));
assertArrayEquals(interfaces, (String[]) compositeData.get(OBJECT_CLASS));
- // keep Properties for next version
- //TabularData propertiesTable = (TabularData) compositeData.get(PROPERTIES);
- //Collection<CompositeData> propertyData = (Collection<CompositeData>) propertiesTable.values();
- //assertEquals(4, propertyData.size());
- //for (CompositeData propertyRow: propertyData) {
- // String key = (String) propertyRow.get(KEY);
- // if (key.equals("x.vendor")) {
- // assertEquals("aries", propertyRow.get(VALUE));
- // assertEquals(STRING, propertyRow.get(TYPE));
- // } else if (key.equals("x.domain")) {
- // assertEquals("test", propertyRow.get(VALUE));
- // assertEquals(STRING, propertyRow.get(TYPE));
- // } else if (key.equals("x.index")) {
- // assertEquals("67", propertyRow.get(VALUE));
- // assertEquals(LONG, propertyRow.get(TYPE));
- // } else if (key.equals("x.optimized")) {
- // assertEquals("true", propertyRow.get(VALUE));
- // assertEquals(BOOLEAN, propertyRow.get(TYPE));
- // } else {
- // fail("unknown key parsed from properties");
- // }
- //}
+
+ TabularData propertiesTable = (TabularData) compositeData.get(PROPERTIES);
+
+ @SuppressWarnings("unchecked")
+ Collection<CompositeData> propertyData = (Collection<CompositeData>) propertiesTable.values();
+
+ assertEquals(4, propertyData.size());
+ for (CompositeData propertyRow: propertyData) {
+ String key = (String) propertyRow.get(KEY);
+ if (key.equals("x.vendor")) {
+ assertEquals("aries", propertyRow.get(VALUE));
+ assertEquals(STRING, propertyRow.get(TYPE));
+ } else if (key.equals("x.domain")) {
+ assertEquals("test", propertyRow.get(VALUE));
+ assertEquals(STRING, propertyRow.get(TYPE));
+ } else if (key.equals("x.index")) {
+ assertEquals("67", propertyRow.get(VALUE));
+ assertEquals(LONG, propertyRow.get(TYPE));
+ } else if (key.equals("x.optimized")) {
+ assertEquals("true", propertyRow.get(VALUE));
+ assertEquals(BOOLEAN, propertyRow.get(TYPE));
+ } else {
+ fail("unknown key parsed from properties");
+ }
+ }
}
-
@Test
public void testFromCompositeData() throws Exception {
-
Map<String, Object> items = new HashMap<String, Object>();
items.put(IDENTIFIER, new Long(99));
items.put(BUNDLE_IDENTIFIER, new Long(5));
items.put(USING_BUNDLES, new Long[] { new Long(10), new Long(11) });
items.put(OBJECT_CLASS, new String[] { "org.apache.aries.jmx.Test", "org.apache.aries.jmx.Mock" });
- //TabularData propertyTable = new TabularDataSupport(PROPERTIES_TYPE);
- //propertyTable.put(PropertyData.newInstance("a", true).toCompositeData());
- //propertyTable.put(PropertyData.newInstance("b", "value").toCompositeData());
- //propertyTable.put(PropertyData.newInstance("c", new int[] {1, 2}).toCompositeData());
- //propertyTable.put(PropertyData.newInstance("d", new Long[] {new Long(3), new Long(4)}).toCompositeData());
- //items.put(PROPERTIES, propertyTable);
+ TabularData propertyTable = new TabularDataSupport(JmxConstants.PROPERTIES_TYPE);
+ propertyTable.put(PropertyData.newInstance("a", true).toCompositeData());
+ propertyTable.put(PropertyData.newInstance("b", "value").toCompositeData());
+ propertyTable.put(PropertyData.newInstance("c", new int[] {1, 2}).toCompositeData());
+ propertyTable.put(PropertyData.newInstance("d", new Long[] {new Long(3), new Long(4)}).toCompositeData());
+ items.put(ServiceStateMBean.PROPERTIES, propertyTable);
CompositeData compositeData = new CompositeDataSupport(SERVICE_TYPE, items);
-
+
ServiceData data = ServiceData.from(compositeData);
assertEquals(99, data.getServiceId());
assertEquals(5, data.getBundleId());
assertArrayEquals(new long[] {10, 11}, data.getUsingBundles());
assertArrayEquals(new String[] { "org.apache.aries.jmx.Test", "org.apache.aries.jmx.Mock" }, data.getServiceInterfaces());
-
- //List<PropertyData<? extends Object>> properties = data.getProperties();
- //assertEquals(4, properties.size());
-
- //for (PropertyData<? extends Object> property: properties) {
- // if (property.getKey().equals("a")) {
- // assertTrue((Boolean) property.getValue());
- // assertEquals(P_BOOLEAN, property.getEncodedType());
- // } else if (property.getKey().equals("b")) {
- // assertEquals("value", property.getValue());
- // assertEquals(STRING, property.getEncodedType());
- // } else if (property.getKey().equals("c")) {
- // assertArrayEquals(new int[] { 1, 2 }, (int[]) property.getValue());
- // assertEquals("Array of int", property.getEncodedType());
- // assertEquals("1,2", property.getEncodedValue());
- // } else if (property.getKey().equals("d")) {
- // assertArrayEquals(new Long[] {new Long(3), new Long(4) }, (Long[]) property.getValue());
- // assertEquals("Array of Long", property.getEncodedType());
- // assertEquals("3,4", property.getEncodedValue());
- // } else {
- // fail("unknown key parsed from properties");
- // }
- //}
- }
+ List<PropertyData<? extends Object>> properties = data.getProperties();
+ assertEquals(4, properties.size());
+
+ for (PropertyData<? extends Object> property: properties) {
+ if (property.getKey().equals("a")) {
+ assertTrue((Boolean) property.getValue());
+ assertEquals(P_BOOLEAN, property.getEncodedType());
+ } else if (property.getKey().equals("b")) {
+ assertEquals("value", property.getValue());
+ assertEquals(STRING, property.getEncodedType());
+ } else if (property.getKey().equals("c")) {
+ assertArrayEquals(new int[] { 1, 2 }, (int[]) property.getValue());
+ assertEquals("Array of int", property.getEncodedType());
+ assertEquals("1,2", property.getEncodedValue());
+ } else if (property.getKey().equals("d")) {
+ assertArrayEquals(new Long[] {new Long(3), new Long(4) }, (Long[]) property.getValue());
+ assertEquals("Array of Long", property.getEncodedType());
+ assertEquals("3,4", property.getEncodedValue());
+ } else {
+ fail("unknown key parsed from properties");
+ }
+ }
+ }
}
Modified: aries/trunk/sandbox/jmx-next/jmx-itests/src/test/java/org/apache/aries/jmx/framework/ServiceStateMBeanTest.java
URL: http://svn.apache.org/viewvc/aries/trunk/sandbox/jmx-next/jmx-itests/src/test/java/org/apache/aries/jmx/framework/ServiceStateMBeanTest.java?rev=1226784&r1=1226783&r2=1226784&view=diff
==============================================================================
--- aries/trunk/sandbox/jmx-next/jmx-itests/src/test/java/org/apache/aries/jmx/framework/ServiceStateMBeanTest.java (original)
+++ aries/trunk/sandbox/jmx-next/jmx-itests/src/test/java/org/apache/aries/jmx/framework/ServiceStateMBeanTest.java Tue Jan 3 14:23:54 2012
@@ -32,9 +32,11 @@ import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
+import java.util.Map;
import java.util.Set;
import javax.management.Notification;
@@ -290,6 +292,47 @@ public class ServiceStateMBeanTest exten
}
@Test
+ @SuppressWarnings("unchecked")
+ public void testServicePropertiesInListServices() throws Exception {
+ ServiceStateMBean mbean = getMBean(ServiceStateMBean.OBJECTNAME, ServiceStateMBean.class);
+
+ ServiceReference<?>[] refs = bundleContext.getAllServiceReferences(InterfaceA.class.getName(), null);
+ assertEquals("Precondition", 1, refs.length);
+ ServiceReference<?> ref = refs[0];
+
+ TabularData svcTab = mbean.listServices();
+ CompositeData svcData = svcTab.get(new Object [] {ref.getProperty(Constants.SERVICE_ID)});
+
+ Set<String> expectedOCs = new HashSet<String>(Arrays.asList(
+ InterfaceA.class.getName(), ManagedService.class.getName()));
+ Set<String> actualOCs = new HashSet<String>(
+ Arrays.asList((String [])svcData.get(Constants.OBJECTCLASS)));
+ assertEquals(expectedOCs, actualOCs);
+
+ Map<String, Object> expectedProperties = new HashMap<String, Object>();
+ for (String key : ref.getPropertyKeys()) {
+ Object value = ref.getProperty(key);
+ if (value.getClass().isArray())
+ continue;
+
+ expectedProperties.put(key, value);
+ }
+
+ Map<String, Object> actualProperties = new HashMap<String, Object>();
+ TabularData actualProps = (TabularData) svcData.get(ServiceStateMBean.PROPERTIES);
+ for (CompositeData cd : (Collection<CompositeData>) actualProps.values()) {
+ Object type = cd.get(JmxConstants.TYPE);
+ if (JmxConstants.STRING.equals(type)) {
+ actualProperties.put((String) cd.get(JmxConstants.KEY), cd.get(JmxConstants.VALUE));
+ } else if (JmxConstants.LONG.equals(type)) {
+ actualProperties.put((String) cd.get(JmxConstants.KEY), Long.valueOf(cd.get(JmxConstants.VALUE).toString()));
+ }
+ }
+
+ assertEquals(expectedProperties, actualProperties);
+ }
+
+ @Test
public void testListServices() throws Exception {
ServiceStateMBean mbean = getMBean(ServiceStateMBean.OBJECTNAME, ServiceStateMBean.class);