587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.helium.signal.ant.types;
|
|
19 |
|
|
20 |
import com.nokia.helium.core.EmailDataSender;
|
628
|
21 |
|
|
22 |
import com.nokia.helium.core.EmailSendException;
|
587
|
23 |
import com.nokia.helium.core.PropertiesSource;
|
|
24 |
import com.nokia.helium.core.TemplateInputSource;
|
|
25 |
import com.nokia.helium.signal.Notifier;
|
|
26 |
import com.nokia.helium.core.TemplateProcessor;
|
|
27 |
import java.util.List;
|
|
28 |
import java.util.Hashtable;
|
|
29 |
import java.util.ArrayList;
|
|
30 |
import java.io.IOException;
|
628
|
31 |
|
|
32 |
import org.apache.tools.ant.BuildException;
|
587
|
33 |
import org.apache.tools.ant.Project;
|
|
34 |
import org.apache.tools.ant.types.DataType;
|
|
35 |
import java.io.File;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* The emailNotifier provides you an easy way to send a you and email containing
|
|
39 |
* the summary of a build failure.
|
|
40 |
*
|
|
41 |
* @ant.type name="emailNotifier" category="Signaling"
|
|
42 |
*/
|
|
43 |
public class EMAILNotifier extends DataType implements Notifier {
|
|
44 |
|
|
45 |
private TemplateProcessor templateProcessor = new TemplateProcessor();
|
|
46 |
private File defaultTemplate;
|
|
47 |
private String title;
|
|
48 |
private String smtp;
|
|
49 |
private String ldap;
|
|
50 |
private String rootdn;
|
|
51 |
private String notifyWhen = "never";
|
|
52 |
private String from;
|
|
53 |
private String additionalRecipients;
|
|
54 |
|
|
55 |
/**
|
|
56 |
* Rendering the template, and sending the result through email.
|
628
|
57 |
*
|
587
|
58 |
* @param signalName
|
628
|
59 |
* - is the name of the signal that has been raised.
|
|
60 |
* @param failStatus
|
|
61 |
* - indicates whether to fail the build or not
|
|
62 |
* @param notifierInput
|
|
63 |
* - contains signal notifier info
|
|
64 |
* @param message
|
|
65 |
* - is the message from the signal that has been raised.
|
587
|
66 |
*/
|
|
67 |
|
|
68 |
@SuppressWarnings("unchecked")
|
|
69 |
public void sendData(String signalName, boolean failStatus,
|
628
|
70 |
NotifierInput notifierInput, String message) {
|
587
|
71 |
if (notifyWhen != null
|
628
|
72 |
&& (notifyWhen.equals("always")
|
|
73 |
|| (notifyWhen.equals("fail") && failStatus) || (notifyWhen
|
|
74 |
.equals("pass") && !failStatus))) {
|
587
|
75 |
if (title == null) {
|
628
|
76 |
throw new BuildException(
|
|
77 |
"The 'title' attribute has not been defined.");
|
587
|
78 |
}
|
|
79 |
|
|
80 |
if (smtp == null) {
|
628
|
81 |
throw new BuildException(
|
|
82 |
"The 'smtp' attribute has not been defined.");
|
587
|
83 |
}
|
|
84 |
|
628
|
85 |
if (ldap == null && from == null) {
|
|
86 |
throw new BuildException(
|
|
87 |
"The 'ldap' attribute has not been defined.");
|
587
|
88 |
}
|
628
|
89 |
EmailDataSender emailSender = createEmailDataSender();
|
587
|
90 |
|
588
|
91 |
Project subProject = getProject().createSubProject();
|
|
92 |
subProject.setProperty("signal.name", signalName);
|
|
93 |
subProject.setProperty("signal.status", "" + failStatus);
|
|
94 |
subProject.setProperty("signal.message", "" + message);
|
628
|
95 |
try {
|
|
96 |
|
|
97 |
File fileToSend = null;
|
|
98 |
if (notifierInput != null) {
|
|
99 |
fileToSend = notifierInput.getFile(".*.html");
|
587
|
100 |
}
|
628
|
101 |
if (fileToSend == null) {
|
|
102 |
if (defaultTemplate != null && defaultTemplate.exists()) {
|
|
103 |
File emailOutputFile;
|
|
104 |
try {
|
|
105 |
emailOutputFile = File.createTempFile("helium_",
|
|
106 |
"email.html");
|
|
107 |
emailOutputFile.deleteOnExit();
|
|
108 |
log("sending data by e-mail:outputDir: "
|
|
109 |
+ emailOutputFile.getAbsolutePath(), Project.MSG_DEBUG);
|
587
|
110 |
|
628
|
111 |
List<TemplateInputSource> sourceList = new ArrayList<TemplateInputSource>();
|
|
112 |
sourceList.add(new PropertiesSource("ant",
|
|
113 |
getProject().getProperties()));
|
|
114 |
Hashtable<String, String> signalProperties = new Hashtable<String, String>();
|
|
115 |
signalProperties.put("signal.name", signalName);
|
|
116 |
signalProperties.put("signal.status", ""
|
|
117 |
+ failStatus);
|
|
118 |
signalProperties
|
|
119 |
.put("signal.message", "" + message);
|
|
120 |
sourceList.add(new PropertiesSource("signaling",
|
|
121 |
signalProperties));
|
587
|
122 |
|
628
|
123 |
templateProcessor.convertTemplate(defaultTemplate,
|
|
124 |
emailOutputFile, sourceList);
|
|
125 |
fileToSend = emailOutputFile;
|
|
126 |
} catch (IOException e) {
|
|
127 |
log("EmailNotifier: IOexception: " + e.getMessage(), Project.MSG_DEBUG);
|
|
128 |
}
|
|
129 |
} else {
|
|
130 |
if (defaultTemplate == null) {
|
|
131 |
log("The 'defaultTemplate' has not been defined.",
|
|
132 |
Project.MSG_WARN);
|
|
133 |
} else if (!defaultTemplate.exists()) {
|
|
134 |
log("Could not find default template: "
|
|
135 |
+ defaultTemplate.getAbsolutePath(),
|
|
136 |
Project.MSG_WARN);
|
|
137 |
}
|
|
138 |
}
|
587
|
139 |
}
|
628
|
140 |
emailSender.sendData("signaling", fileToSend, "application/html",
|
|
141 |
subProject.replaceProperties(title), null);
|
|
142 |
} catch (EmailSendException ese) {
|
|
143 |
log(this.getDataTypeName() + " Warning: " + ese.getMessage(), Project.MSG_WARN);
|
588
|
144 |
}
|
587
|
145 |
}
|
|
146 |
}
|
|
147 |
|
|
148 |
/**
|
628
|
149 |
* Create an EmailDataSender base on this type settings.
|
|
150 |
* @return
|
|
151 |
*/
|
|
152 |
private EmailDataSender createEmailDataSender() {
|
|
153 |
String smtpUpdated = getProject().replaceProperties(smtp);
|
|
154 |
String ldapUpdated = getProject().replaceProperties(ldap);
|
|
155 |
String additionalRecipientsUpdated = getProject()
|
|
156 |
.replaceProperties(additionalRecipients);
|
|
157 |
|
|
158 |
log("Sending data by e-mail.", Project.MSG_DEBUG);
|
|
159 |
EmailDataSender emailSender;
|
|
160 |
if (additionalRecipientsUpdated == null) {
|
|
161 |
additionalRecipientsUpdated = from;
|
|
162 |
} else {
|
|
163 |
additionalRecipientsUpdated += (from != null) ? (additionalRecipientsUpdated.length() > 0 ? "," : "") + from : "";
|
|
164 |
}
|
|
165 |
if (rootdn != null) {
|
|
166 |
String[] to = null;
|
|
167 |
if (additionalRecipientsUpdated != null) {
|
|
168 |
to = additionalRecipientsUpdated.split(",");
|
|
169 |
}
|
|
170 |
emailSender = new EmailDataSender(to, smtpUpdated, ldapUpdated,
|
|
171 |
getProject().replaceProperties(rootdn));
|
|
172 |
} else {
|
|
173 |
emailSender = new EmailDataSender(additionalRecipientsUpdated,
|
|
174 |
smtpUpdated, ldapUpdated);
|
|
175 |
}
|
|
176 |
if (from == null) {
|
|
177 |
try {
|
|
178 |
emailSender.addCurrentUserToAddressList();
|
|
179 |
} catch (EmailSendException ex) {
|
|
180 |
// Consider the error as a warning, let's try to send the email anyway
|
|
181 |
log(this.getDataTypeName() + " Warning: " + ex.getMessage(), Project.MSG_WARN);
|
|
182 |
}
|
|
183 |
}
|
|
184 |
if (from != null) {
|
|
185 |
log("Setting from: " + from);
|
|
186 |
emailSender.setFrom(from);
|
|
187 |
}
|
|
188 |
log("EmailNotifier:arlist: " + additionalRecipientsUpdated, Project.MSG_DEBUG);
|
|
189 |
return emailSender;
|
|
190 |
}
|
|
191 |
|
|
192 |
/**
|
|
193 |
* Set when the notifier should emit the massage. Possible values are:
|
|
194 |
* never, always, fail, pass.
|
|
195 |
*
|
587
|
196 |
* @ant.not-required Default is never.
|
|
197 |
*/
|
628
|
198 |
public void setNotifyWhen(NotifyWhenEnum notifyWhen) {
|
|
199 |
this.notifyWhen = notifyWhen.getValue();
|
587
|
200 |
}
|
|
201 |
|
628
|
202 |
/**
|
|
203 |
* When do we need to notify the user?
|
|
204 |
*
|
|
205 |
* @return
|
|
206 |
*/
|
587
|
207 |
public String getNotifyWhen() {
|
|
208 |
return notifyWhen;
|
|
209 |
}
|
|
210 |
|
|
211 |
/**
|
|
212 |
* Define the template source file to use while rendering the message.
|
|
213 |
*
|
|
214 |
* @ant.required
|
|
215 |
*/
|
|
216 |
public void setDefaultTemplate(File template) {
|
|
217 |
this.defaultTemplate = template;
|
|
218 |
}
|
|
219 |
|
|
220 |
/**
|
|
221 |
* Define the template source file to use while rendering the message.
|
628
|
222 |
*
|
587
|
223 |
* @deprecated
|
|
224 |
* @ant.required
|
|
225 |
*/
|
628
|
226 |
@Deprecated
|
587
|
227 |
public void setTemplateSrc(File template) {
|
628
|
228 |
log(
|
|
229 |
"The usage of the templateSrc attribute is deprecated,"
|
|
230 |
+ " please consider using the defaultTemplate attribute instead.",
|
|
231 |
Project.MSG_ERR);
|
|
232 |
this.defaultTemplate = template;
|
587
|
233 |
}
|
628
|
234 |
|
587
|
235 |
/**
|
|
236 |
* The title of the email.
|
|
237 |
*
|
|
238 |
* @ant.required
|
|
239 |
*/
|
|
240 |
public void setTitle(String title) {
|
|
241 |
this.title = title;
|
|
242 |
}
|
|
243 |
|
|
244 |
/**
|
|
245 |
* The STMP server address.
|
|
246 |
*
|
|
247 |
* @ant.required
|
|
248 |
*/
|
|
249 |
public void setSmtp(String smtp) {
|
|
250 |
this.smtp = smtp;
|
|
251 |
}
|
628
|
252 |
|
587
|
253 |
/**
|
|
254 |
* Who the email is sent from.
|
|
255 |
*
|
|
256 |
* @ant.not-required
|
|
257 |
*/
|
|
258 |
public void setFrom(String from) {
|
|
259 |
this.from = from;
|
|
260 |
}
|
|
261 |
|
|
262 |
/**
|
|
263 |
* Comma separated list of additional email addresses.
|
|
264 |
*
|
|
265 |
* @ant.not-required
|
|
266 |
*/
|
|
267 |
public void setAdditionalRecipients(String ar) {
|
|
268 |
this.additionalRecipients = ar;
|
|
269 |
}
|
|
270 |
|
|
271 |
/**
|
|
272 |
* The LDAP server URL.
|
|
273 |
*
|
628
|
274 |
* @ant.required (or from attribute can be used)
|
587
|
275 |
*/
|
|
276 |
public void setLdap(String ldap) {
|
|
277 |
this.ldap = ldap;
|
|
278 |
}
|
628
|
279 |
|
|
280 |
/**
|
|
281 |
* The LDAP rootdn.
|
|
282 |
* @param rootdn
|
|
283 |
* @ant.required
|
|
284 |
*/
|
587
|
285 |
public void setRootdn(String rootdn) {
|
|
286 |
this.rootdn = rootdn;
|
|
287 |
}
|
|
288 |
|
|
289 |
}
|