1 37 package net.sourceforge.cruisecontrol.publishers.email; 38 39 import net.sourceforge.cruisecontrol.CruiseControlException; 40 import org.apache.log4j.Logger; 41 42 import javax.naming.Context ; 43 import javax.naming.NamingEnumeration ; 44 import javax.naming.directory.Attribute ; 45 import javax.naming.directory.Attributes ; 46 import javax.naming.directory.DirContext ; 47 import javax.naming.directory.InitialDirContext ; 48 import javax.naming.directory.SearchControls ; 49 import javax.naming.directory.SearchResult ; 50 import java.util.Hashtable ; 51 52 55 public class LDAPMapper extends EmailAddressMapper { 56 57 private static final Logger LOG = Logger.getLogger(LDAPMapper.class); 58 59 private String url = null; 60 private String ctxFactory = "com.sun.jndi.ldap.LdapCtxFactory"; private String rootDN = null; 62 private String searchTmpl = "(cn=?)"; private String searchAttr = "mail"; private DirContext ctx = null; 65 66 69 public LDAPMapper() { 70 super(); 71 } 72 73 public void setUrl(String url) { 74 this.url = url; 75 } 76 77 public String getUrl() { 78 return url; 79 } 80 81 public void setRootDN(String rootDN) { 82 this.rootDN = rootDN; 83 } 84 85 public String getRootDN() { 86 return rootDN; 87 } 88 89 public void setSearchTmpl(String searchTmpl) { 90 this.searchTmpl = searchTmpl; 91 } 92 93 public String getSearchTmpl() { 94 return searchTmpl; 95 } 96 97 public void setSearchAttr(String searchAttr) { 98 this.searchAttr = searchAttr; 99 } 100 101 public String getSearchAttr() { 102 return searchAttr; 103 } 104 105 public void setCtxfactory(String ctxFactory) { 106 this.ctxFactory = ctxFactory; 107 } 108 109 public String getCtxFactory() { 110 return ctxFactory; 111 } 112 113 public void validate() throws CruiseControlException { 114 if (getUrl() == null) { 115 throw new CruiseControlException("'url' not specified in configuration file."); 116 } 117 if (getRootDN() == null) { 118 throw new CruiseControlException("'rootDN' not specified in configuration file."); 119 } 120 } 121 122 126 public void open() throws CruiseControlException { 127 Hashtable env = new Hashtable (); 128 129 env.put(Context.INITIAL_CONTEXT_FACTORY, ctxFactory); env.put(Context.PROVIDER_URL, url); 132 try { 133 ctx = new InitialDirContext (env); 134 LOG.debug("LDAPMapper: InitialContext created."); 135 } catch (Exception e) { 136 throw new CruiseControlException(e); 137 } 138 } 139 140 public void close() { 141 if (ctx != null) { 142 try { 143 ctx.close(); 144 LOG.debug("LDAPMapper: InitialContext closed."); 145 } catch (Exception ignored) { 146 } 148 } 149 } 150 151 public String mapUser(String user) { 152 String [] searchAttrs = {searchAttr}; 153 154 SearchControls constraints1 = new SearchControls (); 155 156 constraints1.setSearchScope(SearchControls.SUBTREE_SCOPE); 157 constraints1.setCountLimit(0); 158 constraints1.setTimeLimit(0); 159 160 constraints1.setReturningAttributes(searchAttrs); 161 162 String email = null; 163 StringBuffer s = new StringBuffer (searchTmpl); 164 int idx = s.toString().indexOf("?"); 165 s.replace(idx, idx + 1, user); 166 try { 167 NamingEnumeration ne = ctx.search(rootDN, s.toString(), constraints1); 168 while (ne.hasMore()) { 169 Object o = ne.next(); 170 Attributes attrs = ((SearchResult ) o).getAttributes(); 171 Attribute emailAttr = attrs.get(searchAttr); 172 email = (String ) emailAttr.get(); 173 } 174 } catch (Exception e) { 175 e.printStackTrace(); 176 } 177 LOG.debug("LDAPMapper: Mapping " + user + " to " + email); 178 179 return email; 180 } 181 } 182 | Popular Tags |