Example usage for javax.naming NamingEnumeration close

List of usage examples for javax.naming NamingEnumeration close

Introduction

In this page you can find the example usage for javax.naming NamingEnumeration close.

Prototype

public void close() throws NamingException;

Source Link

Document

Closes this enumeration.

Usage

From source file:it.infn.ct.security.utilities.LDAPUtils.java

public static List<Organization> getOrgList(String country) {
    List<Organization> OrgList = new ArrayList<Organization>();
    NamingEnumeration resultCountries = null;
    DirContext ctx = null;// ww  w.java 2  s .c o m
    try {
        ctx = getContext();
        SearchControls controls = new SearchControls();
        controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        ResourceBundle rb = ResourceBundle.getBundle("ldap");

        String filter;
        if (country == null) {
            filter = "(objectclass=country)";
        } else {
            filter = "(&(objectclass=country)(c=" + country + "))";
        }
        resultCountries = ctx.search(rb.getString("organisationsRoot"), filter, controls);

        while (resultCountries.hasMore()) {
            SearchResult searchResult = (SearchResult) resultCountries.next();
            Attributes attributes = searchResult.getAttributes();
            String countryCode = (String) attributes.get("c").get();
            String countryName = (String) attributes.get("co").get();

            NamingEnumeration resultsOrgs = ctx.search(
                    "c=" + countryCode + "," + rb.getString("organisationsRoot"), "(objectclass=organization)",
                    controls);
            while (resultsOrgs.hasMore()) {
                SearchResult srOrg = (SearchResult) resultsOrgs.next();
                Attributes orgAttrs = srOrg.getAttributes();
                String description = "";
                if ((orgAttrs.get("description")) != null) {
                    description = (String) orgAttrs.get("description").get();
                }

                OrgList.add(new Organization((String) orgAttrs.get("o").get(), countryName, countryCode,
                        description, srOrg.getNameInNamespace()));
            }
            resultsOrgs.close();

        }
    } catch (NameNotFoundException ex) {
        _log.error(ex);
    } catch (NamingException e) {
        throw new RuntimeException(e);
    } finally {
        if (resultCountries != null) {
            try {
                resultCountries.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
    }

    Collections.sort(OrgList, new Comparator<Organization>() {

        public int compare(Organization o1, Organization o2) {
            return o1.getKey().compareTo(o2.getKey());
        }

    });

    return OrgList;

}

From source file:com.globalsight.everest.usermgr.UserLdapHelper.java

static List getListOfEmailInfo(NamingEnumeration p_searchResults) throws NamingException {

    List result = new ArrayList();

    while (p_searchResults.hasMoreElements()) {
        Object searchResultObj = p_searchResults.nextElement();
        if (searchResultObj instanceof SearchResult) {
            SearchResult tmpSearchResult = (SearchResult) searchResultObj;
            Attributes entry = tmpSearchResult.getAttributes();
            result.add(getUserEmailInfo(entry));
        }/*from   w  ww  .j av a  2s.c  o m*/
    }

    p_searchResults.close();
    return result.size() == 0 ? null : result;
}

From source file:com.globalsight.everest.usermgr.UserLdapHelper.java

/**
 * Generates a collection of UserInfo objects from a NamingEnumeration after
 * a search is carried out.//from ww  w  . ja  v a 2 s  .c  o  m
 */
static Vector getUserInfosFromSearchResults(NamingEnumeration p_SearchResults) throws NamingException {

    Vector userInfoList = new Vector();
    while (p_SearchResults.hasMoreElements()) {
        /* Next directory entry */
        Object searchResultObj = p_SearchResults.nextElement();
        if (searchResultObj instanceof SearchResult) {
            SearchResult tempSearchResult = (SearchResult) searchResultObj;
            Attributes entry = tempSearchResult.getAttributes();
            userInfoList.addElement(getUserInfoFromLDAPEntry(entry));
        }
    }
    p_SearchResults.close();

    return userInfoList;
}

From source file:com.globalsight.everest.usermgr.UserLdapHelper.java

/**
 * Generates a collection of User objects from a NamingEnumeration after a
 * search is carried out./*  ww  w  .j  av a  2s.  c  o m*/
 */
static Vector<User> getUsersFromSearchResults(NamingEnumeration p_SearchResults) throws NamingException {

    Vector<User> userList = new Vector<User>();
    while (p_SearchResults.hasMoreElements()) {
        /* Next directory entry */
        Object searchResultObj = p_SearchResults.nextElement();
        if (searchResultObj instanceof SearchResult) {
            SearchResult tempSearchResult = (SearchResult) searchResultObj;
            Attributes entry = tempSearchResult.getAttributes();
            userList.addElement(getUserFromLDAPEntry(entry));
        }
    }

    p_SearchResults.close();
    return userList;
}

From source file:com.globalsight.everest.usermgr.UserLdapHelper.java

/**
 * Get the Uset ID array from a NamingEnumeration
 *///from w w  w.  j a  v  a 2  s .com
static Vector getUIDsVectorFromSearchResults(NamingEnumeration p_SearchResults) throws NamingException {

    Vector uids = new Vector();

    while (p_SearchResults.hasMoreElements()) {
        /* Next directory entry */
        Object searchResultObj = p_SearchResults.nextElement();
        if (searchResultObj instanceof SearchResult) {
            SearchResult tempSearchResult = (SearchResult) searchResultObj;
            Attributes entry = tempSearchResult.getAttributes();
            String uid = getSingleAttributeValue(entry.get(LDAP_ATTR_USERID));
            uids.addElement(uid);
        }
    }

    p_SearchResults.close();

    return uids;
}

From source file:com.globalsight.everest.usermgr.UserLdapHelper.java

static Vector getNamesVectorFromSearchResults(NamingEnumeration p_SearchResults) throws NamingException {
    Vector userNames = new Vector();

    while (p_SearchResults.hasMoreElements()) {
        /* Next directory entry */
        Object searchResultObj = p_SearchResults.nextElement();
        if (searchResultObj instanceof SearchResult) {
            SearchResult tempSearchResult = (SearchResult) searchResultObj;
            Attributes entry = tempSearchResult.getAttributes();
            String userName = getSingleAttributeValue(entry.get(LDAP_ATTR_USER_NAME));
            userNames.addElement(userName);
        }//from   w w w.  ja  v  a2 s .c om
    }

    p_SearchResults.close();

    return userNames;
}

From source file:it.infn.ct.security.utilities.LDAPUtils.java

public static String getOrgDN(String organisation, String countryCode) {
    NamingEnumeration results = null;
    DirContext ctx = null;//  w w w.  j  a v  a2s.c o m
    String dn = null;
    try {
        ctx = getContext();
        SearchControls controls = new SearchControls();
        controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        String retAttrs[] = { "dn" };
        controls.setReturningAttributes(retAttrs);
        ResourceBundle rb = ResourceBundle.getBundle("ldap");

        results = ctx.search("c=" + countryCode + "," + rb.getString("organisationsRoot"),
                "(&(objectclass=organization)(o=" + organisation + "))", controls);

        if (results.hasMore()) {
            SearchResult searchResult = (SearchResult) results.next();
            dn = searchResult.getNameInNamespace();
        }
    } catch (NameNotFoundException ex) {
        _log.error(ex);
    } catch (NamingException e) {
        throw new RuntimeException(e);
    } finally {
        if (results != null) {
            try {
                results.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
        if (ctx != null) {
            try {
                ctx.close();
            } catch (Exception e) {
                // Never mind this.
            }
        }
    }

    return dn;
}

From source file:org.apache.archiva.redback.common.ldap.role.DefaultLdapRoleMapper.java

protected void closeNamingEnumeration(NamingEnumeration namingEnumeration) {
    if (namingEnumeration != null) {
        try {/*  ww w . j a v  a 2s  .  c o m*/
            namingEnumeration.close();
        } catch (NamingException e) {
            log.warn("failed to close NamingEnumeration", e);
        }
    }
}

From source file:org.apache.archiva.redback.common.ldap.role.DefaultLdapRoleMapper.java

private void close(NamingEnumeration namingEnumeration) {
    if (namingEnumeration != null) {
        try {/*from ww  w .j a  v a  2 s  . com*/
            namingEnumeration.close();
        } catch (NamingException e) {
            log.warn("fail to close namingEnumeration: {}", e.getMessage());
        }
    }
}

From source file:com.googlecode.fascinator.authentication.custom.ldap.CustomLdapAuthenticationHandler.java

/**
* Tries to find the value of the given attribute. Note that this method
* only uses the first search result./* ww  w.jav a 2 s.  c  om*/
* 
* @param username
*            a username
* @param attrName
*            the name of the attribute to find
* @return the value of the attribute, or an empty string
*/
public String getAttr(String username, String attrName) {
    String val = "";
    try {
        DirContext dc = new InitialDirContext(env);
        NamingEnumeration<SearchResult> ne = performLdapSearch(username, dc);

        if (ne.hasMore()) {
            val = getAttrValue(attrName, ne.next());
        }

        ne.close();
        dc.close();
    } catch (NamingException ne) {
        log.warn("Failed LDAP lookup getAttr", ne);
        log.warn("username:", username);
        log.warn("attrName:", attrName);
    }

    log.trace(String.format("getAttr search result: %s", val));
    return val;
}