Java Reference
In-Depth Information
between these attributes and Java's
Map
interface, it would be trivial to create a
Map-based version of the
DAO
that simply used a Map to hide the
JNDI
attributes
structure. In this section however, we will create a
DAO
that will use the bean from
the previous section by mapping it to an
LDAP
inetOrgPerson
entry using the
mapping in table 11.2.
Table 11.1
JavaBean to LDAP attribute mapping
Bean property
LDAP attribute
userId
uid
mail
mail
description
description
lastName
sn
firstName
givenName
This mapping will be accomplished in our
DAO
implementation by using meth-
ods to create a bean from an
Attributes
object, or an
Attributes
object from a
bean. While it would be possible to create a reflection-based mapping mechanism
for this, we are going to make our
DAO
implementation very simple and just hard-
code the mapping. Listing 11.5 contains the three methods from our
DAO
imple-
mentation that are responsible for that mapping.
Listing 11.5
Support methods for our LDAP DAO implementation
private Attributes getAttributes(Contact contact){
Attributes returnValue = new BasicAttributes();
returnValue.put("mail", contact.getMail());
returnValue.put("uid", contact.getUserId());
returnValue.put("objectClass", "inetOrgPerson");
returnValue.put(
"description", contact.getDescription());
returnValue.put("sn", contact.getLastName());
returnValue.put("cn", contact.getUserId());
returnValue.put("givenName", contact.getFirstName());
return returnValue;
}
private Contact getContact(Attributes attributes) {
Contact contact = new Contact();
contact.setDescription(
getAttributeValue(attributes, "description"));
contact.setLastName(
getAttributeValue(attributes, "sn"));











