001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package pt.ua.dicoogle.core;
020
021import java.io.File;
022import java.io.IOException;
023import java.net.URI;
024
025import org.apache.commons.io.monitor.FileAlterationListener;
026import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
027import org.apache.commons.io.monitor.FileAlterationMonitor;
028import org.apache.commons.io.monitor.FileAlterationObserver;
029import org.slf4j.LoggerFactory;
030
031
032import org.slf4j.Logger;
033import pt.ua.dicoogle.plugins.PluginController;
034
035
036/**
037 *
038 * @author Luís A. Bastião Silva <bastiao@ua.pt>
039 */
040public class AsyncIndex {
041    private static final Logger logger = LoggerFactory.getLogger(AsyncIndex.class);
042    private final long pollingInterval = 30 * 1000;
043
044    public AsyncIndex() {
045
046        // path to watch
047        String path = ServerSettings.getInstance().getDicoogleDir();
048
049
050        FileAlterationObserver observer = new FileAlterationObserver(path);
051        FileAlterationMonitor monitor =
052                new FileAlterationMonitor(pollingInterval);
053        FileAlterationListener listener = new FileAlterationListenerAdaptor() {
054            // Is triggered when a file is created in the monitored folder
055            @Override
056            public void onFileCreate(File file) {
057
058
059                try {
060                    logger.debug("created {} : {}", file.toPath(), file.toURI());
061                    URI uri = file.toURI();
062                    PluginController.getInstance().index(uri);
063                } catch (Exception ex) {
064                    logger.error(ex.getMessage(), ex);
065                }
066
067
068            }
069            // Is triggered when a file is deleted from the monitored folder
070            @Override
071            public void onFileDelete(File file) {
072                try {
073                    // "file" is the reference to the removed file
074                    System.out.println("File removed: "
075                            + file.getCanonicalPath());
076                    // "file" does not exists anymore in the location
077                    System.out.println("File still exists in location: "
078                            + file.exists());
079
080                    logger.debug("deleted {} : {}", file.toPath(), file.toURI());
081                    try {
082                        URI uri = file.toURI();
083                        PluginController.getInstance().unindex(uri);
084                    } catch (Exception ex) {
085                        logger.error(ex.getMessage(), ex);
086                    }
087                } catch (IOException e) {
088                    e.printStackTrace(System.err);
089                }
090            }
091        };
092        observer.addListener(listener);
093        monitor.addObserver(observer);
094        try {
095            monitor.start();
096        } catch (Exception e) {
097            e.printStackTrace();
098        }
099    }
100}
101