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

The following examples show how to use org.osgi.framework.ServiceRegistration#unregister() . 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: JaxrsTest.java    From aries-jax-rs-whiteboard with Apache License 2.0 6 votes vote down vote up
@Test
public void testApplicationChangeCount() throws Exception {
    Long changeCount = (Long)_runtimeServiceReference.getProperty(
        "service.changecount");

    ServiceRegistration<?> serviceRegistration =
        registerApplication(
            new TestApplication(), JAX_RS_APPLICATION_BASE,
            "/test-counter");

    Long newCount = (Long)_runtimeServiceReference.getProperty(
        "service.changecount");

    assertTrue(changeCount < newCount);

    changeCount = newCount;

    serviceRegistration.unregister();

    newCount = (Long)_runtimeServiceReference.getProperty(
        "service.changecount");

    assertTrue(changeCount < newCount);
}
 
Example 2
Source File: ThymeleafTemplateCollector.java    From wisdom with Apache License 2.0 6 votes vote down vote up
/**
 * Deletes the given template. The service is unregistered, and the cache is cleared.
 *
 * @param template the template
 */
public void deleteTemplate(ThymeLeafTemplateImplementation template) {
    // 1 - unregister the service
    try {
        ServiceRegistration reg = registrations.remove(template);
        if (reg != null) {
            reg.unregister();
        }
    } catch (Exception e) { //NOSONAR
        // May already have been unregistered during the shutdown sequence.
    }

    // 2 - as templates can have dependencies, and expressions kept in memory, we clear all caches.
    // Despite this may really impact performance, it should not happen too often on real systems.
    synchronized (this) {
        engine.getCacheManager().clearAllCaches();
    }
    OgnlRuntime.clearCache();
    // Unfortunately, the previous method do not clear the get and set method cache
    // (ognl.OgnlRuntime.cacheGetMethod and ognl.OgnlRuntime.cacheSetMethod)
    clearMethodCaches();
}
 
Example 3
Source File: DefaultImportationLinkerTest.java    From fuchsia with Apache License 2.0 5 votes vote down vote up
/**
 * Test that unbind() is called when DefaultImportationLinker unbind it from an ImporterService when the.
 * ImporterService leave.
 */
@Test
public void testRemoveImporterServiceBeforeImportDeclaration() throws BinderException {
    osgiHelper.waitForService(ImportationLinker.class, "(" + INSTANCE_NAME_PROPERTY + "=" + linkerInstanceName + ")", 0);

    Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put(INSTANCE_NAME_PROPERTY, "importer-instance");
    props.put(DeclarationBinder.TARGET_FILTER_PROPERTY, "(" + Constants.ID + "=test)");

    ServiceRegistration<ImporterService> sr = context.registerService(ImporterService.class, importer1, props);
    ImporterService is = osgiHelper.waitForService(ImporterService.class, "(" + INSTANCE_NAME_PROPERTY + "=importer-instance)", 0);
    assertThat(is).isNotNull();

    Map<String, Object> metadata = new HashMap<String, Object>();
    metadata.put(Constants.PROTOCOL_NAME, "test");
    metadata.put(Constants.ID, "test");
    metadata.put("n", 0);
    ImportDeclaration iD = ImportDeclarationBuilder.fromMetadata(metadata).build();

    ServiceRegistration iDReg = registerImportDeclaration(iD);
    assertThat(iDReg).isNotNull();
    verify(importer1).addDeclaration(iD);

    assertThat(iD.getStatus().getServiceReferencesBounded()).containsOnly(sr.getReference());

    sr.unregister();
    assertThat(iD.getStatus().getServiceReferencesBounded()).isEmpty();

    iDReg.unregister();
    assertThat(importationLinkerIntrospection.getImportDeclarations()).isEmpty();
    assertThat(importationLinkerIntrospection.getLinkedImporters()).isEmpty();
}
 
Example 4
Source File: DefaultImportationLinkerTest.java    From fuchsia with Apache License 2.0 5 votes vote down vote up
/**
 * Test that bind() method is called when DefaultImportationLinker bind it with an ImporterService and.
 * that unbind() is called when DefaultImportationLinker unbind it from an ImporterService.
 *
 * @throws org.ow2.chameleon.fuchsia.core.exceptions.BinderException
 */
