List of usage examples for javax.naming NamingEnumeration close
public void close() throws NamingException;
From source file:org.springframework.ldap.demo.dao.PersonDaoImpl.java
public List<Person> findAll() { DirContext ctx = createAnonymousContext(); LinkedList<Person> list = new LinkedList<Person>(); NamingEnumeration<?> results = null; try {//from ww w . j ava 2 s .c o m SearchControls controls = new SearchControls(); controls.setSearchScope(SearchControls.SUBTREE_SCOPE); results = ctx.search("", "(objectclass=person)", controls); while (results.hasMore()) { SearchResult searchResult = (SearchResult) results.next(); String dn = searchResult.getName(); Attributes attributes = searchResult.getAttributes(); list.add(mapToPerson(dn, attributes)); } } 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 list; }
From source file:org.springframework.ldap.samples.article.dao.TraditionalPersonDaoImpl.java
public List getAllPersonNames() { DirContext ctx = createAnonymousContext(); LinkedList list = new LinkedList(); NamingEnumeration results = null; try {//from w ww. j a va 2s. c om SearchControls controls = new SearchControls(); controls.setSearchScope(SearchControls.SUBTREE_SCOPE); results = ctx.search("", "(objectclass=person)", controls); while (results.hasMore()) { SearchResult searchResult = (SearchResult) results.next(); Attributes attributes = searchResult.getAttributes(); Attribute attr = attributes.get("cn"); String cn = (String) attr.get(); list.add(cn); } } 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 list; }
From source file:org.springframework.ldap.samples.article.dao.TraditionalPersonDaoImpl.java
public List findAll() { DirContext ctx = createAnonymousContext(); LinkedList list = new LinkedList(); NamingEnumeration results = null; try {//from ww w . j av a2s . c o m SearchControls controls = new SearchControls(); controls.setSearchScope(SearchControls.SUBTREE_SCOPE); results = ctx.search("", "(objectclass=person)", controls); while (results.hasMore()) { SearchResult searchResult = (SearchResult) results.next(); String dn = searchResult.getName(); Attributes attributes = searchResult.getAttributes(); list.add(mapToPerson(dn, attributes)); } } 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 list; }
From source file:org.springframework.ldap.test.unboundid.LdapTestUtils.java
/** * Clear the directory sub-tree starting with the node represented by the * supplied distinguished name.//from w ww . j a v a 2s. com * * @param ctx The DirContext to use for cleaning the tree. * @param name the distinguished name of the root node. * @throws NamingException if anything goes wrong removing the sub-tree. */ public static void clearSubContexts(DirContext ctx, Name name) throws NamingException { NamingEnumeration<?> enumeration = null; try { enumeration = ctx.listBindings(name); while (enumeration.hasMore()) { Binding element = (Binding) enumeration.next(); Name childName = LdapUtils.newLdapName(element.getName()); childName = LdapUtils.prepend(childName, name); try { ctx.unbind(childName); } catch (ContextNotEmptyException e) { clearSubContexts(ctx, childName); ctx.unbind(childName); } } } catch (NamingException e) { LOGGER.debug("Error cleaning sub-contexts", e); } finally { try { enumeration.close(); } catch (Exception e) { // Never mind this } } }
From source file:org.springframework.security.ldap.LdapUtils.java
public static void closeEnumeration(NamingEnumeration ne) { try {/*ww w. ja v a 2s .c o m*/ if (ne != null) { ne.close(); } } catch (NamingException e) { logger.error("Failed to close enumeration.", e); } }
From source file:org.tolven.ldapmgr.LDAPMgrPlugin.java
protected void updateSuffix(DirContext dirContext) { String ldapSuffix = getLDAPSuffix(); NamingEnumeration<SearchResult> namingEnum = null; try {/* w w w.jav a 2 s . com*/ try { String dn = ldapSuffix; Attributes attributes = new BasicAttributes(); Attribute objclass = new BasicAttribute("objectclass"); objclass.add("organization"); objclass.add("dcObject"); attributes.put(objclass); attributes.put("dc", "tolven"); attributes.put("o", "myOrg"); dirContext.createSubcontext(dn, attributes); logger.info("Executed a createSubContext LDAP schema for " + ldapSuffix); } catch (NamingException ex) { //For some reason the search can fail, when the suffix is available, and when not available // The only certainty is to attempt to create it for now } } finally { if (namingEnum != null) { try { namingEnum.close(); } catch (NamingException ex) { throw new RuntimeException("Could not close the naming enumeration for the ldap suffix schema", ex); } } } }
From source file:org.tolven.ldapmgr.LDAPMgrPlugin.java
protected void updateGroups(DirContext dirContext, SearchControls controls) { String ldapSuffix = getLDAPSuffix(); String ldapGroups = getLDAPGroups(); NamingEnumeration<SearchResult> namingEnum = null; try {// ww w.j a va 2 s.c o m boolean schemaExists = false; try { namingEnum = dirContext.search(ldapSuffix, ldapGroups, controls); schemaExists = namingEnum.hasMore(); } catch (NamingException ex) { throw new RuntimeException("Could find groups schema", ex); } if (schemaExists) { logger.info("LDAP schema for " + ldapGroups + " already exists"); } else { String dn = ldapGroups + "," + ldapSuffix; Attributes attributes = new BasicAttributes(); Attribute objclass = new BasicAttribute("objectclass"); objclass.add("organizationalUnit"); attributes.put(objclass); attributes.put(ldapGroups.substring(0, ldapGroups.indexOf("=")), ldapGroups.substring(ldapGroups.indexOf("=") + 1)); try { dirContext.createSubcontext(dn, attributes); } catch (NamingException ex) { throw new RuntimeException("Could not create groups schema", ex); } logger.info("Created LDAP schema for " + ldapGroups); } } finally { if (namingEnum != null) { try { namingEnum.close(); } catch (NamingException ex) { throw new RuntimeException("Could not close the naming enumeration for the ldap groups schema", ex); } } } }
From source file:org.tolven.ldapmgr.LDAPMgrPlugin.java
protected void updatePeople(DirContext dirContext, SearchControls controls) { String ldapSuffix = getLDAPSuffix(); String ldapPeople = getLDAPPeople(); NamingEnumeration<SearchResult> namingEnum = null; try {/*from w w w . j a v a2 s . c om*/ boolean schemaExists = false; try { namingEnum = dirContext.search(ldapSuffix, ldapPeople, controls); schemaExists = namingEnum.hasMore(); } catch (NamingException ex) { throw new RuntimeException("Could find people schema", ex); } if (schemaExists) { logger.info("LDAP schema for " + ldapPeople + " already exists"); } else { String dn = ldapPeople + "," + ldapSuffix; Attributes attributes = new BasicAttributes(); Attribute objclass = new BasicAttribute("objectclass"); objclass.add("organizationalUnit"); attributes.put(objclass); attributes.put(ldapPeople.substring(0, ldapPeople.indexOf("=")), ldapPeople.substring(ldapPeople.indexOf("=") + 1)); try { dirContext.createSubcontext(dn, attributes); } catch (NamingException ex) { throw new RuntimeException("Could not create people schema", ex); } logger.info("Created LDAP schema for " + ldapPeople); } } finally { if (namingEnum != null) { try { namingEnum.close(); } catch (NamingException ex) { throw new RuntimeException("Could not close the naming enumeration for the ldap people schema", ex); } } } }
From source file:org.tolven.ldapmgr.LDAPMgrPlugin.java
protected void updateRootDN(DirContext dirContext, SearchControls controls) { String ldapRootDN = getRootDN(); NamingEnumeration<SearchResult> namingEnum = null; try {// w w w .j av a2 s. c o m boolean schemaExists = false; String name = null; String base = null; try { int index = ldapRootDN.indexOf(","); if (index == -1) { throw new RuntimeException("Expected to find at least one comma in the rootDN"); } else { name = ldapRootDN.substring(0, index); base = ldapRootDN.substring(index + 1); } namingEnum = dirContext.search(base, name, controls); schemaExists = namingEnum.hasMore(); } catch (NamingException ex) { throw new RuntimeException("Could find rootDN schema", ex); } if (schemaExists) { logger.info("LDAP schema for " + ldapRootDN + " already exists"); } else { String dn = name + "," + base; Attributes attributes = new BasicAttributes(); Attribute objclass = new BasicAttribute("objectclass"); objclass.add("organizationalRole"); attributes.put(objclass); attributes.put(name.substring(0, name.indexOf("=")), name.substring(name.indexOf("=") + 1)); try { dirContext.createSubcontext(dn, attributes); } catch (NamingException ex) { throw new RuntimeException("Could not create rootDN schema", ex); } logger.info("Created LDAP schema for " + ldapRootDN); } } finally { if (namingEnum != null) { try { namingEnum.close(); } catch (NamingException ex) { throw new RuntimeException("Could not close the naming enumeration for the ldap rootDN schema", ex); } } } }
From source file:org.tolven.ldapmgr.LDAPMgrPlugin.java
protected void updateUser(String user, String encryptedPassword, DirContext dirContext, SearchControls controls) { NamingEnumeration<SearchResult> namingEnum = null; try {/*from w w w. j a v a2s.com*/ String ldapPeople = getLDAPPeople(); String ldapSuffix = getLDAPSuffix(); boolean schemaExists = false; try { namingEnum = dirContext.search(ldapPeople + "," + ldapSuffix, "uid=" + user, controls); schemaExists = namingEnum.hasMore(); } catch (NamingException ex) { throw new RuntimeException("Could find schema for: " + user, ex); } if (schemaExists) { logger.info("LDAP schema for user " + user + " already exists"); } else { String dn = "uid=" + user + "," + ldapPeople + "," + ldapSuffix; Attributes attributes = new BasicAttributes(); Attribute objclass = new BasicAttribute("objectclass"); objclass.add("inetOrgPerson"); attributes.put(objclass); attributes.put("uid", user); attributes.put("sn", user); attributes.put("cn", user); attributes.put("userPassword", encryptedPassword); try { dirContext.createSubcontext(dn, attributes); } catch (NamingException ex) { throw new RuntimeException("Could not create schema for: " + user, ex); } logger.info("Created LDAP schema for " + user); } } finally { if (namingEnum != null) { try { namingEnum.close(); } catch (NamingException ex) { throw new RuntimeException( "Could not close the naming enumeration for the ldap schema: " + user, ex); } } } }