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