Search in sources :

Example 1 with DependencyContext

use of org.apache.felix.dm.context.DependencyContext in project felix by apache.

the class ComponentImpl method removeInstanceBoundDependencies.

/**
 * Removes and closes all instance bound dependencies.
 * This method is called when a component is destroyed.
 */
private void removeInstanceBoundDependencies() {
    for (DependencyContext dep : m_dependencies) {
        if (dep.isInstanceBound()) {
            m_dependencies.remove(dep);
            generateNameBasedOnServiceAndProperties();
            dep.stop();
        }
    }
}
Also used : DependencyContext(org.apache.felix.dm.context.DependencyContext)

Example 2 with DependencyContext

use of org.apache.felix.dm.context.DependencyContext in project felix by apache.

the class ComponentImpl method add.

@Override
public Component add(final Dependency... dependencies) {
    getExecutor().execute(() -> {
        List<DependencyContext> instanceBoundDeps = new ArrayList<>();
        for (Dependency d : dependencies) {
            DependencyContext dc = (DependencyContext) d;
            if (dc.getComponentContext() != null) {
                m_logger.err("%s can't be added to %s (dependency already added to component %s).", dc, ComponentImpl.this, dc.getComponentContext());
                continue;
            }
            m_dependencyEvents.put(dc, new ConcurrentSkipListSet<Event>());
            m_dependencies.add(dc);
            generateNameBasedOnServiceAndProperties();
            dc.setComponentContext(ComponentImpl.this);
            if (!(m_state == ComponentState.INACTIVE)) {
                dc.setInstanceBound(true);
                instanceBoundDeps.add(dc);
            }
        }
        startDependencies(instanceBoundDeps);
        handleChange();
    });
    return this;
}
Also used : ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Event(org.apache.felix.dm.context.Event) Dependency(org.apache.felix.dm.Dependency) DependencyContext(org.apache.felix.dm.context.DependencyContext)

Example 3 with DependencyContext

use of org.apache.felix.dm.context.DependencyContext in project felix by apache.

the class AbstractDecorator method addDependency.

/**
 * Add a Dependency to all already instantiated services.
 */
public void addDependency(Dependency... dependencies) {
    for (Component component : m_services.values()) {
        Dependency[] copy = Stream.of(dependencies).map(d -> (DependencyContext) d).map(dc -> dc.createCopy()).toArray(Dependency[]::new);
        for (int i = 0; i < dependencies.length; i++) {
            m_depclones.put(dependencies[i], copy[i]);
        }
        component.add(copy);
    }
}
Also used : Component(org.apache.felix.dm.Component) Dependency(org.apache.felix.dm.Dependency) URL(java.net.URL) ComponentStateListener(org.apache.felix.dm.ComponentStateListener) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DependencyContext(org.apache.felix.dm.context.DependencyContext) BundleContext(org.osgi.framework.BundleContext) Stream(java.util.stream.Stream) ComponentContext(org.apache.felix.dm.context.ComponentContext) ConfigurationException(org.osgi.service.cm.ConfigurationException) Map(java.util.Map) DependencyManager(org.apache.felix.dm.DependencyManager) Bundle(org.osgi.framework.Bundle) Dictionary(java.util.Dictionary) ServiceReference(org.osgi.framework.ServiceReference) ServiceRegistration(org.osgi.framework.ServiceRegistration) Dependency(org.apache.felix.dm.Dependency) Component(org.apache.felix.dm.Component)

Example 4 with DependencyContext

use of org.apache.felix.dm.context.DependencyContext in project felix by apache.

the class ComponentImpl method startDependencies.

private void startDependencies(List<DependencyContext> dependencies) {
    // Start first optional dependencies first.
    m_logger.debug("startDependencies.");
    List<DependencyContext> requiredDeps = new ArrayList<>();
    for (DependencyContext d : dependencies) {
        if (d.isRequired()) {
            requiredDeps.add(d);
            continue;
        }
        if (d.needsInstance()) {
            instantiateComponent();
        }
        d.start();
    }
    // now, start required dependencies.
    for (DependencyContext d : requiredDeps) {
        if (d.needsInstance()) {
            instantiateComponent();
        }
        d.start();
    }
}
Also used : ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) DependencyContext(org.apache.felix.dm.context.DependencyContext)

Example 5 with DependencyContext

use of org.apache.felix.dm.context.DependencyContext in project felix by apache.

the class ComponentImpl method remove.

@Override
public Component remove(final Dependency d) {
    getExecutor().execute(() -> {
        DependencyContext dc = (DependencyContext) d;
        // First remove this dependency from the dependency list
        m_dependencies.remove(d);
        generateNameBasedOnServiceAndProperties();
        // the removed dependency).
        if (!(m_state == ComponentState.INACTIVE)) {
            dc.stop();
        }
        // Finally, cleanup the dependency events.
        m_dependencyEvents.remove(d);
        handleChange();
    });
    return this;
}
Also used : DependencyContext(org.apache.felix.dm.context.DependencyContext)

Aggregations

DependencyContext (org.apache.felix.dm.context.DependencyContext)5 ArrayList (java.util.ArrayList)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 Dependency (org.apache.felix.dm.Dependency)2 URL (java.net.URL)1 Dictionary (java.util.Dictionary)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Stream (java.util.stream.Stream)1 Component (org.apache.felix.dm.Component)1 ComponentStateListener (org.apache.felix.dm.ComponentStateListener)1 DependencyManager (org.apache.felix.dm.DependencyManager)1 ComponentContext (org.apache.felix.dm.context.ComponentContext)1 Event (org.apache.felix.dm.context.Event)1 Bundle (org.osgi.framework.Bundle)1 BundleContext (org.osgi.framework.BundleContext)1 ServiceReference (org.osgi.framework.ServiceReference)1 ServiceRegistration (org.osgi.framework.ServiceRegistration)1 ConfigurationException (org.osgi.service.cm.ConfigurationException)1