move LdapConnection to an interface

git-svn-id: https://svn.apache.org/repos/asf/archiva/redback/redback-core/trunk@1477971 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Olivier Lamy 2013-05-01 12:19:20 +00:00
parent d1b77ab248
commit 4a57b05074
9 changed files with 102 additions and 46 deletions

View File

@ -21,6 +21,7 @@ package org.apache.archiva.redback.authentication.ldap;
import org.apache.archiva.redback.authentication.AbstractAuthenticator; import org.apache.archiva.redback.authentication.AbstractAuthenticator;
import org.apache.archiva.redback.common.ldap.connection.DefaultLdapConnection; import org.apache.archiva.redback.common.ldap.connection.DefaultLdapConnection;
import org.apache.archiva.redback.common.ldap.connection.LdapConnection;
import org.apache.archiva.redback.common.ldap.user.UserMapper; import org.apache.archiva.redback.common.ldap.user.UserMapper;
import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory; import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory;
import org.apache.archiva.redback.configuration.UserConfiguration; import org.apache.archiva.redback.configuration.UserConfiguration;
@ -103,8 +104,8 @@ public class LdapBindAuthenticator
log.debug( "Searching for users with filter: '{}' from base dn: {}", filter, mapper.getUserBaseDn() ); log.debug( "Searching for users with filter: '{}' from base dn: {}", filter, mapper.getUserBaseDn() );
DefaultLdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
DefaultLdapConnection authLdapConnection = null; LdapConnection authLdapConnection = null;
NamingEnumeration<SearchResult> results = null; NamingEnumeration<SearchResult> results = null;
try try
{ {
@ -172,13 +173,13 @@ public class LdapBindAuthenticator
return ( source instanceof PasswordBasedAuthenticationDataSource ); return ( source instanceof PasswordBasedAuthenticationDataSource );
} }
private DefaultLdapConnection getLdapConnection() private LdapConnection getLdapConnection()
throws LdapException throws LdapException
{ {
return connectionFactory.getConnection(); return connectionFactory.getConnection();
} }
private void closeLdapConnection( DefaultLdapConnection ldapConnection ) private void closeLdapConnection( LdapConnection ldapConnection )
{ {
if ( ldapConnection != null ) if ( ldapConnection != null )
{ {

View File

@ -38,8 +38,10 @@ import java.util.Properties;
* The configuration for a connection will not change. * The configuration for a connection will not change.
* *
* @author <a href="mailto:trygvis@inamo.no">trygvis</a> * @author <a href="mailto:trygvis@inamo.no">trygvis</a>
* @since 2.1
*/ */
public class DefaultLdapConnection public class DefaultLdapConnection
implements LdapConnection
{ {
private static LdapCtxFactory ctxFactory;// = new LdapCtxFactory(); private static LdapCtxFactory ctxFactory;// = new LdapCtxFactory();
@ -132,6 +134,7 @@ public class DefaultLdapConnection
// Connection Managment // Connection Managment
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
@Override
public Hashtable<Object, Object> getEnvironment() public Hashtable<Object, Object> getEnvironment()
throws LdapException throws LdapException
{ {
@ -236,6 +239,7 @@ public class DefaultLdapConnection
return env; return env;
} }
@Override
public void close() public void close()
{ {
try try
@ -259,16 +263,19 @@ public class DefaultLdapConnection
// Utils // Utils
// ---------------------------------------------------------------------- // ----------------------------------------------------------------------
@Override
public LdapConnectionConfiguration getConfiguration() public LdapConnectionConfiguration getConfiguration()
{ {
return config; return config;
} }
@Override
public List<Rdn> getBaseDnRdns() public List<Rdn> getBaseDnRdns()
{ {
return baseDnRdns; return baseDnRdns;
} }
@Override
public DirContext getDirContext() public DirContext getDirContext()
{ {
return context; return context;

View File

@ -0,0 +1,42 @@
package org.apache.archiva.redback.common.ldap.connection;
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import javax.naming.directory.DirContext;
import javax.naming.ldap.Rdn;
import java.util.Hashtable;
import java.util.List;
/**
* @author Olivier Lamy
*/
public interface LdapConnection
{
Hashtable<Object, Object> getEnvironment()
throws LdapException;
void close();
LdapConnectionConfiguration getConfiguration();
List<Rdn> getBaseDnRdns();
DirContext getDirContext();
}

View File

@ -30,16 +30,16 @@ import javax.naming.spi.StateFactory;
*/ */
public interface LdapConnectionFactory public interface LdapConnectionFactory
{ {
DefaultLdapConnection getConnection() LdapConnection getConnection()
throws LdapException; throws LdapException;
DefaultLdapConnection getConnection( LdapConnectionConfiguration ldapConnectionConfiguration ) LdapConnection getConnection( LdapConnectionConfiguration ldapConnectionConfiguration )
throws LdapException; throws LdapException;
DefaultLdapConnection getConnection( Rdn subRdn ) LdapConnection getConnection( Rdn subRdn )
throws LdapException; throws LdapException;
DefaultLdapConnection getConnection( String bindDn, String password ) LdapConnection getConnection( String bindDn, String password )
throws LdapException; throws LdapException;
LdapName getBaseDnLdapName() LdapName getBaseDnLdapName()

View File

@ -1,4 +1,5 @@
package org.apache.archiva.redback.common.ldap.role; package org.apache.archiva.redback.common.ldap.role;
/* /*
* Licensed to the Apache Software Foundation (ASF) under one * Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file * or more contributor license agreements. See the NOTICE file
@ -19,7 +20,7 @@ package org.apache.archiva.redback.common.ldap.role;
*/ */
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.archiva.redback.common.ldap.connection.DefaultLdapConnection; import org.apache.archiva.redback.common.ldap.connection.LdapConnection;
import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory; import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory;
import org.apache.archiva.redback.components.apacheds.ApacheDs; import org.apache.archiva.redback.components.apacheds.ApacheDs;
import org.apache.archiva.redback.policy.PasswordEncoder; import org.apache.archiva.redback.policy.PasswordEncoder;
@ -89,7 +90,7 @@ public class TestLdapRoleMapper
List<String> roleNames = List<String> roleNames =
Arrays.asList( "Archiva System Administrator", "Internal Repo Manager", "Internal Repo Observer" ); Arrays.asList( "Archiva System Administrator", "Internal Repo Manager", "Internal Repo Observer" );
DefaultLdapConnection ldapConnection; LdapConnection ldapConnection;
DirContext context; DirContext context;

View File

@ -20,6 +20,7 @@ package org.apache.archiva.redback.rest.services;
import org.apache.archiva.redback.common.ldap.MappingException; import org.apache.archiva.redback.common.ldap.MappingException;
import org.apache.archiva.redback.common.ldap.connection.DefaultLdapConnection; import org.apache.archiva.redback.common.ldap.connection.DefaultLdapConnection;
import org.apache.archiva.redback.common.ldap.connection.LdapConnection;
import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory; import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory;
import org.apache.archiva.redback.common.ldap.connection.LdapException; import org.apache.archiva.redback.common.ldap.connection.LdapException;
import org.apache.archiva.redback.common.ldap.role.LdapRoleMapper; import org.apache.archiva.redback.common.ldap.role.LdapRoleMapper;
@ -67,7 +68,7 @@ public class DefaultLdapGroupMappingService
public StringList getLdapGroups() public StringList getLdapGroups()
throws RedbackServiceException throws RedbackServiceException
{ {
DefaultLdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
DirContext context = null; DirContext context = null;
@ -170,7 +171,7 @@ public class DefaultLdapGroupMappingService
// utils // utils
//------------------ //------------------
protected void closeLdapConnection( DefaultLdapConnection ldapConnection ) protected void closeLdapConnection( LdapConnection ldapConnection )
{ {
if ( ldapConnection != null ) if ( ldapConnection != null )
{ {

View File

@ -20,7 +20,7 @@ package org.apache.archiva.redback.rbac.ldap;
*/ */
import org.apache.archiva.redback.common.ldap.MappingException; import org.apache.archiva.redback.common.ldap.MappingException;
import org.apache.archiva.redback.common.ldap.connection.DefaultLdapConnection; import org.apache.archiva.redback.common.ldap.connection.LdapConnection;
import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory; import org.apache.archiva.redback.common.ldap.connection.LdapConnectionFactory;
import org.apache.archiva.redback.common.ldap.connection.LdapException; import org.apache.archiva.redback.common.ldap.connection.LdapException;
import org.apache.archiva.redback.common.ldap.role.LdapRoleMapper; import org.apache.archiva.redback.common.ldap.role.LdapRoleMapper;
@ -71,7 +71,7 @@ import java.util.Set;
* *
* @author Olivier Lamy * @author Olivier Lamy
*/ */
@Service( "rbacManager#ldap" ) @Service("rbacManager#ldap")
public class LdapRbacManager public class LdapRbacManager
extends AbstractRBACManager extends AbstractRBACManager
implements RBACManager, RBACManagerListener implements RBACManager, RBACManagerListener
@ -80,19 +80,19 @@ public class LdapRbacManager
private Logger log = LoggerFactory.getLogger( getClass() ); private Logger log = LoggerFactory.getLogger( getClass() );
@Inject @Inject
@Named( value = "rbacManager#cached" ) @Named(value = "rbacManager#cached")
private RBACManager rbacImpl; private RBACManager rbacImpl;
@Inject @Inject
@Named( value = "ldapRoleMapper#default" ) @Named(value = "ldapRoleMapper#default")
private LdapRoleMapper ldapRoleMapper; private LdapRoleMapper ldapRoleMapper;
@Inject @Inject
@Named( value = "userConfiguration#default" ) @Named(value = "userConfiguration#default")
private UserConfiguration userConf; private UserConfiguration userConf;
@Inject @Inject
@Named( value = "userManager#ldap" ) @Named(value = "userManager#ldap")
private UserManager userManager; private UserManager userManager;
@Inject @Inject
@ -102,15 +102,15 @@ public class LdapRbacManager
private LdapController ldapController; private LdapController ldapController;
@Inject @Inject
@Named( value = "ldapRoleMapperConfiguration#default" ) @Named(value = "ldapRoleMapperConfiguration#default")
private LdapRoleMapperConfiguration ldapRoleMapperConfiguration; private LdapRoleMapperConfiguration ldapRoleMapperConfiguration;
@Inject @Inject
@Named( value = "cache#ldapRoles" ) @Named(value = "cache#ldapRoles")
private Cache<String, Role> rolesCache; private Cache<String, Role> rolesCache;
@Inject @Inject
@Named( value = "cache#userAssignments" ) @Named(value = "cache#userAssignments")
private Cache<String, UserAssignment> userAssignmentsCache; private Cache<String, UserAssignment> userAssignmentsCache;
private boolean writableLdap = false; private boolean writableLdap = false;
@ -174,7 +174,7 @@ public class LdapRbacManager
{ {
if ( writableLdap ) if ( writableLdap )
{ {
DefaultLdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
DirContext context = null; DirContext context = null;
try try
{ {
@ -250,7 +250,7 @@ public class LdapRbacManager
public List<Role> getAllRoles() public List<Role> getAllRoles()
throws RbacManagerException throws RbacManagerException
{ {
DefaultLdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
DirContext context = null; DirContext context = null;
try try
{ {
@ -280,7 +280,7 @@ public class LdapRbacManager
public List<UserAssignment> getAllUserAssignments() public List<UserAssignment> getAllUserAssignments()
throws RbacManagerException throws RbacManagerException
{ {
DefaultLdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
DirContext context = null; DirContext context = null;
try try
{ {
@ -313,7 +313,7 @@ public class LdapRbacManager
} }
} }
protected void closeLdapConnection( DefaultLdapConnection ldapConnection ) protected void closeLdapConnection( LdapConnection ldapConnection )
{ {
if ( ldapConnection != null ) if ( ldapConnection != null )
{ {
@ -420,7 +420,7 @@ public class LdapRbacManager
throws RbacManagerException throws RbacManagerException
{ {
DefaultLdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
DirContext context = null; DirContext context = null;
try try
@ -528,7 +528,7 @@ public class LdapRbacManager
{ {
return role; return role;
} }
DefaultLdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
DirContext context = null; DirContext context = null;
//verify it's a ldap group //verify it's a ldap group
try try
@ -565,7 +565,7 @@ public class LdapRbacManager
public Collection<Role> getUnassignedRoles( String username ) public Collection<Role> getUnassignedRoles( String username )
throws RbacManagerException throws RbacManagerException
{ {
DefaultLdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
DirContext context = null; DirContext context = null;
@ -613,7 +613,7 @@ public class LdapRbacManager
{ {
return ua; return ua;
} }
DefaultLdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
DirContext context = null; DirContext context = null;
try try
{ {
@ -786,7 +786,7 @@ public class LdapRbacManager
rolesCache.remove( role.getName() ); rolesCache.remove( role.getName() );
if ( writableLdap ) if ( writableLdap )
{ {
DefaultLdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
DirContext context = null; DirContext context = null;
try try
{ {
@ -868,7 +868,7 @@ public class LdapRbacManager
{ {
return true; return true;
} }
DefaultLdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
DirContext context = null; DirContext context = null;
try try
{ {
@ -918,7 +918,7 @@ public class LdapRbacManager
{ {
if ( writableLdap ) if ( writableLdap )
{ {
DefaultLdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
DirContext context = null; DirContext context = null;
try try
{ {
@ -956,7 +956,7 @@ public class LdapRbacManager
{ {
if ( writableLdap ) if ( writableLdap )
{ {
DefaultLdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
DirContext context = null; DirContext context = null;
try try
{ {
@ -985,7 +985,7 @@ public class LdapRbacManager
public UserAssignment saveUserAssignment( UserAssignment userAssignment ) public UserAssignment saveUserAssignment( UserAssignment userAssignment )
throws RbacManagerException throws RbacManagerException
{ {
DefaultLdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
DirContext context = null; DirContext context = null;
try try
{ {
@ -1052,7 +1052,7 @@ public class LdapRbacManager
{ {
return true; return true;
} }
DefaultLdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
DirContext context = null; DirContext context = null;
try try
{ {

View File

@ -21,6 +21,7 @@ package org.apache.archiva.redback.users.ldap;
import org.apache.archiva.redback.common.ldap.connection.DefaultLdapConnection; import org.apache.archiva.redback.common.ldap.connection.DefaultLdapConnection;
import org.apache.archiva.redback.common.ldap.connection.LdapConnection;
import org.apache.archiva.redback.common.ldap.user.LdapUser; import org.apache.archiva.redback.common.ldap.user.LdapUser;
import org.apache.archiva.redback.common.ldap.user.UserMapper; import org.apache.archiva.redback.common.ldap.user.UserMapper;
import org.apache.archiva.redback.configuration.UserConfiguration; import org.apache.archiva.redback.configuration.UserConfiguration;
@ -129,7 +130,7 @@ public class LdapUserManager
return guestUser; return guestUser;
} }
DefaultLdapConnection ldapConnection = getLdapConnection(); LdapConnection ldapConnection = getLdapConnection();
try try
{ {
DirContext context = ldapConnection.getDirContext(); DirContext context = ldapConnection.getDirContext();
@ -168,7 +169,7 @@ public class LdapUserManager
{ {
clearFromCache( username ); clearFromCache( username );
} }
DefaultLdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
try try
{ {
ldapConnection = getLdapConnection(); ldapConnection = getLdapConnection();
@ -211,7 +212,7 @@ public class LdapUserManager
return ldapUser; return ldapUser;
} }
DefaultLdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
try try
{ {
@ -278,7 +279,7 @@ public class LdapUserManager
return Collections.emptyList(); return Collections.emptyList();
} }
DefaultLdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
try try
{ {
@ -329,7 +330,7 @@ public class LdapUserManager
*/ */
public List<User> getUsers() public List<User> getUsers()
{ {
DefaultLdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
try try
{ {
@ -385,7 +386,7 @@ public class LdapUserManager
clearFromCache( user.getUsername() ); clearFromCache( user.getUsername() );
} }
DefaultLdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
try try
{ {
@ -429,7 +430,7 @@ public class LdapUserManager
return true; return true;
} }
DefaultLdapConnection ldapConnection = null; LdapConnection ldapConnection = null;
try try
{ {
@ -452,7 +453,7 @@ public class LdapUserManager
} }
} }
private DefaultLdapConnection getLdapConnection() private LdapConnection getLdapConnection()
throws LdapException throws LdapException
{ {
try try
@ -466,7 +467,7 @@ public class LdapUserManager
} }
} }
private void closeLdapConnection( DefaultLdapConnection ldapConnection ) private void closeLdapConnection( LdapConnection ldapConnection )
{ {
if ( ldapConnection != null ) if ( ldapConnection != null )
{ {

View File

@ -20,6 +20,7 @@ package org.apache.archiva.redback.users.ldap;
*/ */
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.archiva.redback.common.ldap.connection.LdapConnection;
import org.apache.archiva.redback.policy.PasswordEncoder; import org.apache.archiva.redback.policy.PasswordEncoder;
import org.apache.archiva.redback.users.User; import org.apache.archiva.redback.users.User;
import org.apache.archiva.redback.common.ldap.connection.DefaultLdapConnection; import org.apache.archiva.redback.common.ldap.connection.DefaultLdapConnection;
@ -147,7 +148,8 @@ public class LdapUserManagerTest
{ {
assertNotNull( connectionFactory ); assertNotNull( connectionFactory );
DefaultLdapConnection connection = null; LdapConnection connection = null;
try try
{ {
connection = connectionFactory.getConnection(); connection = connectionFactory.getConnection();
@ -168,7 +170,8 @@ public class LdapUserManagerTest
public void testDirectUsersExistence() public void testDirectUsersExistence()
throws Exception throws Exception
{ {
DefaultLdapConnection connection = null; LdapConnection connection = null;
try try
{ {
connection = connectionFactory.getConnection(); connection = connectionFactory.getConnection();