Discussions

General J2EE: JNDI search for members of a group

  1. JNDI search for members of a group (3 messages)

    Hi All
     How do I search for members of a particular group in my LDAP directory?? I am doing it this way:

    Hashtable env = new Hashtable(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=carreker.com");

    // Create the initial directory context
    ctx = new InitialDirContext(env);
    BasicAttributes basicAttrs = new BasicAttributes("uniquemembers",null);
    String name= "cn=" + groupName + ", ou=Groups";
    NamingEnumeration grpEnum = ctx.search(name,basicAttrs);


      This query doesnt return me anything. What am i doing wrong??

    Any help is appreciated.
    Thanks in advance
  2. JNDI search for members of a group[ Go to top ]

    Hi All

    >  How do I search for members of a particular group in my LDAP directory?? I am doing it this way:
    >
    > Hashtable env = new Hashtable(11);
    > env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");
    > env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=carreker.com");
    >
    > // Create the initial directory context
    > ctx = new InitialDirContext(env);
    > BasicAttributes basicAttrs = new BasicAttributes("uniquemembers",null);
    > String name= "cn=" + groupName + ", ou=Groups";
    > NamingEnumeration grpEnum = ctx.search(name,basicAttrs);
    >
    >
    >   This query doesnt return me anything. What am i doing wrong??
    >
    > Any help is appreciated.
    > Thanks in advance

    Hi,

    Below follows a piece of code. It might not be perfect but it's a working example. It also checks for group1 > group2 > ... > groupN > member_of relationships.

      private Collection getGroups(Attributes atr, Hashtable fields, InitialDirContext initialContext){
        HashSet result = null;
        try{
          if(atr.get(LDAPConnectionFactory.MEMBER_OF)!=null){
            result = new HashSet();
            for(Enumeration e = atr.get(LDAPConnectionFactory.MEMBER_OF).getAll(); e.hasMoreElements();){
              String groupName = (String)e.nextElement();
              result.add(groupName);
              Collection subGroups = getGroups(groupName, fields, initialContext);
              if(subGroups!=null){
                result.addAll(subGroups);
              }
            }
          }
        }catch(Exception e){
          e.printStackTrace();
        }
        return result;
      }

      private Collection getGroups(String groupName, Hashtable fields, InitialDirContext initialContext) throws NamingException{
        HashSet result = null;
        String filter = "("+(String)fields.get(LDAPConnectionFactory.DISTINGUISHED_NAME)+"="+groupName+")";
        NamingEnumeration answer = initialContext.search(
             (String)initialContext.getEnvironment().get(LDAPConnectionFactory.SCHEMA),
             filter,new SearchControls(SearchControls.SUBTREE_SCOPE,0,0,null,false,false)
          );

        for(Enumeration e = answer;e.hasMoreElements();){
          SearchResult sr = (SearchResult)e.nextElement();
          if(sr.getAttributes().get(LDAPConnectionFactory.MEMBER_OF)!=null){
            result = new HashSet();
            for(Enumeration egr = sr.getAttributes().get(LDAPConnectionFactory.MEMBER_OF).getAll(); egr.hasMoreElements();){
              String egroupName = (String)egr.nextElement();
              result.add(egroupName);
              Collection subGroups = getGroups(egroupName, fields, initialContext);
              if(subGroups != null){
                result.addAll(subGroups);
              }
            }
          }
        }
        return result;
      }

    Hope this helps

    Kalin
  3. JNDI search for members of a group[ Go to top ]

    Thanks Kalin. Whats LDAPConnectionFactory.MEMBER_OF ??? My compiler throws a class not found exception for LDAPConnectionFactory. I am using plain jndi.jar from Sun.

    Thanks
  4. JNDI search for members of a group[ Go to top ]

    Thanks Kalin. Whats LDAPConnectionFactory.MEMBER_OF ??? My compiler throws a class not found exception for LDAPConnectionFactory. I am using plain jndi.jar from Sun.

    >
    > Thanks

    These are simply externalized strings:

      public static final String MEMBER_OF = "memberOf";
      public static final String DISTINGUISHED_NAME = "distinguishedName";
      public static final String SCHEMA = "schema";