Search in sources :

Example 51 with Configuration

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

the class ConfigInstallTest method testInstallUpdateRemoveTemplateConfigFactory.

@Test
public void testInstallUpdateRemoveTemplateConfigFactory() throws Exception {
    final Dictionary<String, Object> cfgData = new Hashtable<String, Object>();
    cfgData.put("foo", "bar");
    cfgData.put(InstallableResource.RESOURCE_IS_TEMPLATE, "true");
    final String cfgFactoryPid = getClass().getSimpleName() + "." + uniqueID();
    final String alias = "alias" + uniqueID();
    // install factory config
    final InstallableResource rsrc = new InstallableResource("/configA/" + cfgFactoryPid + "-" + alias, null, cfgData, null, InstallableResource.TYPE_PROPERTIES, 10);
    installer.updateResources(URL_SCHEME, new InstallableResource[] { rsrc }, null);
    // get factory config
    final Configuration cfg = waitForFactoryConfigValue("After installing", cfgFactoryPid, "foo", "bar");
    // update configuration
    final Dictionary<String, Object> secondData = new Hashtable<String, Object>();
    secondData.put("foo", "bla");
    cfg.update(secondData);
    waitForResource(URL_SCHEME + ":/configA/" + cfgFactoryPid + "-" + alias, ResourceState.IGNORED);
    // get updated factory config
    final Configuration secondCfg = waitForFactoryConfigValue("After updating", cfgFactoryPid, "foo", "bla");
    // remove config
    secondCfg.delete();
    // we have to wait here as no state change is happening
    sleep(200);
    waitForResource(URL_SCHEME + ":/configA/" + cfgFactoryPid + "-" + alias, ResourceState.IGNORED);
    waitForFactoryConfiguration("After deleting", cfgFactoryPid, false);
}
Also used : Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) InstallableResource(org.apache.sling.installer.api.InstallableResource) Test(org.junit.Test)

Example 52 with Configuration

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

the class ConfigInstallTask method execute.

@Override
public void execute(final InstallationContext ctx) {
    synchronized (Coordinator.SHARED) {
        // Get or create configuration, but do not
        // update if the new one has the same values.
        boolean created = false;
        try {
            String location = (String) this.getResource().getDictionary().get(ConfigurationConstants.PROPERTY_BUNDLE_LOCATION);
            if (location == null) {
                // default
                location = Activator.DEFAULT_LOCATION;
            } else if (location.length() == 0) {
                location = null;
            }
            Configuration config = getConfiguration();
            if (config == null) {
                created = true;
                config = createConfiguration(location);
            } else {
                if (ConfigUtil.isSameData(config.getProperties(), getResource().getDictionary())) {
                    this.getLogger().debug("Configuration {} already installed with same data, update request ignored: {}", config.getPid(), getResource());
                    config = null;
                } else {
                    config.setBundleLocation(location);
                }
            }
            if (config != null) {
                config.update(getDictionary());
                ctx.log("Installed configuration {} from resource {}", config.getPid(), getResource());
                if (this.factoryPid != null) {
                    this.aliasPid = config.getPid();
                }
                this.getLogger().debug("Configuration " + config.getPid() + " " + (created ? "created" : "updated") + " from " + getResource());
                this.setFinishedState(ResourceState.INSTALLED, this.getCompositeAliasPid());
                final Operation op = new Coordinator.Operation(config.getPid(), config.getFactoryPid(), false);
                Coordinator.SHARED.add(op);
            } else {
                this.setFinishedState(ResourceState.IGNORED, this.getCompositeAliasPid());
            }
        } catch (Exception e) {
            this.getLogger().debug("Exception during installation of config " + this.getResource() + " : " + e.getMessage() + ". Retrying later.", e);
        }
    }
}
Also used : Configuration(org.osgi.service.cm.Configuration) Operation(org.apache.sling.installer.factories.configuration.impl.Coordinator.Operation)

