KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > publishers > email > LDAPMapper


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2006, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

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 JavaDoc;
43 import javax.naming.NamingEnumeration JavaDoc;
44 import javax.naming.directory.Attribute JavaDoc;
45 import javax.naming.directory.Attributes JavaDoc;
46 import javax.naming.directory.DirContext JavaDoc;
47 import javax.naming.directory.InitialDirContext JavaDoc;
48 import javax.naming.directory.SearchControls JavaDoc;
49 import javax.naming.directory.SearchResult JavaDoc;
50 import java.util.Hashtable JavaDoc;
51
52 /**
53  * @author laineesa
54  */

55 public class LDAPMapper extends EmailAddressMapper {
56
57     private static final Logger LOG = Logger.getLogger(LDAPMapper.class);
58
59     private String JavaDoc url = null;
60     private String JavaDoc ctxFactory = "com.sun.jndi.ldap.LdapCtxFactory"; // commonly used default value
61
private String JavaDoc rootDN = null;
62     private String JavaDoc searchTmpl = "(cn=?)"; // commonly used default value
63
private String JavaDoc searchAttr = "mail"; // commonly used default value
64
private DirContext JavaDoc ctx = null;
65
66     /**
67      *
68      */

69     public LDAPMapper() {
70         super();
71     }
72
73     public void setUrl(String JavaDoc url) {
74         this.url = url;
75     }
76
77     public String JavaDoc getUrl() {
78         return url;
79     }
80
81     public void setRootDN(String JavaDoc rootDN) {
82         this.rootDN = rootDN;
83     }
84
85     public String JavaDoc getRootDN() {
86         return rootDN;
87     }
88
89     public void setSearchTmpl(String JavaDoc searchTmpl) {
90         this.searchTmpl = searchTmpl;
91     }
92
93     public String JavaDoc getSearchTmpl() {
94         return searchTmpl;
95     }
96
97     public void setSearchAttr(String JavaDoc searchAttr) {
98         this.searchAttr = searchAttr;
99     }
100
101     public String JavaDoc getSearchAttr() {
102         return searchAttr;
103     }
104
105     public void setCtxfactory(String JavaDoc ctxFactory) {
106         this.ctxFactory = ctxFactory;
107     }
108
109     public String JavaDoc 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     /*
123      * (non-Javadoc)
124      * @see net.sourceforge.cruisecontrol.publishers.email.EmailAddressMapper#open()
125      */

126     public void open() throws CruiseControlException {
127         Hashtable JavaDoc env = new Hashtable JavaDoc();
128
129         env.put(Context.INITIAL_CONTEXT_FACTORY, ctxFactory); // use jndi provider
130
env.put(Context.PROVIDER_URL, url); // the ldap url to connect to; e.g. "ldap://ca.com:389"
131

132         try {
133             ctx = new InitialDirContext JavaDoc(env);
134             LOG.debug("LDAPMapper: InitialContext created.");
135         } catch (Exception JavaDoc 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 JavaDoc ignored) {
146                 //Ignored
147
}
148         }
149     }
150
151     public String JavaDoc mapUser(String JavaDoc user) {
152         String JavaDoc[] searchAttrs = {searchAttr};
153         /* specify search constraints to search subtree */
154         SearchControls JavaDoc constraints1 = new SearchControls JavaDoc();
155
156         constraints1.setSearchScope(SearchControls.SUBTREE_SCOPE);
157         constraints1.setCountLimit(0);
158         constraints1.setTimeLimit(0);
159
160         constraints1.setReturningAttributes(searchAttrs);
161
162         String JavaDoc email = null;
163         StringBuffer JavaDoc s = new StringBuffer JavaDoc(searchTmpl);
164         int idx = s.toString().indexOf("?");
165         s.replace(idx, idx + 1, user);
166         try {
167             NamingEnumeration JavaDoc ne = ctx.search(rootDN, s.toString(), constraints1);
168             while (ne.hasMore()) {
169                 Object JavaDoc o = ne.next();
170                 Attributes JavaDoc attrs = ((SearchResult JavaDoc) o).getAttributes();
171                 Attribute JavaDoc emailAttr = attrs.get(searchAttr);
172                 email = (String JavaDoc) emailAttr.get();
173             }
174         } catch (Exception JavaDoc e) {
175             e.printStackTrace();
176         }
177         LOG.debug("LDAPMapper: Mapping " + user + " to " + email);
178
179         return email;
180     }
181 }
182
Popular Tags