Thursday, February 09, 2006

Spring's LDAPTemplate

The Spring Framework is my new favourite thing! I toyed with LDAP and Spring recently. There is a Sourceforge project called LDAPTemplate maintained by Mattias Arthursson and Ulrik Sandberg of Jayway which looks to be very impressive. It is built on the same principles as Spring JDBCTemplate and other DAO templates. Unfortunately since I am working with LDAP version 2 (rather than version 3) I couldn't get it to work but the guys are working on fixing this for a future release. I can see this project becoming part of the core Spring framework in the not too distant future.

I did however manage to get an earlier version of the code from LDAPTemplate written by Olivier Jolly working with the following example.

Spring Config file (applicationContext.xml):

<?xml version="1.0" encoding="UTF-8"?>

<bean id="rootContextSource"
<property name="ldapUrl" value="ldap://hostname:389/o=Blah, c=GB"/>
<bean id="ldapTemplate"
<property name="contextSource">
<ref bean="rootContextSource" />

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import javax.naming.NamingEnumeration;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.core.SearchResultCallbackHandler;

public class LDAPTest {

public static void main(String[] args) throws Exception {

// Manually create the Spring Bean Factory
// Note: The factory is driven by the Bean definitions held in ApplicationContext
// Note: There are also other implementations of Resource and Factory, but
// the following combination work well for standalone apps.

ClassPathResource res = new ClassPathResource("applicationContext.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);
SearchControls ctls = new SearchControls();
// Specify the ids of the attributes to return
String[] attrIDs = {"sn", "mail", "uid", "cn", "ou", "postalAddress", "postalCode"};

LdapTemplate ldapTemplate = (LdapTemplate)factory.getBean("ldapTemplate");
List l = (List)
ctls, new SearchResultCallbackHandler() {
LinkedList list = new LinkedList();

public void processSearchResult(SearchResult searchResult) throws javax.naming.NamingException {
Attributes attrs = searchResult.getAttributes();

public Object getResult() {
return list;
Iterator i = l.iterator();
while(i.hasNext()) {
Attributes o = (Attributes);
NamingEnumeration ids = o.getIDs();
String att = (String);
System.out.println(att + ":");


What is it about Spring that makes me use anonymous classes all the time?


Mark McLaren said...

By the way, LdapV2 should be working in the current version of LdapTemplate (1.0 RC-2). We're currently working on the final 1.0 release that should hopefully be released before the end of April.
Note: Comment imported. Original by Mattias Arthursson website: at 2006-04-13 15:34

Mark McLaren said...


I am having trouble in successfully implementing Jayway's method of using LDAP TEmplate API with LDAPv2. So thought I can try your method. Where do I get class file?? which version of Springldap can i find this?
Note: Comment imported. Original by Anonymous at 2009-04-09 16:23

Mark McLaren said...

You can still get UrlContextSource from JASIG and Google Code. I expect the name has probably changed since it became an official Spring project.
Note: Comment imported. Original by markmc website: at 2009-04-27 08:25

for ict 99 said...

The effectiveness of IEEE Project Domains depends very much on the situation in which they are applied. In order to further improve IEEE Final Year Project Domains practices we need to explicitly describe and utilise our knowledge about software domains of software engineering Final Year Project Domains for CSE technologies. This paper suggests a modelling formalism for supporting systematic reuse of software engineering technologies during planning of software projects and improvement programmes in Final Year Project Centers in Chennai.

Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

madin said...

Le scarpe estremamente flessibili sono l'anima di Jimmy Choo. repliche jimmy choo All'inizio degli anni '90, replica formatori Choo era un calzolaio personalizzato nell'East End di Londra, ed era ricercato da molti clienti noti, tra cui Jetsman e la principessa Diana.

madin said...

replica rolex watches are crafted from the finest raw materials and mini for watchassembled with scrupulous attention to detail. Every component is designed, developed and produced in-house to the most exacting standards.