Registering for Object Changes in the Directory : Directory « JNDI LDAP « Java






Registering for Object Changes in the Directory

 


import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.directory.SearchControls;
import javax.naming.event.EventDirContext;
import javax.naming.event.NamingEvent;
import javax.naming.event.NamingExceptionEvent;
import javax.naming.event.NamingListener;
import javax.naming.event.ObjectChangeListener;

public class Main {
  public static void main(String[] argv) throws Exception {
    String url = "ldap://localhost/o=JNDITutorial";
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, url);
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "userDN");
    env.put(Context.SECURITY_CREDENTIALS, "secret");

    EventDirContext ctx = (EventDirContext) (new InitialContext(env).lookup("ou=People"));

    NamingListener listener = new SampleObjListener();

    SearchControls ctls = new SearchControls();
    ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);

    String filter = "(mail=*)";

    ctx.addNamingListener("cn=YourName", filter, ctls, listener);
  }
}

class SampleObjListener implements ObjectChangeListener {
  public SampleObjListener() {
  }

  public void objectChanged(NamingEvent evt) {
    System.out.println(evt.getNewBinding().getName());
    System.out.println(evt.getOldBinding().getName());
  }

  public void namingExceptionThrown(NamingExceptionEvent evt) {
    System.out.println(evt.getException());
  }
}

   
  








Related examples in the same category

1.Creating an Initial Context to a Directory
2.Reading an Object's Attributes from the Directory
3.Modifying an Object's Attributes in the Directory
4.Creating a Directory Entry
5.Adding a Binding with Attributes to the Directory
6.Performing a Basic Directory Search
7.Searching the Directory by Using a Search Filter
8.Searching a Subtree in the Directory
9.Cancelling a Directory Search
10.Getting an Object's Schema from the Directory
11.Getting an Attribute's Schema from the Directory
12.Authenticating to the Directory
13.Registering for Namespace Changes in the Directory