Search in sources :

Example 1 with Configuration

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

the class ConfigAdminBinding method initializeBus.

/**
     * Publishes the items with the configuration values.
     */
private void initializeBus() {
    delayedExecutor.cancel();
    delayedExecutor.schedule(new TimerTask() {

        @Override
        public void run() {
            for (ConfigAdminBindingProvider provider : providers) {
                for (String itemName : provider.getItemNames()) {
                    ConfigAdminBindingConfig bindingConfig = provider.getBindingConfig(itemName);
                    Configuration config = getConfiguration(bindingConfig);
                    postUpdate(config, bindingConfig);
                }
            }
        }
    }, 3000);
}
Also used : TimerTask(java.util.TimerTask) ConfigAdminBindingConfig(org.openhab.binding.configadmin.internal.ConfigAdminGenericBindingProvider.ConfigAdminBindingConfig) Configuration(org.osgi.service.cm.Configuration) ConfigAdminBindingProvider(org.openhab.binding.configadmin.ConfigAdminBindingProvider)

Example 2 with Configuration

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

the class ConfigurationTests method testConfiguration.

@SuppressWarnings({ "rawtypes", "unchecked" })
public void testConfiguration() throws Exception {
    Bundle tb3Bundle = installBundle("tb3.jar");
    Configuration configurationA = null, configurationB = null;
    try {
        Filter filter = bundleContext.createFilter("(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + tb3Bundle.getBundleId() + "))");
        ServiceTracker<CdiContainer, CdiContainer> containerTracker = new ServiceTracker<>(bundleContext, filter, null);
        containerTracker.open();
        containerTracker.waitForService(timeout);
        ServiceReference<CdiContainer> serviceReference = containerTracker.getServiceReference();
        assertNotNull(serviceReference);
        assertEquals(CdiEvent.Type.WAITING_FOR_CONFIGURATIONS, serviceReference.getProperty(CdiConstants.CDI_CONTAINER_STATE));
        configurationA = configurationAdmin.getConfiguration("configA", "?");
        Dictionary<String, Object> properties = new Hashtable<>();
        properties.put("ports", new int[] { 12, 4567 });
        configurationA.update(properties);
        configurationB = configurationAdmin.getConfiguration("configB", "?");
        properties = new Hashtable<>();
        properties.put("color", "green");
        properties.put("ports", new int[] { 80 });
        configurationB.update(properties);
        containerTracker.close();
        filter = bundleContext.createFilter("(&(objectClass=" + CdiContainer.class.getName() + ")(service.bundleid=" + tb3Bundle.getBundleId() + ")(" + CdiConstants.CDI_CONTAINER_STATE + "=CREATED))");
        containerTracker = new ServiceTracker<>(bundleContext, filter, null);
        containerTracker.open();
        containerTracker.waitForService(timeout);
        ServiceTracker<BeanService, BeanService> stA = new ServiceTracker<BeanService, BeanService>(bundleContext, bundleContext.createFilter("(&(objectClass=org.apache.aries.cdi.test.interfaces.BeanService)(bean=A))"), null);
        stA.open(true);
        BeanService<Callable<int[]>> beanService = stA.waitForService(timeout);
        assertNotNull(beanService);
        assertEquals("blue", beanService.doSomething());
        assertArrayEquals(new int[] { 12, 4567 }, beanService.get().call());
        ServiceTracker<BeanService, BeanService> stB = new ServiceTracker<BeanService, BeanService>(bundleContext, bundleContext.createFilter("(&(objectClass=org.apache.aries.cdi.test.interfaces.BeanService)(bean=B))"), null);
        stB.open(true);
        beanService = stB.waitForService(timeout);
        assertNotNull(beanService);
        assertEquals("green", beanService.doSomething());
        assertArrayEquals(new int[] { 80 }, beanService.get().call());
    } finally {
        if (configurationA != null) {
            try {
                configurationA.delete();
            } catch (Exception e) {
            // ignore
            }
        }
        if (configurationB != null) {
            try {
                configurationB.delete();
            } catch (Exception e) {
            // ignore
            }
        }
        tb3Bundle.uninstall();
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Bundle(org.osgi.framework.Bundle) Hashtable(java.util.Hashtable) Callable(java.util.concurrent.Callable) Filter(org.osgi.framework.Filter) BeanService(org.apache.aries.cdi.test.interfaces.BeanService) CdiContainer(org.osgi.service.cdi.CdiContainer)

Example 3 with Configuration

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

the class ConnectionLifecycleTest method testDeleteOfConfig.

@Test
public void testDeleteOfConfig() throws Exception {
    Assume.assumeTrue("Not a configuration test", isConfigured());
    txControl.required(() -> connection.createStatement().execute("Insert into TEST_TABLE values ( 'Hello World!' )"));
    assertEquals("Hello World!", txControl.notSupported(() -> {
        ResultSet rs = connection.createStatement().executeQuery("Select * from TEST_TABLE");
        rs.next();
        return rs.getString(1);
    }));
    ConfigurationAdmin cm = getService(ConfigurationAdmin.class, 5000);
    Configuration[] configurations = cm.listConfigurations("(service.factoryPid=org.apache.aries.tx.control.jdbc.*)");
    assertNotNull(configurations);
    assertEquals(1, configurations.length);
    configurations[0].delete();
    Thread.sleep(2000);
    try {
        assertEquals("Hello World!", txControl.notSupported(() -> {
            ResultSet rs = connection.createStatement().executeQuery("Select * from TEST_TABLE");
            rs.next();
            return rs.getString(1);
        }));
        fail("Should not be accessible");
    } catch (ScopedWorkException swe) {
        assertTrue(swe.getCause().toString(), swe.getCause() instanceof TransactionException);
        assertEquals("There was a problem getting hold of a database connection", swe.getCause().getMessage());
    }
}
Also used : ScopedWorkException(org.osgi.service.transaction.control.ScopedWorkException) TransactionException(org.osgi.service.transaction.control.TransactionException) Configuration(org.osgi.service.cm.Configuration) ResultSet(java.sql.ResultSet) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) Test(org.junit.Test)

