LDAP监听器测试例子

LDAP被广泛用来存储企业基础数据,如人员、组织机构、设备等等,而且LDAP通常作为集中的数据源,被各个业务系统使用。员工入职,需要及时为员工创建各个系统的帐号。目前基本上每个公司都有好几套业务系统,可能有很多个管理员,人多了麻烦事就来了……如果当LDAP服务器数据变化时,实时响应,执行同步或者其他一些操作,那就可以极大提高企业信息化水平,减少不必要的维护成本。说了那么多废话,下面才是正题,一个简单的ldap监听器例子,可以响应ldap服务器数据的变化,那样就可以做我们想要做的事情了。

 

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.naming.event.EventContext;
import javax.naming.event.NamespaceChangeListener;
import javax.naming.event.NamingEvent;
import javax.naming.event.NamingExceptionEvent;
import javax.naming.event.NamingListener;
import javax.naming.event.ObjectChangeListener;
import javax.naming.ldap.UnsolicitedNotificationEvent;
import javax.naming.ldap.UnsolicitedNotificationListener;

/**
 * LDAP 事件监听器测试
 */
class LDAPListenerTest {

    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");
        env.put(Context.SECURITY_PRINCIPAL, "cn=Manager");
        env.put(Context.SECURITY_CREDENTIALS, "secret");
        try {
            // Get event context for registering listener
            String baseDN = "ou=07,ou=csg,o=serc,c=cn";
            EventContext ctx = (EventContext) (new InitialContext(env).lookup(baseDN));
            // Create listener
            NamingListener unsolListener = new UnsolicitedNotificationListener() {

                public void notificationReceived(UnsolicitedNotificationEvent evt) {
                    System.out.println("received: " + evt + ",notification:" + evt.getNotification());
                }

                public void namingExceptionThrown(NamingExceptionEvent evt) {
                    System.out.println(">>> UnsolListener got an exception");
                    evt.getException().printStackTrace();
                }
            };
            NamingListener namespaceListener = new NamespaceChangeListener() {

                public void objectAdded(NamingEvent evt) {
                    System.out.println("objectAdded: " + evt.getOldBinding() + "\n=> " + evt.getNewBinding());
                    System.out.println("\tchangeInfo: " + evt.getChangeInfo());
                }

                public void objectRemoved(NamingEvent evt) {
                    System.out.println("objectRemoved: " + evt.getOldBinding() + "\n=> " + evt.getNewBinding());
                    System.out.println("\tchangeInfo: " + evt.getChangeInfo());
                }

                public void objectRenamed(NamingEvent evt) {
                    System.out.println("objectRenamed: " + evt.getOldBinding() + "\n=> " + evt.getNewBinding());
                    System.out.println("\tchangeInfo: " + evt.getChangeInfo());
                }

                public void namingExceptionThrown(NamingExceptionEvent evt) {
                    System.err.println(">>>NamespaceChangeListener Exception");
                    evt.getException().printStackTrace();
                }
            };
            NamingListener objectListener = new ObjectChangeListener() {

                public void objectChanged(NamingEvent evt) {
                    System.out.println("objectChanged: " + evt.getOldBinding() + "\n\t=> " + evt.getNewBinding());
                    System.out.println("\tchangeInfo: " + evt.getChangeInfo());
                }

                public void namingExceptionThrown(NamingExceptionEvent evt) {
                    System.err.println(">>>ObjectChangeListener Exception");
                    evt.getException().printStackTrace();
                }
            };
            // Register listener with context (all targets equivalent)
            ctx.addNamingListener("", EventContext.SUBTREE_SCOPE, unsolListener);
            ctx.addNamingListener("", EventContext.SUBTREE_SCOPE, namespaceListener);
            ctx.addNamingListener("", EventContext.SUBTREE_SCOPE, objectListener);
            System.out.println("Listener to LDAP server ...");
            // Wait 1 minutes for listener to receive events
            try {
                Thread.sleep(60000 * 60);
            } catch (InterruptedException e) {
                System.out.println("sleep interrupted");
            }
            // Not strictly necessary if we're going to close context anyhow
            ctx.removeNamingListener(unsolListener);
            // Close context when we're done
            ctx.close();
            System.out.println("Remove Listener ...");
        } catch (NamingException e) {
            e.printStackTrace();
        }
    }
}

 

测试环境是OpenDS2.0,如有什么问题欢迎联系我,[email protected]

你可能感兴趣的:(Java)