Search in sources :

Example 41 with ConfigurationAdmin

use of org.osgi.service.cm.ConfigurationAdmin 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 42 with ConfigurationAdmin

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

the class ConfigInstallerTest method testCreateConfigAndObserveCMDeleted.

public void testCreateConfigAndObserveCMDeleted() throws Exception {
    File file = File.createTempFile("test", ".config");
    try (OutputStream os = new FileOutputStream(file)) {
        os.write("key=F\"1137191584\"\n".getBytes("UTF-8"));
    }
    String pid = file.getName().substring(0, file.getName().indexOf(".config"));
    final Capture<Dictionary<String, Object>> props = new Capture<>();
    EasyMock.expect(mockConfiguration.getProperties()).andReturn(null);
    EasyMock.expect(mockBundleContext.getProperty((String) EasyMock.anyObject())).andReturn(null).anyTimes();
    EasyMock.expect(mockConfigurationAdmin.listConfigurations((String) EasyMock.anyObject())).andReturn(null);
    EasyMock.expect(mockConfigurationAdmin.getConfiguration(pid, "?")).andReturn(mockConfiguration);
    ServiceReference<ConfigurationAdmin> sr = EasyMock.createMock(ServiceReference.class);
    mockConfiguration.update(EasyMock.capture(props));
    EasyMock.expectLastCall();
    EasyMock.expect(mockConfigurationAdmin.getConfiguration(pid, "?")).andReturn(mockConfiguration);
    EasyMock.expect(mockConfiguration.getProperties()).andAnswer(new IAnswer<Dictionary<String, Object>>() {

        @Override
        public Dictionary<String, Object> answer() throws Throwable {
            return props.getValue();
        }
    });
    EasyMock.expect(mockConfiguration.getPid()).andReturn(pid);
    EasyMock.replay(mockConfiguration, mockConfigurationAdmin, mockBundleContext, sr);
    ConfigInstaller ci = new ConfigInstaller(mockBundleContext, mockConfigurationAdmin, new FileInstall());
    ci.install(file);
    ci.doConfigurationEvent(new ConfigurationEvent(sr, ConfigurationEvent.CM_UPDATED, null, pid));
    ci.doConfigurationEvent(new ConfigurationEvent(sr, ConfigurationEvent.CM_DELETED, null, pid));
    assertFalse("Configuration file should be deleted", file.isFile());
}
Also used : ConfigurationEvent(org.osgi.service.cm.ConfigurationEvent) Dictionary(java.util.Dictionary) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) Capture(org.easymock.Capture) FileOutputStream(java.io.FileOutputStream) File(java.io.File) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 43 with ConfigurationAdmin

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

the class ConfigurationEventToEventAdminBridge method configurationEvent.

@Override
public void configurationEvent(final ConfigurationEvent event) {
    final ServiceReference<EventAdmin> ref = m_context.getServiceReference(EventAdmin.class);
    if (null != ref) {
        final EventAdmin eventAdmin = m_context.getService(ref);
        if (null != eventAdmin) {
            try {
                final String topic;
                switch(event.getType()) {
                    case ConfigurationEvent.CM_UPDATED:
                        topic = "org/osgi/service/cm/ConfigurationEvent/CM_UPDATED";
                        break;
                    case ConfigurationEvent.CM_DELETED:
                        topic = "org/osgi/service/cm/ConfigurationEvent/CM_DELETED";
                        break;
                    case ConfigurationEvent.CM_LOCATION_CHANGED:
                        topic = "org/osgi/service/cm/ConfigurationEvent/CM_LOCATION_CHANGED";
                        break;
                    default:
                        return;
                }
                final Dictionary<String, Object> properties = new Hashtable<String, Object>();
                if (null != event.getFactoryPid()) {
                    properties.put("cm.factoryPid", event.getFactoryPid());
                }
                properties.put("cm.pid", event.getPid());
                final ServiceReference<ConfigurationAdmin> eventRef = event.getReference();
                if (null == eventRef) {
                    throw new IllegalArgumentException("ConfigurationEvent.getReference() may not be null");
                }
                properties.put(EventConstants.SERVICE, eventRef);
                properties.put(EventConstants.SERVICE_ID, eventRef.getProperty(EventConstants.SERVICE_ID));
                final Object objectClass = eventRef.getProperty(Constants.OBJECTCLASS);
                if (!(objectClass instanceof String[]) || !Arrays.asList((String[]) objectClass).contains(ConfigurationAdmin.class.getName())) {
                    throw new IllegalArgumentException("Bad objectclass: " + objectClass);
                }
                properties.put(EventConstants.SERVICE_OBJECTCLASS, objectClass);
                properties.put(EventConstants.SERVICE_PID, eventRef.getProperty(EventConstants.SERVICE_PID));
                eventAdmin.postEvent(new Event(topic, properties));
            } finally {
                m_context.ungetService(ref);
            }
        }
    }
}
Also used : EventAdmin(org.osgi.service.event.EventAdmin) Hashtable(java.util.Hashtable) Event(org.osgi.service.event.Event) ConfigurationEvent(org.osgi.service.cm.ConfigurationEvent) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin)

