1 package org.apache.archiva.redback.users.cached;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.redback.components.cache.Cache;
23 import org.apache.archiva.redback.users.User;
24 import org.apache.archiva.redback.users.UserManagerException;
25 import org.apache.archiva.redback.users.UserManagerListener;
26 import org.apache.archiva.redback.users.UserNotFoundException;
27 import org.apache.archiva.redback.users.UserQuery;
28 import org.apache.archiva.redback.users.UserManager;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.stereotype.Service;
33 import javax.inject.Inject;
34 import javax.inject.Named;
35 import java.util.List;
40 * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
42 @Service( "userManager#cached" )
43 public class CachedUserManager
44 implements UserManager, UserManagerListener
47 private Logger log = LoggerFactory.getLogger( getClass() );
50 @Named( value = "userManager#configurable" )
51 private UserManager userImpl;
54 @Named( value = "cache#users" )
55 private Cache usersCache;
57 public boolean isReadOnly()
59 return userImpl.isReadOnly();
62 public User createGuestUser()
63 throws UserManagerException
65 return userImpl.createGuestUser();
68 public User addUser( User user )
69 throws UserManagerException
73 usersCache.remove( user.getUsername() );
75 return this.userImpl.addUser( user );
78 public void addUserManagerListener( UserManagerListener listener )
80 this.userImpl.addUserManagerListener( listener );
83 public void addUserUnchecked( User user )
84 throws UserManagerException
88 usersCache.remove( user.getUsername() );
90 this.userImpl.addUserUnchecked( user );
93 public User createUser( String username, String fullName, String emailAddress )
94 throws UserManagerException
96 usersCache.remove( username );
97 return this.userImpl.createUser( username, fullName, emailAddress );
100 public void deleteUser( String username )
101 throws UserNotFoundException, UserManagerException
103 usersCache.remove( username );
104 this.userImpl.deleteUser( username );
107 public void eraseDatabase()
111 this.userImpl.eraseDatabase();
119 public User findUser( String username )
120 throws UserNotFoundException, UserManagerException
122 if ( GUEST_USERNAME.equals( username ) )
124 return getGuestUser();
127 Object el = usersCache.get( username );
134 User user = this.userImpl.findUser( username );
135 usersCache.put( username, user );
140 public User getGuestUser()
141 throws UserNotFoundException, UserManagerException
143 Object el = usersCache.get( GUEST_USERNAME );
150 User user = this.userImpl.getGuestUser();
151 usersCache.put( GUEST_USERNAME, user );
156 public UserQuery createUserQuery()
158 return userImpl.createUserQuery();
162 public List<User> findUsersByQuery( UserQuery query )
163 throws UserManagerException
165 log.debug( "NOT CACHED - .findUsersByQuery(UserQuery)" );
166 return this.userImpl.findUsersByQuery( query );
169 public List<User> findUsersByEmailKey( String emailKey, boolean orderAscending )
170 throws UserManagerException
172 log.debug( "NOT CACHED - .findUsersByEmailKey(String, boolean)" );
173 return this.userImpl.findUsersByEmailKey( emailKey, orderAscending );
176 public List<User> findUsersByFullNameKey( String fullNameKey, boolean orderAscending )
177 throws UserManagerException
179 log.debug( "NOT CACHED - .findUsersByFullNameKey(String, boolean)" );
180 return this.userImpl.findUsersByFullNameKey( fullNameKey, orderAscending );
183 public List<User> findUsersByUsernameKey( String usernameKey, boolean orderAscending )
184 throws UserManagerException
186 log.debug( "NOT CACHED - .findUsersByUsernameKey(String, boolean)" );
187 return this.userImpl.findUsersByUsernameKey( usernameKey, orderAscending );
190 public String getId()
195 public List<User> getUsers()
196 throws UserManagerException
198 log.debug( "NOT CACHED - .getUsers()" );
199 return this.userImpl.getUsers();
202 public List<User> getUsers( boolean orderAscending )
203 throws UserManagerException
205 log.debug( "NOT CACHED - .getUsers(boolean)" );
206 return this.userImpl.getUsers( orderAscending );
209 public void removeUserManagerListener( UserManagerListener listener )
211 this.userImpl.removeUserManagerListener( listener );
214 public User updateUser( User user )
215 throws UserNotFoundException, UserManagerException
217 return updateUser( user, false );
220 public User updateUser( User user, boolean passwordChangeRequired )
221 throws UserNotFoundException, UserManagerException
225 usersCache.remove( user.getUsername() );
227 return this.userImpl.updateUser( user, passwordChangeRequired );
230 public boolean userExists( String userName )
231 throws UserManagerException
233 if ( usersCache.hasKey( userName ) )
238 return this.userImpl.userExists( userName );
241 public void userManagerInit( boolean freshDatabase )
243 if ( userImpl instanceof UserManager )
245 ( (UserManagerListener) this.userImpl ).userManagerInit( freshDatabase );
251 public void userManagerUserAdded( User user )
253 if ( userImpl instanceof UserManager )
255 ( (UserManagerListener) this.userImpl ).userManagerUserAdded( user );
260 usersCache.remove( user.getUsername() );
264 public void userManagerUserRemoved( User user )
266 if ( userImpl instanceof UserManager )
268 ( (UserManagerListener) this.userImpl ).userManagerUserRemoved( user );
273 usersCache.remove( user.getUsername() );
277 public void userManagerUserUpdated( User user )
279 if ( userImpl instanceof UserManager )
281 ( (UserManagerListener) this.userImpl ).userManagerUserUpdated( user );
286 usersCache.remove( user.getUsername() );
290 public UserManager getUserImpl()
295 public void setUserImpl( UserManager userImpl )
297 this.userImpl = userImpl;
300 public Cache getUsersCache()
305 public void setUsersCache( Cache usersCache )
307 this.usersCache = usersCache;
310 public void initialize()
312 // no op configurable impl do the job
315 public boolean isFinalImplementation()
320 public String getDescriptionKey()
322 return "archiva.redback.usermanager.cached";