@Test
public void testBindOnImportDeclaration() throws BinderException {
    osgiHelper.waitForService(ImportationLinker.class, "(" + INSTANCE_NAME_PROPERTY + "=" + linkerInstanceName + ")", 0);

    Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put(INSTANCE_NAME_PROPERTY, "importer-instance");
    props.put(DeclarationBinder.TARGET_FILTER_PROPERTY, "(" + Constants.ID + "=test)");

    ServiceRegistration<ImporterService> sr = context.registerService(ImporterService.class, importer1, props);

    ImporterService is = osgiHelper.waitForService(ImporterService.class, "(" + INSTANCE_NAME_PROPERTY + "=importer-instance)", 0);
    assertThat(is).isNotNull();

    Map<String, Object> metadata = new HashMap<String, Object>();
    metadata.put(Constants.PROTOCOL_NAME, "test");
    metadata.put(Constants.ID, "test");
    metadata.put("n", 0);
    ImportDeclaration iD = ImportDeclarationBuilder.fromMetadata(metadata).build();

    ServiceRegistration iDReg = registerImportDeclaration(iD);
    assertThat(iDReg).isNotNull();
    assertThat(iD.getStatus().getServiceReferencesBounded()).containsOnly(sr.getReference());

    iDReg.unregister();
    assertThat(iD.getStatus().getServiceReferencesBounded()).doesNotContain(sr.getReference());
    assertThat(importationLinkerIntrospection.getImportDeclarations()).isEmpty();

    sr.unregister();
    assertThat(importationLinkerIntrospection.getLinkedImporters()).isEmpty();
}
 
Example 5
Source File: BaseThingHandlerFactory.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
private void unregisterConfigStatusProvider(Thing thing) {
    ServiceRegistration<ConfigStatusProvider> serviceRegistration = configStatusProviders
            .remove(thing.getUID().getAsString());
    if (serviceRegistration != null) {
        serviceRegistration.unregister();
    }
}
 
Example 6
Source File: OSGiEventManagerOSGiTest.java    From openhab-core with Eclipse Public License 2.0 5 votes vote down vote up
@After
public void cleanUp() {
    for (ServiceRegistration<?> service : serviceRegistrations.values()) {
        service.unregister();
    }
    serviceRegistrations.clear();
}
 
Example 7
Source File: DataItemFactory.java    From neoscada with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public synchronized void disposeAllItems ()
{
    for ( final ServiceRegistration<DataItem> reg : this.itemRegs.values () )
    {
        reg.unregister ();
    }
    this.items.clear ();
    this.itemRegs.clear ();
}
 
Example 8
Source File: BlueZHandlerFactory.java    From smarthome with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected synchronized void removeHandler(ThingHandler thingHandler) {
    if (thingHandler instanceof BluetoothAdapter) {
        UID uid = ((BluetoothAdapter) thingHandler).getUID();
        ServiceRegistration<?> serviceReg = this.serviceRegs.remove(uid);
        if (serviceReg != null) {
            serviceReg.unregister();
        }
    }
}
 
Example 9
Source File: FactoryImpl.java    From knopflerfish.org with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void destroy(Object handle) {
  ServiceRegistration<Repository> sr = repositoryRegistrations.remove(handle);
  if(sr != null) {
    sr.unregister();
    while(repositoryRegistrations.values().remove(sr)) {}; // Remove all mappings in case user provided custom handle
  }
}
 
Example 10
Source File: Activator.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
private void stopTaskService() {
	ServiceRegistration<ITaskService> reg = taskServiceRegistration;
	taskServiceRegistration = null;
	taskService = null;
	if (reg != null)
		reg.unregister();
}
 
