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-626 re-use context for LDAP
Date Tue, 12 Jul 2016 14:01:22 GMT
Repository: activemq-artemis
Updated Branches:
  refs/heads/master 1d886730b -> 08ab1f708


ARTEMIS-626 re-use context for LDAP


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

Branch: refs/heads/master
Commit: f3a8619d7eeabded75f3725f2e77af267e8cb450
Parents: 1d88673
Author: jbertram <jbertram@apache.org>
Authored: Mon Jul 11 13:07:35 2016 -0500
Committer: jbertram <jbertram@apache.org>
Committed: Mon Jul 11 13:08:34 2016 -0500

----------------------------------------------------------------------
 .../spi/core/security/jaas/LDAPLoginModule.java | 74 +++++++++++---------
 1 file changed, 39 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq-artemis/blob/f3a8619d/artemis-server/src/main/java/org/apache/activemq/artemis/spi/core/security/jaas/LDAPLoginModule.java
----------------------------------------------------------------------
diff --git a/artemis-server/src/main/java/org/apache/activemq/artemis/spi/core/security/jaas/LDAPLoginModule.java
b/artemis-server/src/main/java/org/apache/activemq/artemis/spi/core/security/jaas/LDAPLoginModule.java
index edcf38e..8bb5405 100644
--- a/artemis-server/src/main/java/org/apache/activemq/artemis/spi/core/security/jaas/LDAPLoginModule.java
+++ b/artemis-server/src/main/java/org/apache/activemq/artemis/spi/core/security/jaas/LDAPLoginModule.java
@@ -145,12 +145,15 @@ public class LDAPLoginModule implements LoginModule {
       return true;
    }
 
-   protected void close(DirContext context) {
-      try {
-         context.close();
-      }
-      catch (Exception e) {
-         ActiveMQServerLogger.LOGGER.error(e.toString());
+   protected void closeContext() {
+      if (context != null) {
+         try {
+            context.close();
+            context = null;
+         }
+         catch (Exception e) {
+            ActiveMQServerLogger.LOGGER.error(e.toString());
+         }
       }
    }
 
@@ -159,13 +162,11 @@ public class LDAPLoginModule implements LoginModule {
       MessageFormat userSearchMatchingFormat;
       boolean userSearchSubtreeBool;
 
-      DirContext context = null;
-
       if (logger.isDebugEnabled()) {
          logger.debug("Create the LDAP initial context.");
       }
       try {
-         context = open();
+         openContext();
       }
       catch (NamingException ne) {
          FailedLoginException ex = new FailedLoginException("Error opening LDAP connection");
@@ -246,7 +247,7 @@ public class LDAPLoginModule implements LoginModule {
                }
             }
             catch (URISyntaxException e) {
-               close(context);
+               closeContext();
                FailedLoginException ex = new FailedLoginException("Error parsing absolute
name as URI.");
                ex.initCause(e);
                throw ex;
@@ -282,12 +283,13 @@ public class LDAPLoginModule implements LoginModule {
          }
       }
       catch (CommunicationException e) {
+         closeContext();
          FailedLoginException ex = new FailedLoginException("Error contacting LDAP");
          ex.initCause(e);
          throw ex;
       }
       catch (NamingException e) {
-         close(context);
+         closeContext();
          FailedLoginException ex = new FailedLoginException("Error contacting LDAP");
          ex.initCause(e);
          throw ex;
@@ -453,34 +455,36 @@ public class LDAPLoginModule implements LoginModule {
       return values;
    }
 
-   protected DirContext open() throws NamingException {
-      try {
-         Hashtable<String, String> env = new Hashtable<>();
-         env.put(Context.INITIAL_CONTEXT_FACTORY, getLDAPPropertyValue(INITIAL_CONTEXT_FACTORY));
-         if (isLoginPropertySet(CONNECTION_USERNAME)) {
-            env.put(Context.SECURITY_PRINCIPAL, getLDAPPropertyValue(CONNECTION_USERNAME));
-         }
-         else {
-            throw new NamingException("Empty username is not allowed");
-         }
+   protected void openContext() throws NamingException {
+      if (context == null) {
+         try {
+            Hashtable<String, String> env = new Hashtable<>();
+            env.put(Context.INITIAL_CONTEXT_FACTORY, getLDAPPropertyValue(INITIAL_CONTEXT_FACTORY));
+            if (isLoginPropertySet(CONNECTION_USERNAME)) {
+               env.put(Context.SECURITY_PRINCIPAL, getLDAPPropertyValue(CONNECTION_USERNAME));
+            }
+            else {
+               throw new NamingException("Empty username is not allowed");
+            }
+
+            if (isLoginPropertySet(CONNECTION_PASSWORD)) {
+               env.put(Context.SECURITY_CREDENTIALS, getLDAPPropertyValue(CONNECTION_PASSWORD));
+            }
+            else {
+               throw new NamingException("Empty password is not allowed");
+            }
+            env.put(Context.SECURITY_PROTOCOL, getLDAPPropertyValue(CONNECTION_PROTOCOL));
+            env.put(Context.PROVIDER_URL, getLDAPPropertyValue(CONNECTION_URL));
+            env.put(Context.SECURITY_AUTHENTICATION, getLDAPPropertyValue(AUTHENTICATION));
+            context = new InitialDirContext(env);
 
-         if (isLoginPropertySet(CONNECTION_PASSWORD)) {
-            env.put(Context.SECURITY_CREDENTIALS, getLDAPPropertyValue(CONNECTION_PASSWORD));
          }
-         else {
-            throw new NamingException("Empty password is not allowed");
+         catch (NamingException e) {
+            closeContext();
+            ActiveMQServerLogger.LOGGER.error(e.toString());
+            throw e;
          }
-         env.put(Context.SECURITY_PROTOCOL, getLDAPPropertyValue(CONNECTION_PROTOCOL));
-         env.put(Context.PROVIDER_URL, getLDAPPropertyValue(CONNECTION_URL));
-         env.put(Context.SECURITY_AUTHENTICATION, getLDAPPropertyValue(AUTHENTICATION));
-         context = new InitialDirContext(env);
-
-      }
-      catch (NamingException e) {
-         ActiveMQServerLogger.LOGGER.error(e.toString());
-         throw e;
       }
-      return context;
    }
 
    private String getLDAPPropertyValue(String propertyName) {


Mime
View raw message