1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.mail.service.jms;
24  
25  import com.liferay.portal.kernel.mail.MailMessage;
26  import com.liferay.portal.kernel.util.ArrayUtil;
27  import com.liferay.portal.kernel.util.MethodInvoker;
28  import com.liferay.portal.kernel.util.MethodWrapper;
29  import com.liferay.portal.util.PropsUtil;
30  import com.liferay.util.mail.MailEngine;
31  
32  import javax.jms.Message;
33  import javax.jms.MessageListener;
34  import javax.jms.ObjectMessage;
35  import javax.jms.Queue;
36  import javax.jms.QueueConnection;
37  import javax.jms.QueueConnectionFactory;
38  import javax.jms.QueueReceiver;
39  import javax.jms.QueueSession;
40  import javax.jms.Session;
41  
42  import javax.mail.internet.InternetAddress;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  
47  /**
48   * <a href="MailConsumer.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class MailConsumer implements MessageListener {
54  
55      public void consume() {
56          try {
57              QueueConnectionFactory qcf = MailQCFUtil.getQCF();
58              QueueConnection con = qcf.createQueueConnection();
59  
60              QueueSession session = con.createQueueSession(
61                  false, Session.AUTO_ACKNOWLEDGE);
62              Queue queue = (Queue)MailQueueUtil.getQueue();
63  
64              QueueReceiver subscriber = session.createReceiver(queue);
65  
66              subscriber.setMessageListener(this);
67  
68              con.start();
69          }
70          catch (Exception e) {
71              _log.error(e, e);
72          }
73      }
74  
75      public void onMessage(Message msg) {
76          try {
77              ObjectMessage objMsg = (ObjectMessage)msg;
78  
79              Object obj = objMsg.getObject();
80  
81              if (obj instanceof MailMessage) {
82                  _onMessage((MailMessage)obj);
83              }
84              else if (obj instanceof MethodWrapper) {
85                  _onMessage((MethodWrapper)obj);
86              }
87          }
88          catch (Exception e) {
89              _log.error(e, e);
90          }
91      }
92  
93      private void _onMessage(MailMessage mailMessage) throws Exception {
94          InternetAddress[] auditTrail = InternetAddress.parse(
95              PropsUtil.get(PropsUtil.MAIL_AUDIT_TRAIL));
96  
97          if (auditTrail.length > 0) {
98              InternetAddress[] bcc = mailMessage.getBCC();
99  
100             if (bcc != null) {
101                 InternetAddress[] allBCC = new InternetAddress[
102                     bcc.length + auditTrail.length];
103 
104                 ArrayUtil.combine(bcc, auditTrail, allBCC);
105 
106                 mailMessage.setBCC(allBCC);
107             }
108             else {
109                 mailMessage.setBCC(auditTrail);
110             }
111         }
112 
113         MailEngine.send(mailMessage);
114     }
115 
116     private void _onMessage(MethodWrapper methodWrapper) throws Exception {
117         MethodInvoker.invoke(methodWrapper);
118     }
119 
120     private static Log _log = LogFactory.getLog(MailConsumer.class);
121 
122 }