Search in sources :

Example 46 with Configuration

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

the class MockOsgi method setConfigForPid.

private static void setConfigForPid(BundleContext bundleContext, String pid, Dictionary<String, Object> properties) {
    ConfigurationAdmin configAdmin = getConfigAdmin(bundleContext);
    if (configAdmin == null) {
        throw new RuntimeException("ConfigurationAdmin service is not registered in bundle context.");
    }
    try {
        Configuration config = configAdmin.getConfiguration(pid);
        config.update(properties);
    } catch (IOException ex) {
        throw new RuntimeException("Unable to update configuration for pid '" + pid + "'.", ex);
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) IOException(java.io.IOException) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 47 with Configuration

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

the class OsgiInstallerTestBase method waitForConfiguration.

/**
     * Internal method to wait for a configuration
     */
private Configuration waitForConfiguration(final String info, final String pid, final String factoryPid, final boolean shouldBePresent) throws Exception {
    final String logKey = factoryPid == null ? "config" : "factory config";
    String msg;
    if (info == null) {
        msg = "";
    } else {
        msg = info + ": ";
    }
    Configuration result = null;
    final long start = System.currentTimeMillis();
    final long end = start + WAIT_FOR_CHANGE_TIMEOUT;
    log(LogService.LOG_DEBUG, "Starting " + logKey + " check at " + start + "; ending by " + end);
    do {
        if (factoryPid != null) {
            result = findFactoryConfiguration(factoryPid);
        } else {
            result = findConfiguration(pid);
        }
        if ((shouldBePresent && result != null) || (!shouldBePresent && result == null)) {
            break;
        }
        log(LogService.LOG_DEBUG, logKey + " check failed at " + System.currentTimeMillis() + "; sleeping");
        sleep(25);
    } while (System.currentTimeMillis() < end);
    if (shouldBePresent && result == null) {
        fail(msg + logKey + " not found (" + (factoryPid != null ? factoryPid : pid) + ")");
    } else if (!shouldBePresent && result != null) {
        fail(msg + logKey + " is still present (" + (factoryPid != null ? factoryPid : pid) + ")");
    }
    return result;
}
Also used : Configuration(org.osgi.service.cm.Configuration)

Example 48 with Configuration

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

the class OsgiConfigurationManager method deleteOsgiConfigs.

private void deleteOsgiConfigs(List<Configuration> configurations) {
    for (Configuration configuration : configurations) {
        String pid = configuration.getPid();
        try {
            configuration.delete();
            log.info("Deleted configuration {}", pid);
        } catch (IOException e) {
            log.warn("Cannot delete configuration {}", pid, e);
        }
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) IOException(java.io.IOException)

Example 49 with Configuration

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

the class OsgiConfigurationManager method getConfig.

public DistributionConfiguration getConfig(ResourceResolver resolver, DistributionComponentKind kind, String name) {
    List<Configuration> configurations = getOsgiConfigurations(kind, name);
    if (configurations == null || configurations.isEmpty()) {
        return null;
    }
    if (configurations.size() > 1) {
        log.warn("Found more than one configuration of kind: {} and with name: {}", new String[] { kind.getName(), name });
    }
    Configuration configuration = configurations.get(0);
    if (configuration != null) {
        @SuppressWarnings("unchecked") Dictionary<String, Object> properties = configuration.getProperties();
        Map<String, Object> result = OsgiUtils.fromDictionary(properties);
        String factoryPid = PropertiesUtil.toString(result.get(ConfigurationAdmin.SERVICE_FACTORYPID), null);
        String type = kind.getType(factoryPid);
        result.put(DistributionComponentConstants.PN_TYPE, type);
        result = filterBeforeRead(result);
        return new DistributionConfiguration(kind, name, result);
    }
    return null;
}
Also used : Configuration(org.osgi.service.cm.Configuration)

Example 50 with Configuration

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

the class DataSourceIT method testDataSourceAsService.

@SuppressWarnings("unchecked")
@Test
public void testDataSourceAsService() throws Exception {
    Configuration config = ca.createFactoryConfiguration(PID, null);
    Dictionary<String, Object> p = new Hashtable<String, Object>();
    p.put("url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE");
    p.put("datasource.name", "test");
    p.put("initialSize", "5");
    p.put("defaultAutoCommit", "default");
    p.put("defaultReadOnly", "false");
    p.put("datasource.svc.properties", new String[] { "initSQL=SELECT 1" });
    p.put("maxActive", 70);
    config.update(p);
    Filter filter = context.createFilter("(&(objectclass=javax.sql.DataSource)(datasource.name=test))");
    ServiceTracker<DataSource, DataSource> st = new ServiceTracker<DataSource, DataSource>(context, filter, null);
    st.open();
    DataSource ds = st.waitForService(10000);
    assertNotNull(ds);
    Connection conn = ds.getConnection();
    assertNotNull(conn);
    //Cannot access directly so access via reflection
    assertEquals("70", getProperty(ds, "poolProperties.maxActive"));
    assertEquals("5", getProperty(ds, "poolProperties.initialSize"));
    assertEquals("SELECT 1", getProperty(ds, "poolProperties.initSQL"));
    assertEquals("false", getProperty(ds, "poolProperties.defaultReadOnly"));
    assertNull(getProperty(ds, "poolProperties.defaultAutoCommit"));
    config = ca.listConfigurations("(datasource.name=test)")[0];
    Dictionary dic = config.getProperties();
    dic.put("defaultReadOnly", Boolean.TRUE);
    config.update(dic);
    TimeUnit.MILLISECONDS.sleep(100);
    assertEquals("true", getProperty(ds, "poolProperties.defaultReadOnly"));
}
Also used : Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) Filter(org.osgi.framework.Filter) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Hashtable(java.util.Hashtable) Connection(java.sql.Connection) DataSource(javax.sql.DataSource) Test(org.junit.Test)

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