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.UserManagerListener;
25 import org.apache.archiva.redback.users.UserNotFoundException;
26 import org.apache.archiva.redback.users.UserQuery;
27 import org.apache.archiva.redback.users.UserManager;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.stereotype.Service;
32 import javax.inject.Inject;
33 import javax.inject.Named;
34 import java.util.List;
39 * @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#jdo" )
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()
64 return userImpl.createGuestUser();
67 public User addUser( User user )
71 usersCache.remove( user.getPrincipal() );
73 return this.userImpl.addUser( user );
76 public void addUserManagerListener( UserManagerListener listener )
78 this.userImpl.addUserManagerListener( listener );
81 public void addUserUnchecked( User user )
85 usersCache.remove( user.getPrincipal() );
87 this.userImpl.addUserUnchecked( user );
90 public User createUser( String username, String fullName, String emailAddress )
92 usersCache.remove( username );
93 return this.userImpl.createUser( username, fullName, emailAddress );
96 public void deleteUser( Object principal )
97 throws UserNotFoundException
99 usersCache.remove( principal );
100 this.userImpl.deleteUser( principal );
103 public void deleteUser( String username )
104 throws UserNotFoundException
106 usersCache.remove( username );
107 this.userImpl.deleteUser( username );
110 public void eraseDatabase()
114 this.userImpl.eraseDatabase();
122 public User findUser( String username )
123 throws UserNotFoundException
125 if ( GUEST_USERNAME.equals( username ) )
127 return getGuestUser();
130 Object el = usersCache.get( username );
137 User user = this.userImpl.findUser( username );
138 usersCache.put( username, user );
143 public User getGuestUser()
144 throws UserNotFoundException
146 Object el = usersCache.get( GUEST_USERNAME );
153 User user = this.userImpl.getGuestUser();
154 usersCache.put( GUEST_USERNAME, user );
159 public User findUser( Object principal )
160 throws UserNotFoundException
162 Object el = usersCache.get( principal );
169 User user = this.userImpl.findUser( principal );
170 usersCache.put( principal, user );
175 public UserQuery createUserQuery()
177 return userImpl.createUserQuery();
181 public List<User> findUsersByQuery( UserQuery query )
183 log.debug( "NOT CACHED - .findUsersByQuery(UserQuery)" );
184 return this.userImpl.findUsersByQuery( query );
187 public List<User> findUsersByEmailKey( String emailKey, boolean orderAscending )
189 log.debug( "NOT CACHED - .findUsersByEmailKey(String, boolean)" );
190 return this.userImpl.findUsersByEmailKey( emailKey, orderAscending );
193 public List<User> findUsersByFullNameKey( String fullNameKey, boolean orderAscending )
195 log.debug( "NOT CACHED - .findUsersByFullNameKey(String, boolean)" );
196 return this.userImpl.findUsersByFullNameKey( fullNameKey, orderAscending );
199 public List<User> findUsersByUsernameKey( String usernameKey, boolean orderAscending )
201 log.debug( "NOT CACHED - .findUsersByUsernameKey(String, boolean)" );
202 return this.userImpl.findUsersByUsernameKey( usernameKey, orderAscending );
205 public String getId()
207 return "Cached User Manager [" + this.userImpl.getId() + "]";
210 public List<User> getUsers()
212 log.debug( "NOT CACHED - .getUsers()" );
213 return this.userImpl.getUsers();
216 public List<User> getUsers( boolean orderAscending )
218 log.debug( "NOT CACHED - .getUsers(boolean)" );
219 return this.userImpl.getUsers( orderAscending );
222 public void removeUserManagerListener( UserManagerListener listener )
224 this.userImpl.removeUserManagerListener( listener );
227 public User updateUser( User user )
228 throws UserNotFoundException
230 return updateUser( user, false );
233 public User updateUser( User user, boolean passwordChangeRequired )
234 throws UserNotFoundException
238 usersCache.remove( user.getPrincipal() );
240 return this.userImpl.updateUser( user, passwordChangeRequired );
243 public boolean userExists( Object principal )
245 if ( usersCache.hasKey( principal ) )
250 return this.userImpl.userExists( principal );
253 public void userManagerInit( boolean freshDatabase )
255 if ( userImpl instanceof UserManager )
257 ( (UserManagerListener) this.userImpl ).userManagerInit( freshDatabase );
263 public void userManagerUserAdded( User user )
265 if ( userImpl instanceof UserManager )
267 ( (UserManagerListener) this.userImpl ).userManagerUserAdded( user );
272 usersCache.remove( user.getPrincipal() );
276 public void userManagerUserRemoved( User user )
278 if ( userImpl instanceof UserManager )
280 ( (UserManagerListener) this.userImpl ).userManagerUserRemoved( user );
285 usersCache.remove( user.getPrincipal() );
289 public void userManagerUserUpdated( User user )
291 if ( userImpl instanceof UserManager )
293 ( (UserManagerListener) this.userImpl ).userManagerUserUpdated( user );
298 usersCache.remove( user.getPrincipal() );
302 public UserManager getUserImpl()
307 public void setUserImpl( UserManager userImpl )
309 this.userImpl = userImpl;
312 public Cache getUsersCache()
317 public void setUsersCache( Cache usersCache )
319 this.usersCache = usersCache;