Search in sources :

Example 36 with ConfigurationException

use of org.osgi.service.cm.ConfigurationException in project openhab1-addons by openhab.

the class SonosBinding method updated.

@Override
@SuppressWarnings("rawtypes")
public void updated(Dictionary config) throws ConfigurationException {
    if (config != null) {
        Enumeration keys = config.keys();
        while (keys.hasMoreElements()) {
            String key = (String) keys.nextElement();
            // don't want to process here ...
            if ("service.pid".equals(key)) {
                continue;
            }
            if ("pollingPeriod".equals(key)) {
                pollingPeriod = Integer.parseInt((String) config.get(key));
                logger.debug("Setting polling period to {} ms", pollingPeriod);
                continue;
            }
            Matcher matcher = EXTRACT_SONOS_CONFIG_PATTERN.matcher(key);
            if (!matcher.matches()) {
                logger.debug("given sonos-config-key '" + key + "' does not follow the expected pattern '<sonosId>.<udn>'");
                continue;
            }
            matcher.reset();
            matcher.find();
            String sonosID = matcher.group(1);
            SonosZonePlayer sonosConfig = sonosZonePlayerCache.getById(sonosID);
            if (sonosConfig == null) {
                sonosConfig = new SonosZonePlayer(sonosID, self);
                sonosZonePlayerCache.add(sonosConfig);
            }
            String configKey = matcher.group(2);
            String value = (String) config.get(key);
            if ("udn".equals(configKey)) {
                sonosConfig.setUdn(new UDN(value));
                logger.debug("Add predefined Sonos device with UDN {}", sonosConfig.getUdn());
            } else {
                throw new ConfigurationException(configKey, "the given configKey '" + configKey + "' is unknown");
            }
        }
    }
    setProperlyConfigured(true);
}
Also used : Enumeration(java.util.Enumeration) Matcher(java.util.regex.Matcher) ConfigurationException(org.osgi.service.cm.ConfigurationException) UDN(org.teleal.cling.model.types.UDN)

Example 37 with ConfigurationException

use of org.osgi.service.cm.ConfigurationException in project felix by apache.

the class TestManagedServiceConfigurableProperties method testDynamicStringInstance2.

