This project has retired. For details please refer to its
Attic page.
ArchivaConfigurableUsersManager xref
1 package org.apache.archiva.web.security;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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.UserNotFoundException;
29 import org.apache.archiva.redback.users.UserQuery;
30 import org.springframework.context.ApplicationContext;
31 import org.springframework.stereotype.Service;
32
33 import javax.annotation.PostConstruct;
34 import javax.inject.Inject;
35 import javax.inject.Named;
36 import java.util.ArrayList;
37 import java.util.LinkedHashMap;
38 import java.util.List;
39 import java.util.Map;
40
41
42
43
44
45 @Service("userManager#archiva")
46 public class ArchivaConfigurableUsersManager
47 extends AbstractUserManager
48 {
49
50 @Inject
51 private RedbackRuntimeConfigurationAdmin redbackRuntimeConfigurationAdmin;
52
53 @Inject
54 private ApplicationContext applicationContext;
55
56 private Map<String, UserManager> userManagerPerId;
57
58 @Inject
59 @Named(value = "cache#users")
60 private Cache<String, User> usersCache;
61
62 private boolean useUsersCache;
63
64 @PostConstruct
65 @Override
66 public void initialize()
67 {
68 try
69 {
70 List<String> userManagerImpls =
71 redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().getUserManagerImpls();
72 log.info( "use userManagerImpls: '{}'", userManagerImpls );
73
74 userManagerPerId = new LinkedHashMap<>( userManagerImpls.size() );
75 for ( String id : userManagerImpls )
76 {
77 UserManager userManagerImpl = applicationContext.getBean( "userManager#" + id, UserManager.class );
78 setUserManagerImpl( userManagerImpl );
79 userManagerPerId.put( id, userManagerImpl );
80 }
81
82 this.useUsersCache = redbackRuntimeConfigurationAdmin.getRedbackRuntimeConfiguration().isUseUsersCache();
83 }
84 catch ( RepositoryAdminException e )
85 {
86
87 log.error( e.getMessage(), e );
88 throw new RuntimeException( e.getMessage(), e );
89 }
90 }
91
92 protected boolean useUsersCache()
93 {
94 return this.useUsersCache;
95 }
96
97 @Override
98 public User addUser( User user )
99 throws UserManagerException
100 {
101 user = userManagerPerId.get( user.getUserManagerId() ).addUser( user );
102
103 if ( useUsersCache() )
104 {
105 usersCache.put( user.getUsername(), user );
106 }
107
108 return user;
109 }
110
111 @Override
112 public void addUserUnchecked( User user )
113 throws UserManagerException
114 {
115 userManagerPerId.get( user.getUserManagerId() ).addUserUnchecked( user );
116
117 if ( useUsersCache() )
118 {
119 usersCache.put( user.getUsername(), user );
120 }
121 }
122
123 @Override
124 public User createUser( String username, String fullName, String emailAddress )
125 throws UserManagerException
126 {
127 Exception lastException = null;
128 boolean allFailed = true;
129 User user = null;
130 for ( UserManager userManager : userManagerPerId.values() )
131 {
132 try
133 {
134 if ( !userManager.isReadOnly() )
135 {
136 user = userManager.createUser( username, fullName, emailAddress );
137 allFailed = false;
138 }
139 }
140 catch ( Exception e )
141 {
142 lastException = e;
143 }
144 }
145 if ( lastException != null && allFailed )
146 {
147 throw new UserManagerException( lastException.getMessage(), lastException );
148 }
149 return user;
150 }
151
152 @Override
153 public UserQuery createUserQuery()
154 {
155 return userManagerPerId.values().iterator().next().createUserQuery();
156 }
157
158
159 @Override
160 public void deleteUser( String username )
161 throws UserNotFoundException, UserManagerException
162 {
163 Exception lastException = null;
164 boolean allFailed = true;
165 User user = null;
166 for ( UserManager userManager : userManagerPerId.values() )
167 {
168 try
169 {
170 if ( !userManager.isReadOnly() )
171 {
172 userManager.deleteUser( username );
173 allFailed = false;
174 }
175 }
176 catch ( Exception e )
177 {
178 lastException = e;
179 }
180 }
181 if ( lastException != null && allFailed )
182 {
183 throw new UserManagerException( lastException.getMessage(), lastException );
184 }
185 }
186
187 @Override
188 public void eraseDatabase()
189 {
190 for ( UserManager userManager : userManagerPerId.values() )
191 {
192 userManager.eraseDatabase();
193 }
194 }
195
196 @Override
197 public User findUser( String username, boolean useCache )
198 throws UserNotFoundException, UserManagerException
199 {
200 User user = null;
201 if ( useUsersCache() && useCache )
202 {
203 user = usersCache.get( username );
204 if ( user != null )
205 {
206 return user;
207 }
208
209 }
210 Exception lastException = null;
211 for ( UserManager userManager : userManagerPerId.values() )
212 {
213 try
214 {
215 user = userManager.findUser( username );
216 if ( user != null )
217 {
218 if ( useUsersCache() )
219 {
220 usersCache.put( username, user );
221 }
222 return user;
223 }
224 }
225 catch ( UserNotFoundException e )
226 {
227 lastException = e;
228 }
229 catch ( Exception e )
230 {
231 lastException = e;
232 }
233 }
234
235 if ( user == null )
236 {
237 if ( lastException != null )
238 {
239 if ( lastException instanceof UserNotFoundException )
240 {
241 throw (UserNotFoundException) lastException;
242 }
243 throw new UserManagerException( lastException.getMessage(), lastException );
244 }
245 }
246
247 return user;
248 }
249
250 @Override
251 public User findUser( String username )
252 throws UserManagerException
253 {
254 return findUser( username, useUsersCache() );
255 }
256
257
258 @Override
259 public User getGuestUser()
260 throws UserNotFoundException, UserManagerException
261 {
262 return findUser( GUEST_USERNAME );
263 }
264
265 @Override
266 public List<User> findUsersByEmailKey( String emailKey, boolean orderAscending )
267 throws UserManagerException
268 {
269 List<User> users = new ArrayList<>();
270
271 for ( UserManager userManager : userManagerPerId.values() )
272 {
273 List<User> found = userManager.findUsersByEmailKey( emailKey, orderAscending );
274 if ( found != null )
275 {
276 users.addAll( found );
277 }
278 }
279 return users;
280 }
281
282 @Override
283 public List<User> findUsersByFullNameKey( String fullNameKey, boolean orderAscending )
284 throws UserManagerException
285 {
286 List<User> users = new ArrayList<>();
287
288 for ( UserManager userManager : userManagerPerId.values() )
289 {
290 List<User> found = userManager.findUsersByFullNameKey( fullNameKey, orderAscending );
291 if ( found != null )
292 {
293 users.addAll( found );
294 }
295 }
296 return users;
297 }
298
299 @Override
300 public List<User> findUsersByQuery( UserQuery query )
301 throws UserManagerException
302 {
303 List<User> users = new ArrayList<>();
304
305 for ( UserManager userManager : userManagerPerId.values() )
306 {
307 List<User> found = userManager.findUsersByQuery( query );
308 if ( found != null )
309 {
310 users.addAll( found );
311 }
312 }
313 return users;
314 }
315
316 @Override
317 public List<User> findUsersByUsernameKey( String usernameKey, boolean orderAscending )
318 throws UserManagerException
319 {
320 List<User> users = new ArrayList<>();
321
322 for ( UserManager userManager : userManagerPerId.values() )
323 {
324 List<User> found = userManager.findUsersByUsernameKey( usernameKey, orderAscending );
325 if ( found != null )
326 {
327 users.addAll( found );
328 }
329 }
330 return users;
331 }
332
333 @Override
334 public String getId()
335 {
336 return null;
337 }
338
339 @Override
340 public List<User> getUsers()
341 throws UserManagerException
342 {
343 List<User> users = new ArrayList<>();
344
345 for ( UserManager userManager : userManagerPerId.values() )
346 {
347 List<User> found = userManager.getUsers();
348 if ( found != null )
349 {
350 users.addAll( found );
351 }
352 }
353 return users;
354 }
355
356 @Override
357 public List<User> getUsers( boolean orderAscending )
358 throws UserManagerException
359 {
360 List<User> users = new ArrayList<>();
361
362 for ( UserManager userManager : userManagerPerId.values() )
363 {
364 List<User> found = userManager.getUsers( orderAscending );
365 if ( found != null )
366 {
367 users.addAll( found );
368 }
369 }
370 return users;
371 }
372
373 @Override
374 public boolean isReadOnly()
375 {
376 boolean readOnly = false;
377
378 for ( UserManager userManager : userManagerPerId.values() )
379 {
380 readOnly = readOnly || userManager.isReadOnly();
381 }
382 return readOnly;
383 }
384
385 @Override
386 public User updateUser( User user )
387 throws UserNotFoundException, UserManagerException
388 {
389
390 UserManager userManager = userManagerPerId.get( user.getUserManagerId() );
391
392 user = userManager.updateUser( user );
393
394 if ( useUsersCache() )
395 {
396 usersCache.put( user.getUsername(), user );
397 }
398
399 return user;
400 }
401
402 @Override
403 public User updateUser( User user, boolean passwordChangeRequired )
404 throws UserNotFoundException, UserManagerException
405 {
406 user = userManagerPerId.get( user.getUserManagerId() ).updateUser( user, passwordChangeRequired );
407
408 if ( useUsersCache() )
409 {
410 usersCache.put( user.getUsername(), user );
411 }
412
413 return user;
414 }
415
416 public void setUserManagerImpl( UserManager userManagerImpl )
417 {
418
419 log.debug( "setUserManagerImpl cannot be used in this implementation" );
420 }
421
422 @Override
423 public User createGuestUser()
424 throws UserManagerException
425 {
426 Exception lastException = null;
427 boolean allFailed = true;
428 User user = null;
429 for ( UserManager userManager : userManagerPerId.values() )
430 {
431 try
432 {
433 if ( !userManager.isReadOnly() )
434 {
435 user = userManager.createGuestUser();
436 allFailed = false;
437 }
438 }
439 catch ( Exception e )
440 {
441 lastException = e;
442 }
443 }
444 if ( lastException != null && allFailed )
445 {
446 throw new UserManagerException( lastException.getMessage(), lastException );
447 }
448 return user;
449 }
450
451
452 @Override
453 public boolean userExists( String userName )
454 throws UserManagerException
455 {
456 Exception lastException = null;
457 boolean allFailed = true;
458 boolean exists = false;
459 for ( UserManager userManager : userManagerPerId.values() )
460 {
461 try
462 {
463
464 if ( userManager.userExists( userName ) )
465 {
466 exists = true;
467 }
468 allFailed = false;
469
470 }
471 catch ( Exception e )
472 {
473 lastException = e;
474 }
475 }
476 if ( lastException != null && allFailed )
477 {
478 throw new UserManagerException( lastException.getMessage(), lastException );
479 }
480 return exists;
481 }
482
483
484 @Override
485 public boolean isFinalImplementation()
486 {
487 return false;
488 }
489
490 @Override
491 public String getDescriptionKey()
492 {
493 return "archiva.redback.usermanager.configurable.archiva";
494 }
495
496
497 }