001/*
002 * (C) Copyright 2006-2012 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 */
019package org.nuxeo.ecm.platform.scanimporter.listener;
020
021import org.apache.commons.logging.Log;
022import org.apache.commons.logging.LogFactory;
023import org.nuxeo.ecm.core.event.Event;
024import org.nuxeo.ecm.core.event.EventListener;
025import org.nuxeo.ecm.platform.scanimporter.processor.ScannedFileImporter;
026
027/**
028 * Listen to Scheduler events to check if new scanned files are availables Trigger the importer if not already busy.
029 *
030 * @author Thierry Delprat
031 */
032public class IngestionTrigger implements EventListener {
033
034    private static final Log log = LogFactory.getLog(IngestionTrigger.class);
035
036    private static boolean ingestionInProgress = false;
037
038    public static String START_EVENT = "ScanIngestionStart";
039
040    // fired via the Scheduler
041    @Override
042    public void handleEvent(Event event) {
043
044        if (!START_EVENT.equals(event.getName())) {
045            return;
046        }
047
048        if (ingestionInProgress) {
049            log.info("Ingestion already in progress, waiting for next wake up");
050            return;
051        } else {
052            log.info("Start injection process");
053        }
054
055        ingestionInProgress = true;
056        try {
057            ScannedFileImporter importer = new ScannedFileImporter();
058            if (event.getContext().getProperty("Testing") != null) {
059                event.getContext().setProperty("Tested", true);
060            } else {
061                importer.doImport();
062            }
063        } finally {
064            ingestionInProgress = false;
065        }
066    }
067
068}