Refactor EncryptionSupport to use tracker

Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/e380f888
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/e380f888
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/e380f888

Branch: refs/heads/master
Commit: e380f888a6c2d7f08513129e8c74a37c5d2afd4a
Parents: ab04a21
Author: Christian Schneider <ch...@die-schneider.net>
Authored: Tue Aug 22 13:58:52 2017 +0200
Committer: Christian Schneider <ch...@die-schneider.net>
Committed: Tue Aug 22 13:58:52 2017 +0200

----------------------------------------------------------------------
 .../modules/encryption/EncryptionSupport.java   | 162 ++++++++++---------
 1 file changed, 82 insertions(+), 80 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/e380f888/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/encryption/EncryptionSupport.java
----------------------------------------------------------------------
diff --git 
a/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/encryption/EncryptionSupport.java
 
b/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/encryption/EncryptionSupport.java
index ff2f12a..036fc9b 100644
--- 
a/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/encryption/EncryptionSupport.java
+++ 
b/jaas/modules/src/main/java/org/apache/karaf/jaas/modules/encryption/EncryptionSupport.java
@@ -17,115 +17,117 @@ package org.apache.karaf.jaas.modules.encryption;
 import org.apache.karaf.jaas.modules.Encryption;
 import org.apache.karaf.jaas.modules.EncryptionService;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.FrameworkUtil;
 import org.osgi.framework.InvalidSyntaxException;
 import org.osgi.framework.ServiceReference;
+import org.osgi.util.tracker.ServiceTracker;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.SortedMap;
 
 public class EncryptionSupport {
-
     private final Logger logger = 
LoggerFactory.getLogger(EncryptionSupport.class);
 
     private BundleContext bundleContext;
-
     private Encryption encryption;
     private String encryptionPrefix;
     private String encryptionSuffix;
-    protected Map<String, ?> options;
+    private boolean debug;
+    private boolean enabled;
+    private String name;
 
-    protected boolean debug;
+    private Map<String, String> encOpts;
 
 
     public EncryptionSupport(Map<String, ?> options) {
-        this.options = options;
         this.debug = Boolean.parseBoolean((String) options.get("debug"));
         // the bundle context is set in the Config JaasRealm by default
         this.bundleContext = (BundleContext) 
options.get(BundleContext.class.getName());
+        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");
+        enabled = Boolean.parseBoolean(encOpts.remove("enabled"));
+        if (!enabled) {
+            if (debug) {
+                logger.debug("Encryption is disabled.");
+            }
+        }
+        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);
+            }
+        }
     }
 
     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 (encryption != null) {
+            return encryption;
+        }
+        if (!enabled) {
+            return null;
+        }
 
-            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.");
+        ServiceTracker<EncryptionService, EncryptionService> tracker = new 
ServiceTracker<>(bundleContext, getFilter(), null);
+        tracker.open();
+        try {
+            return getEncryptionInternal(tracker);
+        } finally {
+            tracker.close();
+        }
+    }
+
+    private Encryption getEncryptionInternal(ServiceTracker<EncryptionService, 
EncryptionService> tracker) {
+        try {
+            tracker.waitForService(20000);
+        } catch (InterruptedException e1) {
+            return null;
+        }
+        SortedMap<ServiceReference<EncryptionService>, EncryptionService> 
tracked = tracker.getTracked();
+        if (tracked.isEmpty()) {
+            throw new IllegalStateException(noEncryptionServiceMsg());
+        }
+        for (EncryptionService encryptionService : tracked.values()) {
+            try {
+                Encryption encr = encryptionService.createEncryption(encOpts);
+                if (encr != null) {
+                    this.encryption = encr;
+                    return encryption;
                 }
+            } catch (IllegalStateException e) {
+                // continue
             }
         }
-        return encryption;
+        throw new IllegalStateException("No EncryptionService supporting the 
required options could be found.");
+    }
+
+    private String noEncryptionServiceMsg() {
+        if (name != null && name.length() > 0) {
+            return "Encryption service " + name + " not found. Please check 
that the encryption service is correctly set up.";
+        } else {
+            return "No encryption service found. Please install the Karaf 
encryption feature and check that the encryption algorithm is supported.";
+        }
+    }
+
+    private org.osgi.framework.Filter getFilter() {
+        String nameFilter = name != null && name.length() > 0 ? "(name=" + 
name + ")" : null;
+        String objFilter = "(objectClass=" + EncryptionService.class.getName() 
+ ")";
+        String filter = nameFilter == null ? objFilter : "&(" + nameFilter + 
objFilter + ")"; 
+        try {
+            return FrameworkUtil.createFilter(filter);
+        } catch (InvalidSyntaxException e) {
+            throw new IllegalStateException(e);
+        }
     }
 
     public String getEncryptionSuffix() {

Reply via email to