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