public static void main(String[] args) { // set up environment to access the server Properties env = new Properties(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.PROVIDER_URL, "ldap://" + ldapServerName + "/" + rootContext); env.put(Context.SECURITY_PRINCIPAL, rootdn); env.put(Context.SECURITY_CREDENTIALS, rootpass); try { // obtain initial directory context using the environment DirContext ctx = new InitialDirContext(env); // create some random number to add to the directory Integer i = new Integer(28420); System.out.println("Adding " + i + " to directory..."); ctx.bind("cn=myRandomInt", i); i = new Integer(98765); System.out.println("i is now: " + i); i = (Integer) ctx.lookup("cn=myRandomInt"); System.out.println("Retrieved i from directory with value: " + i); } catch (NameAlreadyBoundException nabe) { System.err.println("value has already been bound!"); } catch (Exception e) { System.err.println(e); } }
void exploreNext(int depth, DirContext ctx, String path) throws NamingException { NamingEnumeration<NameClassPair> names = ctx.list(path); while (names.hasMore()) { Object obj = names.next(); NameClassPair ncp = (NameClassPair) obj; if (ncp.getClassName().equals("weblogic.jndi.internal.ServerNamingNode")) { System.out.println(createBlanks(depth) + ncp.getName()); exploreNext(depth + 1, ctx, path + "/" + ncp.getName()); } else { System.out.print(createBlanks(depth) + "[" + ncp.getName()); System.out.print("] - "); try { // System.out.println(ctx.lookup(path+"/"+ncp.getName())); System.out.println(ncp.getClassName()); } catch (Exception ex) { System.out.println(""); } } } }