Java Code Examples for org.osgi.framework.ServiceRegistration#setProperties()

The following examples show how to use org.osgi.framework.ServiceRegistration#setProperties() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Utils.java    From aries-jax-rs-whiteboard with Apache License 2.0 6 votes vote down vote up
public static void updateProperty(
    ServiceRegistration<?> serviceRegistration, String key, Object value) {

    CachingServiceReference<?> serviceReference =
        new CachingServiceReference<>(serviceRegistration.getReference());

    Dictionary<String, Object> properties = new Hashtable<>();

    for (String propertyKey : serviceReference.getPropertyKeys()) {
        properties.put(
            propertyKey, serviceReference.getProperty(propertyKey));
    }

    properties.put(key, value);

    serviceRegistration.setProperties(properties);
}
 
Example 2
Source File: Listener.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * DOCUMENT ME!
 *
 * @param event DOCUMENT ME!
 */
public void serviceChanged(ServiceDiscoveryEvent event) {
    ServiceItem item = event.getPostEventServiceItem();
    Object service = item.service;
    Hashtable prop = new Hashtable();
    prop.put(JiniDriver.ENTRIES, item.attributeSets);
    prop.put(JiniDriver.SERVICE_ID, item.serviceID.toString());
    Debug.printDebugInfo(10,
        "Changing Jini Service properties in OSGi Framework " +
        item.serviceID.toString());

    ServiceRegistration reg = (ServiceRegistration) services.get(service);

    if (reg != null) {
        reg.setProperties(prop);
    } else {
        Debug.printDebugInfo(10, "Service is no longer in OSGi Framework");
    }

    Debug.printDebugInfo(10,
        "Changing Jini Service properties in OSGi Framework Completed" +
        item.serviceID.toString());
}
 
Example 3
Source File: ComponentTestSuite.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Test setup: ComponentA references ComponentB,
 *             ComponentB references ComponentC,TestService2
 *             ComponentC references TestService
 *             ComponentD provides TestService and reference ComponentA
 * before: no components are started.
 * action: TestService and TestService2 is registered
 * after: all components are activated
 *
 * then:
 *
 * before: all components are activated
 * action: modify TestService2 to block ComponentB
 * after: only ComponentC is active
 *
 * then:
 *
 * before: all components are activated
 * action: unregister TestService and TestService2
 * after: all components are deactivated
 *
 * (the components call bump when they are (de-)actived)
 */

