Search in sources :

Example 46 with ConfigurationException

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

the class ConfigurationSupport method updated0.

void updated0(Dictionary config) throws ConfigurationException {
    // validate hashed password
    if (isPasswordHashed(config)) {
        osgiManager.updateConfiguration(config);
    } else {
        // hash the password, update config and wait for the
        // updated configuration to be supplied later
        final BundleContext bc = this.osgiManager.getBundleContext();
        final ServiceReference ref = bc.getServiceReference(ConfigurationAdmin.class.getName());
        if (ref != null) {
            final ConfigurationAdmin ca = (ConfigurationAdmin) bc.getService(ref);
            if (ca != null) {
                try {
                    Configuration cfg = ca.getConfiguration(this.osgiManager.getConfigurationPid());
                    Dictionary newConfig = cfg.getProperties();
                    if (newConfig != null) {
                        String pwd = (String) config.get(OsgiManager.PROP_PASSWORD);
                        // password can be null, see FELIX-4995
                        final String hashedPassword = null == pwd ? OsgiManager.DEFAULT_PASSWORD : Password.hashPassword(pwd);
                        newConfig.put(OsgiManager.PROP_PASSWORD, hashedPassword);
                        cfg.update(newConfig);
                    }
                } catch (Exception e) {
                    // IllegalStateException from hashing password
                    throw new ConfigurationException(OsgiManager.PROP_PASSWORD, "Cannot update password property", e);
                } finally {
                    bc.ungetService(ref);
                }
            }
        }
    }
}
Also used : Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) ConfigurationException(org.osgi.service.cm.ConfigurationException) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) PrivilegedActionException(java.security.PrivilegedActionException) ConfigurationException(org.osgi.service.cm.ConfigurationException) BundleContext(org.osgi.framework.BundleContext) ServiceReference(org.osgi.framework.ServiceReference)

Example 47 with ConfigurationException

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

the class ConfigurationTest method testConfigurationFailure.

@Test
public void testConfigurationFailure() throws Throwable {
    final Ensure e = new Ensure();
    // Create our configuration dependency
    final ConfigurationDependencyImpl conf = new ConfigurationDependencyImpl();
    conf.setPid("some.pid");
    // Create another required dependency
    final SimpleServiceDependency requiredDependency = new SimpleServiceDependency();
    requiredDependency.setRequired(true);
    requiredDependency.setCallbacks("addDep", null);
    // Create our component, which will fail when handling configuration update
    ComponentImpl c = new ComponentImpl();
    c.setImplementation(new Object() {

        volatile Dictionary m_conf;

        public void updated(Dictionary conf) {
            debug("updated: conf=%s", conf);
            m_conf = conf;
            if ("invalid".equals(conf.get("conf"))) {
                // We refuse the first configuration.
                debug("refusing configuration");
                e.step(1);
                // Set our acceptUpdate flag to true, so next update will be successful
                throw new RuntimeException("update failed (expected)");
            } else {
                debug("accepting configuration");
                e.step(2);
            }
        }

        public void addDep() {
            if ("invalid".equals(m_conf.get("conf"))) {
                e.throwable(new Exception("addDep should not be called"));
            }
            e.step(3);
            debug("addDep");
        }

        void init(Component c) {
            if ("invalid".equals(m_conf.get("conf"))) {
                e.throwable(new Exception("init should not be called"));
            }
            e.step(4);
            debug("init");
        }

        void start() {
            if ("invalid".equals(m_conf.get("conf"))) {
                e.throwable(new Exception("start should not be called"));
            }
            e.step(5);
            debug("start");
        }
    });
    // Add the dependencies
    c.add(conf);
    c.add(requiredDependency);
    // Start our component ("requiredDependency" is not yet available, so we'll stay in WAITING_FOR_REQUIRED state).
    c.start();
    // Enabled "requiredDependency"
    requiredDependency.add(new EventImpl());
    // Now, act as the configuration admin service and inject a wrong dependency
    try {
        Hashtable props = new Hashtable();
        props.put("conf", "invalid");
        conf.updated(props);
    } catch (ConfigurationException err) {
        warn("got expected configuration error");
    }
    e.waitForStep(1, 5000);
    e.ensure();
    // Now, inject another valid configuration
    try {
        Hashtable props = new Hashtable();
        props.put("conf", "valid");
        conf.updated(props);
    } catch (ConfigurationException err) {
        warn("got unexpected configuration error");
        e.throwable(err);
    }
    // This time, our component should be started properly.
    e.waitForStep(5, 5000);
    e.ensure();
}
Also used : Dictionary(java.util.Dictionary) ConfigurationException(org.osgi.service.cm.ConfigurationException) Hashtable(java.util.Hashtable) ConfigurationDependencyImpl(org.apache.felix.dm.impl.ConfigurationDependencyImpl) Component(org.apache.felix.dm.Component) ComponentImpl(org.apache.felix.dm.impl.ComponentImpl) ConfigurationException(org.osgi.service.cm.ConfigurationException) Test(org.junit.Test)

