use of java.util.Dictionary in project aries by apache.
the class AggregateConverter method convert.
public Object convert(Object fromValue, final ReifiedType type) throws Exception {
// Discard null values
if (fromValue == null) {
return null;
}
// First convert service proxies
if (fromValue instanceof Convertible) {
return ((Convertible) fromValue).convert(type);
} else if (fromValue instanceof UnwrapperedBeanHolder) {
UnwrapperedBeanHolder holder = (UnwrapperedBeanHolder) fromValue;
if (isAssignable(holder.unwrapperedBean, type)) {
return BeanRecipe.wrap(holder, type.getRawClass());
} else {
fromValue = BeanRecipe.wrap(holder, Object.class);
}
} else if (isAssignable(fromValue, type)) {
// If the object is an instance of the type, just return it
return fromValue;
}
final Object finalFromValue = fromValue;
ConversionResult result = null;
AccessControlContext acc = blueprintContainer.getAccessControlContext();
if (acc == null) {
result = convertWithConverters(fromValue, type);
} else {
result = AccessController.doPrivileged(new PrivilegedExceptionAction<ConversionResult>() {
public ConversionResult run() throws Exception {
return convertWithConverters(finalFromValue, type);
}
}, acc);
}
if (result == null) {
if (fromValue instanceof Number && Number.class.isAssignableFrom(unwrap(toClass(type)))) {
return convertToNumber((Number) fromValue, toClass(type));
} else if (fromValue instanceof String) {
return convertFromString((String) fromValue, toClass(type), blueprintContainer);
} else if (toClass(type).isArray() && (fromValue instanceof Collection || fromValue.getClass().isArray())) {
return convertToArray(fromValue, type);
} else if (Map.class.isAssignableFrom(toClass(type)) && (fromValue instanceof Map || fromValue instanceof Dictionary)) {
return convertToMap(fromValue, type);
} else if (Dictionary.class.isAssignableFrom(toClass(type)) && (fromValue instanceof Map || fromValue instanceof Dictionary)) {
return convertToDictionary(fromValue, type);
} else if (Collection.class.isAssignableFrom(toClass(type)) && (fromValue instanceof Collection || fromValue.getClass().isArray())) {
return convertToCollection(fromValue, type);
} else {
throw new Exception("Unable to convert value " + fromValue + " to type " + type);
}
}
return result.value;
}
use of java.util.Dictionary in project aries by apache.
the class BlueprintContainerImpl method readDirectives.
private void readDirectives() {
Dictionary headers = bundle.getHeaders();
String symbolicName = (String) headers.get(Constants.BUNDLE_SYMBOLICNAME);
List<PathElement> paths = HeaderParser.parseHeader(symbolicName);
String timeoutDirective = paths.get(0).getDirective(BlueprintConstants.TIMEOUT_DIRECTIVE);
if (timeoutDirective != null) {
LOGGER.debug("Timeout directive: {}", timeoutDirective);
timeout = Integer.parseInt(timeoutDirective);
}
String graceperiod = paths.get(0).getDirective(BlueprintConstants.GRACE_PERIOD);
if (graceperiod != null) {
LOGGER.debug("Grace-period directive: {}", graceperiod);
waitForDependencies = Boolean.parseBoolean(graceperiod);
}
xmlValidation = paths.get(0).getDirective(BlueprintConstants.XML_VALIDATION);
// enabled if null or "true"; structure-only if "structure"; disabled otherwise
LOGGER.debug("Xml-validation directive: {}", xmlValidation);
}
use of java.util.Dictionary in project aries by apache.
the class AggregateConverter method convertToDictionary.
private Object convertToDictionary(Object obj, ReifiedType type) throws Exception {
ReifiedType keyType = type.getActualTypeArgument(0);
ReifiedType valueType = type.getActualTypeArgument(1);
if (obj instanceof Dictionary) {
Dictionary newDic = new Hashtable();
Dictionary dic = (Dictionary) obj;
boolean converted = false;
for (Enumeration keyEnum = dic.keys(); keyEnum.hasMoreElements(); ) {
Object key = keyEnum.nextElement();
try {
Object nk = convert(key, keyType);
Object ov = dic.get(key);
Object nv = convert(ov, valueType);
newDic.put(nk, nv);
converted |= nk != key || nv != ov;
} catch (Exception t) {
throw new Exception("Unable to convert from " + obj + " to " + type + "(error converting map entry)", t);
}
}
return converted ? newDic : obj;
} else {
Dictionary newDic = new Hashtable();
for (Map.Entry e : ((Map<Object, Object>) obj).entrySet()) {
try {
newDic.put(convert(e.getKey(), keyType), convert(e.getValue(), valueType));
} catch (Exception t) {
throw new Exception("Unable to convert from " + obj + " to " + type + "(error converting map entry)", t);
}
}
return newDic;
}
}
use of java.util.Dictionary in project aries by apache.
the class ComponentTest method testComponent.
@Test
public void testComponent() throws IOException {
OSGi<?> program = configurations("org.components.MyComponent").flatMap(props -> services(Service.class).flatMap(ms -> just(new Component(props, ms)).flatMap(component -> register(Component.class, component, new HashMap<>()).distribute(ign -> dynamic(highestService(ServiceOptional.class), component::setOptional, c -> component.setOptional(null)), ign -> dynamic(services(ServiceForList.class), component::addService, component::removeService)))));
ServiceTracker<Component, Component> serviceTracker = new ServiceTracker<>(_bundleContext, Component.class, null);
serviceTracker.open();
CountDownLatch countDownLatch = new CountDownLatch(1);
_bundleContext.registerService(ManagedService.class, dictionary -> countDownLatch.countDown(), new Hashtable<String, Object>() {
{
put("service.pid", "org.components.MyComponent");
}
});
Configuration factoryConfiguration = null;
try (OSGiResult<?> run = program.run(_bundleContext)) {
factoryConfiguration = _configurationAdmin.createFactoryConfiguration("org.components.MyComponent");
factoryConfiguration.update(new Hashtable<>());
countDownLatch.await(10, TimeUnit.SECONDS);
assertNull(serviceTracker.getService());
ServiceRegistration<Service> serviceRegistration = _bundleContext.registerService(Service.class, new Service(), new Hashtable<>());
Component component = serviceTracker.waitForService(10 * 1000);
assertNotNull(component);
assertNull(component.getOptional());
ServiceRegistration<ServiceOptional> serviceRegistration2 = _bundleContext.registerService(ServiceOptional.class, new ServiceOptional(), new Hashtable<>());
Thread.sleep(1000L);
assertNotNull(component.getOptional());
ServiceOptional serviceOptional = new ServiceOptional();
ServiceRegistration<ServiceOptional> serviceRegistration3 = _bundleContext.registerService(ServiceOptional.class, serviceOptional, new Hashtable<String, Object>() {
{
put("service.ranking", 1);
}
});
assertEquals(serviceOptional, component.getOptional());
serviceRegistration3.unregister();
assertNotNull(component.getOptional());
serviceRegistration2.unregister();
assertNull(component.getOptional());
ServiceRegistration<ServiceForList> serviceRegistration4 = _bundleContext.registerService(ServiceForList.class, new ServiceForList(), new Hashtable<>());
ServiceRegistration<ServiceForList> serviceRegistration5 = _bundleContext.registerService(ServiceForList.class, new ServiceForList(), new Hashtable<>());
assertEquals(2, component.getServiceForLists().size());
serviceRegistration4.unregister();
assertEquals(1, component.getServiceForLists().size());
serviceRegistration5.unregister();
assertEquals(0, component.getServiceForLists().size());
serviceRegistration.unregister();
assertNull(serviceTracker.getService());
} catch (IOException ioe) {
} catch (InterruptedException e) {
Assert.fail("Timeout waiting for configuration");
} finally {
serviceTracker.close();
if (factoryConfiguration != null) {
factoryConfiguration.delete();
}
}
}
use of java.util.Dictionary in project aries by apache.
the class DSLTest method testConfiguration.
@Test
public void testConfiguration() throws IOException, InterruptedException {
ServiceReference<ConfigurationAdmin> serviceReference = bundleContext.getServiceReference(ConfigurationAdmin.class);
ConfigurationAdmin configurationAdmin = bundleContext.getService(serviceReference);
AtomicReference<Dictionary<?, ?>> atomicReference = new AtomicReference<>(null);
Configuration configuration = null;
CountDownLatch countDownLatch = new CountDownLatch(1);
try (OSGiResult<Dictionary<String, ?>> result = configuration("test.configuration").run(bundleContext, x -> {
atomicReference.set(x);
countDownLatch.countDown();
})) {
assertNull(atomicReference.get());
configuration = configurationAdmin.getConfiguration("test.configuration");
configuration.update(new Hashtable<>());
countDownLatch.await(10, TimeUnit.SECONDS);
assertNotNull(atomicReference.get());
} finally {
bundleContext.ungetService(serviceReference);
if (configuration != null) {
configuration.delete();
}
}
}
Aggregations