Search in sources :

Example 1 with ConfigurationEvent

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

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

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

the class RegionConfigurationSupport method start.

public void start() {
    // register as listener for configurations
    Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put(Constants.SERVICE_DESCRIPTION, "Declarative Services Configuration Support Listener");
    props.put(Constants.SERVICE_VENDOR, "The Apache Software Foundation");
    // If RegionConfigurationSupport *directly* implements ConfigurationListener then we get NoClassDefFoundError
    // when SCR is started without a wiring to an exporter of Config Admin API. This construction allows the
    // class loading exception to be caught and confined.
    ConfigurationListener serviceDelegator = new ConfigurationListener() {

        @Override
        public void configurationEvent(ConfigurationEvent event) {
            RegionConfigurationSupport.this.configurationEvent(event);
        }
    };
    this.m_registration = caBundleContext.registerService(ConfigurationListener.class, serviceDelegator, props);
}
Also used : ConfigurationListener(org.osgi.service.cm.ConfigurationListener) ConfigurationEvent(org.osgi.service.cm.ConfigurationEvent) Hashtable(java.util.Hashtable)

Example 4 with ConfigurationEvent

use of org.osgi.service.cm.ConfigurationEvent in project fabric8 by jboss-fuse.

the class OsgiUtils method deleteCmConfigurationAndWait.

/**
 * Deletes CM configuration and waits for CM_DELETE event to be send
 * @param bundleContext
 * @param config
 * @param timeout
 * @param unit
 * @return
 */
public static boolean deleteCmConfigurationAndWait(BundleContext bundleContext, final Configuration config, final String pid, long timeout, TimeUnit unit) throws IOException, InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final ServiceRegistration<ConfigurationListener> registration = bundleContext.registerService(ConfigurationListener.class, new ConfigurationListener() {

        @Override
        public void configurationEvent(ConfigurationEvent event) {
            if (event.getType() == ConfigurationEvent.CM_DELETED && event.getPid() != null && event.getPid().equals(pid)) {
                latch.countDown();
            }
        }
    }, null);
    config.delete();
    try {
        return latch.await(timeout, unit);
    } finally {
        registration.unregister();
    }
}
Also used : ConfigurationListener(org.osgi.service.cm.ConfigurationListener) ConfigurationEvent(org.osgi.service.cm.ConfigurationEvent) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 5 with ConfigurationEvent

use of org.osgi.service.cm.ConfigurationEvent in project fabric8 by jboss-fuse.

the class OsgiUtils method updateCmConfigurationAndWait.

/**
 * Updates CM configuration and waits for CM_UPDATE event to be send
 * @param bundleContext
 * @param config
 * @param properties
 * @param timeout
 * @param unit
 * @return
 */
public static boolean updateCmConfigurationAndWait(BundleContext bundleContext, final Configuration config, Dictionary<String, Object> properties, long timeout, TimeUnit unit) throws IOException, InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final ServiceRegistration<ConfigurationListener> registration = bundleContext.registerService(ConfigurationListener.class, new ConfigurationListener() {

        @Override
        public void configurationEvent(ConfigurationEvent event) {
            if (event.getType() == ConfigurationEvent.CM_UPDATED && event.getPid() != null && event.getPid().equals(config.getPid())) {
                latch.countDown();
            }
        }
    }, null);
    config.update(properties);
    try {
        return latch.await(timeout, unit);
    } finally {
        registration.unregister();
    }
}
Also used : ConfigurationListener(org.osgi.service.cm.ConfigurationListener) ConfigurationEvent(org.osgi.service.cm.ConfigurationEvent) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

ConfigurationEvent (org.osgi.service.cm.ConfigurationEvent)16 ConfigurationListener (org.osgi.service.cm.ConfigurationListener)8 Test (org.junit.Test)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 Hashtable (java.util.Hashtable)4 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)4 FeatureDTO (com.amitinside.featureflags.dto.FeatureDTO)3 Dictionary (java.util.Dictionary)3 Capture (org.easymock.Capture)3 Bundle (org.osgi.framework.Bundle)3 BundleEvent (org.osgi.framework.BundleEvent)3 File (java.io.File)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 Configuration (org.osgi.service.cm.Configuration)2 FabricException (io.fabric8.api.FabricException)1 RuntimeProperties (io.fabric8.api.RuntimeProperties)1 BootstrapConfiguration (io.fabric8.zookeeper.bootstrap.BootstrapConfiguration)1 IOException (java.io.IOException)1 Field (java.lang.reflect.Field)1