1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.mail.messaging;
16  
17  import com.liferay.mail.util.HookFactory;
18  import com.liferay.portal.kernel.log.Log;
19  import com.liferay.portal.kernel.log.LogFactoryUtil;
20  import com.liferay.portal.kernel.mail.MailMessage;
21  import com.liferay.portal.kernel.messaging.Message;
22  import com.liferay.portal.kernel.messaging.MessageListener;
23  import com.liferay.portal.kernel.util.ArrayUtil;
24  import com.liferay.portal.kernel.util.MethodInvoker;
25  import com.liferay.portal.kernel.util.MethodWrapper;
26  import com.liferay.portal.util.PropsValues;
27  import com.liferay.util.mail.MailEngine;
28  
29  import javax.mail.internet.InternetAddress;
30  
31  /**
32   * <a href="MailMessageListener.java.html"><b><i>View Source</i></b></a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class MailMessageListener implements MessageListener {
37  
38      public void receive(Message message) {
39          try {
40              doReceive(message);
41          }
42          catch (Exception e) {
43              _log.error("Unable to process message " + message, e);
44          }
45      }
46  
47      protected void doMailMessage(MailMessage mailMessage) throws Exception {
48          InternetAddress[] auditTrail = InternetAddress.parse(
49              PropsValues.MAIL_AUDIT_TRAIL);
50  
51          if (auditTrail.length > 0) {
52              InternetAddress[] bcc = mailMessage.getBCC();
53  
54              if (bcc != null) {
55                  InternetAddress[] allBCC = new InternetAddress[
56                      bcc.length + auditTrail.length];
57  
58                  ArrayUtil.combine(bcc, auditTrail, allBCC);
59  
60                  mailMessage.setBCC(allBCC);
61              }
62              else {
63                  mailMessage.setBCC(auditTrail);
64              }
65          }
66  
67          MailEngine.send(mailMessage);
68      }
69  
70      protected void doMethodWrapper(MethodWrapper methodWrapper)
71          throws Exception {
72  
73          MethodInvoker.invoke(methodWrapper, HookFactory.getInstance());
74      }
75  
76      protected void doReceive(Message message) throws Exception {
77          Object payload = message.getPayload();
78  
79          if (payload instanceof MailMessage) {
80              doMailMessage((MailMessage)payload);
81          }
82          else if (payload instanceof MethodWrapper) {
83              doMethodWrapper((MethodWrapper)payload);
84          }
85      }
86  
87      private static Log _log = LogFactoryUtil.getLog(MailMessageListener.class);
88  
89  }