View Javadoc

1   /*
2    * CompositeAccountMapper.java
3    *
4    * Created on May 4, 2004, 4:27 PM
5    */
6   
7   package gov.bnl.gums;
8   
9   import java.util.*;
10  
11  /*** Maps a user to an account by using the first of a list of policies that
12   * returns a result. This is used to define an aggregate policy of any
13   * kind of policies.
14   * <p>
15   * An example of such policy is: the user is first mapped by looking for
16   * an entry in the database to a manual map; if that is not found, the NIS
17   * map is used to determine the account; if that is not found, a generic
18   * group account is used.
19   * <p>
20   * From a developer point of view, it iterates over a list of AccountMappers
21   * until one of them doesn't return null. All the exception pass straight 
22   * through.
23   * <p>
24   * @todo it might be better to define an exception
25   * AccountMappingException that goes through, while the other are catched,
26   * logged and the iteration continues. 
27   *
28   * @author  Gabriele Carcassi
29   */
30  public class CompositeAccountMapper implements AccountMapper {
31      
32      private List mappers = new ArrayList();
33  
34      /***
35       * Assigns the list of mappers. The list must contain
36       * only objects of class AccountMapper.
37       * <p>
38       * The list provided will be copied: further modification of the mappers
39       * parameters won't affect the list inside the object.
40       * @param mappers a List of AccountMapper objects
41       */
42      public void setMappers(List mappers) {
43          this.mappers = new ArrayList(mappers);
44      }
45      
46      /***
47       * Returns the list of mappers used by this composite mapper.
48       * <p>
49       * The list is an unmodifiable copy of the internal list. To change
50       * the list of mappers one will have to create a new list and use the
51       * setMappers() method. To simply add another mapper to the list, call
52       * addMapper().
53       * @return a List of AccountMapper objects
54       */
55      public List getMappers() {
56          return Collections.unmodifiableList(mappers);
57      }
58      
59      public String mapUser(String userDN) {
60          Iterator iter = mappers.iterator();
61          while (iter.hasNext()) {
62              AccountMapper mapper = (AccountMapper) iter.next();
63              String acc = mapper.mapUser(userDN);
64              if (acc != null) return acc;
65          }
66          return null;
67      }
68      
69      /***
70       * Adds a mapper at the end of the list of mapper used by this composite.
71       * @param mapper The new mapper to be used only if all the previous mappers failed
72       */
73      public void addMapper(AccountMapper mapper) {
74          mappers.add(mapper);
75      }
76      
77  }