001/*
002 * #%L
003 * Netarchivesuite - harvester
004 * %%
005 * Copyright (C) 2005 - 2014 The Royal Danish Library, the Danish State and University Library,
006 *             the National Library of France and the Austrian National Library.
007 * %%
008 * This program is free software: you can redistribute it and/or modify
009 * it under the terms of the GNU Lesser General Public License as
010 * published by the Free Software Foundation, either version 2.1 of the
011 * License, or (at your option) any later version.
012 * 
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016 * GNU General Lesser Public License for more details.
017 * 
018 * You should have received a copy of the GNU General Lesser Public
019 * License along with this program.  If not, see
020 * <http://www.gnu.org/licenses/lgpl-2.1.html>.
021 * #L%
022 */
023package dk.netarkivet.harvester.webinterface;
024
025import java.io.File;
026import java.util.Locale;
027
028import javax.servlet.jsp.JspWriter;
029
030import org.slf4j.Logger;
031import org.slf4j.LoggerFactory;
032
033import dk.netarkivet.common.exceptions.ArgumentNotValid;
034import dk.netarkivet.harvester.datamodel.IngestDomainList;
035
036
037/**
038 * This class manages a thread of ingesting domains.
039 */
040
041public class DomainIngester extends Thread {
042    /** The log. */
043    //Log log = LogFactory.getLog(DomainIngester.class.getName());
044    private static final Logger log = LoggerFactory.getLogger(DomainIngester.class);
045    /** Whether or not the ingesting process is finished yet. */
046    private boolean done = false;
047    /** If an exception is thrown during ingest, it gets stored here. */
048    private Exception e;
049    /** The file containg a list of domains to ingest. */
050    private File ingestFile;
051    /** The JspWriter. */
052    private JspWriter out;
053    /** The locale used in forwarding errormessages to the user. */
054    private Locale l;
055
056    /**
057     * Create a new ingester for a given session and outpout, reading domains from a file.
058     *
059     * @param out The writer that goes into HTML output
060     * @param ingestFile The file of domains to ingest.
061     * @param l the given locale
062     */
063    public DomainIngester(JspWriter out, File ingestFile, Locale l) {
064        // TODO Should we validate the JspWriter
065        // We currently don't, so we don't do it here either.
066        ArgumentNotValid.checkNotNull(ingestFile, "File ingestFile");
067        ArgumentNotValid.checkNotNull(l, "Locale l");
068
069        this.out = out;
070        this.ingestFile = ingestFile;
071        this.l = l;
072    }
073
074    /**
075     * Starts the ingesting thread. When 'done' is set to true, the thread is finished, and any exceptions are found in
076     * 'e'.
077     */
078    public void run() {
079        try {
080            new IngestDomainList().updateDomainInfo(ingestFile, out, l);
081        } catch (Exception ex) {
082            this.e = ex;
083            log.warn("Update domains failed", ex);
084        } finally {
085            done = true;
086        }
087    }
088
089    /**
090     * Check whether or not the DomainIngester is finished.
091     *
092     * @return true if finished; false otherwise
093     */
094    public boolean isDone() {
095        return done;
096    }
097
098    /**
099     * @return any exception catched during ingest
100     */
101    public Exception getException() {
102        return e;
103    }
104}