FileDocCategorySizeDatePackage
TestLDAP.javaAPI DocExample2137Sun Jul 06 01:05:38 BST 2003antipatterns

TestLDAP

public class TestLDAP extends Object

Fields Summary
static final String
ldapServerName
static final String
rootdn
static final String
rootpass
static final String
rootContext
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)

        
               
                // set up environment to access the server
                
                Properties env = new Properties();
                
                env.put( Context.INITIAL_CONTEXT_FACTORY,
                         "com.sun.jndi.ldap.LdapCtxFactory" );
                env.put( Context.PROVIDER_URL, "ldap://" + ldapServerName + "/" + rootContext );
                env.put( Context.SECURITY_PRINCIPAL, rootdn );
                env.put( Context.SECURITY_CREDENTIALS, rootpass );
                
                try {
                    // obtain initial directory context using the environment
                   DirContext ctx = new InitialDirContext(env);
                   DirContext people = (DirContext)ctx.lookup("ou=people");
                   
                   BasicAttributes mandatory = new BasicAttributes("cn", "schlomo");
                   mandatory.put("objectClass", "person");
                   mandatory.put("sn", "testa2");
                   mandatory.put("telephoneNumber", "555-1212");
                   
                   people.createSubcontext("cn=schlomo", mandatory);
                   
                   NamingEnumeration ne = people.list("");
                   while(ne.hasMore()) {
                        NameClassPair ncp = (NameClassPair)ne.next();
                        System.out.println("name: " + ncp.getName());
                   }
                   
                } catch ( NameAlreadyBoundException nabe ) {
                        System.err.println( "value has already been bound!" );
                        nabe.printStackTrace();
                } catch ( Exception e ) {
                        System.err.println( e );
                }