This project has retired. For details please refer to its Attic page.
CachedUserManager xref
View Javadoc

1   package org.apache.archiva.redback.users.cached;
2   
3   /*
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
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
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;
32  
33  import javax.inject.Inject;
34  import javax.inject.Named;
35  import java.util.List;
36  
37  /**
38   * CachedUserManager
39   *
40   * @author <a href="mailto:joakim@erdfelt.com">Joakim Erdfelt</a>
41   */
42  @Service("userManager#cached")
43  public class CachedUserManager
44      implements UserManager, UserManagerListener
45  {
46  
47      private Logger log = LoggerFactory.getLogger( getClass() );
48  
49      @Inject
50      @Named(value = "userManager#default")
51      private UserManager userImpl;
52  
53      @Inject
54      @Named(value = "cache#users")
55      private Cache<String, User> usersCache;
56  
57      public boolean isReadOnly()
58      {
59          return userImpl.isReadOnly();
60      }
61  
62      public User createGuestUser()
63          throws UserManagerException
64      {
65          return userImpl.createGuestUser();
66      }
67  
68      public User addUser( User user )
69          throws UserManagerException
70      {
71          if ( user != null )
72          {
73              usersCache.remove( user.getUsername() );
74          }
75          return this.userImpl.addUser( user );
76      }
77  
78      public void addUserManagerListener( UserManagerListener listener )
79      {
80          this.userImpl.addUserManagerListener( listener );
81      }
82  
83      public void addUserUnchecked( User user )
84          throws UserManagerException
85      {
86          if ( user != null )
87          {
88              usersCache.remove( user.getUsername() );
89          }
90          this.userImpl.addUserUnchecked( user );
91      }
92  
93      public User createUser( String username, String fullName, String emailAddress )
94          throws UserManagerException
95      {
96          usersCache.remove( username );
97          return this.userImpl.createUser( username, fullName, emailAddress );
98      }
99  
100     public void deleteUser( String username )
101         throws UserNotFoundException, UserManagerException
102     {
103         usersCache.remove( username );
104         this.userImpl.deleteUser( username );
105     }
106 
107     public void eraseDatabase()
108     {
109         try
110         {
111             this.userImpl.eraseDatabase();
112         }
113         finally
114         {
115             usersCache.clear();
116         }
117     }
118 
119     public User findUser( String username )
120         throws UserNotFoundException, UserManagerException
121     {
122         if ( GUEST_USERNAME.equals( username ) )
123         {
124             return getGuestUser();
125         }
126 
127         User el = usersCache.get( username );
128         if ( el != null )
129         {
130             return el;
131         }
132         else
133         {
134             User user = this.userImpl.findUser( username );
135             usersCache.put( username, user );
136             return user;
137         }
138     }
139 
140     public User getGuestUser()
141         throws UserNotFoundException, UserManagerException
142     {
143         User el = usersCache.get( GUEST_USERNAME );
144         if ( el != null )
145         {
146             return el;
147         }
148         else
149         {
150             User user = this.userImpl.getGuestUser();
151             usersCache.put( GUEST_USERNAME, user );
152             return user;
153         }
154     }
155 
156     public UserQuery createUserQuery()
157     {
158         return userImpl.createUserQuery();
159     }
160 
161 
162     public List<User> findUsersByQuery( UserQuery query )
163         throws UserManagerException
164     {
165         log.debug( "NOT CACHED - .findUsersByQuery(UserQuery)" );
166         return this.userImpl.findUsersByQuery( query );
167     }
168 
169     public List<User> findUsersByEmailKey( String emailKey, boolean orderAscending )
170         throws UserManagerException
171     {
172         log.debug( "NOT CACHED - .findUsersByEmailKey(String, boolean)" );
173         return this.userImpl.findUsersByEmailKey( emailKey, orderAscending );
174     }
175 
176     public List<User> findUsersByFullNameKey( String fullNameKey, boolean orderAscending )
177         throws UserManagerException
178     {
179         log.debug( "NOT CACHED - .findUsersByFullNameKey(String, boolean)" );
180         return this.userImpl.findUsersByFullNameKey( fullNameKey, orderAscending );
181     }
182 
183     public List<User> findUsersByUsernameKey( String usernameKey, boolean orderAscending )
184         throws UserManagerException
185     {
186         log.debug( "NOT CACHED - .findUsersByUsernameKey(String, boolean)" );
187         return this.userImpl.findUsersByUsernameKey( usernameKey, orderAscending );
188     }
189 
190     public String getId()
191     {
192         return "cached";
193     }
194 
195     public List<User> getUsers()
196         throws UserManagerException
197     {
198         log.debug( "NOT CACHED - .getUsers()" );
199         return this.userImpl.getUsers();
200     }
201 
202     public List<User> getUsers( boolean orderAscending )
203         throws UserManagerException
204     {
205         log.debug( "NOT CACHED - .getUsers(boolean)" );
206         return this.userImpl.getUsers( orderAscending );
207     }
208 
209     public void removeUserManagerListener( UserManagerListener listener )
210     {
211         this.userImpl.removeUserManagerListener( listener );
212     }
213 
214     public User updateUser( User user )
215         throws UserNotFoundException, UserManagerException
216     {
217         return updateUser( user, false );
218     }
219 
220     public User updateUser( User user, boolean passwordChangeRequired )
221         throws UserNotFoundException, UserManagerException
222     {
223         if ( user != null )
224         {
225             usersCache.remove( user.getUsername() );
226         }
227         return this.userImpl.updateUser( user, passwordChangeRequired );
228     }
229 
230     public boolean userExists( String userName )
231         throws UserManagerException
232     {
233         if ( usersCache.hasKey( userName ) )
234         {
235             return true;
236         }
237 
238         return this.userImpl.userExists( userName );
239     }
240 
241     public void userManagerInit( boolean freshDatabase )
242     {
243         if ( userImpl instanceof UserManager )
244         {
245             ( (UserManagerListener) this.userImpl ).userManagerInit( freshDatabase );
246         }
247 
248         usersCache.clear();
249     }
250 
251     public void userManagerUserAdded( User user )
252     {
253         if ( userImpl instanceof UserManager )
254         {
255             ( (UserManagerListener) this.userImpl ).userManagerUserAdded( user );
256         }
257 
258         if ( user != null )
259         {
260             usersCache.remove( user.getUsername() );
261         }
262     }
263 
264     public void userManagerUserRemoved( User user )
265     {
266         if ( userImpl instanceof UserManager )
267         {
268             ( (UserManagerListener) this.userImpl ).userManagerUserRemoved( user );
269         }
270 
271         if ( user != null )
272         {
273             usersCache.remove( user.getUsername() );
274         }
275     }
276 
277     public void userManagerUserUpdated( User user )
278     {
279         if ( userImpl instanceof UserManager )
280         {
281             ( (UserManagerListener) this.userImpl ).userManagerUserUpdated( user );
282         }
283 
284         if ( user != null )
285         {
286             usersCache.remove( user.getUsername() );
287         }
288     }
289 
290     public UserManager getUserImpl()
291     {
292         return userImpl;
293     }
294 
295     public void setUserImpl( UserManager userImpl )
296     {
297         this.userImpl = userImpl;
298     }
299 
300     public Cache getUsersCache()
301     {
302         return usersCache;
303     }
304 
305     public void setUsersCache( Cache usersCache )
306     {
307         this.usersCache = usersCache;
308     }
309 
310     public void initialize()
311     {
312         // no op configurable impl do the job
313     }
314 
315     public boolean isFinalImplementation()
316     {
317         return false;
318     }
319 
320     public String getDescriptionKey()
321     {
322         return "archiva.redback.usermanager.cached";
323     }
324 }