欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

openDS监听器

程序员文章站 2022-05-16 22:10:34
...

-------------------------------------------------------------备忘-------------------------------------------------------------------------

/**  
 * 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=root");   
        env.put(Context.SECURITY_CREDENTIALS, "root");   
        try {   
            // Get event context for registering listener   
           // String baseDN = "ou=07,ou=csg,o=serc,c=cn";   
        	String baseDN = "dc=example,dc=com";   
            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();   
        }   
    }   
}  

 -------------------------------------------------------程序来源网络 但是都经过本人测试---------------------------------------------------------------------

 

上一篇: vue3.0 vite安装

下一篇: Watch侦听器