From d4be31c15945fcbc97383e2e315101f86148f7f1 Mon Sep 17 00:00:00 2001 From: Ulrik Sandberg Date: Sun, 18 Jan 2009 21:56:52 +0000 Subject: [PATCH] LDAP-153: Added a pooling context source that creates mutable ldap contexts. --- .../pool/MutableDelegatingLdapContext.java | 61 +++++++++++++++++++ .../factory/PoolingMutableContextSource.java | 49 +++++++++++++++ .../MutableDelegatingLdapContextTest.java | 35 +++++++++++ .../PoolingMutableContextSourceTest.java | 46 ++++++++++++++ 4 files changed, 191 insertions(+) create mode 100644 core/src/main/java/org/springframework/ldap/pool/MutableDelegatingLdapContext.java create mode 100644 core/src/main/java/org/springframework/ldap/pool/factory/PoolingMutableContextSource.java create mode 100644 core/src/test/java/org/springframework/ldap/pool/MutableDelegatingLdapContextTest.java create mode 100644 core/src/test/java/org/springframework/ldap/pool/factory/PoolingMutableContextSourceTest.java diff --git a/core/src/main/java/org/springframework/ldap/pool/MutableDelegatingLdapContext.java b/core/src/main/java/org/springframework/ldap/pool/MutableDelegatingLdapContext.java new file mode 100644 index 00000000..b8020599 --- /dev/null +++ b/core/src/main/java/org/springframework/ldap/pool/MutableDelegatingLdapContext.java @@ -0,0 +1,61 @@ +/* + * Copyright 2005-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ldap.pool; + +import javax.naming.NamingException; +import javax.naming.ldap.Control; +import javax.naming.ldap.LdapContext; + +import org.apache.commons.pool.KeyedObjectPool; +import org.springframework.ldap.pool.factory.PoolingMutableContextSource; + +/** + * Used by {@link PoolingMutableContextSource} to wrap a {@link LdapContext}, + * delegating most methods to the underlying context. This class extends + * {@link DelegatingLdapContext}, allowing request controls to be set on the + * wrapped ldap context. This enables the Spring LDAP pooling to be used for + * scenarios such as paged results. + * + * @author Ulrik Sandberg + */ +public class MutableDelegatingLdapContext extends DelegatingLdapContext { + + /** + * Create a new mutable delegating ldap context for the specified pool, + * context and context type. + * + * @param keyedObjectPool The pool the delegate context was checked out + * from. + * @param delegateLdapContext The ldap context to delegate operations to. + * @param dirContextType The type of context, used as a key for the pool. + * @throws IllegalArgumentException if any of the arguments are null + */ + public MutableDelegatingLdapContext(KeyedObjectPool keyedObjectPool, LdapContext delegateLdapContext, + DirContextType dirContextType) { + super(keyedObjectPool, delegateLdapContext, dirContextType); + } + + /* + * @see + * org.springframework.ldap.pool.DelegatingLdapContext#setRequestControls + * (javax.naming.ldap.Control[]) + */ + public void setRequestControls(Control[] requestControls) throws NamingException { + assertOpen(); + getDelegateLdapContext().setRequestControls(requestControls); + } +} diff --git a/core/src/main/java/org/springframework/ldap/pool/factory/PoolingMutableContextSource.java b/core/src/main/java/org/springframework/ldap/pool/factory/PoolingMutableContextSource.java new file mode 100644 index 00000000..3b75a965 --- /dev/null +++ b/core/src/main/java/org/springframework/ldap/pool/factory/PoolingMutableContextSource.java @@ -0,0 +1,49 @@ +/* + * Copyright 2005-2009 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.ldap.pool.factory; + +import javax.naming.directory.DirContext; +import javax.naming.ldap.LdapContext; + +import org.springframework.dao.DataAccessResourceFailureException; +import org.springframework.ldap.pool.DelegatingDirContext; +import org.springframework.ldap.pool.DirContextType; +import org.springframework.ldap.pool.MutableDelegatingLdapContext; + +/** + * A {@link PoolingContextSource} subclass that creates + * {@link MutableDelegatingLdapContext} instances. This enables the Spring LDAP + * pooling to be used in scenarios that require request controls to be set, such + * as paged results. + */ +public class PoolingMutableContextSource extends PoolingContextSource { + protected DirContext getContext(DirContextType dirContextType) { + final DirContext dirContext; + try { + dirContext = (DirContext) this.keyedObjectPool.borrowObject(dirContextType); + } + catch (Exception e) { + throw new DataAccessResourceFailureException("Failed to borrow DirContext from pool.", e); + } + + if (dirContext instanceof LdapContext) { + return new MutableDelegatingLdapContext(this.keyedObjectPool, (LdapContext) dirContext, dirContextType); + } + + return new DelegatingDirContext(this.keyedObjectPool, dirContext, dirContextType); + } +} diff --git a/core/src/test/java/org/springframework/ldap/pool/MutableDelegatingLdapContextTest.java b/core/src/test/java/org/springframework/ldap/pool/MutableDelegatingLdapContextTest.java new file mode 100644 index 00000000..c5721186 --- /dev/null +++ b/core/src/test/java/org/springframework/ldap/pool/MutableDelegatingLdapContextTest.java @@ -0,0 +1,35 @@ +/* + * Copyright 2005-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.ldap.pool; + +/** + * Unit tests for the MutableDelegatingLdapContext class. + * + * @author Ulrik Sandberg + */ +public class MutableDelegatingLdapContextTest extends AbstractPoolTestCase { + public void testSupportedMethodsAllowedToCall() throws Exception { + ldapContextMock.setRequestControls(null); + replay(); + + final MutableDelegatingLdapContext delegatingLdapContext = new MutableDelegatingLdapContext( + keyedObjectPoolMock, ldapContextMock, DirContextType.READ_ONLY); + + delegatingLdapContext.setRequestControls(null); + + verify(); + } +} diff --git a/core/src/test/java/org/springframework/ldap/pool/factory/PoolingMutableContextSourceTest.java b/core/src/test/java/org/springframework/ldap/pool/factory/PoolingMutableContextSourceTest.java new file mode 100644 index 00000000..3dcf21e5 --- /dev/null +++ b/core/src/test/java/org/springframework/ldap/pool/factory/PoolingMutableContextSourceTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2005-2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.ldap.pool.factory; + +import javax.naming.directory.DirContext; + +import org.springframework.ldap.pool.AbstractPoolTestCase; +import org.springframework.ldap.pool.MutableDelegatingLdapContext; + +/** + * Unit tests for the PoolingMutableContextSource class. + * + * @author Ulrik Sandberg + */ +public class PoolingMutableContextSourceTest extends AbstractPoolTestCase { + + public void testGetReadOnlyLdapContext() throws Exception { + + contextSourceControl.expectAndReturn(contextSourceMock.getReadOnlyContext(), ldapContextMock); + + replay(); + + final PoolingMutableContextSource poolingContextSource = new PoolingMutableContextSource(); + poolingContextSource.setContextSource(contextSourceMock); + + // Get a context + final DirContext result = poolingContextSource.getReadOnlyContext(); + + verify(); + + assertEquals(MutableDelegatingLdapContext.class, result.getClass()); + } +}