Novell is now a part of Micro Focus

Sample Code: Adding a fullname to users without one

Novell Cool Solutions: Tip

Digg This - Slashdot This

Posted: 18 May 2005

This sample code does an LDAP search for users with no fullname attribute value and fills it in with "givenname sn"

It requires the tree IP address or DNS name, search container, username and password for a user who can modify fullname and read givenname and sn. It should compile with JDK 1.3 or later (it needs JNDI and LDAP classes).

You can also find this sample code at:

// This is code to find users in a specified container (and below) where the users do not have full names
// A full name will be assembled from the given name and surname as found
// an admin level username and password need to be put in below to make this work
// and you need to specify the context to look for users.
// There's no looping in here. Either add it yourself or use cron to schedule 
// repeat runs of this app.
// There is no multi-threading, no persistent search use, etc.,
// so this may not scale well to large numbers of users.

import java.util.Hashtable;
import javax.naming.*;
import javax.naming.ldap.*;

public class fullname {

   public static void main(String[] args) {

     try {
       Hashtable env = new Hashtable();

       env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
       env.put(Context.STATE_FACTORIES, "PersonStateFactory");
       env.put(Context.OBJECT_FACTORIES, "PersonObjectFactory");
       env.put(Context.PROVIDER_URL, "ldap://");  // SET YOUR SERVER AND STARTING CONTEXT HERE
       env.put(Context.SECURITY_PRINCIPAL, "cn=admin, o=myorg");  // SET USER THAT CAN SEARCH AND MODIFY FULL NAME HERE
       env.put(Context.SECURITY_CREDENTIALS, "password");  // SET PASSWORD HERE

       env.put(LdapContext.CONTROL_FACTORIES, "com.sun.jndi.ldap.ControlFactory");
       DirContext ctx = new InitialDirContext(env);

       // Specify the search filter to match all users with no full name
       String filter = "(&(objectClass=Person) (!(fullName=*)))";
       // limit returned attributes to those we care about
       String[] attrIDs = {"sn", "givenName", "fullName"};
       SearchControls ctls = new SearchControls();
       // comment out next line to limit to one container otherwise it'll walk down the tree

       // Search for objects using filter and controls
       NamingEnumeration answer ="", filter, ctls);

       // cycle through result set
       while (answer.hasMore()) {
           SearchResult sr = (SearchResult);
           System.out.println(">>>" + sr.getName());
           String dn = sr.getName();
           Attributes attrs = sr.getAttributes();
           String givenName = " ";
           String surName = " ";
           try {
             givenName = attrs.get("givenName").toString();
           } catch (Exception err) {
             givenName = " ";
           try {
             surName = attrs.get("sn").toString();
           } catch (Exception e2) {
             surName = " ";
           // eDir returns "attribute name : attribute value on get method so strip off up to ": "
           attrs.put("fullName", givenName.substring(givenName.indexOf(':')+2) + ' ' + surName.substring(surName.indexOf(':')+2));
           ctx.modifyAttributes(sr.getName(), DirContext.REPLACE_ATTRIBUTE, attrs);

      // Close the context when we're done
     catch(NamingException ne) {

Novell Cool Solutions (corporate web communities) are produced by WebWise Solutions.

© Copyright Micro Focus or one of its affiliates