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 that ingests (i.e. creates) new domains.
039 */
040public class DomainIngester extends Thread {
041    /** The log. */
042    private static final Logger log = LoggerFactory.getLogger(DomainIngester.class);
043    /** Whether or not the ingesting process is finished yet. */
044    private boolean done = false;
045    /** If an exception is thrown during ingest, it gets stored here. */
046    private Exception e;
047    /** The file containg a list of domains to ingest. */
048    private File ingestFile;
049    /** The JspWriter. */
050    private JspWriter out;
051    /** The locale used in forwarding errormessages to the user. */
052    private Locale l;
053
054    /**
055     * Create a new ingester for a given session and output, reading domains from a file.
056     *
057     * @param out The writer that goes into HTML output
058     * @param ingestFile The file with a list of domains to ingest.
059     * @param l the given locale
060     */
061    public DomainIngester(JspWriter out, File ingestFile, Locale l) {
062        ArgumentNotValid.checkNotNull(ingestFile, "File ingestFile");
063        ArgumentNotValid.checkNotNull(l, "Locale l");
064
065        this.out = out;
066        this.ingestFile = ingestFile;
067        this.l = l;
068    }
069
070    /**
071     * Starts the ingesting thread. When 'done' is set to true, the thread is finished, and any exceptions are found in
072     * 'e'.
073     */
074    public void run() {
075        try {
076            new IngestDomainList().updateDomainInfo(ingestFile, out, l);
077        } catch (Exception ex) {
078            this.e = ex;
079            log.warn("Update domains failed", ex);
080        } finally {
081            done = true;
082        }
083    }
084
085    /**
086     * Check whether or not the DomainIngester is finished.
087     *
088     * @return true if finished; false otherwise
089     */
090    public boolean isDone() {
091        return done;
092    }
093
094    /**
095     * @return any exception caught during ingest
096     */
097    public Exception getException() {
098        return e;
099    }
100}