Adams Bros Blog

1Jan/112

Java LDAP Persistence API

I've started a project called LDAP Persistence API for Java, or LPA for short. It is a Java framework for interacting with LDAP entries using pure Java objects. It is based on Java annotations.

It is no were near to being complete, but the query functionality is working. I still have a lot of work to do, in order to make it be able to persist existing annotated Java objects to LDAP.  I'm likely to create some sort of intermediate step, until I can finish that.

A quick example of how easy it is to use, pulled directly from one of our Classes and unit tests. The following class, is a portion of our LdapOrganization Class, showing how simple it is to annotate the class to be automatically loaded from LDAP.

/**
 * This file is part of the Ldap Persistence API (LPA).
 *
 * Copyright Trenton D. Adams
 *
 * LPA is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * LPA is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with LPA.  If not, see .
 *
 * See the COPYING file for more information.
 */
package ca.tnt.ldaputils.impl;
 
import ca.tnt.ldaputils.ILdapGroup;
import ca.tnt.ldaputils.ILdapOrganization;
import ca.tnt.ldaputils.annotations.LdapAttribute;
import ca.tnt.ldaputils.annotations.LdapEntity;
import ca.tnt.ldaputils.annotations.TypeHandler;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.log4j.Logger;
 
import java.util.*;
 
/**
 * Implements an LDAP organization object.
 *
 * Created :  16-Apr-2006 10:25:36 PM MST
 *
 * Modified : $Date$ UTC
 *
 * Revision : $Revision$
 *
 *
 * @author Trenton D. Adams
 */
@LdapEntity(requiredObjectClasses = {"organization"})
public class LdapOrganization extends LdapEntry
    implements ILdapOrganization, Comparable, TypeHandler
{
    private static final Logger logger = Logger.getLogger(
        LdapOrganization.class);
 
    @LdapAttribute(name = "businessCategory", aggregateClass = LdapGroup.class,
        referencedDNMethod = "getCategoryDN"
        /*"cn=?,ou=bus-categories,dc=example,dc=com"*/
    )
    private SortedMap businessCategories;
 
    @LdapAttribute(name = "telephoneNumber")
    private String telephoneNumber;
    @LdapAttribute(name = "facsimileTelephoneNumber")
    private String facsimileTelephoneNumber;
    @LdapAttribute(name = "street")
    private String street;
    @LdapAttribute(name = "postOfficeBox")
    private String postOfficeBox;
    @LdapAttribute(name = "postalAddress")
    private String postalAddress;
    @LdapAttribute(name = "postalCode")
    private String postalCode;
    @LdapAttribute(name = "l")
    private String locality;
    @LdapAttribute(name = "o")
    private String organization;
 
    // ... getters, setters, etc.
}

The following is a unit test, testing the above class. Take special note of how it is a single method call, to retrieve your entry, simply by calling manager.find(Class theClass, ldapName).

        private LdapManager manager;
 
        manager = new LdapManager("localhost", "",
            "uid=admin,ou=system", "secret");
 
        final LdapName ldapName = new LdapName(
            "o=Pulp Mill.,ou=businesses,dc=example,dc=com");
        final LdapOrganization ldapEntry = (LdapOrganization) manager.find(
            LdapOrganization.class, ldapName);
        final SortedMap categories =
            ldapEntry.getBusinessCategories();
        Assert.assertNotNull("Manufacturing category does not exist",
            categories.get("Manufacturing"));
        Assert.assertNotNull("Pulp & Paper Products category does not exist",
            categories.get("Pulp & Paper Products"));
        Assert.assertEquals("Pulp Mill business categories", 2,
            categories.size());
        Assert.assertEquals("Organization should be Pulp Mill",
            "Pulp Mill.", ldapEntry.getOrganization());

We have a good start on the project, but we have a very long way to go. If anyone is interested in helping out with the framework, please wander over to github, and check it out.

Comments (2) Trackbacks (0)
  1. JNDI?

  2. JNDI? Yes, we use JNDI, obviously. However, JNDI is not a “persistence API”. It does not provide automated facilities for turning LDAP entries into Java objects, or Java objects into LDAP entries.


Leave a comment

 

No trackbacks yet.