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; } }
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(); } }