Search in sources :

Example 56 with ServiceRegistration

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

the class ExtraServicePropertiesTest method testExtraFactoryServiceProperties.

/**
 * Tests if a Service instantiated by a Factory set can provide its service properties from its start method.
 */
public void testExtraFactoryServiceProperties() {
    Ensure e = new Ensure();
    ServiceRegistration sr = register(e, ExtraFactoryServiceProperties.ENSURE);
    e.waitForStep(3, 10000);
    sr.unregister();
}
Also used : Ensure(org.apache.felix.dm.itest.util.Ensure) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 57 with ServiceRegistration

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

the class FELIX5337Test method testCatchAllServicesUsingAnnotation.

public void testCatchAllServicesUsingAnnotation() {
    Ensure e = new Ensure();
    ServiceRegistration sr = register(e, FELIX5337.ENSURE);
    // wait for S to be started
    e.waitForStep(2, 5000);
    // remove our sequencer: this will stop S
    sr.unregister();
    // ensure that S is stopped and destroyed
    e.waitForStep(3, 5000);
}
Also used : Ensure(org.apache.felix.dm.itest.util.Ensure) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 58 with ServiceRegistration

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

the class FactoryConfigurationAdapterAnnotationTest method testFactoryConfigurationAdapterAnnotation.

@SuppressWarnings("serial")
public void testFactoryConfigurationAdapterAnnotation() throws Throwable {
    Ensure e = new Ensure();
    ServiceRegistration sr = register(e, ServiceProvider.ENSURE);
    ConfigurationAdmin cm = (ConfigurationAdmin) context.getService(context.getServiceReference(ConfigurationAdmin.class.getName()));
    try {
        // Create a factory configuration in order to instantiate the ServiceProvider
        org.osgi.service.cm.Configuration cf = cm.createFactoryConfiguration("FactoryPidTest", null);
        cf.update(new Hashtable() {

            {
                put("foo2", "bar2");
            }
        });
        // Wait for the ServiceProvider activation.
        e.waitForStep(2, MAXWAIT);
        // Update conf
        cf.update(new Hashtable() {

            {
                put("foo2", "bar2_modified");
            }
        });
        // Wait for effective update
        e.waitForStep(4, MAXWAIT);
        // Remove configuration.
        cf.delete();
        // Check if ServiceProvider has been stopped.
        e.waitForStep(6, MAXWAIT);
        e.ensure();
        sr.unregister();
    } catch (IOException err) {
        err.printStackTrace();
        Assert.fail("can't create factory configuration");
    }
}
Also used : Hashtable(java.util.Hashtable) IOException(java.io.IOException) Ensure(org.apache.felix.dm.itest.util.Ensure) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 59 with ServiceRegistration

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

the class Felix4357Test method testSingleProperty.

public void testSingleProperty() {
    Ensure e = new Ensure();
    ServiceRegistration sr = register(e, Felix4357.ENSURE);
    // wait for S to be started
    e.waitForStep(30, 10000);
    // remove our sequencer: this will stop S
    sr.unregister();
}
Also used : Ensure(org.apache.felix.dm.itest.util.Ensure) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 60 with ServiceRegistration

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

the class Felix method registerService.

/**
 * Implementation for BundleContext.registerService(). Registers
 * a service for the specified bundle bundle.
 *
 * @param classNames A string array containing the names of the classes
 *                under which the new service is available.
 * @param svcObj The service object or <code>ServiceFactory</code>.
 * @param dict A dictionary of properties that further describe the
 *             service or null.
 * @return A <code>ServiceRegistration</code> object or null.
 */
ServiceRegistration registerService(BundleContextImpl context, String[] classNames, Object svcObj, Dictionary dict) {
    if (classNames == null) {
        throw new NullPointerException("Service class names cannot be null.");
    } else if (svcObj == null) {
        throw new IllegalArgumentException("Service object cannot be null.");
    }
    ServiceRegistration reg = null;
    // service object is a service factory.
    if (!(svcObj instanceof ServiceFactory)) {
        for (int i = 0; i < classNames.length; i++) {
            Class clazz = Util.loadClassUsingClass(svcObj.getClass(), classNames[i], m_secureAction);
            if (clazz == null) {
                throw new IllegalArgumentException("Cannot cast service: " + classNames[i]);
            } else if (!clazz.isAssignableFrom(svcObj.getClass())) {
                throw new IllegalArgumentException("Service object is not an instance of \"" + classNames[i] + "\".");
            }
        }
    }
    reg = m_registry.registerService(context.getBundle(), classNames, svcObj, dict);
    // to invoke the callback with all existing service listeners.
    if (HookRegistry.isHook(classNames, org.osgi.framework.hooks.service.ListenerHook.class, svcObj)) {
        org.osgi.framework.hooks.service.ListenerHook lh = (org.osgi.framework.hooks.service.ListenerHook) getService(this, reg.getReference(), false);
        if (lh != null) {
            try {
                m_secureAction.invokeServiceListenerHookAdded(lh, m_dispatcher.getAllServiceListeners());
            } catch (Throwable th) {
                m_logger.log(reg.getReference(), Logger.LOG_WARNING, "Problem invoking service registry hook", th);
            } finally {
                this.ungetService(this, reg.getReference(), null);
            }
        }
    }
    this.fireServiceEvent(new ServiceEvent(ServiceEvent.REGISTERED, reg.getReference()), null);
    return reg;
}
Also used : ServiceFactory(org.osgi.framework.ServiceFactory) ServiceEvent(org.osgi.framework.ServiceEvent) 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