public void runTest() {
  Bundle c1 = null;
  ServiceRegistration<?> reg = null;
  ServiceRegistration<?> reg2 = null;
  try {
    reg = bc.registerService(TestService.class.getName(), new TestService(), new Hashtable<String,Object>());
    reg2 = bc.registerService(TestService2.class.getName(), new TestService2(), new Hashtable<String,Object>());

    counter = 0;
    c1 = Util.installBundle(bc, "componentA_test-1.0.1.jar");
    c1.start();

    Thread.sleep(SLEEP_TIME);

    ServiceReference<?> ref = bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentB");
    assertNotNull("Should get serviceRef B", ref);
    assertNotNull("Should get service B", bc.getService(ref));

    ref = bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentC");
    assertNotNull("Should get serviceRef C", ref);
    assertNotNull("Should get service C", bc.getService(ref));

    assertEquals("Should have been activate(B&C)/bind(C) bumped", 102, counter);
    Hashtable<String, Object> p = new Hashtable<String,Object>();
    p.put("block","yes");
    reg2.setProperties(p);

    Thread.sleep(SLEEP_TIME);
    assertNull("Should not get B", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentB"));
    assertNotNull("Should still get C", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentC"));
    assertEquals("Should have been deactivate B", 112, counter);

    reg.unregister();
    reg = null;

    Thread.sleep(SLEEP_TIME);

    assertNull("Should not get C", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentC"));
    assertEquals("Should have been deactivate/unbind C bumped", 1122, counter);

    counter = 0;
  } catch (Exception e) {
    e.printStackTrace();
    fail("Test4: got unexpected exception " + e);
  } finally {
    if (c1 != null) {
      try {
        c1.uninstall();
      } catch (BundleException be) {
        be.printStackTrace();
        fail("Test4: got uninstall exception " + be);
      }
    }
    if (reg != null) {
      reg.unregister();
    }
    if (reg2 != null) {
      reg2.unregister();
    }
  }
}
 
Example 4
Source File: ComponentTestSuite.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Test setup: ComponentA references ComponentB,
 *             ComponentB references ComponentC,TestService2
 *             ComponentC references TestService
 *             ComponentD provides TestService and reference ComponentA
 * before: no components are started.
 * action: TestService and TestService2 is registered
 * after: all components are activated
 *
 * then:
 *
 * before: all components are activated
 * action: register second TestService, then set high service ranking and
 *         then unregister first TestService
 * after: all components are still activated
 *
 * then:
 *
 * before: all components are activated
 * action: unregister second TestService
 * after: all components are deactivated
 *
 * (the components call bump when they are (de-)actived)
 */

public void runTest() {
  Bundle c1 = null;
  ServiceRegistration<?> reg = null;
  ServiceRegistration<?> regSecond = null;
  ServiceRegistration<?> reg2 = null;
  try {
    reg = bc.registerService(TestService.class.getName(), new TestService(), new Hashtable<String,Object>());
    reg2 = bc.registerService(TestService2.class.getName(), new TestService2(), new Hashtable<String,Object>());

    counter = 0;
    c1 = Util.installBundle(bc, "componentA_test-1.0.1.jar");
    c1.start();

    Thread.sleep(SLEEP_TIME);

    ServiceReference<?> ref = bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentB");
    assertNotNull("Should get serviceRef B", ref);
    assertNotNull("Should get service B", bc.getService(ref));

    ref = bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentC");
    assertNotNull("Should get serviceRef C", ref);
    assertNotNull("Should get service C", bc.getService(ref));

    assertEquals("Should have been activate(B&C)/bind(C) bumped", 102, counter);
    regSecond = bc.registerService(TestService.class.getName(), new TestService(), new Hashtable<String,Object>());
    Hashtable<String,Object> p = new Hashtable<String,Object>();
    p.put(Constants.SERVICE_RANKING, new Integer(7));
    regSecond.setProperties(p);
    reg.unregister();
    reg = null;

    Thread.sleep(SLEEP_TIME);
    assertNotNull("Should still get B", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentB"));
    assertNotNull("Should still get C", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentC"));
    assertEquals("Should have been deactivate B", 1202, counter);

    regSecond.unregister();
    regSecond = null;

    Thread.sleep(SLEEP_TIME);
    assertNull("Should not get B", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentB"));
    assertNull("Should not get C", bc.getServiceReference("org.knopflerfish.bundle.componentA_test.ComponentC"));
    assertEquals("Should have been deactivate/unbind C bumped", 3322, counter);

    counter = 0;
  } catch (Exception e) {
    e.printStackTrace();
    fail("Test5: got unexpected exception " + e);
  } finally {
    if (c1 != null) {
      try {
        c1.uninstall();
      } catch (BundleException be) {
        be.printStackTrace();
        fail("Test5: got uninstall exception " + be);
      }
    }
    if (reg != null) {
      reg.unregister();
    }
    if (regSecond != null) {
      regSecond.unregister();
    }
    if (reg2 != null) {
      reg2.unregister();
    }
  }
}
 
Example 5
Source File: DefaultImportationLinkerTest.java    From fuchsia with Apache License 2.0 4 votes vote down vote up
@Test
public void testReconfigureImporterTargetFilter() throws BinderException {
    // create ImportDeclaration1
    Map<String, Object> metadata1 = new HashMap<String, Object>();
    metadata1.put(Constants.PROTOCOL_NAME, "test");
    metadata1.put(Constants.ID, "test1");
    ImportDeclaration iD1 = ImportDeclarationBuilder.fromMetadata(metadata1).build();
    ServiceRegistration iDReg1 = registerImportDeclaration(iD1);
    assertThat(iDReg1).isNotNull();
    assertThat(importationLinkerIntrospection.getImportDeclarations()).containsOnly(iD1);

    // create ImportDeclaration2
    Map<String, Object> metadata2 = new HashMap<String, Object>();
    metadata2.put(Constants.PROTOCOL_NAME, "test");
    metadata2.put(Constants.ID, "test2");
    ImportDeclaration iD2 = ImportDeclarationBuilder.fromMetadata(metadata2).build();
    ServiceRegistration iDReg2 = registerImportDeclaration(iD2);
    assertThat(iDReg2).isNotNull();
    assertThat(importationLinkerIntrospection.getImportDeclarations()).containsOnly(iD1, iD2);

    // register the importer
    Dictionary<String, Object> importerProps = new Hashtable<String, Object>();
    importerProps.put(INSTANCE_NAME_PROPERTY, "importer1-instance");
    importerProps.put(DeclarationBinder.TARGET_FILTER_PROPERTY, "(" + Constants.ID + "=test*)");
    ServiceRegistration<ImporterService> sr1 = context.registerService(ImporterService.class, importer1, importerProps);
    ImporterService is1 = osgiHelper.waitForService(ImporterService.class, "(" + INSTANCE_NAME_PROPERTY + "=importer1-instance)", 0);
    assertThat(is1).isNotNull();
    verify(importer1).addDeclaration(iD1);
    verify(importer1).addDeclaration(iD2);

    // reconfigure the importer
    importerProps = new Hashtable<String, Object>();
    importerProps.put(INSTANCE_NAME_PROPERTY, "importer1-instance");
    importerProps.put(DeclarationBinder.TARGET_FILTER_PROPERTY, "(" + Constants.ID + "=test1)");
    sr1.setProperties(importerProps);

    verify(importer1).removeDeclaration(iD2);
    verify(importer1, never()).removeDeclaration(iD1);
    verify(importer1, times(1)).addDeclaration(iD1);

    assertThat(iD1.getStatus().getServiceReferencesBounded()).containsOnly(sr1.getReference());
    // reconfigure the importer
    importerProps = new Hashtable<String, Object>();
    importerProps.put(INSTANCE_NAME_PROPERTY, "importer1-instance");
    importerProps.put(DeclarationBinder.TARGET_FILTER_PROPERTY, "(" + Constants.ID + "=test2)");
    sr1.setProperties(importerProps);

    verify(importer1).removeDeclaration(iD1);
    verify(importer1, times(2)).addDeclaration(iD2);
}
 
Example 6
Source File: JaxrsTest.java    From aries-jax-rs-whiteboard with Apache License 2.0 3 votes vote down vote up
@SuppressWarnings("serial")
@Test
public void testApplicationRebase() {
    assertEquals(0, getRuntimeDTO().applicationDTOs.length);

    ServiceRegistration<Application> serviceRegistration =
        registerApplication(new TestApplication());

    assertEquals(1, getRuntimeDTO().applicationDTOs.length);

    WebTarget webTarget = createDefaultTarget().path("/test-application");

    Response response = webTarget.request().get();

    assertEquals("Hello application", response.readEntity(String.class));

    serviceRegistration.setProperties(
        new Hashtable<String, Object>() {{
            put(JAX_RS_APPLICATION_BASE, "/test-application-rebased");
        }});

    webTarget = createDefaultTarget().path("/test-application-rebased");

    response = webTarget.request().get();

    assertEquals("Hello application", response.readEntity(String.class));

    serviceRegistration.setProperties(
        new Hashtable<String, Object>() {{
            put(JAX_RS_APPLICATION_BASE, "/test-application-rebased-again");
        }});

    webTarget =
        createDefaultTarget().
            path("/test-application-rebased-again");

    response = webTarget.request().get();

    assertEquals("Hello application", response.readEntity(String.class));
}