View Javadoc

1   /**
2    * Copyright 2005-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * This is a basic implementation of the KualiReporterService. Currently, it only has
37   * a mail service as reporting mechanism.
38   * 
39   * @author Kuali Rice Team (rice.collab@kuali.org)
40   *
41   */
42  public class KualiExceptionIncidentServiceImpl implements KualiExceptionIncidentService {
43      private Logger LOG=Logger.getLogger(KualiExceptionIncidentServiceImpl.class);
44      
45      /**
46       * An list to send incident emails to.
47       */
48      private String incidentMailingList;
49      
50      /**
51       * This property must be defined in the base configuration file for specifying
52       * the mailing list for the report to be sent.
53       * <p>Example:
54       * <code>
55       * <param name="KualiReporterServiceImpl.REPORT_MAIL_LIST">a@y,b@z</param>
56       * </code>
57       */
58      public static final String REPORT_MAIL_LIST=String.format(
59              "%s.REPORT_MAIL_LIST",KualiExceptionIncidentServiceImpl.class.getSimpleName());
60      /**
61       * A Mailer for sending report.
62       */
63      private Mailer mailer;
64      
65      /**
66       * Sets the Mailer mail service.
67       * @param mailer the mail service the mailService to set
68       */
69      public final void setMailer(Mailer mailer) {
70          this.mailer = mailer;
71      }
72      
73      /**
74       * An email template is used to construct an email to be sent by the mail service.
75       */
76      private MailMessage messageTemplate;
77  
78      /**
79       * Mails the report using the mail service from the mail template.
80       *
81       * @param subject the subject text of the email
82       * @param message the body text of the email
83       * @throws IllegalStateException if the Mailer is not set up.
84       * @see org.kuali.rice.krad.service.KualiExceptionIncidentService#emailReport(java.lang.String, java.lang.String)
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         // Send mail
102         MailMessage msg=createMailMessage(subject, message);
103         mailer.sendEmail(msg);
104 
105         if (LOG.isTraceEnabled()) {
106             LOG.trace("EXIT");
107         }
108     }
109 
110     /**
111      * Creates an instance of MailMessage from the inputs using the given
112      * template.
113      * 
114      * @param subject the subject line text
115      * @param message the body of the email message
116      * @return MailMessage
117      * @throws IllegalStateException if the <codeREPORT_MAIL_LIST</code> is not set
118      * or messageTemplate does not have ToAddresses already set.
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         // Copy input message reference for creating an instance of mail message
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         // First check if message template already define mailing list
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         // Set mail message subject
167         msg.setSubject((subject==null)?"":subject);
168 
169         // Set mail message body
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      * This overridden method send email to the specified list of addresses.
183      * 
184      * @see org.kuali.rice.krad.service.KualiExceptionIncidentService#report(org.kuali.rice.krad.exception.KualiExceptionIncident)
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      * This method first separate a composite string of the format
209      * "string token string".
210      * <p>Example: 1,2,a,b where ',' is the token
211      * 
212      * @param s
213      * @param token
214      * @return
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      * Returns the messageTemplate for the report email
238      * @return the messageTemplate
239      */
240     public final MailMessage getMessageTemplate() {
241         return this.messageTemplate;
242     }
243 
244     /**
245      * Sets the messageTemplate
246      * @param messageTemplate the messageTemplate to set
247      */
248     public final void setMessageTemplate(MailMessage messageTemplate) {
249         this.messageTemplate = messageTemplate;
250     }
251 
252     /**
253      * This overridden method create an instance of the KualiExceptionIncident.
254      * 
255      * @see org.kuali.rice.krad.service.KualiExceptionIncidentService#getExceptionIncident(
256      * java.lang.Exception,java.util.Map)
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      * This overridden method create an instance of ExceptionIncident from list of
282      * name-value pairs as exception incident information.
283      * 
284      * @see org.kuali.rice.krad.service.KualiExceptionIncidentService#getExceptionIncident(java.util.Map)
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      * Returns the incident report mailing list.
305 	 * @return the incidentMailingList
306 	 */
307 	public String getIncidentMailingList() {
308 		return this.incidentMailingList;
309 	}
310 
311 	/**
312      * Sets the incident report mailing list.
313 	 * @param incidentMailingList the incidentMailingList to set
314 	 */
315 	public void setIncidentMailingList(String incidentMailingList) {
316 		this.incidentMailingList = incidentMailingList;
317 	}
318 
319 }