Search in sources :

Example 6 with Component

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

the class ServiceRaceTest method doTest.

void doTest(int loop) throws Throwable {
    debug("loop#%d -------------------------", loop);
    final Ensure step = new Ensure(false);
    // Create one client component, which depends on many service dependencies
    final ComponentImpl client = new ComponentImpl();
    final Client theClient = new Client(step);
    client.setImplementation(theClient);
    // Create client service dependencies
    final SimpleServiceDependency[] dependencies = new SimpleServiceDependency[DEPENDENCIES];
    for (int i = 0; i < DEPENDENCIES; i++) {
        dependencies[i] = new SimpleServiceDependency();
        dependencies[i].setRequired(true);
        dependencies[i].setCallbacks("add", "remove");
        client.add(dependencies[i]);
    }
    final ConfigurationDependencyImpl confDependency = new ConfigurationDependencyImpl();
    confDependency.setPid("mypid");
    client.add(confDependency);
    // Create Configuration (concurrently).
    // We have to simulate the configuration update, using a component state listener, which will
    // trigger an update thread, but only once the component is started.
    final ComponentStateListener listener = new ComponentStateListener() {

        private volatile Dictionary m_conf;

        public void changed(Component c, ComponentState state) {
            if (state == ComponentState.WAITING_FOR_REQUIRED && m_conf == null) {
                m_conf = new Hashtable();
                m_conf.put("foo", "bar");
                m_threadpool.execute(new Runnable() {

                    public void run() {
                        try {
                            confDependency.updated(m_conf);
                        } catch (ConfigurationException e) {
                            warn("configuration failed", e);
                        }
                    }
                });
            }
        }
    };
    client.add(listener);
    // Start the client (concurrently)
    m_threadpool.execute(new Runnable() {

        public void run() {
            client.start();
            // injected only one the tracker has been opened ...
            for (int i = 0; i < DEPENDENCIES; i++) {
                final SimpleServiceDependency dep = dependencies[i];
                final Event added = new EventImpl(i);
                m_threadpool.execute(new Runnable() {

                    public void run() {
                        dep.add(added);
                    }
                });
            }
        }
    });
    // Ensure that client has been started.
    int expectedStep = 1 + /* conf */
    DEPENDENCIES + 1;
    step.waitForStep(expectedStep, STEP_WAIT);
    Assert.assertEquals(DEPENDENCIES, theClient.getDependencies());
    Assert.assertNotNull(theClient.getConfiguration());
    client.remove(listener);
    // Stop the client and all dependencies concurrently.
    for (int i = 0; i < DEPENDENCIES; i++) {
        final SimpleServiceDependency dep = dependencies[i];
        final Event removed = new EventImpl(i);
        m_threadpool.execute(new Runnable() {

            public void run() {
                dep.remove(removed);
            }
        });
    }
    m_threadpool.execute(new Runnable() {

        public void run() {
            client.stop();
        }
    });
    m_threadpool.execute(new Runnable() {

        public void run() {
            try {
                // simulate a configuration suppression.
                confDependency.updated(null);
            } catch (ConfigurationException e) {
                warn("error while unconfiguring", e);
            }
        }
    });
    // Ensure that client has been stopped, then destroyed, then unbound from all dependencies
    // stop/destroy
    expectedStep += 2;
    // removed all dependencies
    expectedStep += DEPENDENCIES;
    step.waitForStep(expectedStep, STEP_WAIT);
    step.ensure();
    Assert.assertEquals(0, theClient.getDependencies());
    debug("finished one test loop");
    if ((loop + 1) % 100 == 0) {
        long duration = System.currentTimeMillis() - m_timeStamp;
        warn("Performed 100 tests (total=%d) in %d ms.", (loop + 1), duration);
        m_timeStamp = System.currentTimeMillis();
    }
}
Also used : Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) ConfigurationDependencyImpl(org.apache.felix.dm.impl.ConfigurationDependencyImpl) ComponentImpl(org.apache.felix.dm.impl.ComponentImpl) ConfigurationException(org.osgi.service.cm.ConfigurationException) Event(org.apache.felix.dm.context.Event) Component(org.apache.felix.dm.Component) ComponentStateListener(org.apache.felix.dm.ComponentStateListener) ComponentState(org.apache.felix.dm.ComponentState)

Example 7 with Component

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

the class ComponentBuilder method build.

