Search in sources :

Example 26 with Dictionary

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;
}
Also used : Dictionary(java.util.Dictionary) AccessControlContext(java.security.AccessControlContext) Collection(java.util.Collection) PrivilegedExceptionAction(java.security.PrivilegedExceptionAction) HashMap(java.util.HashMap) Map(java.util.Map) UnwrapperedBeanHolder(org.apache.aries.blueprint.container.BeanRecipe.UnwrapperedBeanHolder)

Example 27 with Dictionary

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);
}
Also used : Dictionary(java.util.Dictionary) PathElement(org.apache.aries.blueprint.utils.HeaderParser.PathElement)

Example 28 with Dictionary

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;
    }
}
Also used : Dictionary(java.util.Dictionary) Enumeration(java.util.Enumeration) Hashtable(java.util.Hashtable) ReifiedType(org.osgi.service.blueprint.container.ReifiedType) HashMap(java.util.HashMap) Map(java.util.Map)

Example 29 with Dictionary

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();
        }
    }
}
Also used : OSGi.configurations(org.apache.aries.osgi.functional.OSGi.configurations) BeforeClass(org.junit.BeforeClass) TimeoutException(java.util.concurrent.TimeoutException) HashMap(java.util.HashMap) OSGi.onClose(org.apache.aries.osgi.functional.OSGi.onClose) ArrayList(java.util.ArrayList) OSGi.register(org.apache.aries.osgi.functional.OSGi.register) HighestRankingRouter.highest(org.apache.aries.osgi.functional.test.HighestRankingRouter.highest) Configuration(org.osgi.service.cm.Configuration) OSGi.apply(org.apache.aries.osgi.functional.OSGi.apply) ServiceReference(org.osgi.framework.ServiceReference) Hashtable(java.util.Hashtable) ServiceRegistration(org.osgi.framework.ServiceRegistration) AfterClass(org.junit.AfterClass) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) IOException(java.io.IOException) BundleContext(org.osgi.framework.BundleContext) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) OSGi.just(org.apache.aries.osgi.functional.OSGi.just) Assert.assertNull(org.junit.Assert.assertNull) OSGi.services(org.apache.aries.osgi.functional.OSGi.services) ServiceTracker(org.osgi.util.tracker.ServiceTracker) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) OSGi(org.apache.aries.osgi.functional.OSGi) ManagedService(org.osgi.service.cm.ManagedService) Assert(org.junit.Assert) OSGi.bundleContext(org.apache.aries.osgi.functional.OSGi.bundleContext) FrameworkUtil(org.osgi.framework.FrameworkUtil) OSGiResult(org.apache.aries.osgi.functional.OSGiResult) Dictionary(java.util.Dictionary) Assert.assertEquals(org.junit.Assert.assertEquals) Configuration(org.osgi.service.cm.Configuration) ServiceTracker(org.osgi.util.tracker.ServiceTracker) ManagedService(org.osgi.service.cm.ManagedService) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 30 with Dictionary

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();
        }
    }
}
Also used : Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ConfigurationAdmin(org.osgi.service.cm.ConfigurationAdmin) Test(org.junit.Test)

Aggregations

Dictionary (java.util.Dictionary)161 Hashtable (java.util.Hashtable)61 Test (org.junit.Test)48 Configuration (org.osgi.service.cm.Configuration)33 Enumeration (java.util.Enumeration)27 Properties (java.util.Properties)24 BundleContext (org.osgi.framework.BundleContext)21 ServiceReference (org.osgi.framework.ServiceReference)21 HashMap (java.util.HashMap)19 ServiceRegistration (org.osgi.framework.ServiceRegistration)19 Map (java.util.Map)17 ArrayList (java.util.ArrayList)15 Bundle (org.osgi.framework.Bundle)15 IOException (java.io.IOException)13 LinkedHashMap (java.util.LinkedHashMap)12 Matchers.anyString (org.mockito.Matchers.anyString)11 ConfigurationAdmin (org.osgi.service.cm.ConfigurationAdmin)10 ComponentContext (org.osgi.service.component.ComponentContext)10 TreeMap (java.util.TreeMap)9 MinionIdentity (org.opennms.minion.core.api.MinionIdentity)9