001/* 002 * #%L 003 * Netarchivesuite - archive 004 * %% 005 * Copyright (C) 2005 - 2018 The Royal Danish 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.util.ArrayList; 026import java.util.Collections; 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.exceptions.ArgumentNotValid; 034import dk.netarkivet.common.utils.batch.FileBatchJob; 035 036/** 037 * Container for batch jobs. Messages of this class should be sent to a BAMON queue from where they are collected by a 038 * BitarchiveMonitorServer. The BitarchiveMonitorServer also creates instances of this class and sends them to the 039 * individual bitarchive machines. 040 * <p> 041 * The response to this message comes in the form of a BatchReplyMessage placed on the senders queue. 042 */ 043@SuppressWarnings({"serial"}) 044public class BatchMessage extends ArchiveMessage { 045 /** The batch job, this message is sent to initiate. */ 046 private FileBatchJob job; 047 /** The id of this replica. */ 048 private String replicaId; 049 /** The list of arguments for the batchjob. */ 050 private List<String> args; 051 /** The ID for the batch process. */ 052 private String batchID; 053 054 /** 055 * Creates a BatchMessage object which can be used to initiate a batch job. This is used by BitarchiveMonitorServer 056 * to create the message sent to the bitarchive machines. 057 * <p> 058 * Note: The id for the batchjob is the empty string, which removes the possibility of terminating the batchjob 059 * remotely while it is running. 060 * 061 * @param to The channel to which the batch message is to be sent 062 * @param job The batch job to be executed 063 * @param replicaId id of this replica. 064 */ 065 public BatchMessage(ChannelID to, FileBatchJob job, String replicaId) { 066 this(to, Channels.getError(), job, replicaId, "", new String[] {}); 067 } 068 069 /** 070 * Creates a BatchMessage object which can be used to initiate a batch job. 071 * <p> 072 * Note: The id for the batchjob is the empty string, which removes the possibility of terminating the batchjob 073 * remotely while it is running. 074 * 075 * @param to The channel to which the batch message is to be sent 076 * @param replyTo The channel whereto the reply to this message is sent. 077 * @param job The batch job to be executed 078 * @param replicaId id of this replica. 079 * @param arguments The arguments for initialising the batchjob. 080 * @throws ArgumentNotValid If the job is null, or the replica is either null or the empty string. 081 */ 082 public BatchMessage(ChannelID to, ChannelID replyTo, FileBatchJob job, String replicaId, String... arguments) { 083 this(to, replyTo, job, replicaId, "", arguments); 084 } 085 086 /** 087 * Creates a BatchMessage object which can be used to initiate a batch job. 088 * 089 * @param to The channel to which the batch message is to be sent 090 * @param replyTo The channel whereto the reply to this message is sent. 091 * @param job The batch job to be executed 092 * @param replicaId id of this replica. 093 * @param batchId The id for the process which runs the batchjob. 094 * @param arguments The arguments for initialising the batchjob. This is allowed to be null. 095 * @throws ArgumentNotValid If the job is null, or the replica is either null or the empty string. 096 */ 097 public BatchMessage(ChannelID to, ChannelID replyTo, FileBatchJob job, String replicaId, String batchId, 098 String... arguments) throws ArgumentNotValid { 099 super(to, replyTo); 100 ArgumentNotValid.checkNotNull(job, "job"); 101 ArgumentNotValid.checkNotNullOrEmpty(replicaId, "String replicaId"); 102 ArgumentNotValid.checkNotNull(batchId, "String batchId"); 103 this.job = job; 104 this.replicaId = replicaId; 105 this.batchID = batchId; 106 this.args = new ArrayList<String>(); 107 if (arguments != null && !(arguments.length == 0)) { 108 Collections.addAll(this.args, arguments); 109 } 110 } 111 112 /** 113 * Retrieves batch job. 114 * 115 * @return Batch job 116 */ 117 public FileBatchJob getJob() { 118 return job; 119 } 120 121 /** 122 * Returns the replica id. 123 * 124 * @return the replica id 125 */ 126 public String getReplicaId() { 127 return replicaId; 128 } 129 130 /** 131 * Returns the arguments for the batchjob. 132 * 133 * @return The arguments for the batchjob. 134 */ 135 public List<String> getArgs() { 136 return args; 137 } 138 139 /** 140 * Returns the predefined ID for the batch process. If no Id is available, then the message id is returned. 141 * 142 * @return The ID for the batch process, or the message id, if no specific batch id has been declared. 143 */ 144 public String getBatchID() { 145 // if the batchId is empty, then use the message id as process id. 146 if (batchID.isEmpty()) { 147 return super.getID(); 148 } 149 return batchID; 150 } 151 152 /** 153 * Should be implemented as a part of the visitor pattern. fx.: public void accept(ArchiveMessageVisitor v) { 154 * v.visit(this); } 155 * 156 * @param v A message visitor 157 */ 158 public void accept(ArchiveMessageVisitor v) { 159 v.visit(this); 160 } 161 162 /** 163 * Retrieval of a string representation of this object. 164 * 165 * @return A string representation of the instance of class. 166 * @see dk.netarkivet.common.distribute.NetarkivetMessage#toString() 167 */ 168 public String toString() { 169 return super.toString() + " Job: " + job.getClass().getName() + ", on filename-pattern: " 170 + job.getFilenamePattern() + ", for replica: " + replicaId; 171 } 172}