Search in sources :

Example 1 with ConfigurationDependency

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

the class DependencyBuilder method createConfigurationDependency.

private Dependency createConfigurationDependency(DependencyManager dm, Bundle b, String pid, String callback, String confProxyType, boolean propagate, boolean required) throws ClassNotFoundException {
    if (pid == null) {
        throw new IllegalArgumentException("pid attribute not provided in ConfigurationDependency declaration");
    }
    ConfigurationDependency cd = dm.createConfigurationDependency();
    cd.setPid(pid);
    if (confProxyType != null) {
        Class<?> confProxyTypeClass = b.loadClass(confProxyType);
        cd.setCallback(callback, confProxyTypeClass);
    } else {
        cd.setCallback(callback);
    }
    cd.setPropagate(propagate);
    cd.setRequired(required);
    return cd;
}
Also used : ConfigurationDependency(org.apache.felix.dm.ConfigurationDependency)

Example 2 with ConfigurationDependency

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

the class DiagnosticsTest method testConfigurationDependencyMissing.

public void testConfigurationDependencyMissing() throws Exception {
    DependencyManager dm = getDM();
    ConfigurationDependency configurationDependency1 = dm.createConfigurationDependency().setPid("missing.configuration.pid");
    Component component1 = dm.createComponent().setImplementation(Object.class).add(configurationDependency1);
    m_dm.add(component1);
    DependencyGraph graph = DependencyGraph.getGraph(ComponentState.UNREGISTERED, DependencyState.REQUIRED_UNAVAILABLE);
    assertEquals(1, graph.getAllComponents().size());
    assertEquals(1, graph.getAllDependencies().size());
    List<MissingDependency> missingServiceDependencies = graph.getMissingDependencies("service");
    assertTrue(missingServiceDependencies.isEmpty());
    List<MissingDependency> missingConfigDependencies = graph.getMissingDependencies("configuration");
    assertEquals(1, missingConfigDependencies.size());
    MissingDependency missingConfigDependency = missingConfigDependencies.get(0);
    assertEquals("missing.configuration.pid", missingConfigDependency.getName());
}
Also used : ConfigurationDependency(org.apache.felix.dm.ConfigurationDependency) MissingDependency(org.apache.felix.dm.diagnostics.MissingDependency) DependencyManager(org.apache.felix.dm.DependencyManager) DependencyGraph(org.apache.felix.dm.diagnostics.DependencyGraph) Component(org.apache.felix.dm.Component)

Example 3 with ConfigurationDependency

