[ 
https://issues.apache.org/jira/browse/DIRAPI-202?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14211923#comment-14211923
 ] 

Chris Harris commented on DIRAPI-202:
-------------------------------------

Hi, [[email protected]]

I switched Muppet back to my simpler Person object.  Muppet works for my method 
that executes a basic search and a basic search that uses an EntryMapper, just 
like the Person object.  I was swapping out Person with Muppet from your 
examples/tests for sanity checks.  I've switched back to using Person to not 
complicate things.

I'm on M26-SNAPSHOT now and still experiencing the issue.

I put the try/catch blocks in, but my JUnit test is still getting the following 
Stack Trace:
java.lang.RuntimeException: ERR_02002_FAILURE_ON_UNDERLYING_CURSOR Failure on 
underlying Cursor.
        at 
org.apache.directory.api.ldap.model.cursor.CursorIterator.next(CursorIterator.java:86)
        at 
org.apache.directory.ldap.client.template.LdapConnectionTemplate.search(LdapConnectionTemplate.java:668)
        at 
org.apache.directory.ldap.client.template.LdapConnectionTemplate.search(LdapConnectionTemplate.java:621)
        at 
com.baxter.mcoe.baxwho.ldap.LdapClient.searchLdapForCeoUsingTemplate(LdapClient.java:79)
        at 
com.baxter.mcoe.baxwho.ldap.LdapClientTest.testSearch(LdapClientTest.java:40)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
        at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
        at java.lang.reflect.Method.invoke(Method.java:611)
        at 
org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
        at 
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at 
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at 
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
        at 
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
        at 
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
        at 
org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:55)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at 
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
        at 
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at 
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
        at 
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
        at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
        at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
        at 
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: 
org.apache.directory.api.ldap.model.cursor.CursorLdapReferralException
        at 
org.apache.directory.ldap.client.api.EntryCursorImpl.get(EntryCursorImpl.java:164)
        at 
org.apache.directory.ldap.client.api.EntryCursorImpl.get(EntryCursorImpl.java:50)
        at 
org.apache.directory.api.ldap.model.cursor.CursorIterator.next(CursorIterator.java:80)
        ... 31 more


Here's the method that uses the LdapConnectionTemplate:
public Person searchLdapForCeoUsingTemplate() {
                LdapConnectionConfig config = new LdapConnectionConfig();
                config.setLdapHost( host );
                config.setLdapPort( port );
                config.setName( dn );
                config.setCredentials( pwd );

                DefaultLdapConnectionFactory factory = new 
DefaultLdapConnectionFactory( config );
                factory.setTimeOut( 30000 );

                // optional, values below are defaults
                GenericObjectPool.Config poolConfig = new 
GenericObjectPool.Config();
                poolConfig.lifo = true;
                poolConfig.maxActive = 8;
                poolConfig.maxIdle = 8;
                poolConfig.maxWait = -1L;
                poolConfig.minEvictableIdleTimeMillis = 1000L * 60L * 30L;
                poolConfig.minIdle = 0;
                poolConfig.numTestsPerEvictionRun = 3;
                poolConfig.softMinEvictableIdleTimeMillis = -1L;
                poolConfig.testOnBorrow = false;
                poolConfig.testOnReturn = false;
                poolConfig.testWhileIdle = false;
                poolConfig.timeBetweenEvictionRunsMillis = -1L;
                poolConfig.whenExhaustedAction = 
GenericObjectPool.WHEN_EXHAUSTED_BLOCK;

                LdapConnectionTemplate ldapConnectionTemplate = 
                    new LdapConnectionTemplate( new LdapConnectionPool(
                        new ValidatingPoolableLdapConnectionFactory( factory ), 
poolConfig ) );
                
                List<Person> allThePeople = ldapConnectionTemplate.search( 
                            searchBase, 
                            query, 
                            SearchScope.SUBTREE,
                            Person.getEntryMapper() );
                
                for (Person p : allThePeople) {
                        System.out.println(p.getFirstName());
                        System.out.println(p.getLastName());
                }
                
                return allThePeople.get(0);
        }


Here's my Person class:
public class Person {
        
        protected Person() {
                
        }
        
