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.event.Event;
037import org.nuxeo.ecm.core.event.EventListener;
038import org.nuxeo.ecm.platform.mail.utils.MailCoreHelper;
039
040/**
041 * Listener that listens for MailReceivedEvent.
042 * <p>
043 * The email connection corresponding to every MailFolder document found in the repository is checked for new incoming
044 * email.
045 *
046 * @author Catalin Baican
047 */
048public class MailEventListener implements EventListener {
049
050    public static final String EVENT_NAME = "MailReceivedEvent";
051
052    public static final String PIPE_NAME = "nxmail";
053
054    public static final String INBOX = "INBOX";
055
056    private static final Log log = LogFactory.getLog(MailEventListener.class);
057
058    protected Lock lock = new ReentrantLock();
059
060    @Override
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 {
083            CoreSession coreSession = CoreInstance.getCoreSession(null);
084            StringBuilder query = new StringBuilder();
085            query.append("SELECT * FROM MailFolder ");
086            query.append(" WHERE ecm:isTrashed = 0 ");
087            query.append(" AND ecm:isProxy = 0 ");
088            DocumentModelList mailFolderList = coreSession.query(query.toString());
089
090            for (DocumentModel currentMailFolder : mailFolderList) {
091                try {
092                    MailCoreHelper.checkMail(currentMailFolder, coreSession);
093                } catch (AuthenticationFailedException e) {
094                    // If authentication to an account fails, continue with
095                    // the next folder
096                    log.warn("Error connecting to email account", e);
097                    continue;
098                }
099            }
100        } catch (MessagingException e) {
101            log.error("MailEventListener error...", e);
102        }
103    }
104
105}