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