Search in sources :

Example 86 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project sling by apache.

the class LogTracer method unregisterLogCollector.

private void unregisterLogCollector() {
    synchronized (logCollectorRegCount) {
        int count = logCollectorRegCount.decrementAndGet();
        if (count == 0) {
            ServiceRegistration reg = logCollectorReg.getAndSet(null);
            reg.unregister();
        }
    }
}
Also used : ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 87 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project sling by apache.

the class MockAdapterManagerImpl method registerAdapterFactory.

/**
     * Unregisters the {@link AdapterFactory} referred to by the service
     * <code>reference</code> from the registry.
     */
private void registerAdapterFactory(final ComponentContext context, final ServiceReference reference) {
    final String[] adaptables = PropertiesUtil.toStringArray(reference.getProperty(ADAPTABLE_CLASSES));
    final String[] adapters = PropertiesUtil.toStringArray(reference.getProperty(ADAPTER_CLASSES));
    if (adaptables == null || adaptables.length == 0 || adapters == null || adapters.length == 0) {
        return;
    }
    // DISABLED IN THIS COPY OF CLASS
    /*
        for (String clazz : adaptables) {
            if (!checkPackage(packageAdmin, clazz)) {
                log.warn("Adaptable class {} in factory service {} is not in an exported package.", clazz, reference.getProperty(Constants.SERVICE_ID));
            }
        }

        for (String clazz : adapters) {
            if (!checkPackage(packageAdmin, clazz)) {
                log.warn("Adapter class {} in factory service {} is not in an exported package.", clazz, reference.getProperty(Constants.SERVICE_ID));
            }
        }
        */
    final AdapterFactoryDescriptor factoryDesc = new AdapterFactoryDescriptor(context, reference, adapters);
    for (final String adaptable : adaptables) {
        AdapterFactoryDescriptorMap adfMap = null;
        synchronized (this.descriptors) {
            adfMap = descriptors.get(adaptable);
            if (adfMap == null) {
                adfMap = new AdapterFactoryDescriptorMap();
                descriptors.put(adaptable, adfMap);
            }
        }
        synchronized (adfMap) {
            adfMap.put(reference, factoryDesc);
        }
    }
    // clear the factory cache to force rebuild on next access
    this.factoryCache.clear();
    // register adaption
    final Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put(SlingConstants.PROPERTY_ADAPTABLE_CLASSES, adaptables);
    props.put(SlingConstants.PROPERTY_ADAPTER_CLASSES, adapters);
    ServiceRegistration adaptionRegistration = this.context.getBundleContext().registerService(Adaption.class.getName(), AdaptionImpl.INSTANCE, props);
    if (log.isDebugEnabled()) {
        log.debug("Registered service {} with {} : {} and {} : {}", new Object[] { Adaption.class.getName(), SlingConstants.PROPERTY_ADAPTABLE_CLASSES, Arrays.toString(adaptables), SlingConstants.PROPERTY_ADAPTER_CLASSES, Arrays.toString(adapters) });
    }
    factoryDesc.setAdaption(adaptionRegistration);
    // send event
    final EventAdmin localEA = this.eventAdmin;
    if (localEA != null) {
        localEA.postEvent(new Event(SlingConstants.TOPIC_ADAPTER_FACTORY_ADDED, props));
    }
}
Also used : AdapterFactoryDescriptorMap(org.apache.sling.adapter.internal.AdapterFactoryDescriptorMap) Adaption(org.apache.sling.adapter.Adaption) EventAdmin(org.osgi.service.event.EventAdmin) AdapterFactoryDescriptor(org.apache.sling.adapter.internal.AdapterFactoryDescriptor) Hashtable(java.util.Hashtable) Event(org.osgi.service.event.Event) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 88 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project sling by apache.

the class MockBundleContextDynamicReferencesTest method testAddRemoveMandatoryMultipleService.

@Test
public void testAddRemoveMandatoryMultipleService() {
    ServiceRegistration reg2b = bundleContext.registerService(ServiceInterface2.class.getName(), dependency2b, null);
    assertDependencies2(dependency2a, dependency2b);
    reg2b.unregister();
    assertDependencies2(dependency2a);
    // in real OSGi this should fail - but this is not covered by the current implementation. so test the real implementation here.
    reg2a.unregister();
    assertDependencies2();
}
Also used : ServiceInterface2(org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.ServiceInterface2) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 89 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project sling by apache.

the class MockBundleContextStaticGreedyReferencesTest method testAddRemoveOptionalUnaryService.

@Test
public void testAddRemoveOptionalUnaryService() {
    ServiceRegistration reg1aOptional = bundleContext.registerService(ServiceInterface1Optional.class.getName(), dependency1aOptional, null);
    assertDependency1Optional(dependency1aOptional);
    reg1aOptional.unregister();
    assertDependency1Optional(null);
}
Also used : ServiceInterface1Optional(org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.ServiceInterface1Optional) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 90 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project sling by apache.

the class MockBundleContextStaticGreedyReferencesTest method testAddRemoveOptionalMultipleService.

@Test
public void testAddRemoveOptionalMultipleService() {
    ServiceRegistration reg3a = bundleContext.registerService(ServiceInterface3.class.getName(), dependency3a, null);
    assertDependencies3(dependency3a);
    ServiceRegistration reg3b = bundleContext.registerService(ServiceInterface3.class.getName(), dependency3b, null);
    assertDependencies3(dependency3a, dependency3b);
    reg3a.unregister();
    assertDependencies3(dependency3b);
    reg3b.unregister();
    assertDependencies3();
}
Also used : ServiceInterface3(org.apache.sling.testing.mock.osgi.OsgiServiceUtilTest.ServiceInterface3) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Aggregations

ServiceRegistration (org.osgi.framework.ServiceRegistration)365 Test (org.junit.Test)106 Hashtable (java.util.Hashtable)94 Bundle (org.osgi.framework.Bundle)64 BundleContext (org.osgi.framework.BundleContext)64 Dictionary (java.util.Dictionary)62 Ensure (org.apache.felix.dm.itest.util.Ensure)42 Properties (java.util.Properties)39 ServiceReference (org.osgi.framework.ServiceReference)38 ArrayList (java.util.ArrayList)34 HashMap (java.util.HashMap)28 ServiceFactory (org.osgi.framework.ServiceFactory)21 IOException (java.io.IOException)16 Map (java.util.Map)15 List (java.util.List)12 ServiceEvent (org.osgi.framework.ServiceEvent)12 HashSet (java.util.HashSet)11 CountDownLatch (java.util.concurrent.CountDownLatch)10 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)10 Collection (java.util.Collection)9