@Test
public void testDynamicStringInstance2() {
    ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
    assertNotNull("Check FS availability", fooRef);
    String fooP = (String) fooRef.getProperty("foo");
    Integer barP = (Integer) fooRef.getProperty("bar");
    String bazP = (String) fooRef.getProperty("baz");
    assertEquals("Check foo equality", fooP, "foo");
    assertEquals("Check bar equality", barP, new Integer(2));
    assertEquals("Check baz equality", bazP, "baz");
    ServiceReference msRef = osgiHelper.getServiceReferenceByPID(ManagedService.class.getName(), "instance");
    assertNotNull("Check ManagedService availability", msRef);
    // Configuration of baz
    Dictionary<String, Object> conf = new Hashtable<String, Object>();
    conf.put("baz", "zab");
    conf.put("foo", "oof");
    conf.put("bar", "0");
    ManagedService ms = (ManagedService) osgiHelper.getContext().getService(msRef);
    try {
        ms.updated(conf);
    } catch (ConfigurationException e) {
        fail("Configuration Exception : " + e);
    }
    // Recheck props
    fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
    fooP = (String) fooRef.getProperty("foo");
    barP = (Integer) fooRef.getProperty("bar");
    bazP = (String) fooRef.getProperty("baz");
    assertEquals("Check foo equality", fooP, "oof");
    assertEquals("Check bar equality", barP, new Integer(0));
    assertEquals("Check baz equality", bazP, "zab");
    // Check field value
    FooService fs = (FooService) osgiHelper.getContext().getService(fooRef);
    Properties p = fs.fooProps();
    fooP = (String) p.get("foo");
    barP = (Integer) p.get("bar");
    assertEquals("Check foo field equality", fooP, "oof");
    assertEquals("Check bar field equality", barP, new Integer(0));
    osgiHelper.getContext().ungetService(fooRef);
    osgiHelper.getContext().ungetService(msRef);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ManagedService(org.osgi.service.cm.ManagedService) ConfigurationException(org.osgi.service.cm.ConfigurationException) Hashtable(java.util.Hashtable) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 38 with ConfigurationException

use of org.osgi.service.cm.ConfigurationException in project felix by apache.

the class TestManagedServiceConfigurableProperties method testDynamicInstance3.

@Test
public void testDynamicInstance3() {
    ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance3.getInstanceName());
    assertNotNull("Check FS availability", fooRef);
    Object fooP = fooRef.getProperty("foo");
    Object barP = fooRef.getProperty("bar");
    Object bazP = fooRef.getProperty("baz");
    // No values ... no properties.
    assertEquals("Check foo equality", fooP, null);
    assertEquals("Check bar equality", barP, null);
    assertEquals("Check baz equality", bazP, null);
    ServiceReference msRef = osgiHelper.getServiceReferenceByPID(ManagedService.class.getName(), "instance-3");
    assertNotNull("Check ManagedServiceFactory availability", msRef);
    // Configuration of baz
    Dictionary<String, Object> conf = new Hashtable<String, Object>();
    conf.put("baz", "zab");
    conf.put("foo", "oof");
    conf.put("bar", new Integer(0));
    ManagedService ms = (ManagedService) osgiHelper.getContext().getService(msRef);
    try {
        ms.updated(conf);
    } catch (ConfigurationException e) {
        fail("Configuration Exception : " + e);
    }
    // Recheck props
    fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance3.getInstanceName());
    fooP = (String) fooRef.getProperty("foo");
    barP = (Integer) fooRef.getProperty("bar");
    bazP = (String) fooRef.getProperty("baz");
    assertEquals("Check foo equality", fooP, "oof");
    assertEquals("Check bar equality", barP, new Integer(0));
    assertEquals("Check baz equality", bazP, "zab");
    // Check field value
    FooService fs = (FooService) osgiHelper.getContext().getService(fooRef);
    Properties p = fs.fooProps();
    fooP = (String) p.get("foo");
    barP = (Integer) p.get("bar");
    assertEquals("Check foo field equality", fooP, "oof");
    assertEquals("Check bar field equality", barP, new Integer(0));
    osgiHelper.getContext().ungetService(fooRef);
    osgiHelper.getContext().ungetService(msRef);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ManagedService(org.osgi.service.cm.ManagedService) ConfigurationException(org.osgi.service.cm.ConfigurationException) Hashtable(java.util.Hashtable) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 39 with ConfigurationException

use of org.osgi.service.cm.ConfigurationException in project felix by apache.

the class TestManagedServiceConfigurableProperties method testDynamicInstance1.

@Test
public void testDynamicInstance1() {
    ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance1.getInstanceName());
    assertNotNull("Check FS availability", fooRef);
    String fooP = (String) fooRef.getProperty("foo");
    Integer barP = (Integer) fooRef.getProperty("bar");
    String bazP = (String) fooRef.getProperty("baz");
    assertEquals("Check foo equality", fooP, "foo");
    assertEquals("Check bar equality", barP, new Integer(2));
    assertEquals("Check baz equality", bazP, "baz");
    ServiceReference msRef = osgiHelper.getServiceReferenceByPID(ManagedService.class.getName(), "FooProvider-3");
    assertNotNull("Check ManagedServiceFactory availability", msRef);
    // Configuration of baz
    Dictionary<String, Object> conf = new Hashtable<String, Object>();
    conf.put("baz", "zab");
    conf.put("foo", "oof");
    conf.put("bar", new Integer(0));
    ManagedService ms = (ManagedService) osgiHelper.getContext().getService(msRef);
    try {
        ms.updated(conf);
    } catch (ConfigurationException e) {
        fail("Configuration Exception : " + e);
    }
    // Re-check props
    fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance1.getInstanceName());
    fooP = (String) fooRef.getProperty("foo");
    barP = (Integer) fooRef.getProperty("bar");
    bazP = (String) fooRef.getProperty("baz");
    assertEquals("Check foo equality", fooP, "oof");
    assertEquals("Check bar equality", barP, new Integer(0));
    assertEquals("Check baz equality", bazP, "zab");
    // Check field value
    FooService fs = (FooService) osgiHelper.getContext().getService(fooRef);
    Properties p = fs.fooProps();
    fooP = (String) p.get("foo");
    barP = (Integer) p.get("bar");
    assertEquals("Check foo field equality", fooP, "oof");
    assertEquals("Check bar field equality", barP, new Integer(0));
    osgiHelper.getContext().ungetService(fooRef);
    osgiHelper.getContext().ungetService(msRef);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ManagedService(org.osgi.service.cm.ManagedService) ConfigurationException(org.osgi.service.cm.ConfigurationException) Hashtable(java.util.Hashtable) Properties(java.util.Properties) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 40 with ConfigurationException

