01: package ru.emdev.EmForge.security;
02:
03: import java.util.ArrayList;
04: import java.util.Collection;
05: import java.util.Collections;
06: import java.util.Comparator;
07: import java.util.List;
08:
09: import org.acegisecurity.userdetails.UserDetails;
10: import org.acegisecurity.userdetails.UsernameNotFoundException;
11: import org.springframework.dao.DataAccessException;
12:
13: /** Class agreegates several user-details services into one service */
14: public class AgreegatingUserDetailsService implements
15: EmForgeUserDetailsService {
16: private Collection<EmForgeUserDetailsService> m_services;
17:
18: public void setServices(
19: Collection<EmForgeUserDetailsService> i_services) {
20: m_services = i_services;
21: }
22:
23: public Collection<UserDetails> getAllUsers() {
24: List<UserDetails> result = new ArrayList<UserDetails>();
25:
26: // enumerate all services
27: for (EmForgeUserDetailsService service : m_services) {
28: result.addAll(service.getAllUsers());
29: }
30:
31: // sort result
32: Collections.sort(result, new Comparator<UserDetails>() {
33: public int compare(UserDetails user1, UserDetails user2) {
34: return user1.getUsername().compareTo(
35: user2.getUsername());
36: }
37: });
38:
39: // TODO Auto-generated method stub
40: return result;
41: }
42:
43: public UserDetails loadUserByUsername(String username)
44: throws UsernameNotFoundException, DataAccessException {
45: // enumerate all services
46: for (EmForgeUserDetailsService service : m_services) {
47: try {
48: UserDetails user = service.loadUserByUsername(username);
49:
50: if (user != null) {
51: return user;
52: }
53: } catch (UsernameNotFoundException nfe) {
54: // ignore it
55: } catch (DataAccessException dae) {
56: // ignore it
57: }
58: }
59:
60: throw new UsernameNotFoundException("user " + username
61: + " not found in specified services");
62: }
63:
64: }
|