1 package org.apache.archiva.web.security;
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
21 import org.apache.archiva.admin.model.RepositoryAdminException;
22 import org.apache.archiva.admin.model.runtime.RedbackRuntimeConfigurationAdmin;
23 import org.apache.archiva.redback.components.cache.Cache;
24 import org.apache.archiva.redback.users.AbstractUserManager;
25 import org.apache.archiva.redback.users.User;
26 import org.apache.archiva.redback.users.UserManager;
27 import org.apache.archiva.redback.users.UserManagerException;
28 import org.apache.archiva.redback.users.UserManagerListener;
29 import org.apache.archiva.redback.users.UserNotFoundException;
30 import org.apache.archiva.redback.users.UserQuery;
31 import org.apache.archiva.redback.users.configurable.ConfigurableUserManager;
32 import org.springframework.context.ApplicationContext;
33 import org.springframework.stereotype.Service;
35 import javax.annotation.PostConstruct;
36 import javax.inject.Inject;
37 import javax.inject.Named;
38 import java.util.ArrayList;
39 import java.util.LinkedHashMap;
40 import java.util.List;
44 * @author Olivier Lamy
47 @Service("userManager#archiva")
48 public class ArchivaConfigurableUsersManager
49 extends AbstractUserManager
53 private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
56 private ApplicationContext applicationContext;
58 private Map<String, UserManager> userManagerPerId;
61 @Named(value = "cache#users")
62 private Cache<String, User> usersCache;
64 private boolean useUsersCache;
67 public void initialize()
71 List<String> userManagerImpls =
72 redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls();
73 log.info( "use userManagerImpls: '{}'", userManagerImpls );
75 userManagerPerId = new LinkedHashMap<String, UserManager>( userManagerImpls.size() );
76 for ( String id : userManagerImpls )
78 UserManager userManagerImpl = applicationContext.getBean( "userManager#" + id, UserManager.class );
79 setUserManagerImpl( userManagerImpl );
80 userManagerPerId.put( id, userManagerImpl );
83 this.useUsersCache = redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().isUseUsersCache();
85 catch ( RepositoryAdminException e )
87 // revert to a default one ?
88 log.error( e.getMessage(), e );
89 throw new RuntimeException( e.getMessage(), e );
93 protected boolean useUsersCache()
95 return this.useUsersCache;
98 public User addUser( User user )
99 throws UserManagerException
101 user = userManagerPerId.get( user.getUserManagerId() ).addUser( user );
103 if ( useUsersCache() )
105 usersCache.put( user.getUsername(), user );
111 public void addUserUnchecked( User user )
112 throws UserManagerException
114 userManagerPerId.get( user.getUserManagerId() ).addUserUnchecked( user );
116 if ( useUsersCache() )
118 usersCache.put( user.getUsername(), user );
122 public User createUser( String username, String fullName, String emailAddress )
123 throws UserManagerException
125 Exception lastException = null;
126 boolean allFailed = true;
128 for ( UserManager userManager : userManagerPerId.values() )
132 if ( !userManager.isReadOnly() )
134 user = userManager.createUser( username, fullName, emailAddress );
138 catch ( Exception e )
143 if ( lastException != null && allFailed )
145 throw new UserManagerException( lastException.getMessage(), lastException );
150 public UserQuery createUserQuery()
152 return userManagerPerId.values().iterator().next().createUserQuery();
156 public void deleteUser( String username )
157 throws UserNotFoundException, UserManagerException
159 Exception lastException = null;
160 boolean allFailed = true;
162 for ( UserManager userManager : userManagerPerId.values() )
166 if ( !userManager.isReadOnly() )
168 userManager.deleteUser( username );
172 catch ( Exception e )
177 if ( lastException != null && allFailed )
179 throw new UserManagerException( lastException.getMessage(), lastException );
183 public void eraseDatabase()
185 for ( UserManager userManager : userManagerPerId.values() )
187 userManager.eraseDatabase();
191 public User findUser( String username )
192 throws UserManagerException
196 if ( useUsersCache() )
198 user = usersCache.get( username );
205 Exception lastException = null;
206 for ( UserManager userManager : userManagerPerId.values() )
210 user = userManager.findUser( username );
213 if ( useUsersCache() )
215 usersCache.put( username, user );
220 catch ( UserNotFoundException e )
224 catch ( Exception e )
232 if ( lastException != null )
234 if ( lastException instanceof UserNotFoundException )
236 throw (UserNotFoundException) lastException;
238 throw new UserManagerException( lastException.getMessage(), lastException );
247 public User getGuestUser()
248 throws UserNotFoundException, UserManagerException
250 return findUser( GUEST_USERNAME );
253 public List<User> findUsersByEmailKey( String emailKey, boolean orderAscending )
254 throws UserManagerException
256 List<User> users = new ArrayList<User>();
258 for ( UserManager userManager : userManagerPerId.values() )
260 List<User> found = userManager.findUsersByEmailKey( emailKey, orderAscending );
263 users.addAll( found );
269 public List<User> findUsersByFullNameKey( String fullNameKey, boolean orderAscending )
270 throws UserManagerException
272 List<User> users = new ArrayList<User>();
274 for ( UserManager userManager : userManagerPerId.values() )
276 List<User> found = userManager.findUsersByFullNameKey( fullNameKey, orderAscending );
279 users.addAll( found );
285 public List<User> findUsersByQuery( UserQuery query )
286 throws UserManagerException
288 List<User> users = new ArrayList<User>();
290 for ( UserManager userManager : userManagerPerId.values() )
292 List<User> found = userManager.findUsersByQuery( query );
295 users.addAll( found );
301 public List<User> findUsersByUsernameKey( String usernameKey, boolean orderAscending )
302 throws UserManagerException
304 List<User> users = new ArrayList<User>();
306 for ( UserManager userManager : userManagerPerId.values() )
308 List<User> found = userManager.findUsersByUsernameKey( usernameKey, orderAscending );
311 users.addAll( found );
317 public String getId()
322 public List<User> getUsers()
323 throws UserManagerException
325 List<User> users = new ArrayList<User>();
327 for ( UserManager userManager : userManagerPerId.values() )
329 List<User> found = userManager.getUsers();
332 users.addAll( found );
338 public List<User> getUsers( boolean orderAscending )
339 throws UserManagerException
341 List<User> users = new ArrayList<User>();
343 for ( UserManager userManager : userManagerPerId.values() )
345 List<User> found = userManager.getUsers( orderAscending );
348 users.addAll( found );
354 public boolean isReadOnly()
356 boolean readOnly = false;
358 for ( UserManager userManager : userManagerPerId.values() )
360 readOnly = readOnly || userManager.isReadOnly();
365 public User updateUser( User user )
366 throws UserNotFoundException, UserManagerException
368 user = userManagerPerId.get( user.getUserManagerId() ).updateUser( user );
370 if ( useUsersCache() )
372 usersCache.put( user.getUsername(), user );
378 public User updateUser( User user, boolean passwordChangeRequired )
379 throws UserNotFoundException, UserManagerException
381 user = userManagerPerId.get( user.getUserManagerId() ).updateUser( user, passwordChangeRequired );
383 if ( useUsersCache() )
385 usersCache.put( user.getUsername(), user );
391 public void setUserManagerImpl( UserManager userManagerImpl )
393 // not possible here but we know so no need of log.error
394 log.debug( "setUserManagerImpl cannot be used in this implementation" );
398 public User createGuestUser()
399 throws UserManagerException
401 Exception lastException = null;
402 boolean allFailed = true;
404 for ( UserManager userManager : userManagerPerId.values() )
408 if ( !userManager.isReadOnly() )
410 user = userManager.createGuestUser();
414 catch ( Exception e )
419 if ( lastException != null && allFailed )
421 throw new UserManagerException( lastException.getMessage(), lastException );
427 public boolean userExists( String userName )
428 throws UserManagerException
430 Exception lastException = null;
431 boolean allFailed = true;
432 boolean exists = false;
433 for ( UserManager userManager : userManagerPerId.values() )
438 if ( userManager.userExists( userName ) )
445 catch ( Exception e )
450 if ( lastException != null && allFailed )
452 throw new UserManagerException( lastException.getMessage(), lastException );
459 public boolean isFinalImplementation()
464 public String getDescriptionKey()
466 return "archiva.redback.usermanager.configurable.archiva";