001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.mail.service.jms;
022:
023: import com.liferay.portal.kernel.mail.MailMessage;
024: import com.liferay.portal.kernel.util.ArrayUtil;
025: import com.liferay.portal.kernel.util.MethodInvoker;
026: import com.liferay.portal.kernel.util.MethodWrapper;
027: import com.liferay.portal.util.PropsUtil;
028: import com.liferay.util.mail.MailEngine;
029:
030: import javax.jms.Message;
031: import javax.jms.MessageListener;
032: import javax.jms.ObjectMessage;
033: import javax.jms.Queue;
034: import javax.jms.QueueConnection;
035: import javax.jms.QueueConnectionFactory;
036: import javax.jms.QueueReceiver;
037: import javax.jms.QueueSession;
038: import javax.jms.Session;
039:
040: import javax.mail.internet.InternetAddress;
041:
042: import org.apache.commons.logging.Log;
043: import org.apache.commons.logging.LogFactory;
044:
045: /**
046: * <a href="MailConsumer.java.html"><b><i>View Source</i></b></a>
047: *
048: * @author Brian Wing Shun Chan
049: *
050: */
051: public class MailConsumer implements MessageListener {
052:
053: public void consume() {
054: try {
055: QueueConnectionFactory qcf = MailQCFUtil.getQCF();
056: QueueConnection con = qcf.createQueueConnection();
057:
058: QueueSession session = con.createQueueSession(false,
059: Session.AUTO_ACKNOWLEDGE);
060: Queue queue = (Queue) MailQueueUtil.getQueue();
061:
062: QueueReceiver subscriber = session.createReceiver(queue);
063:
064: subscriber.setMessageListener(this );
065:
066: con.start();
067: } catch (Exception e) {
068: _log.error(e, e);
069: }
070: }
071:
072: public void onMessage(Message msg) {
073: try {
074: ObjectMessage objMsg = (ObjectMessage) msg;
075:
076: Object obj = objMsg.getObject();
077:
078: if (obj instanceof MailMessage) {
079: _onMessage((MailMessage) obj);
080: } else if (obj instanceof MethodWrapper) {
081: _onMessage((MethodWrapper) obj);
082: }
083: } catch (Exception e) {
084: _log.error(e, e);
085: }
086: }
087:
088: private void _onMessage(MailMessage mailMessage) throws Exception {
089: InternetAddress[] auditTrail = InternetAddress.parse(PropsUtil
090: .get(PropsUtil.MAIL_AUDIT_TRAIL));
091:
092: if (auditTrail.length > 0) {
093: InternetAddress[] bcc = mailMessage.getBCC();
094:
095: if (bcc != null) {
096: InternetAddress[] allBCC = new InternetAddress[bcc.length
097: + auditTrail.length];
098:
099: ArrayUtil.combine(bcc, auditTrail, allBCC);
100:
101: mailMessage.setBCC(allBCC);
102: } else {
103: mailMessage.setBCC(auditTrail);
104: }
105: }
106:
107: MailEngine.send(mailMessage);
108: }
109:
110: private void _onMessage(MethodWrapper methodWrapper)
111: throws Exception {
112: MethodInvoker.invoke(methodWrapper);
113: }
114:
115: private static Log _log = LogFactory.getLog(MailConsumer.class);
116:
117: }
|