#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    3
    Rep Power
    0

    JNDI ldap search problems


    Does anyone have any idea why a search using DirContents.search() would return duplicate records where no duplicates exist. Code below.

    public Vector getListByName(String inName)
    {
    vecList = new Vector();
    env= new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://lftdev01:389");
    env.put(Context.SECURITY_PRINCIPAL, "cn=admin,dc=rah,dc=sa,dc=gov,dc=au");
    env.put(Context.SECURITY_CREDENTIALS, "secret");


    try {
    // Create the initial context
    DirContext ctx = new InitialDirContext(env);
    Attributes matchAttrs = new BasicAttributes(true); // ignore attribute name case
    matchAttrs.put(new BasicAttribute("cn", inName));
    String[] attrIDs = {"cn", "uid",};
    SearchControls ctls = new SearchControls();
    NamingEnumeration answer = ctx.search("ou=staff,dc=rah,dc=sa,dc=gov,dc=au", "cn=*"+inName+"*",ctls);
    Vector tmpuid = new Vector();
    Vector tmpcn = new Vector();
    Attribute attr;
    while (answer.hasMore())
    {
    SearchResult sr = (SearchResult)answer.next();
    Attributes att = sr.getAttributes();
    for (NamingEnumeration ae = att.getAll(); ae.hasMore()
    {
    attr = (Attribute)ae.next();
    if(attr.getID().equals("uid"))
    {
    for (NamingEnumeration e = attr.getAll(); e.hasMore(); tmpuid.addElement(String.valueOf(e.next())));
    }
    else
    if(attr.getID().equals("cn"))
    {
    for (NamingEnumeration e = attr.getAll(); e.hasMore(); tmpcn.addElement(String.valueOf(e.next())));
    }

    }
    int i = 0;
    while(i < tmpuid.size())
    {
    vecList.addElement(tmpuid.elementAt(i)+"|"+tmpcn.elementAt(i));
    i++;
    }
    }

    // Close the context when we're done
    ctx.close();
    } catch (NamingException e) {System.out.println("List failed: " + e);}
    return vecList;
    }

    Thanks in advance,
    Jeremy
  2. #2
  3. Psycho Canadian
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jan 2001
    Location
    Canada
    Posts
    4,846
    Rep Power
    635
    I'd assume it has something to do with your while (which language is this? java?)
    while (answer.hasMore())
    try it outside the loop
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    3
    Rep Power
    0
    Yep its java, I dont think the loop is the problem though, the duplicates appear throughout the resultset ie 1,2,3,1,4,5,1,2.
    There are no duplicate entries in the ldap however.
    Very Strange.

    This one has me puzzled.


    Think ill move on and come back to it later on in the project.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2004
    Posts
    3
    Rep Power
    0
    Opps, i feel like such a tool, you were right.
    Staring at a computer screen 12 hours a day mst be taking its toll.

    Thanks Viper
  8. #5
  9. Psycho Canadian
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Jan 2001
    Location
    Canada
    Posts
    4,846
    Rep Power
    635

IMN logo majestic logo threadwatch logo seochat tools logo