@Override
public void build(MetaData srvMeta, List<MetaData> depsMeta, Bundle b, DependencyManager dm) throws Exception {
    Component c = dm.createComponent();
    String factory = srvMeta.getString(Params.factorySet, null);
    String factoryName = srvMeta.getString(Params.factoryName, null);
    // Setup Component auto config fields
    setCommonServiceParams(c, srvMeta);
    // Check if we must provide a Component factory set (deprecated), or a ComponentFactory.
    if (factory == null && factoryName == null) {
        Log.instance().info("ComponentBuilder: building service %s with dependencies %s", srvMeta, depsMeta);
        String impl = srvMeta.getString(Params.impl);
        String composition = srvMeta.getString(Params.composition, null);
        String factoryMethod = srvMeta.getString(Params.factoryMethod, null);
        if (factoryMethod == null) {
            c.setImplementation(b.loadClass(impl));
        } else {
            c.setFactory(b.loadClass(impl), factoryMethod);
        }
        c.setComposition(composition);
        // Adds dependencies (except named dependencies, which are managed by the lifecycle
        // handler).
        addUnamedDependencies(b, dm, c, srvMeta, depsMeta);
        // Creates a ServiceHandler, which will filter all service lifecycle callbacks.
        ServiceLifecycleHandler lfcleHandler = new ServiceLifecycleHandler(c, b, dm, srvMeta, depsMeta);
        c.setCallbacks(lfcleHandler, "init", "start", "stop", "destroy");
        // Set the provided services
        Dictionary<String, Object> properties = srvMeta.getDictionary(Params.properties, null);
        String[] services = srvMeta.getStrings(Params.provides, null);
        c.setInterface(services, properties);
    } else if (factory != null) /* deprecated */
    {
        Log.instance().info("ComponentBuilder: providing factory set for service %s with dependencies %s", srvMeta, depsMeta);
        // We don't instantiate the service, but instead we provide a Set in the registry.
        // This Set will act as a factory and another component may registers some
        // service configurations into it in order to fire some service instantiations.
        FactorySet factorySet = new FactorySet(b, srvMeta, depsMeta);
        c.setImplementation(factorySet);
        c.setCallbacks(null, "start", "stop", null);
        Hashtable<String, String> props = new Hashtable<String, String>();
        props.put(ComponentBuilder.FACTORY_NAME, factory);
        c.setInterface(Set.class.getName(), props);
    } else if (factoryName != null) {
        Log.instance().info("ComponentBuilder: providing component factory for service %s with dependencies %s", srvMeta, depsMeta);
        // We don't instantiate the service, but instead we provide a ComponentFactory in the registry.
        // (similar to DS ComponentFactory).
        ComponentFactoryImpl compFactory = new ComponentFactoryImpl(b, srvMeta, depsMeta);
        c.setImplementation(compFactory);
        c.setCallbacks(null, "start", "stop", null);
        Hashtable<String, String> props = new Hashtable<String, String>();
        props.put(ComponentBuilder.FACTORY_NAME, factoryName);
        c.setInterface(ComponentFactory.class.getName(), props);
    }
    dm.add(c);
}
Also used : Hashtable(java.util.Hashtable) ComponentFactory(org.apache.felix.dm.runtime.api.ComponentFactory) Component(org.apache.felix.dm.Component)

Example 8 with Component

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

the class DependencyManagerRuntime method bundleStopped.

/**
 * Unregisters all services for a stopping bundle.
 * @param b
 */
protected void bundleStopped(Bundle b) {
    Log.instance().info("Runtime: Removing services from stopping bundle: %s", b.getSymbolicName());
    DependencyManager dm = m_managers.remove(b);
    if (dm != null) {
        List<Component> services = new ArrayList<Component>(dm.getComponents());
        for (Component service : services) {
            Log.instance().info("Runtime: Removing service: %s", service);
            dm.remove(service);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

Example 9 with Component

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

the class DependencyManagerRuntime method stop.

/**
 * Stops our service. We'll stop all activated DependencyManager services.
 */
protected void stop() {
    Log.instance().info("Runtime: stopping services");
    for (DependencyManager dm : m_managers.values()) {
        List<Component> services = new ArrayList<Component>(dm.getComponents());
        for (Component service : services) {
            dm.remove(service);
        }
    }
    m_managers.clear();
}
Also used : ArrayList(java.util.ArrayList) DependencyManager(org.apache.felix.dm.DependencyManager) Component(org.apache.felix.dm.Component)

Example 10 with Component

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

the class ResourceAdapterServiceBuilder method build.

@Override
public void build(MetaData srvMeta, List<MetaData> depsMeta, Bundle b, DependencyManager dm) throws Exception {
    String filter = srvMeta.getString(Params.filter, null);
    Class<?> implClass = b.loadClass(srvMeta.getString(Params.impl));
    String[] provides = srvMeta.getStrings(Params.provides, null);
    Dictionary<String, Object> properties = srvMeta.getDictionary(Params.properties, null);
    boolean propagate = "true".equals(srvMeta.getString(Params.propagate, "false"));
    String changed = srvMeta.getString(Params.changed, null);
    Component c = dm.createResourceAdapterService(filter, propagate, null, changed);
    c.setInterface(provides, properties);
    String factoryMethod = srvMeta.getString(Params.factoryMethod, null);
    if (factoryMethod == null) {
        c.setImplementation(implClass);
    } else {
        c.setFactory(implClass, factoryMethod);
    }
    setCommonServiceParams(c, srvMeta);
    c.setComposition(srvMeta.getString(Params.composition, null));
    ServiceLifecycleHandler lfcleHandler = new ServiceLifecycleHandler(c, b, dm, srvMeta, depsMeta);
    // The dependencies will be plugged by our lifecycle handler.
    c.setCallbacks(lfcleHandler, "init", "start", "stop", "destroy");
    // Adds dependencies (except named dependencies, which are managed by the lifecycle handler).
    addUnamedDependencies(b, dm, c, srvMeta, depsMeta);
    dm.add(c);
}
Also used : Component(org.apache.felix.dm.Component)

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