Java Reference
In-Depth Information
getAttributeValue(attributes, "givenName"));
contact.setMail(getAttributeValue(attributes, "mail"));
getAttributeValue(attributes, "uid"));
return contact;
private String getAttributeValue(
Attributes attributes, String attrID
) {
Attribute attribute = attributes.get(attrID);
try {
return (null==attribute?"":(String)attribute.get());
} catch (NamingException e) {
throw new DaoException(e);
The Attributes interface is part of the JNDI package that comes with Sun's JDK ,
and is implemented by the BasicAttributes class, which is also part of that pack-
age. The Contact class is our bean that we want to map to our LDAP directory.
Finally, the getAttributeValue() method is a helper method that simplifies the
mapping process by handling null values and turning JNDI -specific exceptions
into DaoException s.
Just as with other DAO implementations, we need to make some decisions
about where and how we will get to our database. If you are working with a J2EE
container that can provide you with a JNDI context, you might be tempted to use
it. If it meets your needs, there is no reason not to. However, there are some trade-
offs when doing this. Although this approach will simplify the code, it will make
testing more difficult. Depending on your requirements, this may be an accept-
able sacrifice.
In this example, we wanted to make things as testable as possible, so we used
constructor-based injection to configure the DAO class at runtime. Because the
i BATIS DAO does not allow this, we also created a default constructor that uses the
two values we wanted as the defaults. In section 11.3, we look at using the Spring
framework for our DAO layer, which allows us to do this via configuration files, but
for now, let's use the default constructor method.
The second constructor takes two parameters for the two settings I hardcoded
into the default constructor. One of those was the determination of the LDAP DN
attribute for our contact bean. This attribute is analogous to the primary key of a
Search WWH ::

Custom Search