1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.krad.service.impl;
17
18 import org.apache.log4j.Logger;
19 import org.kuali.rice.core.api.mail.MailMessage;
20 import org.kuali.rice.core.api.mail.Mailer;
21 import org.kuali.rice.kim.api.identity.Person;
22 import org.kuali.rice.krad.exception.ExceptionIncident;
23 import org.kuali.rice.krad.exception.KualiExceptionIncident;
24 import org.kuali.rice.krad.service.KRADServiceLocator;
25 import org.kuali.rice.krad.service.KualiExceptionIncidentService;
26 import org.kuali.rice.krad.util.GlobalVariables;
27 import org.kuali.rice.krad.util.KRADConstants;
28
29 import java.util.ArrayList;
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Set;
34
35
36
37
38
39
40
41
42 public class KualiExceptionIncidentServiceImpl implements KualiExceptionIncidentService {
43 private Logger LOG=Logger.getLogger(KualiExceptionIncidentServiceImpl.class);
44
45
46
47
48 private String incidentMailingList;
49
50
51
52
53
54
55
56
57
58 public static final String REPORT_MAIL_LIST=String.format(
59 "%s.REPORT_MAIL_LIST",KualiExceptionIncidentServiceImpl.class.getSimpleName());
60
61
62
63 private Mailer mailer;
64
65
66
67
68
69 public final void setMailer(Mailer mailer) {
70 this.mailer = mailer;
71 }
72
73
74
75
76 private MailMessage messageTemplate;
77
78
79
80
81
82
83
84
85
86 @Override
87 public void emailReport(String subject, String message) throws Exception {
88 if (LOG.isTraceEnabled()) {
89 String lm=String.format("ENTRY %s;%s",
90 (subject==null)?"null":subject.toString(),
91 (message==null)?"null":message.toString());
92 LOG.trace(lm);
93 }
94
95 if (mailer == null) {
96 String errorMessage = "mailer property of KualiExceptionIncidentServiceImpl is null";
97 LOG.fatal(errorMessage);
98 throw new IllegalStateException(errorMessage);
99 }
100
101
102 MailMessage msg=createMailMessage(subject, message);
103 mailer.sendEmail(msg);
104
105 if (LOG.isTraceEnabled()) {
106 LOG.trace("EXIT");
107 }
108 }
109
110
111
112
113
114
115
116
117
118
119
120 @SuppressWarnings("unchecked")
121 private MailMessage createMailMessage(String subject, String message)
122 throws Exception{
123 if (LOG.isTraceEnabled()) {
124 String lm=String.format("ENTRY %s%n%s",
125 (subject==null)?"null":subject.toString(),
126 (message==null)?"null":message.toString());
127 LOG.trace(lm);
128 }
129
130 if (messageTemplate == null) {
131 throw new IllegalStateException(String.format(
132 "%s.templateMessage is null or not set",
133 this.getClass().getName()));
134 }
135
136
137 MailMessage msg=new MailMessage();
138
139 Person actualUser = GlobalVariables.getUserSession().getActualPerson();
140 String fromEmail = actualUser.getEmailAddress();
141 if ((fromEmail != null) && (fromEmail != "")) {
142 msg.setFromAddress(fromEmail);
143 } else {
144 msg.setFromAddress(messageTemplate.getFromAddress());
145 }
146
147 msg.setBccAddresses(messageTemplate.getBccAddresses());
148 msg.setCcAddresses(messageTemplate.getCcAddresses());
149
150 Set emails=messageTemplate.getToAddresses();
151 if (emails == null || emails.isEmpty()) {
152 String mailingList= KRADServiceLocator.getKualiConfigurationService().getPropertyValueAsString(
153 REPORT_MAIL_LIST);
154 if (mailingList == null || mailingList.trim().length() == 0) {
155 String em=REPORT_MAIL_LIST+" is not set or messageTemplate does not have ToAddresses already set.";
156 LOG.error(em);
157 throw new IllegalStateException(em);
158 } else {
159 msg.setToAddresses(new HashSet<String>(split(mailingList,
160 KRADConstants.FIELD_CONVERSIONS_SEPARATOR)));
161 }
162 } else {
163 msg.setToAddresses(emails);
164 }
165
166
167 msg.setSubject((subject==null)?"":subject);
168
169
170 msg.setMessage((message==null)?"":message);
171
172 if (LOG.isTraceEnabled()) {
173 String lm=String.format("EXIT %s",
174 (msg==null)?"null":msg.toString());
175 LOG.trace(lm);
176 }
177
178 return msg;
179 }
180
181
182
183
184
185
186 @Override
187 public void report(KualiExceptionIncident exceptionIncident) throws Exception {
188 if (LOG.isTraceEnabled()) {
189 String lm=String.format("ENTRY %s",
190 (exceptionIncident==null)?"null":exceptionIncident.toString());
191 LOG.trace(lm);
192 }
193
194 emailReport(
195 exceptionIncident.getProperty(
196 KualiExceptionIncident.EXCEPTION_REPORT_SUBJECT),
197 exceptionIncident.getProperty(
198 KualiExceptionIncident.EXCEPTION_REPORT_MESSAGE));
199
200 if (LOG.isTraceEnabled()) {
201 String lm=String.format("EXIT");
202 LOG.trace(lm);
203 }
204
205 }
206
207
208
209
210
211
212
213
214
215
216 public List<String> split(String s, String token) {
217 if (LOG.isTraceEnabled()) {
218 String lm=String.format("ENTRY %s;%s", s, token);
219 LOG.trace(lm);
220 }
221
222 String[] sarray=s.split(token);
223 List<String> list=new ArrayList<String>();
224 for (int i=0; i<sarray.length && sarray[i].length() > 0; i++) {
225 list.add(sarray[i]);
226 }
227
228 if (LOG.isTraceEnabled()) {
229 String lm=String.format("EXIT %s", list.toString());
230 LOG.trace(lm);
231 }
232
233 return list;
234 }
235
236
237
238
239
240 public final MailMessage getMessageTemplate() {
241 return this.messageTemplate;
242 }
243
244
245
246
247
248 public final void setMessageTemplate(MailMessage messageTemplate) {
249 this.messageTemplate = messageTemplate;
250 }
251
252
253
254
255
256
257
258 @Override
259 public KualiExceptionIncident getExceptionIncident(Exception exception,
260 Map<String, String> properties) {
261 if ( exception == null ) {
262 return getExceptionIncident(properties);
263 }
264 if (LOG.isTraceEnabled()) {
265 String lm=String.format("ENTRY %s;%s", exception.getMessage(),
266 properties.toString());
267 LOG.trace(lm);
268 }
269
270 KualiExceptionIncident ei=new ExceptionIncident(exception, properties);
271
272 if (LOG.isTraceEnabled()) {
273 String lm=String.format("EXIT %s", ei.toProperties().toString());
274 LOG.trace(lm);
275 }
276
277 return ei;
278 }
279
280
281
282
283
284
285
286 @Override
287 public KualiExceptionIncident getExceptionIncident(Map<String, String> properties) {
288 if (LOG.isTraceEnabled()) {
289 String lm=String.format("ENTRY %s", properties.toString());
290 LOG.trace(lm);
291 }
292
293 ExceptionIncident ei=new ExceptionIncident(properties);
294
295 if (LOG.isTraceEnabled()) {
296 String lm=String.format("EXIT %s", ei.toProperties().toString());
297 LOG.trace(lm);
298 }
299
300 return ei;
301 }
302
303
304
305
306
307 public String getIncidentMailingList() {
308 return this.incidentMailingList;
309 }
310
311
312
313
314
315 public void setIncidentMailingList(String incidentMailingList) {
316 this.incidentMailingList = incidentMailingList;
317 }
318
319 }