Index: redback-common/redback-common-ldap/src/main/java/org/codehaus/plexus/redback/common/ldap/connection/LdapConnection.java =================================================================== --- redback-common/redback-common-ldap/src/main/java/org/codehaus/plexus/redback/common/ldap/connection/LdapConnection.java (revision 875) +++ redback-common/redback-common-ldap/src/main/java/org/codehaus/plexus/redback/common/ldap/connection/LdapConnection.java (working copy) @@ -31,13 +31,14 @@ import javax.naming.Context; import javax.naming.NamingException; import javax.naming.directory.DirContext; -import javax.naming.directory.InitialDirContext; import javax.naming.ldap.LdapName; import javax.naming.ldap.Rdn; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.sun.jndi.ldap.LdapCtxFactory; + /** * The configuration for a connection will not change. * @@ -46,6 +47,8 @@ */ public class LdapConnection { + private static LdapCtxFactory ctxFactory = new LdapCtxFactory(); + private Logger log = LoggerFactory.getLogger( getClass() ); private LdapConnectionConfiguration config; @@ -78,7 +81,7 @@ try { - context = new InitialDirContext( e ); + context = (DirContext) ctxFactory.getInitialContext( e ); } catch ( NamingException ex ) { @@ -106,7 +109,7 @@ try { - context = new InitialDirContext( e ); + context = (DirContext) ctxFactory.getInitialContext( e ); } catch ( NamingException ex ) { Index: redback-users/redback-users-providers/redback-users-ldap/src/main/java/org/codehaus/plexus/redback/users/ldap/ctl/DefaultLdapController.java =================================================================== --- redback-users/redback-users-providers/redback-users-ldap/src/main/java/org/codehaus/plexus/redback/users/ldap/ctl/DefaultLdapController.java (revision 875) +++ redback-users/redback-users-providers/redback-users-ldap/src/main/java/org/codehaus/plexus/redback/users/ldap/ctl/DefaultLdapController.java (working copy) @@ -78,14 +78,28 @@ public boolean userExists( Object key, DirContext context ) throws LdapControllerException { + NamingEnumeration results = null; try { - return searchUsers( key, context ).hasMoreElements(); + results = searchUsers( key, context ); + return results.hasMoreElements(); } catch ( NamingException e ) { throw new LdapControllerException( "Error searching for the existence of user: " + key, e ); } + finally + { + if ( results != null ) + try + { + results.close(); + } + catch ( NamingException e ) + { + log.warn( "Error closing search results", e ); + } + } } protected NamingEnumeration searchUsers( Object key, DirContext context ) @@ -136,9 +150,10 @@ public Collection getUsers( DirContext context ) throws LdapControllerException, MappingException { + NamingEnumeration results = null; try { - NamingEnumeration results = searchUsers( context, null, null ); + results = searchUsers( context, null, null ); Set users = new LinkedHashSet(); while ( results.hasMoreElements() ) @@ -156,6 +171,18 @@ throw new LdapControllerException( message, e ); } + finally + { + if ( results != null ) + try + { + results.close(); + } + catch ( NamingException e ) + { + log.warn( "failed to close search results", e ); + } + } } /** @@ -164,9 +191,10 @@ public List getUsersByQuery( LdapUserQuery query, DirContext context ) throws LdapControllerException, MappingException { + NamingEnumeration results = null; try { - NamingEnumeration results = searchUsers( context, null, query ); + results = searchUsers( context, null, query ); List users = new LinkedList(); while ( results.hasMoreElements() ) @@ -184,6 +212,18 @@ throw new LdapControllerException( message, e ); } + finally + { + if ( results != null ) + try + { + results.close(); + } + catch ( NamingException e ) + { + log.warn( "failed to close search results", e ); + } + } } /** @@ -216,9 +256,10 @@ LdapUserQuery query = new LdapUserQuery(); query.setUsername( username ); + NamingEnumeration result = null; try { - NamingEnumeration result = searchUsers( context, null, query ); + result = searchUsers( context, null, query ); if ( result.hasMoreElements() ) { @@ -237,6 +278,18 @@ throw new LdapControllerException( message, e ); } + finally + { + if ( result != null ) + try + { + result.close(); + } + catch ( NamingException e ) + { + log.warn( "failed to close search results", e ); + } + } } }