Reputation: 8903
I was trying to implement a program to do JNDI lookup for LDAP. I saw there is open source LDAP from Apache viz: apacheds-2.0.0-M20
The below is the program that i wrote:
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import javax.naming.directory.InitialDirContext;
class JndiLDAPLookup {
public static void main(String[] args) throws Exception {
Context ctx = null;
Object obj = null;
try {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.SECURITY_AUTHENTICATION,"simple");
env.put(Context.SECURITY_PRINCIPAL,"uid=admin,ou=system");
env.put(Context.SECURITY_CREDENTIALS,"secret");
env.put(Context.PROVIDER_URL,"ldap://127.0.0.1:10389");
ctx = new InitialDirContext(env);
obj = ctx.lookup("uid=admin,ou=system");
System.out.println("Connection Successful.");
} catch(NamingException nex){
System.out.println("LDAP Connection: FAILED");
nex.printStackTrace();
}
}
}
The above program successfully worked, however i have got some doubts.
The service provider is LDAP --> apacheds-2.0.0-M20 (directory service).
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
Does this tell to use "com.sun.jndi.ldap.LdapCtxFactory" as the LDAP SPI service API?
If this is correct, then isn't LDAP SPI service supposed to be provided by the Apache itself, because I have used the LDAP from Apache?
How "com.sun.jndi.ldap.LdapCtxFactory" is working with Apache DS? "com.sun.jndi.ldap.LdapCtxFactory" is the implementation from Sun/Oracle. Does this mean that any SPI works with any vendor's service provider (in this case the service provider is from Apache)?
Upvotes: 1
Views: 402
Reputation: 310964
The service provider is LDAP --> apacheds-2.0.0-M20 (directory service).
No. The service provider is the JNDI LDAP SPI. The LDAP server is ApacheDS.
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
Does this tell to use "com.sun.jndi.ldap.LdapCtxFactory" as the LDAP SPI service API?
Yes.
- If this is correct, then isn't LDAP SPI service supposed to be provided by the Apache itself, because I have used the LDAP from Apache?
No. Apache is providing an implementation of an LDAP server. The JNDI LDAP SPI is a client.
- How "com.sun.jndi.ldap.LdapCtxFactory" is working with Apache DS?
Because they both speak the LDAP wire protocol.
"com.sun.jndi.ldap.LdapCtxFactory" is the implementation from Sun/Oracle.
It is the implementation of an LDAP client.
Does this mean that any SPI works with any vendor's service provider (in this case the service provider is from Apache)?
Apache is not a service provider in this scenario. It is the LDAP server. Your terminology is confused.
What all the above means is that any LDAP client will work with any LDAP server, which is the point of any protocol definition.
Upvotes: 2