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