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