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.portal.pop;
022:
023: import com.liferay.portal.job.IntervalJob;
024: import com.liferay.portal.kernel.pop.MessageListener;
025: import com.liferay.portal.kernel.util.GetterUtil;
026: import com.liferay.portal.kernel.util.StringPool;
027: import com.liferay.portal.util.PropsUtil;
028: import com.liferay.util.Time;
029: import com.liferay.util.mail.MailEngine;
030:
031: import java.util.Iterator;
032: import java.util.List;
033:
034: import javax.mail.Address;
035: import javax.mail.Flags;
036: import javax.mail.Folder;
037: import javax.mail.Message.RecipientType;
038: import javax.mail.Message;
039: import javax.mail.MessagingException;
040: import javax.mail.Session;
041: import javax.mail.Store;
042: import javax.mail.internet.InternetAddress;
043:
044: import org.apache.commons.logging.Log;
045: import org.apache.commons.logging.LogFactory;
046:
047: import org.quartz.JobExecutionContext;
048: import org.quartz.JobExecutionException;
049:
050: /**
051: * <a href="POPNotificationsJob.java.html"><b><i>View Source</i></b></a>
052: *
053: * @author Brian Wing Shun Chan
054: *
055: */
056: public class POPNotificationsJob implements IntervalJob {
057:
058: public static final long INTERVAL = GetterUtil.getLong(PropsUtil
059: .get(PropsUtil.POP_SERVER_NOTIFICATIONS_INTERVAL))
060: * Time.MINUTE;
061:
062: public long getInterval() {
063: return INTERVAL;
064: }
065:
066: public void execute(JobExecutionContext context)
067: throws JobExecutionException {
068:
069: if (!_executing) {
070: try {
071: _executing = true;
072:
073: if (_log.isDebugEnabled()) {
074: _log.debug("Executing");
075:
076: pollPopServer();
077: }
078: } catch (Exception e) {
079: _log.error(e, e);
080: } finally {
081: _executing = false;
082: }
083: } else {
084: if (_log.isDebugEnabled()) {
085: _log.debug("Not executing");
086: }
087: }
088: }
089:
090: protected String getEmailAddress(Address[] addresses) {
091: if ((addresses == null) || (addresses.length == 0)) {
092: return StringPool.BLANK;
093: }
094:
095: InternetAddress internetAddress = (InternetAddress) addresses[0];
096:
097: return internetAddress.getAddress();
098: }
099:
100: protected void initInboxFolder() throws Exception {
101: if ((_inboxFolder == null) || !_inboxFolder.isOpen()) {
102: initStore();
103:
104: Folder defaultFolder = _store.getDefaultFolder();
105:
106: Folder[] folders = defaultFolder.list();
107:
108: if (folders.length == 0) {
109: throw new MessagingException("Inbox not found");
110: } else {
111: _inboxFolder = folders[0];
112:
113: _inboxFolder.open(Folder.READ_WRITE);
114: }
115: }
116: }
117:
118: protected void initStore() throws Exception {
119: if ((_store == null) || !_store.isConnected()) {
120: Session session = MailEngine.getSession();
121:
122: _store = session.getStore("pop3");
123:
124: String popHost = session.getProperty("mail.pop3.host");
125: String smtpUser = session.getProperty("mail.smtp.user");
126: String smtpPassword = session
127: .getProperty("mail.smtp.password");
128:
129: _store.connect(popHost, smtpUser, smtpPassword);
130: }
131: }
132:
133: protected void nostifyListeners(List listeners, Message message)
134: throws Exception {
135:
136: String from = getEmailAddress(message.getFrom());
137: String recipient = getEmailAddress(message
138: .getRecipients(RecipientType.TO));
139:
140: if (_log.isDebugEnabled()) {
141: _log.debug("From " + from);
142: _log.debug("Recipient " + recipient);
143: }
144:
145: Iterator itr = listeners.iterator();
146:
147: while (itr.hasNext()) {
148: MessageListener messageListener = (MessageListener) itr
149: .next();
150:
151: try {
152: if (messageListener.accept(from, recipient)) {
153: messageListener.deliver(from, recipient, message);
154: }
155: } catch (Exception e) {
156: _log.error(e, e);
157: }
158: }
159: }
160:
161: protected void nostifyListeners(Message[] messages)
162: throws Exception {
163: if (_log.isDebugEnabled()) {
164: _log.debug("Messages " + messages.length);
165: }
166:
167: List listeners = POPServerUtil.getListeners();
168:
169: for (int i = 0; i < messages.length; i++) {
170: Message message = messages[i];
171:
172: if (_log.isDebugEnabled()) {
173: _log.debug("Message " + message);
174: }
175:
176: nostifyListeners(listeners, message);
177: }
178: }
179:
180: protected void pollPopServer() throws Exception {
181: initInboxFolder();
182:
183: Message[] messages = _inboxFolder.getMessages();
184:
185: try {
186: nostifyListeners(messages);
187: } finally {
188: if (_log.isDebugEnabled()) {
189: _log.debug("Deleting messages");
190: }
191:
192: _inboxFolder.setFlags(messages, new Flags(
193: Flags.Flag.DELETED), true);
194:
195: _inboxFolder.close(true);
196: }
197: }
198:
199: private static Log _log = LogFactory
200: .getLog(POPNotificationsJob.class);
201:
202: private boolean _executing;
203: private Store _store;
204: private Folder _inboxFolder;
205:
206: }
|