Example 48 with ConfigurationException

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

the class ServiceRaceTest method doTest.

void doTest(int loop) throws Throwable {
    debug("loop#%d -------------------------", loop);
    final Ensure step = new Ensure(false);
    // Create one client component, which depends on many service dependencies
    final ComponentImpl client = new ComponentImpl();
    final Client theClient = new Client(step);
    client.setImplementation(theClient);
    // Create client service dependencies
    final SimpleServiceDependency[] dependencies = new SimpleServiceDependency[DEPENDENCIES];
    for (int i = 0; i < DEPENDENCIES; i++) {
        dependencies[i] = new SimpleServiceDependency();
        dependencies[i].setRequired(true);
        dependencies[i].setCallbacks("add", "remove");
        client.add(dependencies[i]);
    }
    final ConfigurationDependencyImpl confDependency = new ConfigurationDependencyImpl();
    confDependency.setPid("mypid");
    client.add(confDependency);
    // Create Configuration (concurrently).
    // We have to simulate the configuration update, using a component state listener, which will
    // trigger an update thread, but only once the component is started.
    final ComponentStateListener listener = new ComponentStateListener() {

        private volatile Dictionary m_conf;

        public void changed(Component c, ComponentState state) {
            if (state == ComponentState.WAITING_FOR_REQUIRED && m_conf == null) {
                m_conf = new Hashtable();
                m_conf.put("foo", "bar");
                m_threadpool.execute(new Runnable() {

                    public void run() {
                        try {
                            confDependency.updated(m_conf);
                        } catch (ConfigurationException e) {
                            warn("configuration failed", e);
                        }
                    }
                });
            }
        }
    };
    client.add(listener);
    // Start the client (concurrently)
    m_threadpool.execute(new Runnable() {

        public void run() {
            client.start();
            // injected only one the tracker has been opened ...
            for (int i = 0; i < DEPENDENCIES; i++) {
                final SimpleServiceDependency dep = dependencies[i];
                final Event added = new EventImpl(i);
                m_threadpool.execute(new Runnable() {

                    public void run() {
                        dep.add(added);
                    }
                });
            }
        }
    });
    // Ensure that client has been started.
    int expectedStep = 1 + /* conf */
    DEPENDENCIES + 1;
    step.waitForStep(expectedStep, STEP_WAIT);
    Assert.assertEquals(DEPENDENCIES, theClient.getDependencies());
    Assert.assertNotNull(theClient.getConfiguration());
    client.remove(listener);
    // Stop the client and all dependencies concurrently.
    for (int i = 0; i < DEPENDENCIES; i++) {
        final SimpleServiceDependency dep = dependencies[i];
        final Event removed = new EventImpl(i);
        m_threadpool.execute(new Runnable() {

            public void run() {
                dep.remove(removed);
            }
        });
    }
    m_threadpool.execute(new Runnable() {

        public void run() {
            client.stop();
        }
    });
    m_threadpool.execute(new Runnable() {

        public void run() {
            try {
                // simulate a configuration suppression.
                confDependency.updated(null);
            } catch (ConfigurationException e) {
                warn("error while unconfiguring", e);
            }
        }
    });
    // Ensure that client has been stopped, then destroyed, then unbound from all dependencies
    // stop/destroy
    expectedStep += 2;
    // removed all dependencies
    expectedStep += DEPENDENCIES;
    step.waitForStep(expectedStep, STEP_WAIT);
    step.ensure();
    Assert.assertEquals(0, theClient.getDependencies());
    debug("finished one test loop");
    if ((loop + 1) % 100 == 0) {
        long duration = System.currentTimeMillis() - m_timeStamp;
        warn("Performed 100 tests (total=%d) in %d ms.", (loop + 1), duration);
        m_timeStamp = System.currentTimeMillis();
    }
}
Also used : Dictionary(java.util.Dictionary) Hashtable(java.util.Hashtable) ConfigurationDependencyImpl(org.apache.felix.dm.impl.ConfigurationDependencyImpl) ComponentImpl(org.apache.felix.dm.impl.ComponentImpl) ConfigurationException(org.osgi.service.cm.ConfigurationException) Event(org.apache.felix.dm.context.Event) Component(org.apache.felix.dm.Component) ComponentStateListener(org.apache.felix.dm.ComponentStateListener) ComponentState(org.apache.felix.dm.ComponentState)

