001/*
002 * #%L
003 * Netarchivesuite - archive
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.archive.bitarchive.distribute;
024
025import java.io.File;
026import java.util.Collection;
027import java.util.List;
028
029import dk.netarkivet.archive.distribute.ArchiveMessage;
030import dk.netarkivet.archive.distribute.ArchiveMessageVisitor;
031import dk.netarkivet.common.distribute.ChannelID;
032import dk.netarkivet.common.distribute.Channels;
033import dk.netarkivet.common.distribute.RemoteFile;
034import dk.netarkivet.common.distribute.arcrepository.BatchStatus;
035import dk.netarkivet.common.exceptions.ArgumentNotValid;
036import dk.netarkivet.common.utils.batch.FileBatchJob;
037
038/**
039 * An instance of this class is sent by a bitarchive machine (Bitarchive class) to a BitarchiveMonitorServer to indicate
040 * that that single machine has finished processing a batch job.
041 */
042@SuppressWarnings({"serial"})
043public class BatchEndedMessage extends ArchiveMessage {
044    /**
045     * The identifier for the bitarchive application, that performed the batch-job.
046     */
047    private String baApplicationId;
048    /** The identifier for the message, that initiated the batch-job. */
049    private String originatingBatchMsgId;
050    /** Number of files processed by the batch-job. */
051    private int noOfFilesProcessed;
052    /** Collection of files that the batch-job could not process. */
053    private Collection<File> filesFailed;
054    /** The result of the batchJob. */
055    private RemoteFile rf;
056    /** List of exceptions that occurred during processing. */
057    private List<FileBatchJob.ExceptionOccurrence> exceptions;
058
059    /**
060     * Message to signal from a BitarchiveServer to the BitarchiveMonitorServer that the Bit Archive Application
061     * identified by BA_ApplicationId has completed its part of the batch job.
062     * <p>
063     * Holds status information: list of files processed and a list of ARC files (file names) on which the batch job
064     * failed.
065     *
066     * @param to the channel to which this message is to be sent (must be a BAMON channel)
067     * @param baAppId Identifier for the machine sending this message, usually containing the IP address and http port
068     * number
069     * @param originatingBatchMsgId the Id field from the original batch message
070     * @param rf he remote file reference containing the output of the batch job (may be null if no output is
071     * generated).
072     * @throws ArgumentNotValid If the BA_ApplicationId or the originatingBatchMsgId are null or empty, or if the
073     * channel 'to' is null.
074     */
075    public BatchEndedMessage(ChannelID to, String baAppId, String originatingBatchMsgId, RemoteFile rf)
076            throws ArgumentNotValid {
077        super(to, Channels.getError());
078        ArgumentNotValid.checkNotNull(to, "ChannelID to");
079        ArgumentNotValid.checkNotNullOrEmpty(baAppId, "String baAppId");
080        ArgumentNotValid.checkNotNullOrEmpty(originatingBatchMsgId, "String originatingBatchMsgId");
081
082        this.baApplicationId = baAppId;
083        this.originatingBatchMsgId = originatingBatchMsgId;
084        this.rf = rf;
085    }
086
087    /**
088     * Message to signal from a BitarchiveServer to the BitarchiveMonitorServer that the Bit Archive Application
089     * identified by BA_ApplicationId has completed its part of the batch job.
090     * <p>
091     * Holds status information: list of files processed and a list of ARC files (file names) on which the batch job
092     * failed.
093     *
094     * @param to the channel to which this message is to be sent (must be a BAMON channel)
095     * @param originatingBatchMsgId the Id field from the original batch message
096     * @param status The object containing status info.
097     */
098    public BatchEndedMessage(ChannelID to, String originatingBatchMsgId, BatchStatus status) {
099        super(to, Channels.getError());
100        ArgumentNotValid.checkNotNull(to, "to");
101        ArgumentNotValid.checkNotNullOrEmpty(originatingBatchMsgId, "String originatingBatchMsgId");
102        ArgumentNotValid.checkNotNull(status, "BatchStatus status");
103
104        this.originatingBatchMsgId = originatingBatchMsgId;
105        this.baApplicationId = status.getBitArchiveAppId();
106        this.rf = status.getResultFile();
107        this.noOfFilesProcessed = status.getNoOfFilesProcessed();
108        this.filesFailed = status.getFilesFailed();
109        this.exceptions = status.getExceptions();
110    }
111
112    /**
113     * Returns id information for the bitarchive which generated this message.
114     *
115     * @return the id information
116     */
117    public String getBitarchiveID() {
118        return baApplicationId;
119    }
120
121    /**
122     * Returns the Id of the BatchMessage which originated this message.
123     *
124     * @return the Id
125     */
126    public String getOriginatingBatchMsgID() {
127        return originatingBatchMsgId;
128    }
129
130    /**
131     * Returns the number of files processed by this batch job on this machine.
132     *
133     * @return the number of files processed
134     */
135    public int getNoOfFilesProcessed() {
136        return noOfFilesProcessed;
137    }
138
139    /**
140     * Returns a collection of the names of files on which this batch job. failed
141     *
142     * @return a Collection of strings with the file names
143     */
144    public Collection<File> getFilesFailed() {
145        return filesFailed;
146    }
147
148    /**
149     * Set the number of files processed in batch job.
150     *
151     * @param number The number of processed files
152     */
153    public void setNoOfFilesProcessed(int number) {
154        noOfFilesProcessed = number;
155    }
156
157    /**
158     * Set the files that failed in batch job.
159     *
160     * @param files The collection of files that failed
161     */
162    public void setFilesFailed(Collection<File> files) {
163        filesFailed = files;
164    }
165
166    /**
167     * Should be implemented as a part of the visitor pattern. fx.: public void accept(ArchiveMessageVisitor v) {
168     * v.visit(this); }
169     *
170     * @param v A message visitor
171     */
172    public void accept(ArchiveMessageVisitor v) {
173        v.visit(this);
174    }
175
176    /**
177     * Returns the remote file object containing the output of this job.
178     *
179     * @return the remote file object. May be null if this job generates no output
180     */
181    public RemoteFile getRemoteFile() {
182        return rf;
183    }
184
185    /**
186     * Human readable version of this object.
187     *
188     * @return A human readable version of this object
189     */
190    public String toString() {
191        return "\nBatchEndedMessage for batch job " + originatingBatchMsgId + "\nFrom Bitarchive " + baApplicationId
192                + "\nFilesProcessed = " + noOfFilesProcessed + "\n" + super.toString();
193    }
194
195    /**
196     * Returns the list of the exceptions that occurred during processing.
197     *
198     * @return List of exceptions and occurrence information.
199     */
200    public List<FileBatchJob.ExceptionOccurrence> getExceptions() {
201        return exceptions;
202    }
203
204}