use of org.apache.karaf.jaas.modules.EncryptionService in project karaf by apache.
the class EncryptionSupport method getEncryption.
public Encryption getEncryption() {
if (encryption == null) {
Map<String, String> encOpts = new HashMap<>();
for (String key : options.keySet()) {
if (key.startsWith("encryption.")) {
encOpts.put(key.substring("encryption.".length()), options.get(key).toString());
}
}
encryptionPrefix = encOpts.remove("prefix");
encryptionSuffix = encOpts.remove("suffix");
boolean enabled = Boolean.parseBoolean(encOpts.remove("enabled"));
if (!enabled) {
if (debug) {
logger.debug("Encryption is disabled.");
}
} else {
String name = encOpts.remove("name");
if (debug) {
if (name != null && name.length() > 0) {
logger.debug("Encryption is enabled. Using service " + name + " with options " + encOpts);
} else {
logger.debug("Encryption is enabled. Using options " + encOpts);
}
}
// lookup the encryption service reference
ServiceReference[] encryptionServiceReferences;
try {
encryptionServiceReferences = bundleContext.getServiceReferences(EncryptionService.class.getName(), name != null && name.length() > 0 ? "(name=" + name + ")" : null);
int timeout = 0;
while (encryptionServiceReferences == null || encryptionServiceReferences.length == 0) {
try {
Thread.sleep(500);
} catch (InterruptedException ie) {
// nothing to do
}
encryptionServiceReferences = bundleContext.getServiceReferences(EncryptionService.class.getName(), name != null && name.length() > 0 ? "(name=" + name + ")" : null);
timeout++;
if (timeout == 40)
break;
}
} catch (InvalidSyntaxException e) {
throw new IllegalStateException("The encryption service filter is not well formed.", e);
}
if (encryptionServiceReferences == null || encryptionServiceReferences.length == 0) {
if (name != null && name.length() > 0) {
throw new IllegalStateException("Encryption service " + name + " not found. Please check that the encryption service is correctly set up.");
} else {
throw new IllegalStateException("No encryption service found. Please install the Karaf encryption feature and check that the encryption algorithm is supported.");
}
}
Arrays.sort(encryptionServiceReferences);
for (ServiceReference ref : encryptionServiceReferences) {
try {
EncryptionService encryptionService = (EncryptionService) bundleContext.getService(ref);
if (encryptionService != null) {
try {
encryption = encryptionService.createEncryption(encOpts);
if (encryption != null) {
break;
}
} finally {
bundleContext.ungetService(ref);
}
}
} catch (IllegalStateException e) {
// continue
}
}
if (encryption == null) {
throw new IllegalStateException("No EncryptionService supporting the required options could be found.");
}
}
}
return encryption;
}
Aggregations