        private static final EntryMapper personEntryMapper = 
        new EntryMapper<Person>() {
                @Override
                public Person map( Entry entry ) throws LdapException {
                        try {
                                return new Person.Builder()
                                        .setFirstName(entry.get( "givenName" 
).getString())
                                        .setLastName(entry.get( "sn" 
).getString())
                                        .build();
                        } catch ( Exception e ) {
                System.out.println( "GOTCHA: " + e.getMessage() );
                e.printStackTrace();
                try {
                                        throw( e );
                                } catch (Exception e1) {
                                        // TODO Auto-generated catch block
                                        e1.printStackTrace();
                                }
            }
                        return null;
                }
        };
        
        public static EntryMapper<Person> getEntryMapper() {
        return personEntryMapper;
    }

        public static class Builder {
                private Person person;
                
                public Builder() {
                        this.person = new Person();
                }
                
                public Builder setFirstName(String firstName) {
                        this.person.firstName = firstName;
                        return this;
                }
                
                public Builder setLastName(String lastName) {
                        this.person.lastName = lastName;
                        return this;
                }

                public Person build() {
                        return this.person;
                }
        }
        
        private String firstName;
        private String lastName;
        
        public String getFirstName() {
                return firstName;
        }
        public void setFirstName(String firstName) {
                this.firstName = firstName;
        }
        public String getLastName() {
                return lastName;
        }
        public void setLastName(String lastName) {
                this.lastName = lastName;
        }
}


In case you need the other two methods (that work) for comparison, here they 
are:

public Entry searchLdapForCeo() {
                SearchCursor cursor = new SearchCursorImpl(null, 30000, 
TimeUnit.SECONDS);
        LdapConnection connection = new LdapNetworkConnection(host, port);
        Entry entry = null;
        try {
            connection.bind(dn, pwd);
            
            SearchRequest sr = new SearchRequestImpl();
            sr.setBase(new Dn(searchBase));
            StringBuilder sb = new StringBuilder(query);
            sr.setFilter(sb.toString());
            sr.setScope( SearchScope.SUBTREE );
            cursor = connection.search(sr);
            Response response;

            while (cursor.next() && cursor.isEntry()) {
                response = cursor.get();
                System.out.println(((SearchResultEntry)response).getEntry());
                entry = cursor.getEntry();
            }

            SearchResultDone done = cursor.getSearchResultDone();
        } catch (LdapException ex) {
            Logger.getLogger(LdapClient.class.getName()).log(Level.SEVERE, 
null, ex);
        } catch (CursorException ex) {
            Logger.getLogger(LdapClient.class.getName()).log(Level.SEVERE, 
null, ex);
        } finally {
            cursor.close();
            try {
                connection.close();
            } catch (IOException ex) {
                Logger.getLogger(LdapClient.class.getName()).log(Level.SEVERE, 
null, ex);
            }
        }
        return entry;
        }
        
        public Person searchLdapForCeoUsingHybridApproach() {
                SearchCursor cursor = new SearchCursorImpl(null, 30000, 
TimeUnit.SECONDS);
        LdapConnection connection = new LdapNetworkConnection(host, port);
        Entry entry = null;
        Person p = null;
        try {
            connection.bind(dn, pwd);
            
            SearchRequest sr = new SearchRequestImpl();
            sr.setBase(new Dn(searchBase));
            StringBuilder sb = new StringBuilder(query);
            sr.setFilter(sb.toString());
            sr.setScope( SearchScope.SUBTREE );
            cursor = connection.search(sr);
            Response response;

            while (cursor.next() && cursor.isEntry()) {
                response = cursor.get();
                System.out.println(((SearchResultEntry)response).getEntry());
                entry = cursor.getEntry();
                EntryMapper<Person> em = Person.getEntryMapper();
                p = em.map(entry);
            }

            SearchResultDone done = cursor.getSearchResultDone();
        } catch (LdapException ex) {
            Logger.getLogger(LdapClient.class.getName()).log(Level.SEVERE, 
null, ex);
        } catch (CursorException ex) {
            Logger.getLogger(LdapClient.class.getName()).log(Level.SEVERE, 
null, ex);
        } finally {
            cursor.close();
            try {
                connection.close();
            } catch (IOException ex) {
                Logger.getLogger(LdapClient.class.getName()).log(Level.SEVERE, 
null, ex);
            }
        }

        return p;
    }

