Sunday, February 6, 2011

JNDI LDAP

Steps to take
1. Download http://directory.apache.org/
2. Install Apache Directory Server
3. Install Apache Directory Studio
4. Check whether the Directory Server is running in services.
5.  In server.xml of the C:\Program Files\Apache Directory Server\instances\default\conf add the following partition eg :: this will already present  <jdbmPartition id="example" cacheSize="100" suffix="dc=example,dc=com" optimizerEnabled="true"
syncOnWrite="true">
Add this
<jdbmPartition id="JNDITutorial" suffix="o=JNDITutorial" />
6. Check what is the port on which Active directory server is running. Check in server.xml.
    <tcpTransport address="0.0.0.0" port="10389" nbThreads="8" backLog="50" enableSSL="false"/>
    <tcpTransport address="localhost" port="10636" enableSSL="true"/>
7. Stop and start the server. Start the studio
8. In Active Directory Studio File-> New Conneciton -> Ldap Connection->
               Give Connection Name and local host: and port number as above 10389
9. You should see the default schema in the studio.
10. Import the attached tutorial file.
 More information in the following chapters :
          http://directory.apache.org/apacheds/1.5/143-adding-your-own-partition-resp-suffix.html
          http://java.sun.com/docs/books/tutorial/jndi/ops/list.html
11. Create a Class in eclipse and
package
 test;
import
 javax.naming.*;import
 java.util.Hashtable;
/**
* Demonstrates how to list the name and class of objects in a context.
*
* usage: java List
*/
class
 List {
publicstaticvoid main(String[] args) {

// Set up the environment for creating the initial contextHashtable<String, Object> env =
new Hashtable<String, Object>(11);env.put(Context.
INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.ldap.LdapCtxFactory");env.put(Context.
PROVIDER_URL,"ldap://localhost:10389/o=JNDITutorial");

try {
// Create the initial contextContext ctx =
new InitialContext(env);

// Get listing of contextNamingEnumeration list = ctx.list(
"ou=People");

// Go through each item in list
while (list.hasMore()) {NameClassPair nc = (NameClassPair)list.next();
System.
out.println(nc);}


// Close the context when we're donectx.close();
}
catch (NamingException e) {System.
out.println("List failed: " + e);}
}
}
Run the program.
Voila there you begin with Ldap.

No comments:

Post a Comment