diff --git a/spring-ldap/changelog.txt b/spring-ldap/changelog.txt index e96f130f..dca7d013 100644 --- a/spring-ldap/changelog.txt +++ b/spring-ldap/changelog.txt @@ -39,9 +39,9 @@ the supplied upgrade guide for details. * AcegiAuthenticationSource now supports anonymous authentication. (LDAP-67) -* Added BaseLdapPathAware and BaseLdapNameBeanPostProcessor to be used if the ContextSource - base path is needed by beans. There is now also a DistinguishedNameEditor available for - directly injecting DistinguishedName instances to beans. (LDAP-86) +* Added BaseLdapPathSource, BaseLdapPathAware and BaseLdapNameBeanPostProcessor to be used + if the ContextSource base path is needed by beans. There is now also a DistinguishedNameEditor + available for directly injecting DistinguishedName instances to beans. (LDAP-86) * Added immutableDistinguishedName() method in DistinguishedName to get an immutable copy of the instance. (LDAP-87) diff --git a/spring-ldap/docs/reference/src/configuration.xml b/spring-ldap/docs/reference/src/configuration.xml index eda25947..0a056422 100644 --- a/spring-ldap/docs/reference/src/configuration.xml +++ b/spring-ldap/docs/reference/src/configuration.xml @@ -252,8 +252,8 @@ public class PersonService implements PersonService, BaseL The default behaviour of the BaseLdapPathBeanPostProcessor is to use the base path of the single - defined ContextSource in the ApplicationContext. If more than one - ContextSource is defined, you will need to specify which one to use with the contextSourceName - property. + defined BaseLdapPathSource (AbstractContextSource )in the ApplicationContext. + If more than one BaseLdapPathSource is defined, you will need to specify which one to use with the + baseLdapPathSourceName property. \ No newline at end of file diff --git a/spring-ldap/src/itest/java/conf/baseLdapPathPostProcessorMultiContextSourceOneSpecTestContext.xml b/spring-ldap/src/itest/java/conf/baseLdapPathPostProcessorMultiContextSourceOneSpecTestContext.xml index 0b662f43..7c56d94c 100644 --- a/spring-ldap/src/itest/java/conf/baseLdapPathPostProcessorMultiContextSourceOneSpecTestContext.xml +++ b/spring-ldap/src/itest/java/conf/baseLdapPathPostProcessorMultiContextSourceOneSpecTestContext.xml @@ -39,6 +39,6 @@ - + diff --git a/spring-ldap/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java b/spring-ldap/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java index 8fe1b214..614adc71 100644 --- a/spring-ldap/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java +++ b/spring-ldap/src/main/java/org/springframework/ldap/core/support/AbstractContextSource.java @@ -61,7 +61,7 @@ import org.springframework.ldap.support.LdapUtils; * @author Adam Skogman * @author Ulrik Sandberg */ -public abstract class AbstractContextSource implements ContextSource, InitializingBean { +public abstract class AbstractContextSource implements BaseLdapPathContextSource, InitializingBean { private static final Class DEFAULT_CONTEXT_FACTORY = com.sun.jndi.ldap.LdapCtxFactory.class; @@ -192,6 +192,22 @@ public abstract class AbstractContextSource implements ContextSource, Initializi return base; } + /* + * (non-Javadoc) + * @see org.springframework.ldap.core.support.BaseLdapPathSource#getBaseLdapPath() + */ + public DistinguishedName getBaseLdapPath() { + return getBase().immutableDistinguishedName(); + } + + /* + * (non-Javadoc) + * @see org.springframework.ldap.core.support.BaseLdapPathSource#getBaseLdapPathAsString() + */ + public String getBaseLdapPathAsString() { + return getBaseLdapPath().toString(); + } + /** * Create a DirContext using the supplied environment. * diff --git a/spring-ldap/src/main/java/org/springframework/ldap/core/support/BaseLdapPathBeanPostProcessor.java b/spring-ldap/src/main/java/org/springframework/ldap/core/support/BaseLdapPathBeanPostProcessor.java index 34f98f23..8e01fc6c 100644 --- a/spring-ldap/src/main/java/org/springframework/ldap/core/support/BaseLdapPathBeanPostProcessor.java +++ b/spring-ldap/src/main/java/org/springframework/ldap/core/support/BaseLdapPathBeanPostProcessor.java @@ -34,13 +34,13 @@ import org.springframework.util.StringUtils; * BeanPostProcessor is set, that value will be used. Otherwise, * in order to determine which base LDAP path to supply to the instance the * ApplicationContext is searched for any beans that are - * implementations of {@link AbstractContextSource}. If one single occurrance + * implementations of {@link BaseLdapPathSource}. If one single occurrance * is found, that instance is queried for its base path, and that is what will - * be injected. If more than one {@link AbstractContextSource} instance is + * be injected. If more than one {@link BaseLdapPathSource} instance is * configured in the ApplicationContext, the name of the one to - * use will need to be specified to the contextSourceName + * use will need to be specified to the baseLdapPathSourceName * property; otherwise the post processing will fail. If no - * {@link AbstractContextSource} implementing bean is found in the context and + * {@link BaseLdapPathSource} implementing bean is found in the context and * the basePath property is not set, post processing will also * fail. * @@ -53,7 +53,7 @@ public class BaseLdapPathBeanPostProcessor implements BeanPostProcessor, Applica private DistinguishedName basePath; - private String contextSourceName; + private String baseLdapPathSourceName; public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof BaseLdapPathAware) { @@ -63,26 +63,26 @@ public class BaseLdapPathBeanPostProcessor implements BeanPostProcessor, Applica baseLdapPathAware.setBaseLdapPath(basePath); } else { - AbstractContextSource abstractContextSource = getAbstractContextSourceFromApplicationContext(); - baseLdapPathAware.setBaseLdapPath(abstractContextSource.getBase()); + BaseLdapPathSource ldapPathSource = getBaseLdapPathSourceFromApplicationContext(); + baseLdapPathAware.setBaseLdapPath(ldapPathSource.getBaseLdapPath()); } } return bean; } - AbstractContextSource getAbstractContextSourceFromApplicationContext() { - if (StringUtils.hasLength(contextSourceName)) { - return (AbstractContextSource) applicationContext.getBean(contextSourceName); + BaseLdapPathSource getBaseLdapPathSourceFromApplicationContext() { + if (StringUtils.hasLength(baseLdapPathSourceName)) { + return (BaseLdapPathSource) applicationContext.getBean(baseLdapPathSourceName); } - String[] definedContextSources = applicationContext.getBeanNamesForType(AbstractContextSource.class); + String[] definedContextSources = applicationContext.getBeanNamesForType(BaseLdapPathSource.class); if (definedContextSources.length < 1) { - throw new NoSuchBeanDefinitionException("No AbstractContextSource implementation definition found"); + throw new NoSuchBeanDefinitionException("No BaseLdapPathSource implementation definition found"); } else if (definedContextSources.length > 1) { throw new NoSuchBeanDefinitionException( - "More than AbstractContextSource implementation definition found in current ApplicationContext"); + "More than BaseLdapPathSource implementation definition found in current ApplicationContext"); } - return (AbstractContextSource) applicationContext.getBean(definedContextSources[0]); + return (BaseLdapPathSource) applicationContext.getBean(definedContextSources[0]); } /* @@ -102,7 +102,7 @@ public class BaseLdapPathBeanPostProcessor implements BeanPostProcessor, Applica /** * Set the base path to be injected in all {@link BaseLdapPathAware} beans. * If this property is not set, the default base path will be determined - * from any defined {@link AbstractContextSource} instances available in the + * from any defined {@link BaseLdapPathSource} instances available in the * ApplicationContext. * * @param basePath the base path. @@ -119,8 +119,8 @@ public class BaseLdapPathBeanPostProcessor implements BeanPostProcessor, Applica * @param contextSourceName the name of the ContextSource * bean to use for determining the base path. */ - public void setContextSourceName(String contextSourceName) { - this.contextSourceName = contextSourceName; + public void setBaseLdapPathSourceName(String contextSourceName) { + this.baseLdapPathSourceName = contextSourceName; } } diff --git a/spring-ldap/src/main/java/org/springframework/ldap/core/support/BaseLdapPathContextSource.java b/spring-ldap/src/main/java/org/springframework/ldap/core/support/BaseLdapPathContextSource.java new file mode 100644 index 00000000..f0d607bd --- /dev/null +++ b/spring-ldap/src/main/java/org/springframework/ldap/core/support/BaseLdapPathContextSource.java @@ -0,0 +1,28 @@ +/* + * Copyright 2005-2007 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.core.support; + +import org.springframework.ldap.core.ContextSource; + +/** + * Interface to be implemented by ContextSources that are capable + * of providing the base LDAP path. + * + * @author Mattias Arthursson + */ +public interface BaseLdapPathContextSource extends ContextSource, BaseLdapPathSource { + +} diff --git a/spring-ldap/src/main/java/org/springframework/ldap/core/support/BaseLdapPathSource.java b/spring-ldap/src/main/java/org/springframework/ldap/core/support/BaseLdapPathSource.java new file mode 100644 index 00000000..c26a914e --- /dev/null +++ b/spring-ldap/src/main/java/org/springframework/ldap/core/support/BaseLdapPathSource.java @@ -0,0 +1,46 @@ +/* + * Copyright 2005-2007 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.core.support; + +import org.springframework.ldap.core.ContextSource; +import org.springframework.ldap.core.DistinguishedName; + +/** + * Implementations of this interface are capable of providing a base LDAP path. + * The base LDAP path is the root path to which all LDAP operations performed on + * a particular context are relative. + * + * @see ContextSource + * + * @author Mattias Arthursson + */ +public interface BaseLdapPathSource { + /** + * Get the base LDAP path as a {@link DistinguishedName}. + * + * @return the base LDAP path as a {@link DistinguishedName}. The path will + * be empty if no base path is specified. + */ + DistinguishedName getBaseLdapPath(); + + /** + * Get the base LDAP path as a String. + * + * @return the base LDAP path as a An empty String will be returned if no + * base path is specified. + */ + String getBaseLdapPathAsString(); +} diff --git a/spring-ldap/src/test/java/org/springframework/ldap/core/support/BaseLdapPathBeanPostProcessorTest.java b/spring-ldap/src/test/java/org/springframework/ldap/core/support/BaseLdapPathBeanPostProcessorTest.java index 6f2a6f97..6a64d39b 100644 --- a/spring-ldap/src/test/java/org/springframework/ldap/core/support/BaseLdapPathBeanPostProcessorTest.java +++ b/spring-ldap/src/test/java/org/springframework/ldap/core/support/BaseLdapPathBeanPostProcessorTest.java @@ -81,7 +81,7 @@ public class BaseLdapPathBeanPostProcessorTest extends TestCase { expectedContextSource.setBase(expectedPath); tested = new BaseLdapPathBeanPostProcessor() { - AbstractContextSource getAbstractContextSourceFromApplicationContext() { + BaseLdapPathSource getBaseLdapPathSourceFromApplicationContext() { return expectedContextSource; } }; @@ -99,14 +99,14 @@ public class BaseLdapPathBeanPostProcessorTest extends TestCase { public void testGetAbstractContextSourceFromApplicationContext() throws Exception { applicationContextControl.expectAndReturn(applicationContextMock - .getBeanNamesForType(AbstractContextSource.class), new String[] { "contextSource" }); + .getBeanNamesForType(BaseLdapPathSource.class), new String[] { "contextSource" }); LdapContextSource expectedContextSource = new LdapContextSource(); applicationContextControl.expectAndReturn(applicationContextMock.getBean("contextSource"), expectedContextSource); applicationContextControl.replay(); - AbstractContextSource result = tested.getAbstractContextSourceFromApplicationContext(); + BaseLdapPathSource result = tested.getBaseLdapPathSourceFromApplicationContext(); applicationContextControl.verify(); assertSame(expectedContextSource, result); @@ -114,12 +114,12 @@ public class BaseLdapPathBeanPostProcessorTest extends TestCase { public void testGetAbstractContextSourceFromApplicationContextNoContextSource() throws Exception { applicationContextControl.expectAndReturn(applicationContextMock - .getBeanNamesForType(AbstractContextSource.class), new String[0]); + .getBeanNamesForType(BaseLdapPathSource.class), new String[0]); applicationContextControl.replay(); try { - tested.getAbstractContextSourceFromApplicationContext(); + tested.getBaseLdapPathSourceFromApplicationContext(); fail("NoSuchBeanDefinitionException expected"); } catch (NoSuchBeanDefinitionException expected) { @@ -130,12 +130,12 @@ public class BaseLdapPathBeanPostProcessorTest extends TestCase { public void testGetAbstractContextSourceFromApplicationContextTwoContextSources() throws Exception { applicationContextControl.expectAndReturn(applicationContextMock - .getBeanNamesForType(AbstractContextSource.class), new String[2]); + .getBeanNamesForType(BaseLdapPathSource.class), new String[2]); applicationContextControl.replay(); try { - tested.getAbstractContextSourceFromApplicationContext(); + tested.getBaseLdapPathSourceFromApplicationContext(); fail("NoSuchBeanDefinitionException expected"); } catch (NoSuchBeanDefinitionException expected) { @@ -147,13 +147,13 @@ public class BaseLdapPathBeanPostProcessorTest extends TestCase { public void testGetAbstractContextSourceFromApplicationContextTwoContextSourcesAndSpecifiedName() throws Exception { LdapContextSource expectedContextSource = new LdapContextSource(); - tested.setContextSourceName("myContextSource"); + tested.setBaseLdapPathSourceName("myContextSource"); applicationContextControl.expectAndReturn(applicationContextMock.getBean("myContextSource"), expectedContextSource); applicationContextControl.replay(); - tested.getAbstractContextSourceFromApplicationContext(); + tested.getBaseLdapPathSourceFromApplicationContext(); applicationContextControl.verify(); } }