001/*
002 * (C) Copyright 2006-2009 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.mail.listener;
023
024import java.util.concurrent.locks.Lock;
025import java.util.concurrent.locks.ReentrantLock;
026
027import javax.mail.AuthenticationFailedException;
028import javax.mail.MessagingException;
029
030import org.apache.commons.logging.Log;
031import org.apache.commons.logging.LogFactory;
032import org.nuxeo.ecm.core.api.CoreInstance;
033import org.nuxeo.ecm.core.api.CoreSession;
034import org.nuxeo.ecm.core.api.DocumentModel;
035import org.nuxeo.ecm.core.api.DocumentModelList;
036import org.nuxeo.ecm.core.api.LifeCycleConstants;
037import org.nuxeo.ecm.core.event.Event;
038import org.nuxeo.ecm.core.event.EventListener;
039import org.nuxeo.ecm.platform.mail.utils.MailCoreHelper;
040
041/**
042 * Listener that listens for MailReceivedEvent.
043 * <p>
044 * The email connection corresponding to every MailFolder document found in the repository is checked for new incoming
045 * email.
046 *
047 * @author Catalin Baican
048 */
049public class MailEventListener implements EventListener {
050
051    public static final String EVENT_NAME = "MailReceivedEvent";
052
053    public static final String PIPE_NAME = "nxmail";
054
055    public static final String INBOX = "INBOX";
056
057    private static final Log log = LogFactory.getLog(MailEventListener.class);
058
059    protected Lock lock = new ReentrantLock();
060
061    public void handleEvent(Event event) {
062        String eventId = event.getName();
063
064        if (!EVENT_NAME.equals(eventId)) {
065            return;
066        }
067
068        if (lock.tryLock()) {
069            try {
070                doHandleEvent(event);
071            } finally {
072                lock.unlock();
073            }
074        } else {
075            log.info("Skip email check since it is already running");
076        }
077
078    }
079
080    public void doHandleEvent(Event event) {
081
082        try (CoreSession coreSession = CoreInstance.openCoreSession(null)) {
083            StringBuilder query = new StringBuilder();
084            query.append("SELECT * FROM MailFolder ");
085            query.append(String.format(" WHERE ecm:currentLifeCycleState != '%s' ", LifeCycleConstants.DELETED_STATE));
086            query.append(" AND ecm:isProxy = 0 ");
087            DocumentModelList mailFolderList = coreSession.query(query.toString());
088
089            for (DocumentModel currentMailFolder : mailFolderList) {
090                try {
091                    MailCoreHelper.checkMail(currentMailFolder, coreSession);
092                } catch (AuthenticationFailedException e) {
093                    // If authentication to an account fails, continue with
094                    // the next folder
095                    log.warn("Error connecting to email account", e);
096                    continue;
097                }
098            }
099        } catch (MessagingException e) {
100            log.error("MailEventListener error...", e);
101        }
102    }
103
104}