From 4a57b05074480aa469551fa8b60263b6da5212e1 Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Wed, 1 May 2013 12:19:20 +0000 Subject: [PATCH] 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 --- .../ldap/LdapBindAuthenticator.java | 9 ++-- .../connection/DefaultLdapConnection.java | 7 +++ .../ldap/connection/LdapConnection.java | 42 +++++++++++++++++ .../connection/LdapConnectionFactory.java | 8 ++-- .../common/ldap/role/TestLdapRoleMapper.java | 5 +- .../DefaultLdapGroupMappingService.java | 5 +- .../redback/rbac/ldap/LdapRbacManager.java | 46 +++++++++---------- .../redback/users/ldap/LdapUserManager.java | 19 ++++---- .../users/ldap/LdapUserManagerTest.java | 7 ++- 9 files changed, 102 insertions(+), 46 deletions(-) create mode 100644 redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/LdapConnection.java diff --git a/redback-authentication/redback-authentication-providers/redback-authentication-ldap/src/main/java/org/apache/archiva/redback/authentication/ldap/LdapBindAuthenticator.java b/redback-authentication/redback-authentication-providers/redback-authentication-ldap/src/main/java/org/apache/archiva/redback/authentication/ldap/LdapBindAuthenticator.java index fafe25c0..474b7ab3 100644 --- a/redback-authentication/redback-authentication-providers/redback-authentication-ldap/src/main/java/org/apache/archiva/redback/authentication/ldap/LdapBindAuthenticator.java +++ b/redback-authentication/redback-authentication-providers/redback-authentication-ldap/src/main/java/org/apache/archiva/redback/authentication/ldap/LdapBindAuthenticator.java @@ -21,6 +21,7 @@ package org.apache.archiva.redback.authentication.ldap; import org.apache.archiva.redback.authentication.AbstractAuthenticator; 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.connection.LdapConnectionFactory; 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() ); - DefaultLdapConnection ldapConnection = null; - DefaultLdapConnection authLdapConnection = null; + LdapConnection ldapConnection = null; + LdapConnection authLdapConnection = null; NamingEnumeration results = null; try { @@ -172,13 +173,13 @@ public class LdapBindAuthenticator return ( source instanceof PasswordBasedAuthenticationDataSource ); } - private DefaultLdapConnection getLdapConnection() + private LdapConnection getLdapConnection() throws LdapException { return connectionFactory.getConnection(); } - private void closeLdapConnection( DefaultLdapConnection ldapConnection ) + private void closeLdapConnection( LdapConnection ldapConnection ) { if ( ldapConnection != null ) { diff --git a/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/DefaultLdapConnection.java b/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/DefaultLdapConnection.java index 0c9a6cf0..a4474230 100644 --- a/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/DefaultLdapConnection.java +++ b/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/DefaultLdapConnection.java @@ -38,8 +38,10 @@ import java.util.Properties; * The configuration for a connection will not change. * * @author trygvis + * @since 2.1 */ public class DefaultLdapConnection + implements LdapConnection { private static LdapCtxFactory ctxFactory;// = new LdapCtxFactory(); @@ -132,6 +134,7 @@ public class DefaultLdapConnection // Connection Managment // ---------------------------------------------------------------------- + @Override public Hashtable getEnvironment() throws LdapException { @@ -236,6 +239,7 @@ public class DefaultLdapConnection return env; } + @Override public void close() { try @@ -259,16 +263,19 @@ public class DefaultLdapConnection // Utils // ---------------------------------------------------------------------- + @Override public LdapConnectionConfiguration getConfiguration() { return config; } + @Override public List getBaseDnRdns() { return baseDnRdns; } + @Override public DirContext getDirContext() { return context; diff --git a/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/LdapConnection.java b/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/LdapConnection.java new file mode 100644 index 00000000..2b7243c5 --- /dev/null +++ b/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/LdapConnection.java @@ -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 getEnvironment() + throws LdapException; + + void close(); + + LdapConnectionConfiguration getConfiguration(); + + List getBaseDnRdns(); + + DirContext getDirContext(); +} diff --git a/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/LdapConnectionFactory.java b/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/LdapConnectionFactory.java index 8493cc52..d81b3e23 100644 --- a/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/LdapConnectionFactory.java +++ b/redback-common/redback-common-ldap/src/main/java/org/apache/archiva/redback/common/ldap/connection/LdapConnectionFactory.java @@ -30,16 +30,16 @@ import javax.naming.spi.StateFactory; */ public interface LdapConnectionFactory { - DefaultLdapConnection getConnection() + LdapConnection getConnection() throws LdapException; - DefaultLdapConnection getConnection( LdapConnectionConfiguration ldapConnectionConfiguration ) + LdapConnection getConnection( LdapConnectionConfiguration ldapConnectionConfiguration ) throws LdapException; - DefaultLdapConnection getConnection( Rdn subRdn ) + LdapConnection getConnection( Rdn subRdn ) throws LdapException; - DefaultLdapConnection getConnection( String bindDn, String password ) + LdapConnection getConnection( String bindDn, String password ) throws LdapException; LdapName getBaseDnLdapName() diff --git a/redback-common/redback-common-ldap/src/test/java/org/apache/archiva/redback/common/ldap/role/TestLdapRoleMapper.java b/redback-common/redback-common-ldap/src/test/java/org/apache/archiva/redback/common/ldap/role/TestLdapRoleMapper.java index 79bd43d9..7838373d 100644 --- a/redback-common/redback-common-ldap/src/test/java/org/apache/archiva/redback/common/ldap/role/TestLdapRoleMapper.java +++ b/redback-common/redback-common-ldap/src/test/java/org/apache/archiva/redback/common/ldap/role/TestLdapRoleMapper.java @@ -1,4 +1,5 @@ package org.apache.archiva.redback.common.ldap.role; + /* * Licensed to the Apache Software Foundation (ASF) under one * 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 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.components.apacheds.ApacheDs; import org.apache.archiva.redback.policy.PasswordEncoder; @@ -89,7 +90,7 @@ public class TestLdapRoleMapper List roleNames = Arrays.asList( "Archiva System Administrator", "Internal Repo Manager", "Internal Repo Observer" ); - DefaultLdapConnection ldapConnection; + LdapConnection ldapConnection; DirContext context; diff --git a/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/DefaultLdapGroupMappingService.java b/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/DefaultLdapGroupMappingService.java index 8232077b..319bd53b 100644 --- a/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/DefaultLdapGroupMappingService.java +++ b/redback-integrations/redback-rest/redback-rest-services/src/main/java/org/apache/archiva/redback/rest/services/DefaultLdapGroupMappingService.java @@ -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.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.LdapException; import org.apache.archiva.redback.common.ldap.role.LdapRoleMapper; @@ -67,7 +68,7 @@ public class DefaultLdapGroupMappingService public StringList getLdapGroups() throws RedbackServiceException { - DefaultLdapConnection ldapConnection = null; + LdapConnection ldapConnection = null; DirContext context = null; @@ -170,7 +171,7 @@ public class DefaultLdapGroupMappingService // utils //------------------ - protected void closeLdapConnection( DefaultLdapConnection ldapConnection ) + protected void closeLdapConnection( LdapConnection ldapConnection ) { if ( ldapConnection != null ) { diff --git a/redback-rbac/redback-rbac-providers/redback-rbac-ldap/src/main/java/org/apache/archiva/redback/rbac/ldap/LdapRbacManager.java b/redback-rbac/redback-rbac-providers/redback-rbac-ldap/src/main/java/org/apache/archiva/redback/rbac/ldap/LdapRbacManager.java index cdc9cbfe..dda7283f 100644 --- a/redback-rbac/redback-rbac-providers/redback-rbac-ldap/src/main/java/org/apache/archiva/redback/rbac/ldap/LdapRbacManager.java +++ b/redback-rbac/redback-rbac-providers/redback-rbac-ldap/src/main/java/org/apache/archiva/redback/rbac/ldap/LdapRbacManager.java @@ -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.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.LdapException; import org.apache.archiva.redback.common.ldap.role.LdapRoleMapper; @@ -71,7 +71,7 @@ import java.util.Set; * * @author Olivier Lamy */ -@Service( "rbacManager#ldap" ) +@Service("rbacManager#ldap") public class LdapRbacManager extends AbstractRBACManager implements RBACManager, RBACManagerListener @@ -80,19 +80,19 @@ public class LdapRbacManager private Logger log = LoggerFactory.getLogger( getClass() ); @Inject - @Named( value = "rbacManager#cached" ) + @Named(value = "rbacManager#cached") private RBACManager rbacImpl; @Inject - @Named( value = "ldapRoleMapper#default" ) + @Named(value = "ldapRoleMapper#default") private LdapRoleMapper ldapRoleMapper; @Inject - @Named( value = "userConfiguration#default" ) + @Named(value = "userConfiguration#default") private UserConfiguration userConf; @Inject - @Named( value = "userManager#ldap" ) + @Named(value = "userManager#ldap") private UserManager userManager; @Inject @@ -102,15 +102,15 @@ public class LdapRbacManager private LdapController ldapController; @Inject - @Named( value = "ldapRoleMapperConfiguration#default" ) + @Named(value = "ldapRoleMapperConfiguration#default") private LdapRoleMapperConfiguration ldapRoleMapperConfiguration; @Inject - @Named( value = "cache#ldapRoles" ) + @Named(value = "cache#ldapRoles") private Cache rolesCache; @Inject - @Named( value = "cache#userAssignments" ) + @Named(value = "cache#userAssignments") private Cache userAssignmentsCache; private boolean writableLdap = false; @@ -174,7 +174,7 @@ public class LdapRbacManager { if ( writableLdap ) { - DefaultLdapConnection ldapConnection = null; + LdapConnection ldapConnection = null; DirContext context = null; try { @@ -250,7 +250,7 @@ public class LdapRbacManager public List getAllRoles() throws RbacManagerException { - DefaultLdapConnection ldapConnection = null; + LdapConnection ldapConnection = null; DirContext context = null; try { @@ -280,7 +280,7 @@ public class LdapRbacManager public List getAllUserAssignments() throws RbacManagerException { - DefaultLdapConnection ldapConnection = null; + LdapConnection ldapConnection = null; DirContext context = null; try { @@ -313,7 +313,7 @@ public class LdapRbacManager } } - protected void closeLdapConnection( DefaultLdapConnection ldapConnection ) + protected void closeLdapConnection( LdapConnection ldapConnection ) { if ( ldapConnection != null ) { @@ -420,7 +420,7 @@ public class LdapRbacManager throws RbacManagerException { - DefaultLdapConnection ldapConnection = null; + LdapConnection ldapConnection = null; DirContext context = null; try @@ -528,7 +528,7 @@ public class LdapRbacManager { return role; } - DefaultLdapConnection ldapConnection = null; + LdapConnection ldapConnection = null; DirContext context = null; //verify it's a ldap group try @@ -565,7 +565,7 @@ public class LdapRbacManager public Collection getUnassignedRoles( String username ) throws RbacManagerException { - DefaultLdapConnection ldapConnection = null; + LdapConnection ldapConnection = null; DirContext context = null; @@ -613,7 +613,7 @@ public class LdapRbacManager { return ua; } - DefaultLdapConnection ldapConnection = null; + LdapConnection ldapConnection = null; DirContext context = null; try { @@ -786,7 +786,7 @@ public class LdapRbacManager rolesCache.remove( role.getName() ); if ( writableLdap ) { - DefaultLdapConnection ldapConnection = null; + LdapConnection ldapConnection = null; DirContext context = null; try { @@ -868,7 +868,7 @@ public class LdapRbacManager { return true; } - DefaultLdapConnection ldapConnection = null; + LdapConnection ldapConnection = null; DirContext context = null; try { @@ -918,7 +918,7 @@ public class LdapRbacManager { if ( writableLdap ) { - DefaultLdapConnection ldapConnection = null; + LdapConnection ldapConnection = null; DirContext context = null; try { @@ -956,7 +956,7 @@ public class LdapRbacManager { if ( writableLdap ) { - DefaultLdapConnection ldapConnection = null; + LdapConnection ldapConnection = null; DirContext context = null; try { @@ -985,7 +985,7 @@ public class LdapRbacManager public UserAssignment saveUserAssignment( UserAssignment userAssignment ) throws RbacManagerException { - DefaultLdapConnection ldapConnection = null; + LdapConnection ldapConnection = null; DirContext context = null; try { @@ -1052,7 +1052,7 @@ public class LdapRbacManager { return true; } - DefaultLdapConnection ldapConnection = null; + LdapConnection ldapConnection = null; DirContext context = null; try { diff --git a/redback-users/redback-users-providers/redback-users-ldap/src/main/java/org/apache/archiva/redback/users/ldap/LdapUserManager.java b/redback-users/redback-users-providers/redback-users-ldap/src/main/java/org/apache/archiva/redback/users/ldap/LdapUserManager.java index c0a64aed..dbaa9b83 100644 --- a/redback-users/redback-users-providers/redback-users-ldap/src/main/java/org/apache/archiva/redback/users/ldap/LdapUserManager.java +++ b/redback-users/redback-users-providers/redback-users-ldap/src/main/java/org/apache/archiva/redback/users/ldap/LdapUserManager.java @@ -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.LdapConnection; import org.apache.archiva.redback.common.ldap.user.LdapUser; import org.apache.archiva.redback.common.ldap.user.UserMapper; import org.apache.archiva.redback.configuration.UserConfiguration; @@ -129,7 +130,7 @@ public class LdapUserManager return guestUser; } - DefaultLdapConnection ldapConnection = getLdapConnection(); + LdapConnection ldapConnection = getLdapConnection(); try { DirContext context = ldapConnection.getDirContext(); @@ -168,7 +169,7 @@ public class LdapUserManager { clearFromCache( username ); } - DefaultLdapConnection ldapConnection = null; + LdapConnection ldapConnection = null; try { ldapConnection = getLdapConnection(); @@ -211,7 +212,7 @@ public class LdapUserManager return ldapUser; } - DefaultLdapConnection ldapConnection = null; + LdapConnection ldapConnection = null; try { @@ -278,7 +279,7 @@ public class LdapUserManager return Collections.emptyList(); } - DefaultLdapConnection ldapConnection = null; + LdapConnection ldapConnection = null; try { @@ -329,7 +330,7 @@ public class LdapUserManager */ public List getUsers() { - DefaultLdapConnection ldapConnection = null; + LdapConnection ldapConnection = null; try { @@ -385,7 +386,7 @@ public class LdapUserManager clearFromCache( user.getUsername() ); } - DefaultLdapConnection ldapConnection = null; + LdapConnection ldapConnection = null; try { @@ -429,7 +430,7 @@ public class LdapUserManager return true; } - DefaultLdapConnection ldapConnection = null; + LdapConnection ldapConnection = null; try { @@ -452,7 +453,7 @@ public class LdapUserManager } } - private DefaultLdapConnection getLdapConnection() + private LdapConnection getLdapConnection() throws LdapException { try @@ -466,7 +467,7 @@ public class LdapUserManager } } - private void closeLdapConnection( DefaultLdapConnection ldapConnection ) + private void closeLdapConnection( LdapConnection ldapConnection ) { if ( ldapConnection != null ) { diff --git a/redback-users/redback-users-providers/redback-users-ldap/src/test/java/org/apache/archiva/redback/users/ldap/LdapUserManagerTest.java b/redback-users/redback-users-providers/redback-users-ldap/src/test/java/org/apache/archiva/redback/users/ldap/LdapUserManagerTest.java index aec6d7d6..bc54623d 100644 --- a/redback-users/redback-users-providers/redback-users-ldap/src/test/java/org/apache/archiva/redback/users/ldap/LdapUserManagerTest.java +++ b/redback-users/redback-users-providers/redback-users-ldap/src/test/java/org/apache/archiva/redback/users/ldap/LdapUserManagerTest.java @@ -20,6 +20,7 @@ package org.apache.archiva.redback.users.ldap; */ 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.users.User; import org.apache.archiva.redback.common.ldap.connection.DefaultLdapConnection; @@ -147,7 +148,8 @@ public class LdapUserManagerTest { assertNotNull( connectionFactory ); - DefaultLdapConnection connection = null; + LdapConnection connection = null; + try { connection = connectionFactory.getConnection(); @@ -168,7 +170,8 @@ public class LdapUserManagerTest public void testDirectUsersExistence() throws Exception { - DefaultLdapConnection connection = null; + LdapConnection connection = null; + try { connection = connectionFactory.getConnection();