Announcement Announcement Module
No announcement yet.
Simple Test Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Simple Test

    I'm trying to create a simple test to get my feet wet with LdapTemplate but keep running into issues. I'm sure it is something simple. Any help would be great.

    Here is my simple test

    public void testLookup() throws Exception {
    //set-up Template in Java code

    LdapContextSource source = new LdapContextSource();
    source.setDirObjectFactory(DefaultDirObjectFactory .class);
    source.setContextFactory(com.sun.jndi.ldap.LdapCtx Factory.class);

    LdapTemplate template = new LdapTemplate(source);

    //do look-up
    PersonContextMapper mapper = new PersonContextMapper();
    Object result = _template.lookup("uid=admin,ou=Users", mapper);


    private static class PersonContextMapper
    implements ContextMapper {

    public Object mapFromContext(Object ctx) {
    DirContextAdapter context = (DirContextAdapter) ctx;
    String result = context

    System.out.println("result = " + result + ", ctx " + ctx);

    return result;

    With the above code I'm getting a "need to specifly javax.namingfactory.initial" error. If I uncomment out the setCacheEnviroment I get an emptry result back

    result = null, ctx dn=uid=admin, ou=Users {}

    and if I do a System.setProperty(Context.INITIAL_CONTEXT_FACTORY ,"com.sun.jndi.ldap.LdapCtxFactory" ); first I get a Connection refused exception. Note it the stack trace lists localhost:389 but I set the ip to something else "source.setUrl(ldap://ip)"

    org.springframework.dao.DataRetrievalFailureExcept ion: Unable to communicate with LDAP server; nested exception is javax.naming.CommunicationException: localhost:389 [Root exception is Connection refused: connect]
    javax.naming.CommunicationException: localhost:389 [Root exception is Connection refused: connect]
    at com.sun.jndi.ldap.Connection.<init>(Unknown Source)
    at com.sun.jndi.ldap.LdapClient.<init>(Unknown Source)
    at com.sun.jndi.ldap.LdapClient.getInstance(Unknown Source)
    at com.sun.jndi.ldap.LdapCtx.connect(Unknown Source)
    at com.sun.jndi.ldap.LdapCtx.<init>(Unknown Source)
    at com.sun.jndi.ldap.LdapCtxFactory.getInitialContext (Unknown Source)
    at javax.naming.spi.NamingManager.getInitialContext(U nknown Source)
    at javax.naming.InitialContext.getDefaultInitCtx(Unkn own Source)
    at javax.naming.InitialContext.init(Unknown Source)
    at javax.naming.ldap.InitialLdapContext.<init>(Unknow n Source)
    at irContextInstance(
    at createContext(
    at getReadOnlyContext(
    at net.sf.ldaptemplate.LdapTemplate.executeReadOnly(L
    at net.sf.ldaptemplate.LdapTemplate.lookup(LdapTempla
    at ction.testLookup( )
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest( )
    at junit.framework.TestCase.runBare( )
    at junit.framework.TestResult$1.protect(TestResult.ja va:106)
    at junit.framework.TestResult.runProtected(TestResult .java:124)
    at )
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(
    Caused by: Connection refused: connect
    at Method)
    at Source)
    at Source)
    at Source)
    at Source)
    at Source)
    at Source)
    at<init>(Unknown Source)
    at<init>(Unknown Source)
    at com.sun.jndi.ldap.Connection.createSocket(Unknown Source)
    ... 29 more

    This looks like a great tool, I just need to get over this stumbling block.

  • #2
    Got it.

    nevermind I figured it out based on


    • #3
      Hello mavmh,

      I think problem is that you don't specify port to connect with LDAP server.

      I hope that my reply can help you.