KickJava   Java API By Example, From Geeks To Geeks.

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


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, 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
38 package net.sourceforge.cruisecontrol.publishers.email;
39
40 import net.sourceforge.cruisecontrol.CruiseControlException;
41
42 import org.apache.log4j.Logger;
43 import java.util.Set JavaDoc;
44 import java.util.Iterator JavaDoc;
45
46 public class EmailAddressMapper extends EmailMapper {
47
48     private static final Logger LOG = Logger.getLogger(EmailAddressMapper.class);
49
50     public EmailAddressMapper() {
51     }
52
53     /*
54      * invoked to initialize each time before this mapped is used to map a set of users.
55      * this method can be invoked multiple times during instance lifetime
56      */

57     public void open() throws CruiseControlException {
58     }
59
60     /*
61      * invoked after a set of users has been mapped.
62      * this method can be invoked multiple times during instance lifetime
63      */

64     public void close() {
65     }
66
67     /*
68      * invoked to map a user, return null if this mapper can not map the user.
69      * invoked zero or more times between invocations of open and close
70      */

71     public String JavaDoc mapUser(String JavaDoc user) {
72         return null;
73     }
74     /*
75      * returns whether the result can be cached.
76      * invoked immediately after non-null resulting invocations of mapUser
77      */

78     public boolean cacheable() {
79         return true;
80     }
81
82     /*
83      * If a need exists to override this method, extend EmailMapper instead of this class.
84      *
85      * @see net.sourceforge.cruisecontrol.publishers.EmailMapper#mapUsers(java.util.Set, java.util.Set)
86      */

87     public final void mapUsers(Set JavaDoc users, Set JavaDoc mappedUsers) {
88         try {
89             open();
90
91             // iterate over all users
92
for (Iterator JavaDoc userIterator = users.iterator(); userIterator.hasNext(); ) {
93                 String JavaDoc user = (String JavaDoc) userIterator.next();
94                 String JavaDoc mappedUser = mapUser(user);
95
96                 // if hit, remove from users and add to mappedUsers
97
if (mappedUser != null) {
98                     LOG.debug("Mapped user " + user + " to " + mappedUser);
99                     mappedUsers.add(mappedUser);
100                     userIterator.remove();
101                     // cache the results unless the mapper doesn't want to
102
if (cacheable()) {
103                         EmailMapperHelper.addCacheEntry(getEmailPublisher(), user, mappedUser);
104                     }
105                 }
106             }
107         } catch (CruiseControlException ce) {
108             LOG.error(ce.getMessage());
109         } finally {
110             close();
111         }
112     }
113 }
114
Popular Tags