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());
}
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);
}
}
}
}
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);
}
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();
}
}
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();
}
}
Aggregations