View Javadoc

1   /*
2    * GecosAccountMapper.java
3    *
4    * Created on May 25, 2004, 2:25 PM
5    */
6   
7   package gov.bnl.gums.account;
8   
9   import gov.bnl.gums.CertToolkit;
10  import gov.bnl.gums.GridUser;
11  import gov.bnl.gums.configuration.Configuration;
12  
13  import java.util.*;
14  
15  import org.apache.log4j.Logger;
16  
17  /** 
18   * Maps a user to a local account based on the CN of the certificate and the
19   * gecos field taken from a NIS/YP/LDAP/... database. The mapping can't be perfect, but contains
20   * a series of heuristics that solve up to 90% of the cases, depending on how
21   * the database itself is kept.
22   * <p>
23   * This is an abstact class, and will need the implementation of the method
24   * that retrieves the list of GECOS fields.
25   * <p>
26   * It's suggested not to use this policy by itself, but to have it part of a 
27   * CompositeAccountMapper in which a ManualAccountMapper comes first. This allows
28   * to override those user mapping that are not satisfying.
29   *
30   * @author Gabriele Carcassi, Jay Packard
31   */
32  public abstract class GecosAccountMapper extends AccountMapper {
33      static private Logger log = Logger.getLogger(GecosAccountMapper.class);
34      static private Map gecosMaps = new Hashtable();
35  
36      static public String getTypeStatic() {
37  		return "gecos";
38  	}
39      
40      public GecosAccountMapper() {
41      	super();
42      }
43      
44      public GecosAccountMapper(Configuration configuration) {
45      	super(configuration);
46      }
47      
48      public GecosAccountMapper(Configuration configuration, String name) {
49      	super(configuration, name);
50      }
51      
52      public String getType() {
53  		return "gecos";
54  	}
55      
56      public String mapUser(GridUser user, boolean createIfDoesNotExist) {
57          String[] nameSurname = CertToolkit.parseNameAndSurname(user.getCertificateDN());
58          GecosMap map = gecosMap();
59          log.trace("GECOS findAccount. Name: " + nameSurname[0] + " - Surname: " + nameSurname[1] + " - GECOSMap: " + gecosMap());
60          return map.findAccount(nameSurname[0], nameSurname[1]);
61      }
62      
63      private GecosMap gecosMap() {
64          synchronized (gecosMaps) {
65              GecosMap map = (GecosMap) gecosMaps.get(getMapName());
66              if (map != null) {
67                  if (map.isValid()) {
68                      log.trace("Reusing GECOS map for '" + getMapName() +"'");
69                      return map;
70                  } else {
71                      log.trace("Invalidating expired GECOS map for '" + getMapName() +"'");
72                      gecosMaps.remove(getMapName());
73                  }
74              }
75              log.debug("Creating new GECOS map for '" + getMapName() + "'");
76              map = createMap();
77              gecosMaps.put(getMapName(), map);
78              return map;
79          }
80      }
81      
82      /**
83       * Implements the creation of the map. The implementation is supposed to
84       * connect to the source, and fill an object of the GecosMap type.
85       * 
86       * @return GECOS map object
87       */
88      protected abstract GecosMap createMap();
89      
90      /** 
91       * Returns an ID for the map used/created by this specific mapper. The
92       * name will be used to cache the generated map also across different
93       * instances of the mapper.
94       * 
95       * @return map name as String
96       */
97      protected abstract String getMapName();
98  }