Search in sources :

Example 41 with Dictionary

use of java.util.Dictionary in project karaf by apache.

the class DefaultConverter method convertToDictionary.

private Object convertToDictionary(Object obj, ReifiedType type) throws Exception {
    ReifiedType keyType = type.getActualTypeArgument(0);
    ReifiedType valueType = type.getActualTypeArgument(1);
    Dictionary newDic = new Hashtable();
    if (obj instanceof Dictionary) {
        Dictionary dic = (Dictionary) obj;
        for (Enumeration keyEnum = dic.keys(); keyEnum.hasMoreElements(); ) {
            Object key = keyEnum.nextElement();
            try {
                newDic.put(convert(key, keyType), convert(dic.get(key), valueType));
            } catch (Exception t) {
                throw new Exception("Unable to convert from " + obj + " to " + type + "(error converting map entry)", t);
            }
        }
    } else {
        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) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 42 with Dictionary

use of java.util.Dictionary in project jackrabbit-oak by apache.

the class Utils method loadAndTransformProps.

private static Properties loadAndTransformProps(String cfgPath) throws IOException {
    Dictionary dict = ConfigurationHandler.read(new FileInputStream(cfgPath));
    Properties props = new Properties();
    Enumeration keys = dict.keys();
    while (keys.hasMoreElements()) {
        String key = (String) keys.nextElement();
        props.put(key, dict.get(key));
    }
    return props;
}
Also used : Dictionary(java.util.Dictionary) Enumeration(java.util.Enumeration) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream)

Example 43 with Dictionary

use of java.util.Dictionary in project jackrabbit-oak by apache.

the class BlobStoreFixtureProvider method loadAndTransformProps.

private static Properties loadAndTransformProps(String cfgPath) throws IOException {
    Dictionary dict = ConfigurationHandler.read(new FileInputStream(cfgPath));
    Properties props = new Properties();
    Enumeration keys = dict.keys();
    while (keys.hasMoreElements()) {
        String key = (String) keys.nextElement();
        props.put(key, dict.get(key));
    }
    return props;
}
Also used : Dictionary(java.util.Dictionary) Enumeration(java.util.Enumeration) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream)

Example 44 with Dictionary

use of java.util.Dictionary in project sling by apache.

the class ITConfigFragments method testConfigProviderWithListener.

@Test
public void testConfigProviderWithListener() throws Exception {
    FileConfigProvider fcp = new FileConfigProvider();
    fcp.fileName = "test-reset-config-1.xml";
    bundleContext.registerService(ConfigProvider.class.getName(), fcp, null);
    delay();
    ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("foo.reset.1");
    assertTrue(logger.isDebugEnabled());
    assertNotNull(logger.getAppender("FOO-RESET-FILE-1"));
    fcp.fileName = "test-reset-config-2.xml";
    eventAdmin.sendEvent(new Event(RESET_EVENT_TOPIC, (Dictionary) null));
    delay();
    assertFalse(logger.isDebugEnabled());
    assertTrue(logger.isInfoEnabled());
    assertNotNull(logger.getAppender("FOO-RESET-FILE-2"));
}
Also used : Dictionary(java.util.Dictionary) ConfigProvider(org.apache.sling.commons.log.logback.ConfigProvider) Event(org.osgi.service.event.Event) Test(org.junit.Test)

Example 45 with Dictionary

use of java.util.Dictionary in project sling by apache.

the class ImplementsExtendsTest method setup.

