LDAP-42: Extract useful nested classes from LdapTemplate

http://opensource.atlassian.com/projects/spring/browse/LDAP-42
This commit is contained in:
Ulrik Sandberg
2007-02-02 16:45:31 +00:00
parent b07bd91717
commit 29d161a4ee
5 changed files with 86 additions and 76 deletions

View File

@@ -40,7 +40,7 @@ public class PersonDaoImpl implements PersonDao {
}</emphasis>;
NameClassPairCallbackHandler handler =
ldapTemplate.new AttributesMapperCallbackHandler(new PersonAttributesMapper());
new AttributesMapperCallbackHandler(new PersonAttributesMapper());
return ldapTemplate.search(<emphasis role="bold">executor</emphasis>, handler);
}
@@ -68,7 +68,7 @@ public class PersonDaoImpl implements PersonDao {
};
NameClassPairCallbackHandler handler =
ldapTemplate.<emphasis role="bold">new ContextMapperCallbackHandler(new PersonContextMapper())</emphasis>;
<emphasis role="bold">new ContextMapperCallbackHandler(new PersonContextMapper())</emphasis>;
return ldapTemplate.search(executor, handler);
}

View File

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

View File

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

View File

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

View File

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