Results 1 to 8 of 8

Thread: Persistent search with spring ldap

  1. #1
    Join Date
    Jun 2008
    Posts
    8

    Default 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. #2
    Join Date
    Jul 2005
    Location
    Helsingborg, Sweden
    Posts
    504

    Default

    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();
          }
       }
    Ulrik Sandberg
    Jayway (www.jayway.com)
    Spring LDAP project member

  3. #3
    Join Date
    Jun 2008
    Posts
    8

    Default

    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?

  4. #4
    Join Date
    Jul 2005
    Location
    Helsingborg, Sweden
    Posts
    504

    Default

    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>
    Ulrik Sandberg
    Jayway (www.jayway.com)
    Spring LDAP project member

  5. #5
    Join Date
    Jun 2008
    Posts
    8

    Default

    Is there a way to do it in Java?

  6. #6
    Join Date
    Jul 2005
    Location
    Helsingborg, Sweden
    Posts
    504

    Default

    Code:
    ((AbstractContextSource) ldapTemplate.getContextSource()).setDirObjectFactory(null);
    Ulrik Sandberg
    Jayway (www.jayway.com)
    Spring LDAP project member

  7. #7
    Join Date
    Jun 2008
    Posts
    8

    Default

    It works.

    Thank you.

  8. #8
    Join Date
    Jun 2008
    Posts
    8

    Default Get changed attributes

    How can I extract changed attributes in changed entries?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •