Search in sources :

Example 6 with Configuration

use of org.osgi.service.cm.Configuration in project aries by apache.

the class ManagedServiceFactoryTest method test3.

@Test
public void test3() throws Exception {
    Configuration cf = ca.createFactoryConfiguration("blueprint-sample-managed-service-factory3", null);
    Hashtable<String, String> props = new Hashtable<String, String>();
    props.put("a", "5");
    cf.update(props);
    @SuppressWarnings("rawtypes") ServiceReference sr = getServiceRef(Foo.class, "(&(key=foo3)(a=5))");
    assertNotNull(sr);
    Foo foo = (Foo) context().getService(sr);
    assertNotNull(foo);
    assertEquals(5, foo.getA());
    assertEquals("default", foo.getB());
    assertEquals("5", sr.getProperty("a"));
    assertNull(sr.getProperty("b"));
    props = new Hashtable<String, String>();
    props.put("a", "5");
    props.put("b", "foo");
    cf.update(props);
    // Update after creation
    Thread.sleep(500);
    assertEquals(5, foo.getA());
    assertEquals("foo", foo.getB());
    // Update of service properties
    assertEquals("5", sr.getProperty("a"));
    assertEquals("foo", sr.getProperty("b"));
    cf.delete();
}
Also used : Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) Foo(org.apache.aries.blueprint.itests.cm.service.Foo) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test) AbstractBlueprintIntegrationTest(org.apache.aries.blueprint.itests.AbstractBlueprintIntegrationTest)

Example 7 with Configuration

use of org.osgi.service.cm.Configuration in project aries by apache.

the class ManagedServiceFactoryTest method test1.

@Test
public void test1() throws Exception {
    Configuration cf = ca.createFactoryConfiguration("blueprint-sample-managed-service-factory", null);
    Hashtable<String, String> props = new Hashtable<String, String>();
    props.put("a", "5");
    cf.update(props);
    @SuppressWarnings("rawtypes") ServiceReference sr = getServiceRef(Foo.class, "(key=foo1)");
    Foo foo = (Foo) context().getService(sr);
    assertNotNull(foo);
    assertEquals(5, foo.getA());
    assertEquals("default", foo.getB());
    assertEquals("5", sr.getProperty("a"));
    assertNull(sr.getProperty("b"));
    props = new Hashtable<String, String>();
    props.put("a", "5");
    props.put("b", "foo");
    cf.update(props);
    Thread.sleep(500);
    // No update of bean after creation
    assertEquals(5, foo.getA());
    assertEquals("default", foo.getB());
    // Only initial update of service properties
    assertEquals("5", sr.getProperty("a"));
    assertNull(sr.getProperty("b"));
}
Also used : Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) Foo(org.apache.aries.blueprint.itests.cm.service.Foo) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test) AbstractBlueprintIntegrationTest(org.apache.aries.blueprint.itests.AbstractBlueprintIntegrationTest)

Example 8 with Configuration

use of org.osgi.service.cm.Configuration in project aries by apache.

the class TestConfigAdmin method testManagedServiceFactory.

@SuppressWarnings("rawtypes")
@Test
public void testManagedServiceFactory() throws Exception {
    Configuration cf = ca.createFactoryConfiguration("blueprint-sample-managed-service-factory", null);
    cf.update(getConfig1());
    startTestBundle();
    // Make sure only one service is registered
    // Ask the service registry, not the container, since the container might have got it wrong :)
    Foo foo = context().getService(Foo.class, "(service.pid=blueprint-sample-managed-service-factory.*)");
    ServiceReference[] refs = context().getAllServiceReferences(Foo.class.getName(), "(service.pid=blueprint-sample-managed-service-factory.*)");
    assertNotNull("No services were registered for the managed service factory", refs);
    assertEquals("Multiple services were registered for the same pid.", 1, refs.length);
}
Also used : Configuration(org.osgi.service.cm.Configuration) Foo(org.apache.aries.blueprint.sample.Foo) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 9 with Configuration

use of org.osgi.service.cm.Configuration in project aries by apache.

the class ConfigAdminContentHandler method start.

@Override
public void start(String symbolicName, String contentType, Subsystem subsystem, Coordination coordination) {
    Dictionary<String, Object> configuration = configurations.get(symbolicName);
    if (configuration == null) {
        coordination.fail(new Exception("Cannot start configuration " + symbolicName + " for subsystem " + subsystem.getSymbolicName() + " it was not previously loaded"));
        return;
    }
    try {
        ConfigurationAdmin cm = cmTracker.getService();
        if (cm == null) {
            coordination.fail(new Exception("No Configuration Admin Service found. Cannot apply configuration " + symbolicName + " to subsystem " + subsystem.getSymbolicName()));
            return;
        }
        Configuration[] matchingConfs = cm.listConfigurations(ctx.createFilter("(service.pid=" + symbolicName + ")").toString());
        if (matchingConfs == null || matchingConfs.length == 0) {
            // No configuration exists: create a new one.
            Configuration conf = cm.getConfiguration(symbolicName, "?");
            conf.update(configuration);
        }
        // Update has happened, we can forget the configuration data now
        configurations.remove(symbolicName);
    } catch (InvalidSyntaxException e) {
        // Unlikely to happen.
        coordination.fail(new Exception("Failed to list existing configurations for " + symbolicName + " in subsystem " + subsystem.getSymbolicName(), e));
    } catch (IOException e) {
        coordination.fail(new Exception("Problem applying configuration " + symbolicName + " in subsystem " + subsystem.getSymbolicName(), e));
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException)

Example 10 with Configuration

use of org.osgi.service.cm.Configuration in project aries by apache.

the class ConfigurationAdmin method createFactoryConfigurationForLocation.

/**
     * @see org.osgi.jmx.service.cm.ConfigurationAdminMBean#createFactoryConfigurationForLocation(java.lang.String, java.lang.String)
     */
public String createFactoryConfigurationForLocation(String factoryPid, String location) throws IOException {
    if (factoryPid == null || factoryPid.length() < 1) {
        throw new IOException("Argument factoryPid cannot be null or empty");
    }
    Configuration config = configurationAdmin.createFactoryConfiguration(factoryPid);
    config.setBundleLocation(location);
    return config.getPid();
}
Also used : Configuration(org.osgi.service.cm.Configuration) IOException(java.io.IOException)

Aggregations

Configuration (org.osgi.service.cm.Configuration)226 Test (org.junit.Test)85 Hashtable (java.util.Hashtable)75 IOException (java.io.IOException)55 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)49 Dictionary (java.util.Dictionary)36 ArrayList (java.util.ArrayList)19 HashMap (java.util.HashMap)19 ServiceReference (org.osgi.framework.ServiceReference)19 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)18 Matchers.anyString (org.mockito.Matchers.anyString)16 BundleContext (org.osgi.framework.BundleContext)15 RegistrySourceConfiguration (org.codice.ddf.registry.federationadmin.service.internal.RegistrySourceConfiguration)11 Map (java.util.Map)10 Bundle (org.osgi.framework.Bundle)10 File (java.io.File)9 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)9 Mockito.anyString (org.mockito.Mockito.anyString)9 AbstractIntegrationTest (org.codice.ddf.itests.common.AbstractIntegrationTest)8 SkipUnstableTest (org.codice.ddf.itests.common.annotations.SkipUnstableTest)7