Introduced BaseLdapPathSource and modified AbstractContextSource and

BaseLdapPathBeanPostProcessor accordingly.
This commit is contained in:
Mattias Arthursson
2007-10-29 06:30:55 +00:00
parent 1bd9c6ac7d
commit 90fb37349b
8 changed files with 124 additions and 34 deletions

View File

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

View File

@@ -252,8 +252,8 @@ public class PersonService implements PersonService, <emphasis role="bold">BaseL
</programlisting>
</example>
<para>The default behaviour of the <literal>BaseLdapPathBeanPostProcessor</literal> is to use the base path of the single
defined <literal>ContextSource</literal> in the <literal>ApplicationContext</literal>. If more than one
<literal>ContextSource</literal> is defined, you will need to specify which one to use with the <literal>contextSourceName</literal>
property.</para>
defined <literal>BaseLdapPathSource</literal> (<literal>AbstractContextSource</literal> )in the <literal>ApplicationContext</literal>.
If more than one <literal>BaseLdapPathSource</literal> is defined, you will need to specify which one to use with the
<literal>baseLdapPathSourceName</literal> property.</para>
</sect1>
</chapter>

View File

@@ -39,6 +39,6 @@
<bean
class="org.springframework.ldap.core.support.BaseLdapPathBeanPostProcessor">
<property name="contextSourceName" value="contextSource2" />
<property name="baseLdapPathSourceName" value="contextSource2" />
</bean>
</beans>

View File

@@ -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.
*

View File

@@ -34,13 +34,13 @@ import org.springframework.util.StringUtils;
* <code>BeanPostProcessor</code> is set, that value will be used. Otherwise,
* in order to determine which base LDAP path to supply to the instance the
* <code>ApplicationContext</code> 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 <code>ApplicationContext</code>, the name of the one to
* use will need to be specified to the <code>contextSourceName</code>
* use will need to be specified to the <code>baseLdapPathSourceName</code>
* 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 <code>basePath</code> 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
* <code>ApplicationContext</code>.
*
* @param basePath the base path.
@@ -119,8 +119,8 @@ public class BaseLdapPathBeanPostProcessor implements BeanPostProcessor, Applica
* @param contextSourceName the name of the <code>ContextSource</code>
* bean to use for determining the base path.
*/
public void setContextSourceName(String contextSourceName) {
this.contextSourceName = contextSourceName;
public void setBaseLdapPathSourceName(String contextSourceName) {
this.baseLdapPathSourceName = contextSourceName;
}
}

View File

@@ -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 <code>ContextSources</code> that are capable
* of providing the base LDAP path.
*
* @author Mattias Arthursson
*/
public interface BaseLdapPathContextSource extends ContextSource, BaseLdapPathSource {
}

View File

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

View File

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