Search in sources :

Example 1 with Component

use of org.apache.felix.dm.Component in project felix by apache.

the class Activator method startDeviceManager.

private void startDeviceManager() {
    final String driverFilter = Util.createFilterString("(%s=%s)", new String[] { org.osgi.service.device.Constants.DRIVER_ID, "*" });
    final String deviceFilter = Util.createFilterString("(|(%s=%s)(&(%s=%s)(%s=%s)))", new String[] { Constants.OBJECTCLASS, Device.class.getName(), Constants.OBJECTCLASS, "*", org.osgi.service.device.Constants.DEVICE_CATEGORY, "*" });
    Component svc = createComponent();
    svc.setImplementation(m_deviceManager);
    svc.add(createServiceDependency().setService(LogService.class).setRequired(false));
    svc.add(createServiceDependency().setService(DriverSelector.class).setRequired(false).setAutoConfig(false).setCallbacks("selectorAdded", "selectorRemoved"));
    svc.add(createServiceDependency().setService(DriverLocator.class).setRequired(false).setAutoConfig(false).setCallbacks("locatorAdded", "locatorRemoved"));
    svc.add(createServiceDependency().setService(Driver.class, driverFilter).setRequired(false).setCallbacks("driverAdded", "driverModified", "driverRemoved"));
    svc.add(createServiceDependency().setService(deviceFilter).setRequired(false).setCallbacks("deviceAdded", "deviceModified", "deviceRemoved"));
    m_manager.add(svc);
}
Also used : Device(org.osgi.service.device.Device) Driver(org.osgi.service.device.Driver) Component(org.apache.felix.dm.Component)

Example 2 with Component

use of org.apache.felix.dm.Component in project felix by apache.

the class FactoryConfigurationAdapterImplTest method createConfigurationDependency.

private FactoryConfigurationAdapterImpl createConfigurationDependency(Object service, Class<?> configType) {
    BundleContext bc = mock(BundleContext.class);
    DependencyManager dm = new DependencyManager(bc);
    Component result = dm.createFactoryConfigurationAdapterService("does.not.matter", "updated", false, service, configType);
    // Normally, when creating a factory pid adapter, you specify the class of the adapter implementation which will be instantiated
    // for each created factory pid. To do so, you invoke the setImplementation(Object impl) method, and this methods
    // accepts a class parameter, or an object instance. Usually, you always pass a class, because the intent of a factory pid adapter is to
    // create a component instance for each created factory pid. But in our case, the "service" parameter represents our adapter instance,
    // so just use it as the factory adapter implementation instance:
    result.setImplementation(service);
    // *Important note:* the semantic of the factory conf pid adapter is really similar to a ManagedServiceFactory:
    // - when the factory pid is created, a component is created; called in updated; and called in start().
    // - when the factory pid is updated, the component is called in updated().
    // - but when the factory pid is removed, updated(null) is not invoked (unlike in case of ConfigurationDependency), and the component is simply
    // stopped. This is actually the same semantic as ManagedServiceFactory: when factory pid is removed, ManagedServiceFactory.deleted() is called
    // and the deleted() method is assumed to stop and unregister the service that was registered for the pid being removed.
    dm.add(result);
    return (FactoryConfigurationAdapterImpl) result;
}
Also used : DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component) BundleContext(org.osgi.framework.BundleContext)

Example 3 with Component

use of org.apache.felix.dm.Component in project felix by apache.

the class ComponentTest method testAddDependencyFromInitCallback.

@Test
public void testAddDependencyFromInitCallback() {
    final Ensure e = new Ensure();
    final SimpleServiceDependency d = new SimpleServiceDependency();
    d.setRequired(true);
    ComponentImpl c = new ComponentImpl();
    c.setImplementation(new Object() {

        void init(Component c) {
            e.step(2);
            c.add(d);
        }

        void start() {
            e.step(4);
        }

        void stop() {
            e.step(6);
        }

        void destroy() {
            e.step(7);
        }
    });
    e.step(1);
    c.start();
    e.step(3);
    // NPE?!
    d.add(new EventImpl());
    e.step(5);
    d.remove(new EventImpl());
    c.stop();
    e.step(8);
}
Also used : Component(org.apache.felix.dm.Component) ComponentImpl(org.apache.felix.dm.impl.ComponentImpl) Test(org.junit.Test)

Example 4 with Component

use of org.apache.felix.dm.Component in project felix by apache.

the class ConcurrencyTest method createComponentAddDependencyAndListenerAndAddAnotherDependencyInAParallelThread.

/**
 * Ensure actions from another thread than the current thread executing in the SerialExecutor are being
 * scheduled (added to the queue) rather than being executed immediately.
 */
