Hello,
I've created my first LDAP test based on the Spring LDAP manual using version 1.2.1, but I can't seem to get it going.
Here is my code:
import java.util.List;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import org..ldap.core.AttributesMapper;
import org..ldap.core.LdapTemplate;
public class LdapConnector {
private LdapTemplate ldapTemplate;
public void setLdapTemplate(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
}
public List getAllPersonNames() {
return ldapTemplate.search(quot;quot;, quot;(objectClass=myObjectClass)quot;, new AttributesMapper() {
public Object mapFromAttributes(Attributes attrs) throws NamingException {
return attrs.get(quot;cnquot;).get();
}
});
}
}Here is my test:
import org.apache.log4j.Logger;
import org.junit.Test;
import org..ldap.core.LdapTemplate;
import org..ldap.core.support.LdapContextS ource;
public class LdapConnectorTest {
private static final Logger log = Logger.getLogger(LdapConnectorTest.class);
@Test
public void testGetAllPersonNames() throws Exception {
LdapContextSource ctxt = new LdapContextSource();
ctxt.setucl(quot;ldap--myServer:1389quot;);
ctxt.setBase(quot;o=baseOrgquot;);
ctxt.setUserDn(quot;cn=manager,o=baseOrgquot;);
ctxt.setPassword(quot;myPasswordquot;);
LdapTemplate template = new LdapTemplate(ctxt);
LdapConnector testClass = new LdapConnector();
testClass.setLdapTemplate(template);
log.debug(quot;testingquot;);
for (Object person : testClass.getAllPersonNames()) {
log.debug(quot erson: quot;+ person);
}
}
}
and Here is the stack trace:
testGetAllPersonNames(de.proactiv.civ.alg.security .LdapConnectorTest) Time elapsed: 0.125 sec lt;lt;lt; ERROR!
java.lang.NullPointerException
at java.util.Hashtable.lt;initgt;(Hashtable.java:200)
at org..ldap.core.support.AbstractCont extSource.getAuthenticatedEnv(AbstractContextSourc e.java:449)
at org..ldap.core.support.AbstractCont extSource.getReadOnlyContext(AbstractContextSource .java:107)
at org..ldap.core.LdapTemplate.search( LdapTemplate.java:266)
at org..ldap.core.LdapTemplate.search( LdapTemplate.java:234)
at org..ldap.core.LdapTemplate.search( LdapTemplate.java:548)
at org..ldap.core.LdapTemplate.search( LdapTemplate.java:532)
at org..ldap.core.LdapTemplate.search( LdapTemplate.java:383)
at org..ldap.core.LdapTemplate.search( LdapTemplate.java:406)
at org..ldap.core.LdapTemplate.search( LdapTemplate.java:424)
at myPackage.LdapConnector.getAllPersonNames(LdapConn ector.java:20)I have no problems connecting to the server and performing queries locally using LDAP Administrator.
Does anyone have an idea what I'm doing wrong?
Any suggestions or ideas would be very helpful.
Typically you'll not create the ContextSource programmatically. If you do, make sure you call afterPropertiesSet() to ensure it's properly initialized.
That was it. It worked right away.
Strange though, I thought the Spring container was always transparent with how it called methods so tests like this could be easily wired from the outside just by looking at the context file. Apparently I didn't read the container manual carefully enough.
Thanks very much for the help.
Well, Spring provides a number of hook methods that will automatically be called by the container. afterPropertiesSet() is one of those. Honestly I agree that this is not an ideal setup, and I'm thinking about redoing this so that it's automatically initialized on first use instead. |