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);
}
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();
}
}
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());
}
}
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();
}
}
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"));
}
Aggregations