@Test
public void createComponentAddDependencyAndListenerAndAddAnotherDependencyInAParallelThread() {
    final Semaphore s = new Semaphore(0);
    final ComponentImpl c = new ComponentImpl();
    final SimpleServiceDependency d = new SimpleServiceDependency();
    d.setRequired(true);
    final SimpleServiceDependency d2 = new SimpleServiceDependency();
    d2.setRequired(true);
    final Thread t = new Thread() {

        public void run() {
            c.add(d2);
            s.release();
        }
    };
    ComponentStateListener l = new ComponentStateListener() {

        @Override
        public void changed(Component component, ComponentState state) {
            try {
                c.remove(this);
                // launch a second thread interacting with our ComponentImpl and block this thread until the
                // second thread finished its interaction with our component. We want to ensure the work of
                // the second thread is scheduled after our current job in the serial executor and does not
                // get executed immediately.
                t.start();
                s.acquire();
                Assert.assertEquals("dependency count should be 1", 1, c.getDependencies().size());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    // If not, we may see NullPointers when invoking lifecycle callbacks
    c.setImplementation(new Object());
    c.start();
    c.add(d);
    c.add(l);
    Assert.assertEquals("component should not be available", false, c.isAvailable());
    // sets dependency d to available and triggers our ComponentStateListener
    d.add(new EventImpl());
    // due to the dependency added by the second thread in the serial executor we still expect our component
    // to be unavailable. This work was scheduled in the serial executor and will be executed by the current
    // thread after it finished handling the job for handling the changed() method.
    Assert.assertEquals("component should not be available", false, c.isAvailable());
    c.remove(l);
    Assert.assertEquals("component should not be available", false, c.isAvailable());
    c.remove(d);
    Assert.assertEquals("component should not be available", false, c.isAvailable());
    c.remove(d2);
    Assert.assertEquals("component should be available", true, c.isAvailable());
    c.stop();
    Assert.assertEquals("component should not be available", false, c.isAvailable());
}
Also used : Semaphore(java.util.concurrent.Semaphore) Component(org.apache.felix.dm.Component) ComponentImpl(org.apache.felix.dm.impl.ComponentImpl) ComponentStateListener(org.apache.felix.dm.ComponentStateListener) ComponentState(org.apache.felix.dm.ComponentState) Test(org.junit.Test)

Example 5 with Component

use of org.apache.felix.dm.Component in project felix by apache.

the class ConfigurationTest method testConfigurationFailure.

@Test
public void testConfigurationFailure() throws Throwable {
    final Ensure e = new Ensure();
    // Create our configuration dependency
    final ConfigurationDependencyImpl conf = new ConfigurationDependencyImpl();
    conf.setPid("some.pid");
    // Create another required dependency
    final SimpleServiceDependency requiredDependency = new SimpleServiceDependency();
    requiredDependency.setRequired(true);
    requiredDependency.setCallbacks("addDep", null);
    // Create our component, which will fail when handling configuration update
    ComponentImpl c = new ComponentImpl();
    c.setImplementation(new Object() {

        volatile Dictionary m_conf;

        public void updated(Dictionary conf) {
            debug("updated: conf=%s", conf);
            m_conf = conf;
            if ("invalid".equals(conf.get("conf"))) {
                // We refuse the first configuration.
                debug("refusing configuration");
                e.step(1);
                // Set our acceptUpdate flag to true, so next update will be successful
                throw new RuntimeException("update failed (expected)");
            } else {
                debug("accepting configuration");
                e.step(2);
            }
        }

        public void addDep() {
            if ("invalid".equals(m_conf.get("conf"))) {
                e.throwable(new Exception("addDep should not be called"));
            }
            e.step(3);
            debug("addDep");
        }

        void init(Component c) {
            if ("invalid".equals(m_conf.get("conf"))) {
                e.throwable(new Exception("init should not be called"));
            }
            e.step(4);
            debug("init");
        }

        void start() {
            if ("invalid".equals(m_conf.get("conf"))) {
                e.throwable(new Exception("start should not be called"));
            }
            e.step(5);
            debug("start");
        }
    });
    // Add the dependencies
    c.add(conf);
    c.add(requiredDependency);
    // Start our component ("requiredDependency" is not yet available, so we'll stay in WAITING_FOR_REQUIRED state).
    c.start();
    // Enabled "requiredDependency"
    requiredDependency.add(new EventImpl());
    // Now, act as the configuration admin service and inject a wrong dependency
    try {
        Hashtable props = new Hashtable();
        props.put("conf", "invalid");
        conf.updated(props);
    } catch (ConfigurationException err) {
        warn("got expected configuration error");
    }
    e.waitForStep(1, 5000);
    e.ensure();
    // Now, inject another valid configuration
    try {
        Hashtable props = new Hashtable();
        props.put("conf", "valid");
        conf.updated(props);
    } catch (ConfigurationException err) {
        warn("got unexpected configuration error");
        e.throwable(err);
    }
    // This time, our component should be started properly.
    e.waitForStep(5, 5000);
    e.ensure();
}
Also used : Dictionary(java.util.Dictionary) ConfigurationException(org.osgi.service.cm.ConfigurationException) Hashtable(java.util.Hashtable) ConfigurationDependencyImpl(org.apache.felix.dm.impl.ConfigurationDependencyImpl) Component(org.apache.felix.dm.Component) ComponentImpl(org.apache.felix.dm.impl.ComponentImpl) ConfigurationException(org.osgi.service.cm.ConfigurationException) Test(org.junit.Test)

Aggregations

Component (org.apache.felix.dm.Component)271 DependencyManager (org.apache.felix.dm.DependencyManager)227 Ensure (org.apache.felix.dm.itest.util.Ensure)91 DependencyManagerActivator.component (org.apache.felix.dm.lambda.DependencyManagerActivator.component)65 Hashtable (java.util.Hashtable)59 Assert (org.junit.Assert)46 Dictionary (java.util.Dictionary)32 ServiceReference (org.osgi.framework.ServiceReference)25 Map (java.util.Map)23 DependencyManagerActivator.aspect (org.apache.felix.dm.lambda.DependencyManagerActivator.aspect)21 Bundle (org.osgi.framework.Bundle)17 ServiceRegistration (org.osgi.framework.ServiceRegistration)17 DependencyManagerActivator.adapter (org.apache.felix.dm.lambda.DependencyManagerActivator.adapter)15 ArrayList (java.util.ArrayList)14 ComponentDeclaration (org.apache.felix.dm.ComponentDeclaration)13 HashMap (java.util.HashMap)12 ServiceDependency (org.apache.felix.dm.ServiceDependency)12 Test (org.junit.Test)11 Properties (java.util.Properties)10 DependencyGraph (org.apache.felix.dm.diagnostics.DependencyGraph)10