Example 44 with ConfigurationAdmin

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

the class ConfigurationDependencyAnnotationTest method testConfigurationDependencyWithAnotherExtraDynamicConfigurationAnnotation.

/**
 * Tests a Component two ConfigurationDependency (the second one is "instance bound"
 * and its pid is declared from the init method).
 */
@SuppressWarnings({ "serial", "rawtypes", "unchecked" })
public void testConfigurationDependencyWithAnotherExtraDynamicConfigurationAnnotation() throws Throwable {
    Ensure e = new Ensure();
    ServiceRegistration sr = register(e, ConfigurableComponentWithDynamicExtraConfiguration.ENSURE);
    ConfigurationAdmin cm = (ConfigurationAdmin) context.getService(context.getServiceReference(ConfigurationAdmin.class.getName()));
    try {
        org.osgi.service.cm.Configuration cf = cm.getConfiguration(ConfigurableComponentWithDynamicExtraConfiguration.class.getName(), null);
        cf.update(new Hashtable() {

            {
                put("foo", "bar");
                // Pid of the second Configuration Dependency.
                put("dynamicPid", "dynamicPid");
            }
        });
        org.osgi.service.cm.Configuration extraCf = cm.getConfiguration("dynamicPid", null);
        extraCf.update(new Hashtable() {

            {
                put("foo2", "bar2");
            }
        });
        e.waitForStep(4, MAXWAIT);
        cf.delete();
        extraCf.delete();
        e.waitForStep(5, MAXWAIT);
        e.ensure();
        sr.unregister();
    } catch (IOException err) {
        err.printStackTrace();
        Assert.fail("can't create factory configuration");
    }
}
Also used : ConfigurableComponentWithDynamicExtraConfiguration(org.apache.felix.dm.runtime.itest.components.ConfigurationDependencyAnnotation.ConfigurableComponentWithDynamicExtraConfiguration) Hashtable(java.util.Hashtable) IOException(java.io.IOException) Ensure(org.apache.felix.dm.itest.util.Ensure) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Example 45 with ConfigurationAdmin

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

the class ConfigurationDependencyAnnotationTest method testConfigurationDependencyAnnotation.

/**
 * Tests the ConfigurationDependency annotation.
 */
@SuppressWarnings({ "serial", "rawtypes", "unchecked" })
public void testConfigurationDependencyAnnotation() throws Throwable {
    Ensure e = new Ensure();
    ServiceRegistration sr = register(e, ConfigurableComponent.ENSURE);
    ConfigurationAdmin cm = (ConfigurationAdmin) context.getService(context.getServiceReference(ConfigurationAdmin.class.getName()));
    try {
        org.osgi.service.cm.Configuration cf = cm.getConfiguration(ConfigurableComponent.class.getName(), null);
        cf.update(new Hashtable() {

            {
                put("foo", "bar");
            }
        });
        e.waitForStep(1, MAXWAIT);
        cf.delete();
        e.waitForStep(3, MAXWAIT);
        e.ensure();
        sr.unregister();
    } catch (IOException err) {
        err.printStackTrace();
        Assert.fail("can't create factory configuration");
    }
}
Also used : Hashtable(java.util.Hashtable) ConfigurableComponent(org.apache.felix.dm.runtime.itest.components.ConfigurationDependencyAnnotation.ConfigurableComponent) IOException(java.io.IOException) Ensure(org.apache.felix.dm.itest.util.Ensure) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) ServiceRegistration(org.osgi.framework.ServiceRegistration)

Aggregations

ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)211 Configuration (org.osgi.service.cm.Configuration)114 Test (org.junit.Test)65 Hashtable (java.util.Hashtable)57 ServiceReference (org.osgi.framework.ServiceReference)52 IOException (java.io.IOException)44 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)25 BundleContext (org.osgi.framework.BundleContext)25 Subject (javax.security.auth.Subject)24 Dictionary (java.util.Dictionary)23 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)21 Bundle (org.osgi.framework.Bundle)16 Properties (java.util.Properties)15 File (java.io.File)8 HashSet (java.util.HashSet)7 BundleException (org.osgi.framework.BundleException)7 Method (java.lang.reflect.Method)6 ObjectName (javax.management.ObjectName)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4