ITeye博客

LDAP监听器测试例子

kylixs · 2009-07-15 17:01 · Java · Markdown

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

评论(3)

  1. #3 楼 · zhq502502 · 2018-11-30 15:50

    10年前的文章,现在早就不支持了,这是要害多少人那。 https://stackoverflow.com/questions/12383878/how-to-create-an-objectchangelistener-with-openldap

  2. #2 楼 · Smith小可爱 · 2017-11-01 10:25

    NamespaceChangeListener Exception javax.naming.OperationNotSupportedException: ; remaining name '' at com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3196) at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:3082) at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2888) at com.sun.jndi.ldap.AbstractLdapNamingEnumeration.getNextBatch(AbstractLdapNamingEnumeration.java:148) at com.sun.jndi.ldap.AbstractLdapNamingEnumeration.hasMoreImpl(AbstractLdapNamingEnumeration.java:217) at com.sun.jndi.ldap.AbstractLdapNamingEnumeration.hasMore(AbstractLdapNamingEnumeration.java:189) at com.sun.jndi.ldap.NamingEventNotifier.run(NamingEventNotifier.java:135) at java.lang.Thread.run(Thread.java:745) ObjectChangeListener Exception javax.naming.OperationNotSupportedException: ; remaining name '' at com.sun.jndi.ldap.LdapCtx.mapErrorCode(LdapCtx.java:3196) at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:3082) at com.sun.jndi.ldap.LdapCtx.processReturnCode(LdapCtx.java:2888) at com.sun.jndi.ldap.AbstractLdapNamingEnumeration.getNextBatch(AbstractLdapNamingEnumeration.java:148) at com.sun.jndi.ldap.AbstractLdapNamingEnumeration.hasMoreImpl(AbstractLdapNamingEnumeration.java:217) at com.sun.jndi.ldap.AbstractLdapNamingEnumeration.hasMore(AbstractLdapNamingEnumeration.java:189) at com.sun.jndi.ldap.NamingEventNotifier.run(NamingEventNotifier.java:135) at java.lang.Thread.run(Thread.java:745)

  3. #1 楼 · rukawajw · 2015-05-23 14:20

    报这样的错,怎么处理 Listener to LDAP server ...

    NamespaceChangeListener Exception javax.naming.NamingException: LDAP response read timed out, timeout used:-1ms. at com.sun.jndi.ldap.Connection.readReply(Unknown Source) at com.sun.jndi.ldap.LdapClient.getSearchReply(Unknown Source) at com.sun.jndi.ldap.LdapClient.getSearchReply(Unknown Source) at com.sun.jndi.ldap.LdapCtx.getSearchReply(Unknown Source) at com.sun.jndi.ldap.LdapNamingEnumeration.getNextBatch(Unknown Source) at com.sun.jndi.ldap.LdapNamingEnumeration.hasMoreImpl(Unknown Source) at com.sun.jndi.ldap.LdapNamingEnumeration.hasMore(Unknown Source) at com.sun.jndi.ldap.NamingEventNotifier.run(Unknown Source) at java.lang.Thread.run(Unknown Source) ObjectChangeListener Exception javax.naming.NamingException: LDAP response read timed out, timeout used:-1ms. at com.sun.jndi.ldap.Connection.readReply(Unknown Source) at com.sun.jndi.ldap.LdapClient.getSearchReply(Unknown Source) at com.sun.jndi.ldap.LdapClient.getSearchReply(Unknown Source) at com.sun.jndi.ldap.LdapCtx.getSearchReply(Unknown Source) at com.sun.jndi.ldap.LdapNamingEnumeration.getNextBatch(Unknown Source) at com.sun.jndi.ldap.LdapNamingEnumeration.hasMoreImpl(Unknown Source) at com.sun.jndi.ldap.LdapNamingEnumeration.hasMore(Unknown Source) at com.sun.jndi.ldap.NamingEventNotifier.run(Unknown Source) at java.lang.Thread.run(Unknown Source)

评论功能维护中,当前仅展示历史评论。