001/*
002 * (C) Copyright 2006-2009 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.mail.listener;
021
022import java.util.concurrent.locks.Lock;
023import java.util.concurrent.locks.ReentrantLock;
024
025import javax.mail.AuthenticationFailedException;
026import javax.mail.MessagingException;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.core.api.CoreInstance;
031import org.nuxeo.ecm.core.api.CoreSession;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.DocumentModelList;
034import org.nuxeo.ecm.core.api.LifeCycleConstants;
035import org.nuxeo.ecm.core.event.Event;
036import org.nuxeo.ecm.core.event.EventListener;
037import org.nuxeo.ecm.platform.mail.utils.MailCoreHelper;
038
039/**
040 * Listener that listens for MailReceivedEvent.
041 * <p>
042 * The email connection corresponding to every MailFolder document found in the repository is checked for new incoming
043 * email.
044 *
045 * @author Catalin Baican
046 */
047public class MailEventListener implements EventListener {
048
049    public static final String EVENT_NAME = "MailReceivedEvent";
050
051    public static final String PIPE_NAME = "nxmail";
052
053    public static final String INBOX = "INBOX";
054
055    private static final Log log = LogFactory.getLog(MailEventListener.class);
056
057    protected Lock lock = new ReentrantLock();
058
059    public void handleEvent(Event event) {
060        String eventId = event.getName();
061
062        if (!EVENT_NAME.equals(eventId)) {
063            return;
064        }
065
066        if (lock.tryLock()) {
067            try {
068                doHandleEvent(event);
069            } finally {
070                lock.unlock();
071            }
072        } else {
073            log.info("Skip email check since it is already running");
074        }
075
076    }
077
078    public void doHandleEvent(Event event) {
079
080        try (CoreSession coreSession = CoreInstance.openCoreSession(null)) {
081            StringBuilder query = new StringBuilder();
082            query.append("SELECT * FROM MailFolder ");
083            query.append(String.format(" WHERE ecm:currentLifeCycleState != '%s' ", LifeCycleConstants.DELETED_STATE));
084            query.append(" AND ecm:isProxy = 0 ");
085            DocumentModelList mailFolderList = coreSession.query(query.toString());
086
087            for (DocumentModel currentMailFolder : mailFolderList) {
088                try {
089                    MailCoreHelper.checkMail(currentMailFolder, coreSession);
090                } catch (AuthenticationFailedException e) {
091                    // If authentication to an account fails, continue with
092                    // the next folder
093                    log.warn("Error connecting to email account", e);
094                    continue;
095                }
096            }
097        } catch (MessagingException e) {
098            log.error("MailEventListener error...", e);
099        }
100    }
101
102}