use of org.apache.felix.dm.ConfigurationDependency 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 Component client = m_dm.createComponent();
    final Client clientImpl = new Client(step);
    client.setImplementation(clientImpl);
    // Create client service dependencies
    final ServiceDependency[] dependencies = new ServiceDependency[DEPENDENCIES];
    for (int i = 0; i < DEPENDENCIES; i++) {
        final String filter = "(id=loop" + loop + "." + i + ")";
        dependencies[i] = m_dm.createServiceDependency().setService(Dep.class, filter).setRequired(true).setCallbacks("add", "remove");
        client.add(dependencies[i]);
    }
    String pid = "pid." + loop;
    final ConfigurationDependency confDependency = m_dm.createConfigurationDependency().setPid(pid);
    client.add(confDependency);
    // Create Configuration (concurrently).
    final Configuration conf = m_cm.getConfiguration(pid, null);
    final Hashtable props = new Hashtable();
    props.put("foo", "bar");
    schedule(new Runnable() {

        public void run() {
            try {
                conf.update(props);
            } catch (IOException e) {
                error("update failed", e);
            }
        }
    });
    // Activate the client service dependencies concurrently.
    List<Component> deps = new ArrayList();
    for (int i = 0; i < DEPENDENCIES; i++) {
        Hashtable h = new Hashtable();
        h.put("id", "loop" + loop + "." + i);
        final Component s = m_dm.createComponent().setInterface(Dep.class.getName(), h).setImplementation(new DepImpl());
        deps.add(s);
        schedule(new Runnable() {

            public void run() {
                m_dm.add(s);
            }
        });
    }
    // Start the client (concurrently)
    schedule(new Runnable() {

        public void run() {
            m_dm.add(client);
        }
    });
    // Ensure that client has been started.
    int expectedStep = 1 + /* conf */
    DEPENDENCIES + 1;
    step.waitForStep(expectedStep, STEP_WAIT);
    Assert.assertEquals(DEPENDENCIES, clientImpl.getDependencies());
    Assert.assertNotNull(clientImpl.getConfiguration());
    // Stop all dependencies concurrently.
    for (Component dep : deps) {
        final Component dependency = dep;
        schedule(new Runnable() {

            public void run() {
                m_dm.remove(dependency);
            }
        });
    }
    // Stop client concurrently.
    schedule(new Runnable() {

        public void run() {
            m_dm.remove(client);
        }
    });
    // Remove configuration (asynchronously)
    final Ensure stepConfDeleted = new Ensure(false);
    schedule(new Runnable() {

        public void run() {
            try {
                conf.delete();
                stepConfDeleted.step(1);
            } catch (IOException 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);
    // Make sure configuration is removed
    stepConfDeleted.waitForStep(1, STEP_WAIT);
    step.ensure();
    Assert.assertEquals(0, clientImpl.getDependencies());
    m_threadPool.awaitQuiescence(5000, TimeUnit.MILLISECONDS);
    if (super.errorsLogged()) {
        throw new IllegalStateException("Race test interrupted (some error occured, see previous logs)");
    }
    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 : ConfigurationDependency(org.apache.felix.dm.ConfigurationDependency) Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ServiceDependency(org.apache.felix.dm.ServiceDependency) Ensure(org.apache.felix.dm.itest.util.Ensure) Component(org.apache.felix.dm.Component)

Example 4 with ConfigurationDependency

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

the class ConfigurationDependencyBuilderImpl method build.

@Override
public ConfigurationDependency build() {
    String pid = m_pid == null ? (m_configType != null ? m_configType.getName() : null) : m_pid;
    if (pid == null) {
        throw new IllegalStateException("Pid not specified");
    }
    ConfigurationDependency dep = m_component.getDependencyManager().createConfigurationDependency();
    Objects.nonNull(m_pid);
    dep.setPid(pid);
    dep.setPropagate(m_propagate);
    dep.setRequired(m_required);
    if (m_updateMethodName != null) {
        dep.setCallback(m_updateCallbackInstance, m_updateMethodName, m_configType);
    } else if (m_refs.size() > 0) {
        // setup an internal callback object. When config is updated, we have to call each registered
        // method references.
        // Notice that we need the component to be instantiated in case there is a mref on one of the component instances (unbound method ref), or is used
        // called "needsInstance(true)".
        dep.setCallback(new Object() {

            @SuppressWarnings("unused")
            void updated(Component comp, Dictionary<String, Object> props) {
                m_refs.forEach(mref -> mref.accept(null, comp, props));
            }
        }, "updated", m_hasComponentCallbackRefs);
    }
    return dep;
}
Also used : InstanceCbDictionary(org.apache.felix.dm.lambda.callbacks.InstanceCbDictionary) CbDictionary(org.apache.felix.dm.lambda.callbacks.CbDictionary) Dictionary(java.util.Dictionary) ConfigurationDependency(org.apache.felix.dm.ConfigurationDependency) CbDictionaryComponent(org.apache.felix.dm.lambda.callbacks.CbDictionaryComponent) Component(org.apache.felix.dm.Component) InstanceCbDictionaryComponent(org.apache.felix.dm.lambda.callbacks.InstanceCbDictionaryComponent) CbConfigurationComponent(org.apache.felix.dm.lambda.callbacks.CbConfigurationComponent) InstanceCbConfigurationComponent(org.apache.felix.dm.lambda.callbacks.InstanceCbConfigurationComponent)

Example 5 with ConfigurationDependency

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

the class DependencyBuilder method build.

public Dependency build(Bundle b, DependencyManager dm) throws Exception {
    Dependency dp = null;
    DependencyType type;
    try {
        type = DependencyType.valueOf(m_metaData.getString(Params.type));
    } catch (IllegalArgumentException e) {
        throw new IllegalArgumentException("no \"type\" parameter found from metaData: " + m_metaData);
    }
    switch(type) {
        case ServiceDependency:
            dp = createServiceDependency(b, dm);
            break;
        case ConfigurationDependency:
            dp = createConfigurationDependency(b, dm);
            break;
        case BundleDependency:
            dp = createBundleDependency(dm);
            break;
        case ResourceDependency:
            dp = createResourceDependency(dm);
            break;
        default:
            throw new IllegalArgumentException("Can't build service dependency: " + type);
    }
    return dp;
}
Also used : ConfigurationDependency(org.apache.felix.dm.ConfigurationDependency) Dependency(org.apache.felix.dm.Dependency) ResourceDependency(org.apache.felix.dm.ResourceDependency) BundleDependency(org.apache.felix.dm.BundleDependency) ServiceDependency(org.apache.felix.dm.ServiceDependency)

Aggregations

ConfigurationDependency (org.apache.felix.dm.ConfigurationDependency)5 Component (org.apache.felix.dm.Component)3 ServiceDependency (org.apache.felix.dm.ServiceDependency)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Dictionary (java.util.Dictionary)1 Hashtable (java.util.Hashtable)1 BundleDependency (org.apache.felix.dm.BundleDependency)1 Dependency (org.apache.felix.dm.Dependency)1 DependencyManager (org.apache.felix.dm.DependencyManager)1 ResourceDependency (org.apache.felix.dm.ResourceDependency)1 DependencyGraph (org.apache.felix.dm.diagnostics.DependencyGraph)1 MissingDependency (org.apache.felix.dm.diagnostics.MissingDependency)1 Ensure (org.apache.felix.dm.itest.util.Ensure)1 CbConfigurationComponent (org.apache.felix.dm.lambda.callbacks.CbConfigurationComponent)1 CbDictionary (org.apache.felix.dm.lambda.callbacks.CbDictionary)1 CbDictionaryComponent (org.apache.felix.dm.lambda.callbacks.CbDictionaryComponent)1 InstanceCbConfigurationComponent (org.apache.felix.dm.lambda.callbacks.InstanceCbConfigurationComponent)1 InstanceCbDictionary (org.apache.felix.dm.lambda.callbacks.InstanceCbDictionary)1 InstanceCbDictionaryComponent (org.apache.felix.dm.lambda.callbacks.InstanceCbDictionaryComponent)1