View Javadoc

1   /*
2    * GecosNisAccountMapper.java
3    *
4    * Created on April 13, 2005, 4:21 PM
5    */
6   
7   package gov.bnl.gums;
8   
9   import java.util.Properties;
10  import javax.naming.NamingEnumeration;
11  import javax.naming.directory.Attribute;
12  import javax.naming.directory.Attributes;
13  import javax.naming.directory.DirContext;
14  import javax.naming.directory.InitialDirContext;
15  import javax.naming.directory.SearchResult;
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  
19  /*** Matches the DN with the account information retrieved from an LDAP server.
20   *
21   * @author Gabriele Carcassi
22   */
23  public class GecosLdapAccountMapper extends GecosAccountMapper {
24      static private Log log = LogFactory.getLog(GecosLdapAccountMapper.class);
25      
26      private String jndiLdapUrl;
27      
28      /***
29       * Returns the URL used to describe the LDAP server.
30       * @return LDAP url according to JNDI LDAP driver.
31       */
32      public String getJndiLdapUrl() {
33          return this.jndiLdapUrl;
34      }
35      
36      /***
37       * Changes the LDAP server to use.
38       * @param jndiLdapUrl LDAP url according to JNDI LDAP driver.
39       */
40      public void setJndiLdapUrl(String jndiLdapUrl) {
41          this.jndiLdapUrl = jndiLdapUrl;
42      }
43      
44      private Properties retrieveJndiProperties() {
45          Properties jndiProperties = new java.util.Properties();
46          jndiProperties.put("java.naming.provider.url", jndiLdapUrl);
47          jndiProperties.put("java.naming.factory.initial","com.sun.jndi.ldap.LdapCtxFactory");
48          return jndiProperties;
49      }
50  
51      protected GecosMap createMap() {
52          Properties jndiProperties = retrieveJndiProperties();
53          int nTries = 5;
54          Exception lastException = null;
55          int i = 0;
56          for (; i < nTries; i++) {
57              GecosMap map = new GecosMap();
58              log.debug("Attemp " + i + " to retrieve map for '" + jndiLdapUrl + "'");
59              try {
60                  DirContext jndiCtx = new InitialDirContext(jndiProperties);
61                  NamingEnumeration nisMap = jndiCtx.search("ou=People", "(cn=*)", null);
62                  log.trace("Server responded");
63                  while (nisMap.hasMore()) {
64                      SearchResult res = (SearchResult) nisMap.next();
65                      Attributes atts = res.getAttributes();
66                      String username = (String) atts.get("uid").get();
67                      Attribute gecosAtt = atts.get("gecos");
68                      if (gecosAtt != null) {
69                          String gecos = gecosAtt.get().toString();
70                          map.addEntry(username, gecos);
71                      } else {
72                          log.trace("Found user '" + username + "' with no GECOS field");
73                      }
74                  }
75                  jndiCtx.close();
76                  return map;
77              } catch (javax.naming.NamingException ne) {
78                  log.warn("Error filling the maps for NIS "+jndiLdapUrl, ne);
79                  lastException = ne;
80                  try {
81                      Thread.sleep(100);
82                  } catch (InterruptedException e) {
83                      log.warn("Interrupted", e);
84                  }
85              } catch (Exception e) {
86                  log.warn("Error filling the maps for NIS "+jndiLdapUrl, e);
87                  lastException = e;
88                  try {
89                      Thread.sleep(100);
90                  } catch (InterruptedException ie) {
91                      log.warn("Interrupted", e);
92                  }
93              }
94          }
95          if (i == nTries) {
96              throw new RuntimeException("Couldn't retrieve NIS maps from " + jndiLdapUrl, lastException);
97          }
98          return null;
99      }
100 
101     protected String mapName() {
102         return jndiLdapUrl;
103     }
104 }