use of org.osgi.service.cm.ConfigurationException in project felix by apache.

the class TestManagedServiceConfigurableProperties method testStaticInstance2.

@Test
public void testStaticInstance2() {
    ServiceReference fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
    assertNotNull("Check FS availability", fooRef);
    String fooP = (String) fooRef.getProperty("foo");
    Integer barP = (Integer) fooRef.getProperty("bar");
    String bazP = (String) fooRef.getProperty("baz");
    assertEquals("Check foo equality -1", fooP, "foo");
    assertEquals("Check bar equality -1", barP, new Integer(2));
    assertEquals("Check baz equality -1", bazP, "baz");
    ServiceReference msRef = osgiHelper.getServiceReferenceByPID(ManagedService.class.getName(), "instance");
    assertNotNull("Check ManagedService availability", msRef);
    // Configuration of baz
    Dictionary<String, Object> conf = new Hashtable<String, Object>();
    conf.put("baz", "zab");
    conf.put("bar", new Integer(2));
    conf.put("foo", "foo");
    ManagedService ms = (ManagedService) osgiHelper.getContext().getService(msRef);
    try {
        ms.updated(conf);
    } catch (ConfigurationException e) {
        fail("Configuration Exception : " + e);
    }
    // Recheck props
    fooRef = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), instance2.getInstanceName());
    fooP = (String) fooRef.getProperty("foo");
    barP = (Integer) fooRef.getProperty("bar");
    bazP = (String) fooRef.getProperty("baz");
    assertEquals("Check foo equality -2", fooP, "foo");
    assertEquals("Check bar equality -2", barP, new Integer(2));
    assertEquals("Check baz equality -2", bazP, "zab");
    osgiHelper.getContext().ungetService(fooRef);
    osgiHelper.getContext().ungetService(msRef);
}
Also used : FooService(org.apache.felix.ipojo.runtime.core.services.FooService) ManagedService(org.osgi.service.cm.ManagedService) ConfigurationException(org.osgi.service.cm.ConfigurationException) Hashtable(java.util.Hashtable) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Aggregations

ConfigurationException (org.osgi.service.cm.ConfigurationException)190 Activate (org.apache.felix.scr.annotations.Activate)31 ServiceReference (org.osgi.framework.ServiceReference)30 Matcher (java.util.regex.Matcher)28 Hashtable (java.util.Hashtable)26 Properties (java.util.Properties)22 IOException (java.io.IOException)21 Test (org.junit.Test)21 ManagedService (org.osgi.service.cm.ManagedService)20 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)19 HashMap (java.util.HashMap)17 File (java.io.File)11 URISyntaxException (java.net.URISyntaxException)11 Collection (java.util.Collection)11 HashSet (java.util.HashSet)11 ServiceTracker (org.osgi.util.tracker.ServiceTracker)10 URI (java.net.URI)9 Dictionary (java.util.Dictionary)9 Map (java.util.Map)9 NotFoundException (org.opencastproject.util.NotFoundException)9