From 29d161a4eeea0994c7d694f1d352eb146e5aa5a1 Mon Sep 17 00:00:00 2001 From: Ulrik Sandberg Date: Fri, 2 Feb 2007 16:45:31 +0000 Subject: [PATCH] LDAP-42: Extract useful nested classes from LdapTemplate http://opensource.atlassian.com/projects/spring/browse/LDAP-42 --- spring-ldap/docs/reference/src/executors.xml | 4 +- .../control/LdapTemplatePagedSearchITest.java | 3 +- .../core/AttributesMapperCallbackHandler.java | 43 +++++++++++ .../core/ContextMapperCallbackHandler.java | 39 ++++++++++ .../ldap/core/LdapTemplate.java | 73 ------------------- 5 files changed, 86 insertions(+), 76 deletions(-) create mode 100644 spring-ldap/src/main/java/org/springframework/ldap/core/AttributesMapperCallbackHandler.java create mode 100644 spring-ldap/src/main/java/org/springframework/ldap/core/ContextMapperCallbackHandler.java diff --git a/spring-ldap/docs/reference/src/executors.xml b/spring-ldap/docs/reference/src/executors.xml index 237fa3a1..0bb3c46d 100644 --- a/spring-ldap/docs/reference/src/executors.xml +++ b/spring-ldap/docs/reference/src/executors.xml @@ -40,7 +40,7 @@ public class PersonDaoImpl implements PersonDao { }; NameClassPairCallbackHandler handler = - ldapTemplate.new AttributesMapperCallbackHandler(new PersonAttributesMapper()); + new AttributesMapperCallbackHandler(new PersonAttributesMapper()); return ldapTemplate.search(executor, handler); } @@ -68,7 +68,7 @@ public class PersonDaoImpl implements PersonDao { }; NameClassPairCallbackHandler handler = - ldapTemplate.new ContextMapperCallbackHandler(new PersonContextMapper()); + new ContextMapperCallbackHandler(new PersonContextMapper()); return ldapTemplate.search(executor, handler); } diff --git a/spring-ldap/src/itest-openldap/java/org/springframework/ldap/control/LdapTemplatePagedSearchITest.java b/spring-ldap/src/itest-openldap/java/org/springframework/ldap/control/LdapTemplatePagedSearchITest.java index d49d5d9c..fe52738f 100644 --- a/spring-ldap/src/itest-openldap/java/org/springframework/ldap/control/LdapTemplatePagedSearchITest.java +++ b/spring-ldap/src/itest-openldap/java/org/springframework/ldap/control/LdapTemplatePagedSearchITest.java @@ -25,6 +25,7 @@ import javax.naming.directory.SearchControls; import org.springframework.ldap.Person; import org.springframework.ldap.PersonAttributesMapper; +import org.springframework.ldap.core.AttributesMapperCallbackHandler; import org.springframework.ldap.core.CollectingNameClassPairCallbackHandler; import org.springframework.ldap.core.DistinguishedName; import org.springframework.ldap.core.LdapTemplate; @@ -61,7 +62,7 @@ public class LdapTemplatePagedSearchITest extends protected void onSetUp() throws Exception { super.onSetUp(); PersonAttributesMapper mapper = new PersonAttributesMapper(); - callbackHandler = tested.new AttributesMapperCallbackHandler(mapper); + callbackHandler = new AttributesMapperCallbackHandler(mapper); searchControls = new SearchControls(); searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE); } diff --git a/spring-ldap/src/main/java/org/springframework/ldap/core/AttributesMapperCallbackHandler.java b/spring-ldap/src/main/java/org/springframework/ldap/core/AttributesMapperCallbackHandler.java new file mode 100644 index 00000000..a778dfda --- /dev/null +++ b/spring-ldap/src/main/java/org/springframework/ldap/core/AttributesMapperCallbackHandler.java @@ -0,0 +1,43 @@ +package org.springframework.ldap.core; + +import javax.naming.NameClassPair; +import javax.naming.directory.Attributes; +import javax.naming.directory.SearchResult; + +import org.springframework.ldap.support.LdapUtils; + +/** + * A CollectingNameClassPairCallbackHandler to wrap an AttributesMapper. + * That is, the found object is extracted from the {@link Attributes} of + * each {@link SearchResult}, and then passed to the specified + * AttributesMapper for translation. + * + * @author Mattias Arthursson + * @author Ulrik Sandberg + */ +public class AttributesMapperCallbackHandler extends + CollectingNameClassPairCallbackHandler { + private AttributesMapper mapper; + + public AttributesMapperCallbackHandler(AttributesMapper mapper) { + this.mapper = mapper; + } + + /** + * Cast the NameClassPair to a SearchResult and pass its attributes to + * the AttributesMapper. + * + * @param nameClassPair + * a SearchResult instance. + * @return the Object returned from the Mapper. + */ + public Object getObjectFromNameClassPair(NameClassPair nameClassPair) { + SearchResult searchResult = (SearchResult) nameClassPair; + Attributes attributes = searchResult.getAttributes(); + try { + return mapper.mapFromAttributes(attributes); + } catch (javax.naming.NamingException e) { + throw LdapUtils.convertLdapException(e); + } + } +} \ No newline at end of file diff --git a/spring-ldap/src/main/java/org/springframework/ldap/core/ContextMapperCallbackHandler.java b/spring-ldap/src/main/java/org/springframework/ldap/core/ContextMapperCallbackHandler.java new file mode 100644 index 00000000..eb7cc102 --- /dev/null +++ b/spring-ldap/src/main/java/org/springframework/ldap/core/ContextMapperCallbackHandler.java @@ -0,0 +1,39 @@ +package org.springframework.ldap.core; + +import javax.naming.Binding; +import javax.naming.NameClassPair; + +/** + * A CollectingNameClassPairCallbackHandler to wrap a ContextMapper. That + * is, the found object is extracted from each {@link Binding}, and then + * passed to the specified ContextMapper for translation. + * + * @author Mattias Arthursson + * @author Ulrik Sandberg + */ +public class ContextMapperCallbackHandler extends + CollectingNameClassPairCallbackHandler { + private ContextMapper mapper; + + public ContextMapperCallbackHandler(ContextMapper mapper) { + this.mapper = mapper; + } + + /** + * Cast the NameClassPair to a {@link Binding} and pass its attributes + * to the ContextMapper. + * + * @param nameClassPair + * a SearchResult instance. + * @return the Object returned from the Mapper. + */ + public Object getObjectFromNameClassPair(NameClassPair nameClassPair) { + Binding binding = (Binding) nameClassPair; + Object object = binding.getObject(); + if (object == null) { + throw new ObjectRetrievalException( + "SearchResult did not contain any object."); + } + return mapper.mapFromContext(object); + } +} \ No newline at end of file diff --git a/spring-ldap/src/main/java/org/springframework/ldap/core/LdapTemplate.java b/spring-ldap/src/main/java/org/springframework/ldap/core/LdapTemplate.java index 43cb5b56..332e9d27 100644 --- a/spring-ldap/src/main/java/org/springframework/ldap/core/LdapTemplate.java +++ b/spring-ldap/src/main/java/org/springframework/ldap/core/LdapTemplate.java @@ -27,7 +27,6 @@ import javax.naming.directory.Attributes; import javax.naming.directory.DirContext; import javax.naming.directory.ModificationItem; import javax.naming.directory.SearchControls; -import javax.naming.directory.SearchResult; import org.apache.commons.lang.Validate; import org.apache.commons.logging.Log; @@ -1286,76 +1285,4 @@ public class LdapTemplate implements LdapOperations, InitializingBean { } } } - - /** - * A CollectingNameClassPairCallbackHandler to wrap an AttributesMapper. - * That is, the found object is extracted from the {@link Attributes} of - * each {@link SearchResult}, and then passed to the specified - * AttributesMapper for translation. This class needs to be nested, since we - * want to be able to get hold of the exception translator of this instance. - * - * @author Mattias Arthursson - * @author Ulrik Sandberg - */ - public class AttributesMapperCallbackHandler extends - CollectingNameClassPairCallbackHandler { - private AttributesMapper mapper; - - public AttributesMapperCallbackHandler(AttributesMapper mapper) { - this.mapper = mapper; - } - - /** - * Cast the NameClassPair to a SearchResult and pass its attributes to - * the AttributesMapper. - * - * @param nameClassPair - * a SearchResult instance. - * @return the Object returned from the Mapper. - */ - public Object getObjectFromNameClassPair(NameClassPair nameClassPair) { - SearchResult searchResult = (SearchResult) nameClassPair; - Attributes attributes = searchResult.getAttributes(); - try { - return mapper.mapFromAttributes(attributes); - } catch (javax.naming.NamingException e) { - throw LdapUtils.convertLdapException(e); - } - } - } - - /** - * A CollectingNameClassPairCallbackHandler to wrap a ContextMapper. That - * is, the found object is extracted from each {@link Binding}, and then - * passed to the specified ContextMapper for translation. - * - * @author Mattias Arthursson - * @author Ulrik Sandberg - */ - public class ContextMapperCallbackHandler extends - CollectingNameClassPairCallbackHandler { - private ContextMapper mapper; - - public ContextMapperCallbackHandler(ContextMapper mapper) { - this.mapper = mapper; - } - - /** - * Cast the NameClassPair to a {@link Binding} and pass its attributes - * to the ContextMapper. - * - * @param nameClassPair - * a SearchResult instance. - * @return the Object returned from the Mapper. - */ - public Object getObjectFromNameClassPair(NameClassPair nameClassPair) { - Binding binding = (Binding) nameClassPair; - Object object = binding.getObject(); - if (object == null) { - throw new ObjectRetrievalException( - "SearchResult did not contain any object."); - } - return mapper.mapFromContext(object); - } - } }