@SuppressWarnings("unchecked")
@Before
public void setup() throws ClassNotFoundException, MalformedURLException {
    when(componentCtx.getBundleContext()).thenReturn(bundleContext);
    when(componentCtx.getProperties()).thenReturn(new Hashtable<String, Object>());
    when(bundleContext.registerService(anyString(), anyObject(), any(Dictionary.class))).then(new Answer<ServiceRegistration>() {

        @Override
        public ServiceRegistration answer(InvocationOnMock invocation) throws Throwable {
            final Dictionary<String, Object> props = (Dictionary<String, Object>) invocation.getArguments()[2];
            ServiceRegistration reg = mock(ServiceRegistration.class);
            ServiceReference ref = mock(ServiceReference.class);
            when(reg.getReference()).thenReturn(ref);
            when(ref.getProperty(anyString())).thenAnswer(new Answer<Object>() {

                @Override
                public Object answer(InvocationOnMock invocation) throws Throwable {
                    String key = (String) invocation.getArguments()[0];
                    return props.get(key);
                }
            });
            return reg;
        }
    });
    factory = new ModelAdapterFactory();
    factory.activate(componentCtx);
    factory.bindInjector(new ValueMapInjector(), new ServicePropertiesMap(2, 2));
    factory.bindImplementationPicker(firstImplementationPicker, firstImplementationPickerProps);
    // simulate bundle add for ModelPackageBundleListener
    Dictionary<String, String> headers = new Hashtable<String, String>();
    headers.put(ModelPackageBundleListener.PACKAGE_HEADER, "org.apache.sling.models.testmodels.classes.implextend");
    when(bundle.getHeaders()).thenReturn(headers);
    Vector<URL> classUrls = new Vector<URL>();
    classUrls.add(getClassUrl(ExtendsClassPropertyModel.class));
    classUrls.add(getClassUrl(ImplementsInterfacePropertyModel.class));
    classUrls.add(getClassUrl(ImplementsInterfacePropertyModel2.class));
    classUrls.add(getClassUrl(InvalidImplementsInterfacePropertyModel.class));
    classUrls.add(getClassUrl(InvalidSampleServiceInterface.class));
    classUrls.add(getClassUrl(SampleServiceInterface.class));
    classUrls.add(getClassUrl(SimplePropertyModel.class));
    when(bundle.findEntries(anyString(), anyString(), anyBoolean())).thenReturn(classUrls.elements());
    when(bundle.loadClass(anyString())).then(new Answer<Class<?>>() {

        @Override
        public Class<?> answer(InvocationOnMock invocation) throws ClassNotFoundException {
            String className = (String) invocation.getArguments()[0];
            return ImplementsExtendsTest.this.getClass().getClassLoader().loadClass(className);
        }
    });
    registeredAdapterFactories = (ServiceRegistration[]) factory.listener.addingBundle(bundle, bundleEvent);
}
Also used : Dictionary(java.util.Dictionary) SimplePropertyModel(org.apache.sling.models.testmodels.classes.implextend.SimplePropertyModel) ImplementsInterfacePropertyModel2(org.apache.sling.models.testmodels.classes.implextend.ImplementsInterfacePropertyModel2) Matchers.anyString(org.mockito.Matchers.anyString) URL(java.net.URL) InvalidImplementsInterfacePropertyModel(org.apache.sling.models.testmodels.classes.implextend.InvalidImplementsInterfacePropertyModel) Vector(java.util.Vector) ServiceRegistration(org.osgi.framework.ServiceRegistration) ValueMapInjector(org.apache.sling.models.impl.injectors.ValueMapInjector) Hashtable(java.util.Hashtable) SampleServiceInterface(org.apache.sling.models.testmodels.classes.implextend.SampleServiceInterface) InvalidSampleServiceInterface(org.apache.sling.models.testmodels.classes.implextend.InvalidSampleServiceInterface) ImplementsInterfacePropertyModel(org.apache.sling.models.testmodels.classes.implextend.ImplementsInterfacePropertyModel) InvalidImplementsInterfacePropertyModel(org.apache.sling.models.testmodels.classes.implextend.InvalidImplementsInterfacePropertyModel) ServiceReference(org.osgi.framework.ServiceReference) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ExtendsClassPropertyModel(org.apache.sling.models.testmodels.classes.implextend.ExtendsClassPropertyModel) Matchers.anyObject(org.mockito.Matchers.anyObject) InvalidSampleServiceInterface(org.apache.sling.models.testmodels.classes.implextend.InvalidSampleServiceInterface) Before(org.junit.Before)

Aggregations

Dictionary (java.util.Dictionary)198 Hashtable (java.util.Hashtable)91 Test (org.junit.Test)48 Configuration (org.osgi.service.cm.Configuration)33 BundleDescription (org.eclipse.osgi.service.resolver.BundleDescription)28 State (org.eclipse.osgi.service.resolver.State)28 Enumeration (java.util.Enumeration)27 Properties (java.util.Properties)27 BundleContext (org.osgi.framework.BundleContext)24 ArrayList (java.util.ArrayList)22 ServiceReference (org.osgi.framework.ServiceReference)21 HashMap (java.util.HashMap)20 ServiceRegistration (org.osgi.framework.ServiceRegistration)20 IOException (java.io.IOException)17 Map (java.util.Map)17 Bundle (org.osgi.framework.Bundle)16 LinkedHashMap (java.util.LinkedHashMap)13 List (java.util.List)13 Matchers.anyString (org.mockito.Matchers.anyString)11 MinionIdentity (org.opennms.minion.core.api.MinionIdentity)11