Example 4 with Configuration

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

the class ConfigRepositoryImpl method updateStorage.

protected void updateStorage(String pid, Dictionary<String, Object> props) throws IOException {
    if (storage != null) {
        Configuration cfg = configAdmin.getConfiguration(pid, null);
        // Initialize cfgFile with default location. Value gets overwritten when the existing configuration references a correct location.
        File cfgFile = new File(storage, pid + ".cfg");
        if (cfg != null) {
            Dictionary<String, Object> oldProps = cfg.getProperties();
            if (oldProps != null && oldProps.get(FILEINSTALL_FILE_NAME) != null) {
                try {
                    cfgFile = getCfgFileFromProperties(oldProps);
                    if (cfgFile == null) {
                        throw new IOException("The configuration value '" + oldProps.get(FILEINSTALL_FILE_NAME) + "' for '" + FILEINSTALL_FILE_NAME + "' does not represent a valid file location.");
                    }
                } catch (URISyntaxException | MalformedURLException e) {
                    throw new IOException(e);
                }
            }
        }
        LOGGER.trace("Update {}", cfgFile.getName());
        // update the cfg file
        Properties properties = new Properties(cfgFile);
        for (Enumeration<String> keys = props.keys(); keys.hasMoreElements(); ) {
            String key = keys.nextElement();
            if (!Constants.SERVICE_PID.equals(key) && !ConfigurationAdmin.SERVICE_FACTORYPID.equals(key) && !FILEINSTALL_FILE_NAME.equals(key)) {
                if (props.get(key) != null) {
                    properties.put(key, props.get(key).toString());
                }
            }
        }
        // remove "removed" properties from the cfg file
        ArrayList<String> propertiesToRemove = new ArrayList<>();
        for (String key : properties.keySet()) {
            if (props.get(key) == null && !Constants.SERVICE_PID.equals(key) && !ConfigurationAdmin.SERVICE_FACTORYPID.equals(key) && !FILEINSTALL_FILE_NAME.equals(key)) {
                propertiesToRemove.add(key);
            }
        }
        for (String key : propertiesToRemove) {
            properties.remove(key);
        }
        // save the cfg file
        storage.mkdirs();
        properties.save();
    }
}
Also used : MalformedURLException(java.net.MalformedURLException) Configuration(org.osgi.service.cm.Configuration) ArrayList(java.util.ArrayList) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) URISyntaxException(java.net.URISyntaxException) Properties(org.apache.felix.utils.properties.Properties) File(java.io.File)

Example 5 with Configuration

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

the class ConfigManagedServiceFactoryTest method createNewFactoryConfig.

@Test
public void createNewFactoryConfig() throws Exception {
    executeCommand("config:edit --factory myconfig2\n" + "config:property-set test1 data1\n" + "config:update", new RolePrincipal("manager"));
    Configuration config = configAdmin.listConfigurations("(service.factorypid=myconfig2)")[0];
    assertEquals("data1", config.getProperties().get("test1"));
}
Also used : Configuration(org.osgi.service.cm.Configuration) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal) Test(org.junit.Test)

Aggregations

Configuration (org.osgi.service.cm.Configuration)512 Test (org.junit.Test)237 Hashtable (java.util.Hashtable)151 IOException (java.io.IOException)137 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)129 Dictionary (java.util.Dictionary)91 ServiceReference (org.osgi.framework.ServiceReference)77 Bundle (org.osgi.framework.Bundle)51 Properties (java.util.Properties)44 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)44 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)39 HashMap (java.util.HashMap)33 ArrayList (java.util.ArrayList)30 BundleContext (org.osgi.framework.BundleContext)28 File (java.io.File)23 Map (java.util.Map)23 Mockito.anyString (org.mockito.Mockito.anyString)21 ManagedServiceTestActivator (org.apache.felix.cm.integration.helper.ManagedServiceTestActivator)20 Enumeration (java.util.Enumeration)17 List (java.util.List)16