Search in sources :

Example 36 with Dictionary

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

the class InfoBundleTrackerCustomizer method addingBundle.

/**
     * {@inheritDoc}
     */
@SuppressWarnings("unchecked")
public ServiceRegistration<InfoProvider> addingBundle(Bundle bundle, BundleEvent event) {
    Dictionary headers = bundle.getHeaders();
    String headerEntry = (String) headers.get("Karaf-Info");
    InfoProvider provider = createInfo(headerEntry);
    if (provider == null) {
        if (logger.isDebugEnabled()) {
            logger.debug("Ignore incorrect info {} provided by bundle {}", headerEntry, bundle.getSymbolicName());
        }
        return null;
    }
    return bundle.getBundleContext().registerService(InfoProvider.class, provider, null);
}
Also used : Dictionary(java.util.Dictionary) InfoProvider(org.apache.karaf.shell.commands.info.InfoProvider)

Example 37 with Dictionary

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

the class EncryptablePropertyPlaceholderTest method getOsgiService.

protected <T> T getOsgiService(Class<T> type, String filter, long timeout) {
    ServiceTracker tracker = null;
    try {
        String flt;
        if (filter != null) {
            if (filter.startsWith("(")) {
                flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")" + filter + ")";
            } else {
                flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")(" + filter + "))";
            }
        } else {
            flt = "(" + Constants.OBJECTCLASS + "=" + type.getName() + ")";
        }
        Filter osgiFilter = FrameworkUtil.createFilter(flt);
        tracker = new ServiceTracker(bundleContext, osgiFilter, null);
        tracker.open(true);
        // Note that the tracker is not closed to keep the reference
        // This is buggy, as the service reference may change i think
        Object svc = type.cast(tracker.waitForService(timeout));
        if (svc == null) {
            Dictionary dic = bundleContext.getBundle().getHeaders();
            System.err.println("Test bundle headers: " + explode(dic));
            for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, null))) {
                System.err.println("ServiceReference: " + ref);
            }
            for (ServiceReference ref : asCollection(bundleContext.getAllServiceReferences(null, flt))) {
                System.err.println("Filtered ServiceReference: " + ref);
            }
            throw new RuntimeException("Gave up waiting for service " + flt);
        }
        return type.cast(svc);
    } catch (InvalidSyntaxException e) {
        throw new IllegalArgumentException("Invalid filter", e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}
Also used : Dictionary(java.util.Dictionary) ServiceTracker(org.osgi.util.tracker.ServiceTracker) Filter(org.osgi.framework.Filter) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference)

Example 38 with Dictionary

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

the class ProfileEdit method importPidFromLocalConfigAdmin.

/**
     * Imports the pid to the target Map.
     */
private void importPidFromLocalConfigAdmin(String pid, Map<String, Object> target) {
    try {
        Configuration[] configuration = configurationAdmin.listConfigurations("(service.pid=" + pid + ")");
        if (configuration != null && configuration.length > 0) {
            Dictionary dictionary = configuration[0].getProperties();
            Enumeration keyEnumeration = dictionary.keys();
            while (keyEnumeration.hasMoreElements()) {
                String key = String.valueOf(keyEnumeration.nextElement());
                //file.install.filename needs to be skipped as it specific to the current container.
                if (!key.equals(FILE_INSTALL_FILENAME_PROPERTY)) {
                    String value = String.valueOf(dictionary.get(key));
                    target.put(key, value);
                }
            }
        }
    } catch (Exception e) {
        LOGGER.warn("Error while importing configuration {} to profile.", pid);
    }
}
Also used : Dictionary(java.util.Dictionary) Enumeration(java.util.Enumeration) Configuration(org.osgi.service.cm.Configuration)

Example 39 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 40 with Dictionary

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

the class OsgiConfigLoginModule method login.

public boolean login() throws LoginException {
    try {
        String pid = (String) options.get(PID);
        Configuration config = ConfigAdminHolder.getService().getConfiguration(pid, null);
        Dictionary properties = config.getProperties();
        Callback[] callbacks = new Callback[2];
        callbacks[0] = new NameCallback("Username: ");
        callbacks[1] = new PasswordCallback("Password: ", false);
        try {
            callbackHandler.handle(callbacks);
        } catch (IOException ioe) {
            throw new LoginException(ioe.getMessage());
        } catch (UnsupportedCallbackException uce) {
            throw new LoginException(uce.getMessage() + " not available to obtain information from user");
        }
        String user = ((NameCallback) callbacks[0]).getName();
        String password = new String(((PasswordCallback) callbacks[1]).getPassword());
        String userInfos = (String) properties.get(USER_PREFIX + user);
        if (userInfos == null) {
            if (!this.detailedLoginExcepion) {
                throw new FailedLoginException("login failed");
            } else {
                throw new FailedLoginException("User does not exist");
            }
        }
        String[] infos = userInfos.split(",");
        String storedPassword = infos[0];
        // check the provided password
        if (!checkPassword(password, storedPassword)) {
            if (!this.detailedLoginExcepion) {
                throw new FailedLoginException("login failed");
            } else {
                throw new FailedLoginException("Password for " + user + " does not match");
            }
        }
        principals = new HashSet<>();
        principals.add(new UserPrincipal(user));
        for (int i = 1; i < infos.length; i++) {
            principals.add(new RolePrincipal(infos[i]));
        }
        return true;
    } catch (LoginException e) {
        throw e;
    } catch (Exception e) {
        throw (LoginException) new LoginException("Unable to authenticate user").initCause(e);
    } finally {
        callbackHandler = null;
        options = null;
    }
}
Also used : Dictionary(java.util.Dictionary) Configuration(org.osgi.service.cm.Configuration) IOException(java.io.IOException) UserPrincipal(org.apache.karaf.jaas.boot.principal.UserPrincipal) LoginException(javax.security.auth.login.LoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException) FailedLoginException(javax.security.auth.login.FailedLoginException) PasswordCallback(javax.security.auth.callback.PasswordCallback) NameCallback(javax.security.auth.callback.NameCallback) Callback(javax.security.auth.callback.Callback) NameCallback(javax.security.auth.callback.NameCallback) FailedLoginException(javax.security.auth.login.FailedLoginException) PasswordCallback(javax.security.auth.callback.PasswordCallback) LoginException(javax.security.auth.login.LoginException) FailedLoginException(javax.security.auth.login.FailedLoginException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) RolePrincipal(org.apache.karaf.jaas.boot.principal.RolePrincipal)

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