Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring LDAP - Creation of LdapTemplate in standalone java program - Using Spring LDAP as CDI Resource

I am trying to construct a LdapTemplate object of using spring data.

 public class LDAPTemplate {

        public static void main(String[] args) {
            LdapContextSource lcs = new LdapContextSource();
            lcs.setUrl("ldap://localhost:389/");
            lcs.setUserDn("cn=Manager, dc=example, dc=com");
            lcs.setPassword("secret1");
            lcs.setDirObjectFactory(DefaultDirObjectFactory.class);
            LdapTemplate ldap = new LdapTemplate(lcs);
            ldap.lookup("cn=aaa");

        }

    }

I wanted to know is that the right way to instantiate ldap template object. Because when I perform a lookup, it throws NPE.

I am trying to use LDAP Spring in CDI context without using spring at all. If you have pointers on that would be nice. Does Spring LDAP is dependent on spring?

like image 327
VirtualLogic Avatar asked Mar 16 '14 10:03

VirtualLogic


2 Answers

LdapContextSource is InitializingBean so you need to call afterPropertiesSet...

And the JavaDoc:

When using implementations of this class outside of a Spring Context it is necessary to call afterPropertiesSet() when all properties are set, in order to finish up initialization.

like image 52
Pavel Horal Avatar answered Nov 15 '22 15:11

Pavel Horal


Correct Code

public class LDAPTemplate {
    public static void main(String[] args) {
        LdapContextSource lcs = new LdapContextSource();
        lcs.setUrl("ldap://localhost:389/");
        lcs.setUserDn("cn=Manager, dc=example, dc=com");
        lcs.setPassword("secret1");
        lcs.setDirObjectFactory(DefaultDirObjectFactory.class);
        lcs.afterPropertiesSet();
        LdapTemplate ldap = new LdapTemplate(lcs);
        ldap.lookup("cn=aaa");

    }
}
like image 42
VirtualLogic Avatar answered Nov 15 '22 16:11

VirtualLogic