Announcement Announcement Module
Collapse
No announcement yet.
Persistent search with spring ldap Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Persistent search with spring ldap

    I would like to do a persistent search with spring ldap. In other words, I would like to be notified when a change occurs in a specified subtree. I can do this function with novell ldap kit. Can it be done with spring ldap? Please give some code examples it is possible.

    Thank you.

  • #2
    Persistent search in JNDI is based on event listeners that are registered using an EventDirContext. You could try something like this:

    Code:
       public void testPersistentSearch() throws Exception {
          final SearchControls searchControls = new SearchControls();
          searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
    
          ContextExecutor contextExecutor = new ContextExecutor() {
             public Object executeWithContext(DirContext ctx) throws NamingException {
                EventDirContext ectx = (EventDirContext) ctx.lookup("");
                ectx.addNamingListener("", "(cn=*)", searchControls, new MyEventListener("listener_1"));
                return null;
             }
          };
          ldapTemplate.executeReadOnly(contextExecutor);
    
          // test the listener
          ldapTemplate.unbind("cn=Some Person99,ou=company1,c=Sweden");
       }
    where MyEventlistener looks like this:

    Code:
       /**
        * MyEventlistener class An instance of this class is registered with an
        * EventDirContext object. The registered instance's NamespaceChangeListener
        * interface methods are called when a pertinent event occurs.
        */
       private static class MyEventListener implements NamespaceChangeListener, ObjectChangeListener {
          private String id;
    
          public MyEventListener(String id) {
             this.id = id;
          }
    
          public void objectAdded(NamingEvent evt) {
             System.out.println("\n\n" + id + ">>> object added event. Object Name: " + evt.getNewBinding().getName());
          }
    
          public void objectRemoved(NamingEvent evt) {
             System.out.println("\n\n" + id + ">>> object removed event. Object Name: " + evt.getOldBinding().getName());
          }
    
          public void objectRenamed(NamingEvent evt) {
             System.out.println("\n\n" + id + ">>> object renamed event. New name: " + evt.getNewBinding().getName()
                   + " Old name: " + evt.getOldBinding().getName());
          }
    
          public void objectChanged(NamingEvent evt) {
             System.out.println("\n\n" + id + ">>> object changed event. Object name: " + evt.getNewBinding().getName());
          }
    
          public void namingExceptionThrown(NamingExceptionEvent evt) {
             System.out.println("\n\n" + id + ">>> Listener received a naming exception");
             evt.getException().printStackTrace();
          }
       }

    Comment


    • #3
      Thank you.

      I tried this code, but I have a runtime cast problem:

      java.lang.ClassCastException: org.springframework.ldap.core.DirContextAdapter cannot be cast to javax.naming.event.EventDirContext
      at LdapConnectorTest$1.executeWithContext(LdapConnect orTest.java:27)
      at org.springframework.ldap.core.LdapTemplate.execute WithContext(LdapTemplate.java:784)
      at org.springframework.ldap.core.LdapTemplate.execute ReadOnly(LdapTemplate.java:771)
      at LdapConnectorTest.testGetAllAgentNames(LdapConnect orTest.java:40)
      at LdapConnectorTest.main(LdapConnectorTest.java:81)

      For the code:

      final SearchControls searchControls = new SearchControls();
      searchControls.setSearchScope(SearchControls.SUBTR EE_SCOPE);

      ContextExecutor contextExecutor = new ContextExecutor() {
      public Object executeWithContext(DirContext ctx) throws NamingException {
      EventDirContext ectx = (EventDirContext) ctx.lookup("");
      ectx.addNamingListener("", "(objectClass=*)", searchControls, new MyEventListener("listener_1"));
      return null;
      }
      };

      The problem line is:

      EventDirContext ectx = (EventDirContext) ctx.lookup("");

      What am I doing wrong?

      Comment


      • #4
        By default, all results are converted to a DirContextAdapter using the dirObjectFactory property of the ContextSource. You need to disable that, which can be done in XML using the null element:

        Code:
        <bean id="contextSource" 
              class="org.springframework.ldap.core.support.LdapContextSource" >
           ...
           <property name="dirObjectFactory">
              <null />
           </property>
        </bean>

        Comment


        • #5
          Is there a way to do it in Java?

          Comment


          • #6
            Code:
            ((AbstractContextSource) ldapTemplate.getContextSource()).setDirObjectFactory(null);

            Comment


            • #7
              It works.

              Thank you.

              Comment


              • #8
                Get changed attributes

                How can I extract changed attributes in changed entries?

                Comment

                Working...
                X