> Can't get LdapConnectionTemplate working
> ----------------------------------------
>
>                 Key: DIRAPI-202
>                 URL: https://issues.apache.org/jira/browse/DIRAPI-202
>             Project: Directory Client API
>          Issue Type: Bug
>    Affects Versions: 1.0.0-M24
>         Environment: WAS 8.0 JDK, RAD 9.0.1, Windows 7 Enterprise Edition
>            Reporter: Chris Harris
>            Priority: Blocker
>              Labels: EntryMapper, LdapConnectionTemplate, documentation, 
> newbie
>
> Hi,
> I’ve been following the example code from Section 2.10 using v1.0.0-M24.  My 
> goal is to use an LdapConnectionTemplate to bind to AD, return a 
> response/cursor, and use an EntryMapper to map each cursor iteration’s value 
> to a Person object.
> I’m getting the following error message:
> ERR_02002_FAILURE_ON_UNDERLYING_CURSOR Failure on underlying Cursor
> I’m not sure what’s wrong at this point.  I have a basic query example 
> working that doesn’t use a template.
> I’m including my code, which contains the method with the basic query that 
> does work and the method with the query using the template that doesn’t work.
> Can you help me figure out why searchLdapForCeoUsingTemplate() is not working?
> Here’s LdapClient.java:
> {code}
> /**
> * 
>  */
> import java.io.IOException;
> import java.util.List;
> import java.util.concurrent.TimeUnit;
> import java.util.logging.Level;
> import java.util.logging.Logger;
> import org.apache.commons.pool.impl.GenericObjectPool;
> import org.apache.directory.api.ldap.model.cursor.CursorException;
> import org.apache.directory.api.ldap.model.cursor.SearchCursor;
> import org.apache.directory.api.ldap.model.entry.Entry;
> import org.apache.directory.api.ldap.model.exception.LdapException;
> import org.apache.directory.api.ldap.model.message.Response;
> import org.apache.directory.api.ldap.model.message.SearchRequest;
> import org.apache.directory.api.ldap.model.message.SearchRequestImpl;
> import org.apache.directory.api.ldap.model.message.SearchResultDone;
> import org.apache.directory.api.ldap.model.message.SearchResultEntry;
> import org.apache.directory.api.ldap.model.message.SearchScope;
> import org.apache.directory.api.ldap.model.name.Dn;
> import org.apache.directory.ldap.client.api.DefaultLdapConnectionFactory;
> import org.apache.directory.ldap.client.api.LdapConnection;
> import org.apache.directory.ldap.client.api.LdapConnectionConfig;
> import org.apache.directory.ldap.client.api.LdapConnectionPool;
> import org.apache.directory.ldap.client.api.LdapNetworkConnection;
> import org.apache.directory.ldap.client.api.PoolableLdapConnectionFactory;
> import org.apache.directory.ldap.client.api.SearchCursorImpl;
> import org.apache.directory.ldap.client.template.EntryMapper;
> import org.apache.directory.ldap.client.template.LdapConnectionTemplate;
> /**
> * @author Chris Harris
> *
> */
> public class LdapClient {
>                
>                public LdapClient() {
>                               
>                }
>                
>                private static final EntryMapper personEntryMapper = 
>                               new EntryMapper<Person>() {
>                                              @Override
>                                              public Person map( Entry entry ) 
> throws LdapException {
>                                                             return new 
> Person.Builder()
>                                                                            
> .setFirstName(entry.get( "givenName" ).getString())
>                                                                            
> .setLastName(entry.get( "sn" ).getString())
>                                                                            
> .build();
>                                              }
>                               };
>                               
>                public Person searchLdapForCeoUsingTemplate() {
>                               LdapConnectionConfig config = new 
> LdapConnectionConfig();
>                               config.setLdapHost( <my host> );
>                               config.setLdapPort( <my port> );
>                               config.setName( <my DN> );
>                               config.setCredentials( <my Password> );
>                               DefaultLdapConnectionFactory factory = new 
> DefaultLdapConnectionFactory( config );
>                               factory.setTimeOut( 30000 );
>                               // optional, values below are defaults
>                               GenericObjectPool.Config poolConfig = new 
> GenericObjectPool.Config();
>                               poolConfig.lifo = true;
>                               poolConfig.maxActive = 8;
>                               poolConfig.maxIdle = 8;
>                               poolConfig.maxWait = -1L;
>                               poolConfig.minEvictableIdleTimeMillis = 1000L * 
> 60L * 30L;
>                               poolConfig.minIdle = 0;
>                               poolConfig.numTestsPerEvictionRun = 3;
>                               poolConfig.softMinEvictableIdleTimeMillis = -1L;
>                               poolConfig.testOnBorrow = false;
>                               poolConfig.testOnReturn = false;
>                               poolConfig.testWhileIdle = false;
>                               poolConfig.timeBetweenEvictionRunsMillis = -1L;
>                               poolConfig.whenExhaustedAction = 
> GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
>                               LdapConnectionTemplate ldapConnectionTemplate = 
>                                   new LdapConnectionTemplate( new 
> LdapConnectionPool(
>                                       new PoolableLdapConnectionFactory( 
> factory ), poolConfig ) );
>                               
>                               List<Person> allThePeople = 
> ldapConnectionTemplate.search( 
>                                                  <my search base>, 
>                                                  <my query string>, 
>                                                  SearchScope.SUBTREE,
>                                                  personEntryMapper );
>                               
>                               for (Person p : allThePeople) {
>                                              
> System.out.println(p.getFirstName());
>                                              
> System.out.println(p.getLastName());
>                               }
>                               
>                               return allThePeople.get(0);
>                }
>                
>                public Entry searchLdapForCeo() {
>                               SearchCursor cursor = new 
> SearchCursorImpl(null, 30000, TimeUnit.SECONDS);
>         LdapConnection connection = new LdapNetworkConnection(<my host>, <my 
> port>);
>         Entry entry = null;
>         try {
>             connection.bind(<my DN>, <my password>);
>             
>             SearchRequest sr = new SearchRequestImpl();
>            sr.setBase(new Dn(<my search base>));
>             StringBuilder sb = new StringBuilder(<my query string>);
>             sr.setFilter(sb.toString());
>             sr.setScope( SearchScope.SUBTREE );
>             cursor = connection.search(sr);
>             Response response;
>             while (cursor.next() && cursor.isEntry()) {
>                 response = cursor.get();
>                 System.out.println(((SearchResultEntry)response).getEntry());
>                 entry = cursor.getEntry();
>             }
>             SearchResultDone done = cursor.getSearchResultDone();
>         } catch (LdapException ex) {
>             Logger.getLogger(LdapClient.class.getName()).log(Level.SEVERE, 
> null, ex);
>         } catch (CursorException ex) {
>             Logger.getLogger(LdapClient.class.getName()).log(Level.SEVERE, 
> null, ex);
>         } finally {
>             cursor.close();
>             try {
>                 connection.close();
>             } catch (IOException ex) {
>                 
> Logger.getLogger(LdapClient.class.getName()).log(Level.SEVERE, null, ex);
>             }
>         }
>         return entry;
>                }
> }
> {code}
> Here’s Person.java:
> {code}
> /**
> * 
>  */
> public class Person {
>        
>        protected Person() {
>              
>        }
>        public static class Builder {
>              private Person person;
>              
>              public Builder() {
>                     this.person = new Person();
>              }
>              
>              public Builder setFirstName(String firstName) {
>                     this.person.firstName = firstName;
>                     return this;
>              }
>              
>              public Builder setLastName(String lastName) {
>                     this.person.lastName = lastName;
>                     return this;
>              }
>              public Person build() {
>                     return this.person;
>              }
>        }
>        
>        private String firstName;
>        private String lastName;
>        
>        public String getFirstName() {
>              return firstName;
>        }
>        public void setFirstName(String firstName) {
>              this.firstName = firstName;
>        }
>        public String getLastName() {
>              return lastName;
>        }
>        public void setLastName(String lastName) {
>              this.lastName = lastName;
>        }
> }
> {code}
> Here’s my JUnit test:
> {code}
> /**
> * 
>  */
> import static org.junit.Assert.*;
> import org.junit.After;
> import org.junit.AfterClass;
> import org.junit.Before;
> import org.junit.BeforeClass;
> import org.junit.Test;
> public class LdapClientTest {
>        private static LdapClient ldapClient;
>        
>        @BeforeClass
>        public static void setUpBeforeClass() throws Exception {
>              ldapClient = new LdapClient();
>        }
>        @Before
>        public void setUp() throws Exception {
>        }
>        @Test
>        public void testSearchLdapForCeo() {
>              assertNotEquals(null, ldapClient.searchLdapForCeo());
>        }
>        
>        @Test
>        public void testSearchLdapForCeoUsingLdapSearchTemplate() {
>              assertNotEquals(null, 
> ldapClient.searchLdapForCeoUsingTemplate());
>        }
>        
>        @After
>        public void tearDown() throws Exception {
>        }
>        
>        @AfterClass
>              public static void tearDownAfterClass() throws Exception {
>        }
> }
> {code}



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to