论坛首页 Java企业应用论坛

LDAP监听器测试例子

浏览 3008 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-07-15  

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<String, Object> env = new Hashtable<String, Object>(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,如有什么问题欢迎联系我,kylixs@qq.com

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics