blogger templates blogger widgets
This is part of a list of blog posts.
To browse the contents go to

Events and Listeners

A listener in the JNDI is represented by the NamingListener interface.

The NamingListener interface not only serves as the root interface but also specifies how a registered listener is to be notified of errors. It defines a single method: namingExceptionThrown(). This method is invoked by the service provider when an error occurs while the provider is collecting data for generating events that the listener is seeking. For example, the server might have gone offline or cannot collect any more data in that part of the directory.

NamespaceChangeListener handles events that affect the namespace, including the addition, removal, and renaming of an object.
ObjectChangeListener handles events that affect an object's contents, for example, if an object's binding has been replaced with another or one of an object's attributes has been removed or replaced.

Here is a sample from oracle tutorial:
import javax.naming.event.NamespaceChangeListener;
import javax.naming.event.ObjectChangeListener;
import javax.naming.event.NamingEvent;
import javax.naming.event.NamingExceptionEvent;
import java.util.Hashtable;

/**
  * A sample NamespaceChangeListener and ObjectChangeListener 
  */
public class SampleListener 
implements NamespaceChangeListener, ObjectChangeListener {
    private String id;

    public SampleListener(String id) {
 this.id = id;
    }

    public void objectAdded(NamingEvent evt) {
 System.out.println(id + ">>> added: " + evt.getNewBinding());
    }
    public void objectRemoved(NamingEvent evt) {
 System.out.println(id + ">>> removed: " + evt.getOldBinding());
    }

    public void objectRenamed(NamingEvent evt) {
 System.out.println(id + ">>> renamed: " + evt.getNewBinding() + " from " +
     evt.getOldBinding());
    }

    public void objectChanged(NamingEvent evt) {
 System.out.println(id + ">>> object changed: " + evt.getNewBinding() +
     " from " + evt.getOldBinding());
    }

    public void namingExceptionThrown(NamingExceptionEvent evt) {
 System.out.println(id + ">>> SampleNCListener got an exception");
 evt.getException().printStackTrace();
    }
}

To receive event notifications, a listener registers with an event source.
In the JNDI, the event sources implement either the EventContext or EventDirContext interface. To get an event source, you must look it up using the naming/directory service. That is, you perform a lookup() on an object and then cast the result to an EventContext or EventDirContext. Whether a context supports either of these interfaces is optional. A context that supports neither does not support event notification.

Below is an example for registering Namespace change listener.

import javax.naming.event.NamespaceChangeListener;
import javax.naming.event.NamingEvent;
import javax.naming.event.NamingExceptionEvent;
import java.util.Hashtable;

/**
  * A sample NamespaceChangeListener. 
  */
public class SampleNCListener implements NamespaceChangeListener {
    private String id;

    public SampleNCListener(String id) {
 this.id = id;
    }

    public void objectAdded(NamingEvent evt) {
 System.out.println(id + ">>> added: " + evt.getNewBinding());
    }
    public void objectRemoved(NamingEvent evt) {
 System.out.println(id + ">>> removed: " + evt.getOldBinding());
    }

    public void objectRenamed(NamingEvent evt) {
 System.out.println(id + ">>> renamed: " + evt.getNewBinding() + " from " +
     evt.getOldBinding());
    }

    public void namingExceptionThrown(NamingExceptionEvent evt) {
 System.out.println(id + ">>> SampleNCListener got an exception");
 evt.getException().printStackTrace();
    }
}


Test class,

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

/**
  * Demonstrates how to register a namespace change listener.
*/
class RegNSChanges {
    public static void main(String[] args) {

 // Set up environment for creating initial context
 Hashtable env = new Hashtable(11);
 env.put(Context.INITIAL_CONTEXT_FACTORY, 
     "com.sun.jndi.ldap.LdapCtxFactory");
 env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDItutorial");

 try {
     // Get event context for registering listener
     EventContext ctx = (EventContext)
  (new InitialContext(env).lookup("ou=People"));

     // Create listener
     NamingListener listener = new SampleNCListener("nc1");

     // Register listener for namespace change events
     ctx.addNamingListener("ou=Objects,cn=Rosanna Lee", 
  EventContext.ONELEVEL_SCOPE, listener);

     // Create a separate to make some changes
     new Updater(env, "ou=Objects, cn=Rosanna Lee, ou=People").start();

     // Wait 1 minutes for listener to receive events
     try {
  Thread.sleep(60000);
     } catch (InterruptedException e) {
  System.out.println("sleep interrupted");
     }

     // Not strictly necessary if we're going to close context anyhow
     ctx.removeNamingListener(listener);

     // Close context when we're done
     ctx.close();

 } catch (NamingException e) {
     e.printStackTrace();
 }
    }

    /**
     * Helper thread that updates the namespace.
     */
    static class Updater extends Thread {
 private Hashtable env;
 private String target;

 Updater(Hashtable env, String target) {
     super();
     this.env = (Hashtable)env.clone();
     this.target = target;
     setDaemon(true);  // non-user thread
 }

 public void run() {
     try {
  // Get target context in which to make changes
  Context ctx = (Context)new InitialContext(env).lookup(target);

  // Add an entry to the namespace
  ctx.bind("cn=TestObject", "How are you?");

  // Rename entry 
  ctx.rename("cn=TestObject", "cn=TestingObj");

  // Remove entry
  ctx.unbind("cn=TestingObj");

  // Close context when we're done
  ctx.close();
     } catch (NamingException e) {
  System.out.println("Updater failed");
  e.printStackTrace(System.out);
     }
 }
    }
}

No comments:

Post a Comment