Example 11
Source File: JSONRPCImporter.java    From fuchsia with Apache License 2.0 5 votes vote down vote up
@Override
public void denyImportDeclaration(ImportDeclaration importDeclaration) throws BinderException {
    String id = (String) importDeclaration.getMetadata().get(ID);
    unhandleImportDeclaration(importDeclaration);
    if (clients.containsKey(id)) {
        String klassName = (String) importDeclaration.getMetadata().get(SERVICE_CLASS);
        if (klassName != null) {
            // Unregister the proxy from OSGi
            ServiceRegistration sReg = registrations.remove(id);
            if (sReg != null) {
                sReg.unregister();
            } else {
                throw new IllegalStateException("The serviceRegistration of the given object is null." +
                        "It could not be unregister.");
            }
        } else {
            ComponentInstance componentInstance = componentInstances.remove(id);
            if (componentInstance != null) {
                componentInstance.dispose();
            }
        }
        // Remove the client
        clients.remove(id);
    } else {
        throw new BinderException("The given object has not been created through this factory");
    }
}
 
Example 12
Source File: JaxrsTest.java    From aries-jax-rs-whiteboard with Apache License 2.0 4 votes vote down vote up
@Test
public void testApplicationWithErrorAndHigherRanking() {
    RuntimeDTO runtimeDTO = getRuntimeDTO();

    assertEquals(0, runtimeDTO.applicationDTOs.length);
    assertEquals(0, runtimeDTO.failedApplicationDTOs.length);

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

    try {
        runtimeDTO = getRuntimeDTO();

        assertEquals(1, runtimeDTO.applicationDTOs.length);
        assertEquals(0, runtimeDTO.failedApplicationDTOs.length);

        ServiceRegistration<?> erroredRegistration = registerApplication(
            new TestApplication() {

                @Override
                public Set<Object> getSingletons() {
                    throw new RuntimeException();
                }

            }, "service.ranking", 10);

        runtimeDTO = getRuntimeDTO();

        assertEquals(0, runtimeDTO.applicationDTOs.length);
        assertEquals(2, runtimeDTO.failedApplicationDTOs.length);

        erroredRegistration.unregister();

        runtimeDTO = getRuntimeDTO();

        assertEquals(1, runtimeDTO.applicationDTOs.length);
        assertEquals(0, runtimeDTO.failedApplicationDTOs.length);

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

        assertEquals(200, webTarget.request().get().getStatus());
        assertEquals(
            "Hello application", webTarget.request().get(String.class));
    } finally {
        applicationRegistration.unregister();
    }
}
 
Example 13
Source File: JaxrsTest.java    From aries-jax-rs-whiteboard with Apache License 2.0 4 votes vote down vote up
@Test
public void testGettableAndShadowedNotGettableApplication()
    throws InterruptedException {

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

    assertEquals(0, getRuntimeDTO().applicationDTOs.length);
    assertEquals(0, getRuntimeDTO().failedApplicationDTOs.length);

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

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

    assertEquals(
        "Hello application",
        webTarget.request().get().readEntity(String.class));

    ServiceRegistration<Application> ungettableServiceRegistration =
        registerUngettableApplication("service.ranking", -1);

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

    assertEquals(
        DTOConstants.FAILURE_REASON_SHADOWED_BY_OTHER_SERVICE,
        getRuntimeDTO().failedApplicationDTOs[0].failureReason);

    assertEquals(
        "Hello application",
        webTarget.request().get().readEntity(String.class));

    serviceRegistration.unregister();

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

    assertEquals(404, webTarget.request().get().getStatus());

    ungettableServiceRegistration.unregister();

    assertEquals(0, getRuntimeDTO().applicationDTOs.length);
    assertEquals(0, getRuntimeDTO().failedApplicationDTOs.length);
}
 
Example 14
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 15
Source File: KNXDeviceImporter.java    From fuchsia with Apache License 2.0 3 votes vote down vote up
@Override
protected void denyImportDeclaration(ImportDeclaration importDeclaration) throws BinderException {

    try {

        super.unhandleImportDeclaration(importDeclaration);

        KNXDeclaration knxInput = KNXDeclaration.create(importDeclaration);

        ComponentInstance ci=instances.get(knxInput.getId());

        ServiceRegistration sr=instancesRegistration.get(knxInput.getId());

        if(sr!=null){
            LOG.info("Removing proxy import declaration related to {}",knxInput.getId());
            sr.unregister();
        }

        if(ci!=null){
            LOG.info("Removing proxy instance related to {}",knxInput.getId());
            ci.dispose();
        }

    }catch(Exception e){
        LOG.error("Failed removing declaration with the message {}",e.getMessage(),e);
    }

}
 