Example 49 with ConfigurationException

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

the class RoleRepositoryFileStore method updated.

/**
 * {@inheritDoc}
 */
public void updated(Dictionary properties) throws ConfigurationException {
    boolean writeDisabled = DEFAULT_WRITE_DISABLED;
    int writeDelayValue = DEFAULT_WRITE_DELAY_VALUE;
    TimeUnit writeDelayUnit = DEFAULT_WRITE_DELAY_TIMEUNIT;
    if (properties != null) {
        Object wd = properties.get(KEY_WRITE_DISABLED);
        if (wd == null) {
            throw new ConfigurationException(KEY_WRITE_DISABLED, "Missing write disabled value!");
        }
        try {
            writeDisabled = Boolean.parseBoolean((String) wd);
        } catch (Exception e) {
            throw new ConfigurationException(KEY_WRITE_DISABLED, "Invalid write disabled value!");
        }
        if (!writeDisabled) {
            Object wdv = properties.get(KEY_WRITE_DELAY_VALUE);
            if (wdv == null) {
                throw new ConfigurationException(KEY_WRITE_DELAY_VALUE, "Missing write delay value!");
            }
            try {
                writeDelayValue = Integer.parseInt((String) wdv);
            } catch (Exception e) {
                throw new ConfigurationException(KEY_WRITE_DELAY_VALUE, "Invalid write delay value!");
            }
            if (writeDelayValue <= 0) {
                throw new ConfigurationException(KEY_WRITE_DELAY_VALUE, "Invalid write delay value!");
            }
            Object wdu = properties.get(KEY_WRITE_DELAY_TIMEUNIT);
            if (wdu != null) {
                try {
                    writeDelayUnit = TimeUnit.valueOf(((String) wdu).toUpperCase());
                } catch (Exception e) {
                    throw new ConfigurationException(KEY_WRITE_DELAY_TIMEUNIT, "Invalid write delay unit!");
                }
            }
        }
    }
    ResettableTimer timer = (ResettableTimer) m_timerRef.get();
    if (timer != null) {
        timer.shutDown();
    }
    m_timerRef.compareAndSet(timer, writeDisabled ? null : new ResettableTimer(this, writeDelayValue, writeDelayUnit));
}
Also used : ConfigurationException(org.osgi.service.cm.ConfigurationException) TimeUnit(java.util.concurrent.TimeUnit) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) ConfigurationException(org.osgi.service.cm.ConfigurationException)

Example 50 with ConfigurationException

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

the class RoleRepositoryFileStoreTest method testUpdateConfigurationWithKeyWriteDelayValueNegativeValueFail.

/**
 * Tests that calling updated with the key "background.write.delay.value" set to a negative value fails.
 */
public void testUpdateConfigurationWithKeyWriteDelayValueNegativeValueFail() throws Exception {
    Properties properties = new Properties();
    properties.put(RoleRepositoryFileStore.KEY_WRITE_DISABLED, "false");
    properties.put(RoleRepositoryFileStore.KEY_WRITE_DELAY_VALUE, "-1");
    properties.put(RoleRepositoryFileStore.KEY_WRITE_DELAY_TIMEUNIT, "seconds");
    try {
        m_store.updated(properties);
        fail("ConfigurationException expected!");
    } catch (ConfigurationException e) {
    // Ok; expected
    }
}
Also used : ConfigurationException(org.osgi.service.cm.ConfigurationException) Properties(java.util.Properties)

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