From commits-return-19034-apmail-activemq-commits-archive=activemq.apache.org@activemq.apache.org Mon Jun 11 09:25:15 2012 Return-Path: X-Original-To: apmail-activemq-commits-archive@www.apache.org Delivered-To: apmail-activemq-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id C3C6F9F23 for ; Mon, 11 Jun 2012 09:25:15 +0000 (UTC) Received: (qmail 42840 invoked by uid 500); 11 Jun 2012 09:25:15 -0000 Delivered-To: apmail-activemq-commits-archive@activemq.apache.org Received: (qmail 42748 invoked by uid 500); 11 Jun 2012 09:25:14 -0000 Mailing-List: contact commits-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@activemq.apache.org Delivered-To: mailing list commits@activemq.apache.org Received: (qmail 42726 invoked by uid 99); 11 Jun 2012 09:25:13 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 11 Jun 2012 09:25:13 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 11 Jun 2012 09:25:12 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 4E0EC2388980 for ; Mon, 11 Jun 2012 09:24:52 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1348761 - in /activemq/trunk/activemq-core/src: main/java/org/apache/activemq/security/CachedLDAPAuthorizationMap.java test/java/org/apache/activemq/security/AbstractCachedLDAPAuthorizationMapLegacyTest.java Date: Mon, 11 Jun 2012 09:24:52 -0000 To: commits@activemq.apache.org From: dejanb@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120611092452.4E0EC2388980@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: dejanb Date: Mon Jun 11 09:24:51 2012 New Revision: 1348761 URL: http://svn.apache.org/viewvc?rev=1348761&view=rev Log: https://issues.apache.org/jira/browse/AMQ-3845 - handle LDAP restarts in cached ldap authorization map Modified: activemq/trunk/activemq-core/src/main/java/org/apache/activemq/security/CachedLDAPAuthorizationMap.java activemq/trunk/activemq-core/src/test/java/org/apache/activemq/security/AbstractCachedLDAPAuthorizationMapLegacyTest.java Modified: activemq/trunk/activemq-core/src/main/java/org/apache/activemq/security/CachedLDAPAuthorizationMap.java URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/main/java/org/apache/activemq/security/CachedLDAPAuthorizationMap.java?rev=1348761&r1=1348760&r2=1348761&view=diff ============================================================================== --- activemq/trunk/activemq-core/src/main/java/org/apache/activemq/security/CachedLDAPAuthorizationMap.java (original) +++ activemq/trunk/activemq-core/src/main/java/org/apache/activemq/security/CachedLDAPAuthorizationMap.java Mon Jun 11 09:24:51 2012 @@ -94,6 +94,22 @@ public class CachedLDAPAuthorizationMap protected HashMap entries = new HashMap(); + protected DirContext createContext() throws NamingException { + Hashtable env = new Hashtable(); + env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory); + if (connectionUsername != null || !"".equals(connectionUsername)) { + env.put(Context.SECURITY_PRINCIPAL, connectionUsername); + } + if (connectionPassword != null || !"".equals(connectionPassword)) { + env.put(Context.SECURITY_CREDENTIALS, connectionPassword); + } + env.put(Context.SECURITY_PROTOCOL, connectionProtocol); + env.put(Context.PROVIDER_URL, connectionURL); + env.put(Context.SECURITY_AUTHENTICATION, authentication); + //env.put("com.sun.jndi.ldap.connect.pool", "true"); + return new InitialDirContext(env); + } + /** * Returns the existing open context or creates a new one and registers listeners for * push notifications if such an update style is enabled. This implementation should not @@ -105,23 +121,20 @@ public class CachedLDAPAuthorizationMap */ protected DirContext open() throws NamingException { if (context != null) { - return context; - } - - try { - Hashtable env = new Hashtable(); - env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory); - if (connectionUsername != null || !"".equals(connectionUsername)) { - env.put(Context.SECURITY_PRINCIPAL, connectionUsername); + boolean alive = true; + try { + context.getAttributes(""); + } catch (Exception e) { + LOG.info("LDAP connection failed", e); + alive = false; } - if (connectionPassword != null || !"".equals(connectionPassword)) { - env.put(Context.SECURITY_CREDENTIALS, connectionPassword); + if (alive) { + return context; } - env.put(Context.SECURITY_PROTOCOL, connectionProtocol); - env.put(Context.PROVIDER_URL, connectionURL); - env.put(Context.SECURITY_AUTHENTICATION, authentication); - context = new InitialDirContext(env); + } + try { + context = createContext(); if (refreshInterval == -1 && !refreshDisabled) { eventContext = ((EventDirContext)context.lookup("")); @@ -158,6 +171,7 @@ public class CachedLDAPAuthorizationMap eventContext.addNamingListener(tempSearchBase, getFilterForPermissionType(permissionType), constraints, this.new CachedLDAPAuthorizationMapNamespaceChangeListener(DestinationType.TEMP, permissionType)); } + } } catch (NamingException e) { context = null; @@ -616,8 +630,15 @@ public class CachedLDAPAuthorizationMap * and are the refresh interval has elapsed. */ protected void checkForUpdates() { - if (!refreshDisabled && (refreshInterval != -1 && System.currentTimeMillis() >= lastUpdated + refreshInterval)) { - + if (context == null || (!refreshDisabled && (refreshInterval != -1 && System.currentTimeMillis() >= lastUpdated + refreshInterval))) { + if (context == null) { + try { + context = createContext(); + } catch (NamingException ne) { + // LDAP is down, use already cached values + return; + } + } reset(); setTempDestinationAuthorizationEntry(null); entries.clear(); @@ -688,6 +709,7 @@ public class CachedLDAPAuthorizationMap * {@link AuthorizationEntry} is not setup for concurrent access. */ public synchronized Set getReadACLs(ActiveMQDestination destination) { + checkForUpdates(); return super.getReadACLs(destination); } @@ -696,6 +718,7 @@ public class CachedLDAPAuthorizationMap * {@link AuthorizationEntry} is not setup for concurrent access. */ public synchronized Set getWriteACLs(ActiveMQDestination destination) { + checkForUpdates(); return super.getWriteACLs(destination); } @@ -850,6 +873,7 @@ public class CachedLDAPAuthorizationMap * @param namingExceptionEvent the exception event */ public void namingExceptionThrown(NamingExceptionEvent namingExceptionEvent) { + context = null; LOG.error("Caught unexpected exception.", namingExceptionEvent.getException()); } Modified: activemq/trunk/activemq-core/src/test/java/org/apache/activemq/security/AbstractCachedLDAPAuthorizationMapLegacyTest.java URL: http://svn.apache.org/viewvc/activemq/trunk/activemq-core/src/test/java/org/apache/activemq/security/AbstractCachedLDAPAuthorizationMapLegacyTest.java?rev=1348761&r1=1348760&r2=1348761&view=diff ============================================================================== --- activemq/trunk/activemq-core/src/test/java/org/apache/activemq/security/AbstractCachedLDAPAuthorizationMapLegacyTest.java (original) +++ activemq/trunk/activemq-core/src/test/java/org/apache/activemq/security/AbstractCachedLDAPAuthorizationMapLegacyTest.java Mon Jun 11 09:24:51 2012 @@ -274,6 +274,53 @@ public abstract class AbstractCachedLDAP failedACLs = map.getReadACLs(new ActiveMQQueue("TEST.FOO")); assertEquals("set size: " + failedACLs, 1, failedACLs.size()); } + + @Test + public void testRestartAsync() throws Exception { + testRestart(false); + } + + @Test + public void testRestartSync() throws Exception { + testRestart(true); + } + + public void testRestart(boolean sync) throws Exception { + map.query(); + if (sync) { + map.setRefreshInterval(1); + } + + Set failedACLs = map.getReadACLs(new ActiveMQQueue("FAILED")); + assertEquals("set size: " + failedACLs, 0, failedACLs.size()); + + failedACLs = map.getReadACLs(new ActiveMQQueue("TEST.FOO")); + assertEquals("set size: " + failedACLs, 2, failedACLs.size()); + + getLdapServer().stop(); + + Thread.sleep(1000); + + failedACLs = map.getReadACLs(new ActiveMQQueue("TEST.FOO")); + assertEquals("set size: " + failedACLs, 2, failedACLs.size()); + + getLdapServer().start(); + + Thread.sleep(1000); + + connection = getLdapConnection(); + + LdifReader reader = new LdifReader(getAddLdif()); + + for (LdifEntry entry : reader) { + connection.add(entry.getEntry()); + } + + Thread.sleep(2000); + + failedACLs = map.getReadACLs(new ActiveMQQueue("FAILED")); + assertEquals("set size: " + failedACLs, 2, failedACLs.size()); + } protected CachedLDAPAuthorizationMap createMap() { return new CachedLDAPAuthorizationMap();