public ServiceRegistrationImpl( BundleImpl bundle, long serviceId, String[] classNames, Object service, Dictionary<String, ?> properties) { this.bundle = bundle; this.serviceId = serviceId; this.classNames = classNames; this.service = service; if ((properties == null) || properties.isEmpty()) this.properties = EMPTY_PROPERTIES; else { Enumeration<String> keys = properties.keys(); Map<String, Object> thisProperties = newCaseInsensitiveMapInstance(); while (keys.hasMoreElements()) { String key = keys.nextElement(); if (Constants.OBJECTCLASS.equalsIgnoreCase(key) || Constants.SERVICE_ID.equalsIgnoreCase(key)) continue; else if (thisProperties.containsKey(key)) throw new IllegalArgumentException(key); else thisProperties.put(key, properties.get(key)); } this.properties = thisProperties.isEmpty() ? EMPTY_PROPERTIES : thisProperties; } }
private static ServiceReferenceDTO getServiceReferenceDTO(ServiceReference<?> ref) { if (ref == null) { return null; } Bundle b = ref.getBundle(); if (b == null) { // service has been unregistered return null; } ServiceReferenceDTO dto = new ServiceReferenceDTO(); dto.bundle = b.getBundleId(); String[] keys = ref.getPropertyKeys(); Map<String, Object> properties = newMap(keys.length); for (String k : keys) { Object v = ref.getProperty(k); if (Constants.SERVICE_ID.equals(k)) { dto.id = ((Long) v).longValue(); } properties.put(k, mapValue(v)); } dto.properties = properties; Bundle[] using = ref.getUsingBundles(); final int length = (using == null) ? 0 : using.length; long[] usingBundles = new long[length]; for (int i = 0; i < length; i++) { usingBundles[i] = using[i].getBundleId(); } dto.usingBundles = usingBundles; return dto; }
public Object getProperty(String key) { Object value; if (Constants.OBJECTCLASS.equalsIgnoreCase(key)) value = classNames; else if (Constants.SERVICE_ID.equalsIgnoreCase(key)) value = serviceId; else synchronized (properties) { value = properties.get(key); } return value; }
@Test public void testSetProperties() throws Exception { Bundle bundle = installBundle(getBundleArchiveA()); try { bundle.start(); BundleContext bundleContext = bundle.getBundleContext(); assertNotNull(bundleContext); String propertyA = "org.jboss.osgi.test.PropertyA"; String propertyALower = "org.jboss.osgi.test.propertya"; Hashtable<String, Object> properties = new Hashtable<String, Object>(); properties.put(propertyA, "testA"); ServiceRegistration registration = bundleContext.registerService(BundleContext.class.getName(), bundleContext, properties); assertNotNull(registration); ServiceReference reference = registration.getReference(); assertNotNull(reference); assertEquals("testA", reference.getProperty(propertyA)); assertEquals("testA", reference.getProperty(propertyALower)); Object serviceID = reference.getProperty(Constants.SERVICE_ID); Object objectClass = reference.getProperty(Constants.OBJECTCLASS); assertAllReferences(bundleContext, null, "(" + propertyA + "=testA)", reference); assertAllReferences(bundleContext, null, "(" + propertyALower + "=testA)", reference); assertAllReferences( bundleContext, null, "(" + Constants.SERVICE_ID + "=" + serviceID + ")", reference); bundleContext.addServiceListener(this); properties = new Hashtable<String, Object>(); properties.put(propertyA, "testAChanged"); registration.setProperties(properties); assertServiceEvent(ServiceEvent.MODIFIED, reference); assertEquals("testAChanged", reference.getProperty(propertyA)); assertNoAllReferences(bundleContext, null, "(" + propertyA + "=testA)"); assertNoAllReferences(bundleContext, null, "(" + propertyALower + "=testA)"); assertAllReferences(bundleContext, null, "(" + propertyA + "=testAChanged)", reference); assertAllReferences(bundleContext, null, "(" + propertyALower + "=testAChanged)", reference); registration.setProperties(null); assertServiceEvent(ServiceEvent.MODIFIED, reference); assertNull(reference.getProperty(propertyA)); assertNoAllReferences(bundleContext, null, "(" + propertyA + "=testA)"); assertNoAllReferences(bundleContext, null, "(" + propertyALower + "=testA)"); assertNoAllReferences(bundleContext, null, "(" + propertyA + "=testAChanged)"); assertNoAllReferences(bundleContext, null, "(" + propertyALower + "=testAChanged)"); properties = new Hashtable<String, Object>(); properties.put(propertyA, "testA2"); properties.put(Constants.SERVICE_ID, "rubbish1"); properties.put(Constants.OBJECTCLASS, "rubbish2"); registration.setProperties(properties); assertServiceEvent(ServiceEvent.MODIFIED, reference); assertEquals("testA2", reference.getProperty(propertyA)); assertEquals("testA2", reference.getProperty(propertyALower)); assertEquals(serviceID, reference.getProperty(Constants.SERVICE_ID)); assertEquals(serviceID, reference.getProperty(Constants.SERVICE_ID.toLowerCase())); assertEquals(objectClass, reference.getProperty(Constants.OBJECTCLASS)); assertEquals(objectClass, reference.getProperty(Constants.OBJECTCLASS.toLowerCase())); try { assertNoAllReferences(bundleContext, null, "(" + Constants.SERVICE_ID + "=rubbish1)"); fail("NumberFormatException expected"); } catch (NumberFormatException ex) { // expected } assertAllReferences( bundleContext, null, "(" + Constants.SERVICE_ID + "=" + serviceID + ")", reference); properties = new Hashtable<String, Object>(); properties.put("a", "1"); properties.put("A", "2"); try { registration.setProperties(properties); fail("Should not be here!"); } catch (IllegalArgumentException t) { // expected } assertNoServiceEvent(); registration.unregister(); assertServiceEvent(ServiceEvent.UNREGISTERING, reference); try { registration.setProperties(new Hashtable<String, Object>()); fail("Should not be here!"); } catch (IllegalStateException t) { // expected } assertNoServiceEvent(); } finally { bundle.uninstall(); } }