From 2746ce6d94447e3984a9cb0190443c4e376734a2 Mon Sep 17 00:00:00 2001 From: Ulrik Sandberg Date: Mon, 9 Apr 2007 18:20:26 +0000 Subject: [PATCH] Improved overview doc comparing traditional and new approach. --- spring-ldap/docs/reference/src/overview.xml | 101 ++++++++++++-------- 1 file changed, 59 insertions(+), 42 deletions(-) diff --git a/spring-ldap/docs/reference/src/overview.xml b/spring-ldap/docs/reference/src/overview.xml index 7a866cd9..885ee7e0 100644 --- a/spring-ldap/docs/reference/src/overview.xml +++ b/spring-ldap/docs/reference/src/overview.xml @@ -31,7 +31,8 @@ want, adding it to a list. The traditional way of implementing this person name search method - in Java LDAP looks like this: + in Java LDAP looks like this, where the code marked as bold actually + performs tasks related to the business purpose of the method: package com.example.dao; @@ -54,14 +55,14 @@ public class TraditionalPersonDaoImpl implements PersonDao { try { SearchControls controls = new SearchControls(); controls.setSearchScope(SearchControls.SUBTREE_SCOPE); - results = ctx.search("", "(objectclass=person)", controls); + results = ctx.search("", "(objectclass=person)", controls); while (results.hasMore()) { SearchResult searchResult = (SearchResult) results.next(); Attributes attributes = searchResult.getAttributes(); - Attribute attr = attributes.get("cn"); + Attribute attr = attributes.get("cn"); String cn = (String) attr.get(); - list.add(cn); + list.add(cn); } } catch (NameNotFoundException e) { // The base context was not found. @@ -84,13 +85,14 @@ public class TraditionalPersonDaoImpl implements PersonDao { } } } - return list; + return list; } } - By using the Spring LDAP AttributesMapper, we get - the exact same functionality with the following code: + By using the Spring LDAP classes AttributesMapper + and LdapTemplate, we get the exact same functionality + with the following code: package com.example.dao; @@ -103,17 +105,55 @@ public class PersonDaoImpl implements PersonDao { } public List getAllPersonNames() { - return ldapTemplate.search( - "", "(objectclass=person)", + return ldapTemplate.search( + "", "(objectclass=person)", new AttributesMapper() { public Object mapFromAttributes(Attributes attrs) throws NamingException { - return attrs.get("cn").get(); + return attrs.get("cn").get(); } }); } } + + The amount of boiler-plate code is significantly less than in the + traditional example. The LdapTemplate version of the + search method performs the search, maps the attributes to a string using + the given AttributesMapper, collects the strings in an + internal list, and finally returns the list. + + Note that the PersonDaoImpl code simply assumes + that it has an LdapTemplate instance, rather than + looking one up somewhere. It provides a set method for this purpose. There + is nothing Spring-specific about this "Inversion of Control". Anyone that + can create an instance of PersonDaoImpl can also set + the LdapTemplate on it. However, Spring provides a very + flexible and easy way of achieving this (see http://static.springframework.org/spring/docs/current/reference/beans.html). + The Spring container can be told to wire up an instance of + LdapTemplate with its required dependencies and inject + it into the PersonDao instance. This wiring can be + defined in various ways, but the most common is through XML: + + + <beans> + <bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource"> + <property name="url" value="ldap://localhost:389" /> + <property name="base" value="dc=example,dc=com" /> + <property name="userDn" value="cn=Manager" /> + <property name="password" value="secret" /> + </bean> + + <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate"> + <constructor-arg ref="contextSource" /> + </bean> + + <bean id="personDao" class="com.example.dao.PersonDaoImpl"> + <property name="ldapTemplate" ref="ldapTemplate" /> + </bean> +</beans> + @@ -137,8 +177,7 @@ public class PersonDaoImpl implements PersonDao { - spring-dao (exception hierarchy enabling sophisticated error - handling independent of the data access approach in use) + spring-dao (basic transaction support) @@ -156,7 +195,8 @@ public class PersonDaoImpl implements PersonDao { - ldapbp (Sun LDAP Boosetr Pack, for required controls) + ldapbp (Sun LDAP Booster Pack, for required LDAP + controls) @@ -178,30 +218,6 @@ public class PersonDaoImpl implements PersonDao { acegi-security - - Set up the required beans in your Spring context file and inject the - LdapTemplate into your data access object: - - - <beans> - ... - <bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource"> - <property name="url" value="ldap://localhost:389" /> - <property name="base" value="dc=example,dc=com" /> - <property name="userDn" value="cn=Manager" /> - <property name="password" value="secret" /> - </bean> - - <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate"> - <constructor-arg ref="contextSource" /> - </bean> - - <bean id="myDataAccessObject" class="com.example.MyDataAccessObject"> - <property name="ldapTemplate" ref="ldapTemplate" /> - </bean> - ... -</beans> - @@ -216,11 +232,12 @@ public class PersonDaoImpl implements PersonDao { org.acegisecurity package.
- Spring LDAP package structure - - - - + Spring LDAP package structure + + + + +