LDAP-153: Added a pooling context source that creates mutable ldap contexts.

This commit is contained in:
Ulrik Sandberg
2009-01-18 21:56:52 +00:00
parent c37bae73fb
commit d4be31c159
4 changed files with 191 additions and 0 deletions

View File

@@ -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);
}
}

View File

@@ -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);
}
}

View File

@@ -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();
}
}

View File

@@ -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());
}
}