Example 53 with Configuration

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

the class ConfigInstallTest method testInstallAndRemoveConfig.

@Test
public void testInstallAndRemoveConfig() throws Exception {
    final Dictionary<String, Object> cfgData = new Hashtable<String, Object>();
    cfgData.put("foo", "bar");
    final String cfgPid = getClass().getSimpleName() + "." + uniqueID();
    assertNull("Config " + cfgPid + " must not be found before test", findConfiguration(cfgPid));
    // install config
    final InstallableResource[] rsrc = getInstallableResource(cfgPid, cfgData);
    installer.updateResources(URL_SCHEME, rsrc, null);
    Configuration cfg = waitForConfiguration("After installing", cfgPid, true);
    assertEquals("Config value must match", "bar", cfg.getProperties().get("foo"));
    // remove resource
    installer.updateResources(URL_SCHEME, null, new String[] { rsrc[0].getId() });
    waitForConfiguration("After removing", cfgPid, false);
    // Reinstalling with same digest must work
    installer.updateResources(URL_SCHEME, rsrc, null);
    cfg = waitForConfiguration("After reinstalling", cfgPid, true);
    assertEquals("Config value must match", "bar", cfg.getProperties().get("foo"));
    // remove again
    installer.updateResources(URL_SCHEME, null, new String[] { rsrc[0].getId() });
    waitForConfiguration("After removing for the second time", cfgPid, false);
}
Also used : Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable) InstallableResource(org.apache.sling.installer.api.InstallableResource) Test(org.junit.Test)

Example 54 with Configuration

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

the class ConfigDumpServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    final String configPid = req.getPathInfo().substring(1);
    final Configuration cfg = configAdmin.getConfiguration(configPid);
    if (cfg == null || cfg.getProperties() == null) {
        resp.sendError(HttpServletResponse.SC_NOT_FOUND, "No config found with PID=" + configPid + " (the PID is extracted from the path information that follows this servlet's path");
        return;
    }
    final SortedSet<String> keys = new TreeSet<String>();
    final Enumeration<?> e = cfg.getProperties().keys();
    while (e.hasMoreElements()) {
        keys.add(e.nextElement().toString());
    }
    final StringBuilder b = new StringBuilder();
    b.append(configPid).append("#");
    for (String key : keys) {
        final Object value = cfg.getProperties().get(key);
        b.append(key).append("=(").append(value.getClass().getSimpleName()).append(")").append(prettyprint(value)).append("#");
    }
    b.append("#EOC#");
    resp.setContentType("text/plain");
    resp.setCharacterEncoding("UTF-8");
    resp.getWriter().write(b.toString());
    resp.getWriter().flush();
}
Also used : Configuration(org.osgi.service.cm.Configuration) TreeSet(java.util.TreeSet)

Example 55 with Configuration

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

the class AnonymousAccessConfigServlet method doPost.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    response.setContentType(request.getContentType());
    String action = request.getParameter("action");
    if ("disable".equals(action)) {
        int existingModifiedCounter = modifiedCounter;
        Configuration config = configAdmin.getConfiguration(AUTH_PID, null);
        Dictionary props = config.getProperties();
        if (props == null) {
            props = new Hashtable();
        }
        props.put(PROP_AUTH_ANNONYMOUS, Boolean.FALSE);
        config.update(props);
        waitForModified(existingModifiedCounter, TIMEOUT);
    } else if ("enable".equals(action)) {
        int existingModifiedCounter = modifiedCounter;
        Configuration config = configAdmin.getConfiguration(AUTH_PID, null);
        Dictionary props = config.getProperties();
        if (props == null) {
            props = new Hashtable();
        }
        props.put(PROP_AUTH_ANNONYMOUS, Boolean.TRUE);
        config.update(props);
        waitForModified(existingModifiedCounter, TIMEOUT);
    }
    response.getWriter().println("ok");
}
Also used : Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) Hashtable(java.util.Hashtable)

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