Search in sources :

Example 91 with ServiceRegistration

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

the class MockBundleContextTest method testServiceFactoryRegistration.

@Test
public void testServiceFactoryRegistration() throws InvalidSyntaxException {
    // prepare test services
    Class<String> clazz = String.class;
    final String service = "abc";
    Dictionary<String, Object> properties1 = getServiceProperties(null);
    ServiceRegistration reg = bundleContext.registerService(clazz, new ServiceFactory<String>() {

        @Override
        public String getService(Bundle bundle, ServiceRegistration<String> registration) {
            return service;
        }

        @Override
        public void ungetService(Bundle bundle, ServiceRegistration<String> registration, String service) {
        // do nothing
        }
    }, properties1);
    ServiceReference<String> ref = bundleContext.getServiceReference(clazz);
    assertNotNull(ref);
    assertSame(reg.getReference(), ref);
    assertSame(service, bundleContext.getService(ref));
    bundleContext.ungetService(ref);
}
Also used : Bundle(org.osgi.framework.Bundle) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 92 with ServiceRegistration

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

the class MockBundleContextTest method testObjectClassFilterMatches.

@Test
public void testObjectClassFilterMatches() throws InvalidSyntaxException {
    Filter filter = bundleContext.createFilter("(" + Constants.OBJECTCLASS + "=" + Integer.class.getName() + ")");
    ServiceRegistration serviceRegistration = bundleContext.registerService(Integer.class.getName(), Integer.valueOf(1), null);
    assertTrue(filter.match(serviceRegistration.getReference()));
}
Also used : Filter(org.osgi.framework.Filter) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 93 with ServiceRegistration

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

the class MockBundleContextTest method testServiceUnregistration.

@Test
public void testServiceUnregistration() {
    // prepare test services
    String clazz1 = String.class.getName();
    Object service1 = new Object();
    Dictionary<String, Object> properties1 = getServiceProperties(null);
    ServiceRegistration reg1 = bundleContext.registerService(clazz1, service1, properties1);
    assertNotNull(bundleContext.getServiceReference(clazz1));
    reg1.unregister();
    assertNull(bundleContext.getServiceReference(clazz1));
}
Also used : ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 94 with ServiceRegistration

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

the class OsgiServiceUtilTest method testServiceFactoryViaManualRegistration.

@Test
public void testServiceFactoryViaManualRegistration() {
    final ServiceFactory1 serviceFactory1 = new ServiceFactory1();
    bundleContext.registerService(ServiceFactory1.class.getName(), new ServiceFactory() {

        @Override
        public Object getService(Bundle bundle, ServiceRegistration registration) {
            return serviceFactory1;
        }

        @Override
        public void ungetService(Bundle bundle, ServiceRegistration registration, Object service) {
        // nothing to do
        }
    }, null);
    assertSame(serviceFactory1, bundleContext.getService(bundleContext.getServiceReference(ServiceFactory1.class.getName())));
}
Also used : ServiceFactory(org.osgi.framework.ServiceFactory) Bundle(org.osgi.framework.Bundle) ServiceRegistration(org.osgi.framework.ServiceRegistration) Test(org.junit.Test)

Example 95 with ServiceRegistration

use of org.osgi.framework.ServiceRegistration in project webservices-axiom by apache.

the class OSGiOMMetaFactoryLocator method addingBundle.

@Override
public List<RegisteredImplementation> addingBundle(Bundle bundle, BundleEvent event) {
    URL descriptorUrl = bundle.getEntry(ImplementationFactory.DESCRIPTOR_RESOURCE);
    if (descriptorUrl != null) {
        List<Implementation> discoveredImplementations = ImplementationFactory.parseDescriptor(new OSGiLoader(bundle), descriptorUrl);
        List<RegisteredImplementation> registeredImplementations = new ArrayList<RegisteredImplementation>(discoveredImplementations.size());
        synchronized (this) {
            implementations.addAll(discoveredImplementations);
            loadImplementations(implementations);
        }
        for (Implementation implementation : discoveredImplementations) {
            List<ServiceRegistration<?>> registrations = new ArrayList<ServiceRegistration<?>>();
            List<ServiceReference<?>> references = new ArrayList<ServiceReference<?>>();
            for (Feature feature : implementation.getFeatures()) {
                List<String> clazzes = new ArrayList<String>();
                clazzes.add(OMMetaFactory.class.getName());
                for (Class<?> extensionInterface : feature.getExtensionInterfaces()) {
                    clazzes.add(extensionInterface.getName());
                }
                Hashtable<String, Object> properties = new Hashtable<String, Object>();
                properties.put("implementationName", implementation.getName());
                properties.put("feature", feature.getName());
                properties.put(Constants.SERVICE_RANKING, feature.getPriority());
                ServiceRegistration<?> registration = bundle.getBundleContext().registerService(clazzes.toArray(new String[clazzes.size()]), implementation.getMetaFactory(), properties);
                registrations.add(registration);
                ServiceReference<?> reference = registration.getReference();
                references.add(reference);
                // Let the OSGi runtime know that the axiom-api bundle is using the service
                apiBundleContext.getService(reference);
            }
            registeredImplementations.add(new RegisteredImplementation(implementation, registrations, references));
        }
        return registeredImplementations;
    } else {
        return null;
    }
}
Also used : Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) URL(java.net.URL) ServiceReference(org.osgi.framework.ServiceReference) OMMetaFactory(org.apache.axiom.om.OMMetaFactory) ServiceRegistration(org.osgi.framework.ServiceRegistration)

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