Example 16
Source File: Activator.java    From tmxeditor8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * (non-Javadoc).
 * @param reference
 *            the reference
 * @param service
 *            the service
 * @see org.osgi.util.tracker.ServiceTrackerCustomizer#removedService(org.osgi.framework.ServiceReference,
 *      java.lang.Object)
 */
public void removedService(ServiceReference reference, Object service) {
	ServiceRegistration registration = (ServiceRegistration) service;
	registration.unregister();
	bundleContext.ungetService(reference);
}
 
Example 17
Source File: Activator.java    From translationstudio8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * (non-Javadoc).
 * @param reference
 *            the reference
 * @param service
 *            the service
 * @see org.osgi.util.tracker.ServiceTrackerCustomizer#removedService(org.osgi.framework.ServiceReference,
 *      java.lang.Object)
 */
public void removedService(ServiceReference reference, Object service) {
	ServiceRegistration registration = (ServiceRegistration) service;
	registration.unregister();
	bundleContext.ungetService(reference);
}
 
Example 18
Source File: JaxrsTest.java    From aries-jax-rs-whiteboard with Apache License 2.0 2 votes vote down vote up
@Test
public void testInvalidExtension() throws InterruptedException {
    WebTarget webTarget = createDefaultTarget().path("test");

    RuntimeDTO runtimeDTO = getRuntimeDTO();

    assertEquals(0, runtimeDTO.defaultApplication.extensionDTOs.length);

    registerAddon(new TestAddon());

    ServiceRegistration<?> filterRegistration = registerInvalidExtension(
        "Filter");

    runtimeDTO = getRuntimeDTO();

    assertEquals(0, runtimeDTO.defaultApplication.extensionDTOs.length);

    assertEquals(1, runtimeDTO.failedExtensionDTOs.length);
    assertEquals(
        (long)filterRegistration.getReference().getProperty(
            "service.id"),
        runtimeDTO.failedExtensionDTOs[0].serviceId);
    assertEquals(
        DTOConstants.FAILURE_REASON_NOT_AN_EXTENSION_TYPE,
        runtimeDTO.failedExtensionDTOs[0].failureReason);

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

    assertEquals(
        "This should say hello", "Hello test",
        response.readEntity(String.class));

    assertNull(response.getHeaders().getFirst("Filtered"));

    filterRegistration.unregister();

    runtimeDTO = getRuntimeDTO();

    assertEquals(0, runtimeDTO.defaultApplication.extensionDTOs.length);

    assertEquals(0, runtimeDTO.failedExtensionDTOs.length);
}
 
Example 19
Source File: Activator.java    From tmxeditor8 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * (non-Javadoc).
 * @param reference
 *            the reference
 * @param service
 *            the service
 * @see org.osgi.util.tracker.ServiceTrackerCustomizer#removedService(org.osgi.framework.ServiceReference,
 *      java.lang.Object)
 */
public void removedService(ServiceReference reference, Object service) {
	ServiceRegistration registration = (ServiceRegistration) service;
	registration.unregister();
	bundleContext.ungetService(reference);
}
 
Example 20
Source File: JaxrsTest.java    From aries-jax-rs-whiteboard with Apache License 2.0 2 votes vote down vote up
@Test
public void testApplicationOverride() throws InterruptedException {
    WebTarget webTarget = createDefaultTarget().path("test-application");

    JaxrsServiceRuntime runtime = getJaxrsServiceRuntime();

    ServiceRegistration<?> serviceRegistration2;

    assertEquals(0, runtime.getRuntimeDTO().applicationDTOs.length);
    assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);

    registerApplication(new TestApplication());

    assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
    assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);

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

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

    serviceRegistration2 = registerApplication(
        new TestApplicationConflict(), "service.ranking", 1);

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

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

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

    assertEquals(
        "conflict",
        webTarget.path("conflict").request().get(String.class));

    serviceRegistration2.unregister();

    assertEquals(1, runtime.getRuntimeDTO().applicationDTOs.length);
    assertEquals(0, runtime.getRuntimeDTO().failedApplicationDTOs.length);

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

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