activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clebertsuco...@apache.org
Subject [1/2] activemq-artemis git commit: ARTEMIS-841 Hash Processors lock free lazy singleton instantiation
Date Mon, 07 Nov 2016 18:48:05 GMT
Repository: activemq-artemis
Updated Branches:
  refs/heads/master 6c664c1cb -> e83eb3829


ARTEMIS-841 Hash Processors lock free lazy singleton instantiation


Project: http://git-wip-us.apache.org/repos/asf/activemq-artemis/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq-artemis/commit/3281698f
Tree: http://git-wip-us.apache.org/repos/asf/activemq-artemis/tree/3281698f
Diff: http://git-wip-us.apache.org/repos/asf/activemq-artemis/diff/3281698f

Branch: refs/heads/master
Commit: 3281698f9f15660922a8f835e2e6593088f533bf
Parents: 6c664c1
Author: Francesco Nigro <nigro.fra@gmail.com>
Authored: Mon Nov 7 13:33:52 2016 +0100
Committer: Clebert Suconic <clebertsuconic@apache.org>
Committed: Mon Nov 7 13:47:22 2016 -0500

----------------------------------------------------------------------
 .../artemis/utils/PasswordMaskingUtil.java      | 90 +++++++++++---------
 1 file changed, 49 insertions(+), 41 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/3281698f/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/PasswordMaskingUtil.java
----------------------------------------------------------------------
diff --git a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/PasswordMaskingUtil.java
b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/PasswordMaskingUtil.java
index bee3861..6360fb2 100644
--- a/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/PasswordMaskingUtil.java
+++ b/artemis-commons/src/main/java/org/apache/activemq/artemis/utils/PasswordMaskingUtil.java
@@ -25,64 +25,73 @@ import org.apache.activemq.artemis.api.core.ActiveMQException;
 import org.apache.activemq.artemis.api.core.ActiveMQExceptionType;
 import org.apache.activemq.artemis.logs.ActiveMQUtilBundle;
 
-public class PasswordMaskingUtil {
+public final class PasswordMaskingUtil {
 
-   private static final String PLAINTEXT_PROCESSOR = "plaintext";
-   private static final String SECURE_PROCESSOR = "secure";
+   private PasswordMaskingUtil() {
 
-   private static final Map<String, HashProcessor> processors = new HashMap<>();
+   }
 
-   //stored password takes 2 forms, ENC() or plain text
-   public static HashProcessor getHashProcessor(String storedPassword) throws Exception {
+   private static final class LazyPlainTextProcessorHolder {
+
+      private LazyPlainTextProcessorHolder() {
 
-      if (!isEncoded(storedPassword)) {
-         return getPlaintextProcessor();
       }
-      return getSecureProcessor();
+
+      private static final HashProcessor INSTANCE = new NoHashProcessor();
+
    }
 
-   private static boolean isEncoded(String storedPassword) {
-      if (storedPassword == null) {
-         return true;
+   private static final class LazySecureProcessorHolder {
+
+      private LazySecureProcessorHolder() {
+
       }
 
-      if (storedPassword.startsWith("ENC(") && storedPassword.endsWith(")")) {
-         return true;
+      private static final HashProcessor INSTANCE;
+      private static final Exception EXCEPTION;
+
+      static {
+         HashProcessor processor = null;
+         Exception exception = null;
+         final String codecDesc = "org.apache.activemq.artemis.utils.DefaultSensitiveStringCodec;algorithm=one-way";
+         try {
+            final DefaultSensitiveStringCodec codec = (DefaultSensitiveStringCodec) PasswordMaskingUtil.getCodec(codecDesc);
+            processor = new SecureHashProcessor(codec);
+         } catch (Exception e) {
+            //THE STACK TRACE IS THE ORIGINAL ONE!
+            exception = e;
+         } finally {
+            EXCEPTION = exception;
+            INSTANCE = processor;
+         }
       }
-      return false;
    }
 
-   public static HashProcessor getHashProcessor() {
-      HashProcessor processor = null;
-      try {
-         processor = getSecureProcessor();
-      } catch (Exception e) {
-         processor = getPlaintextProcessor();
+   //stored password takes 2 forms, ENC() or plain text
+   public static HashProcessor getHashProcessor(String storedPassword) throws Exception {
+
+      if (!isEncoded(storedPassword)) {
+         return LazyPlainTextProcessorHolder.INSTANCE;
       }
-      return processor;
+      final Exception secureProcessorException = LazySecureProcessorHolder.EXCEPTION;
+      if (secureProcessorException != null) {
+         //reuse old descriptions/messages of the exception but refill the stack trace
+         throw new RuntimeException(secureProcessorException);
+      }
+      return LazySecureProcessorHolder.INSTANCE;
    }
 
-   public static HashProcessor getPlaintextProcessor() {
-      synchronized (processors) {
-         HashProcessor plain = processors.get(PLAINTEXT_PROCESSOR);
-         if (plain == null) {
-            plain = new NoHashProcessor();
-            processors.put(PLAINTEXT_PROCESSOR, plain);
-         }
-         return plain;
-      }
+   private static boolean isEncoded(String storedPassword) {
+      return storedPassword == null || (storedPassword.startsWith("ENC(") && storedPassword.endsWith(")"));
    }
 
-   public static HashProcessor getSecureProcessor() throws Exception {
-      synchronized (processors) {
-         HashProcessor processor = processors.get(SECURE_PROCESSOR);
-         if (processor == null) {
-            DefaultSensitiveStringCodec codec = (DefaultSensitiveStringCodec) getCodec("org.apache.activemq.artemis.utils.DefaultSensitiveStringCodec;algorithm=one-way");
-            processor = new SecureHashProcessor(codec);
-            processors.put(SECURE_PROCESSOR, processor);
-         }
-         return processor;
+   public static HashProcessor getHashProcessor() {
+      HashProcessor processor = LazySecureProcessorHolder.INSTANCE;
+      //it can be null due to a previous failed attempts to instantiate it!
+      if (processor == null) {
+         processor = LazyPlainTextProcessorHolder.INSTANCE;
       }
+      return processor;
    }
 
    /*
@@ -142,5 +151,4 @@ public class PasswordMaskingUtil {
       return new DefaultSensitiveStringCodec();
    }
 
-
 }


Mime
View raw message