FileDocCategorySizeDatePackage
LdapPersonCommand.javaAPI DocExample2498Sun Jul 20 18:43:54 BST 2003antipatterns.model

LdapPersonCommand

public class LdapPersonCommand extends Object implements PersonCommand

Fields Summary
private DirContext
peopleContext
private Vector
people
private String
firstName
private String
lastName
Constructors Summary
Methods Summary
public java.util.ListgetPeople()

        return people;
    
public voidinitialize(javax.servlet.http.HttpSession session)

        Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY,
            "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://localhost/o=jndiTest");
        env.put(Context.SECURITY_PRINCIPAL, "cn=Manager, o=jndiTest");
        env.put(Context.SECURITY_CREDENTIALS, "secret");
        
        DirContext initalContext = new InitialDirContext(env);
        peopleContext = (DirContext)initalContext.lookup("ou=people");
    
        people = new Vector();
    
public voidrunCommand()

        String filter = "(objectclass=person)";
              
        if (firstName != null && lastName != null) {
            filter = "& (cn=" + firstName + ") (sn=" + lastName + ")";
        } else if (firstName != null) {
            filter = "cn=" + firstName; 
        } else if(lastName != null) {
            filter = "sn=" + lastName;
        }
        
        System.out.println("filter = " + filter);
        
        SearchControls sc = new SearchControls();
        sc.setSearchScope(SearchControls.ONELEVEL_SCOPE);
      
        NamingEnumeration peopleEnum = peopleContext.search("", filter, sc);
        while(peopleEnum.hasMore()) {
            NameClassPair personName = (NameClassPair)peopleEnum.next();
            Attributes personAttrs = 
                peopleContext.getAttributes(personName.getName());
            Attribute cn = personAttrs.get("cn");
            Attribute sn = personAttrs.get("sn");
            Attribute phone = personAttrs.get("telephoneNumber");
        
            people.add(new Person((String)cn.get(), 
                                  (String)sn.get(), 
                                  (String)phone.get()));
         }
        
         peopleContext.close();
    
public voidsetFirstName(java.lang.String firstName)

        this.firstName = firstName;
    
public voidsetLastName(java.lang.String lastName)

        this.lastName = lastName;