Line data Source code
1 : // Copyright (C) 2020 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.server.update; 16 : 17 : import com.google.common.collect.ImmutableList; 18 : import com.google.gerrit.extensions.restapi.RestApiException; 19 : import com.google.gerrit.server.submit.MergeOpRepoManager; 20 : import java.util.Collection; 21 : import java.util.Optional; 22 : import java.util.stream.Collectors; 23 : 24 : public class SubmissionExecutor { 25 : 26 : private final ImmutableList<SubmissionListener> submissionListeners; 27 : private final boolean dryrun; 28 69 : private ImmutableList<BatchUpdateListener> additionalListeners = ImmutableList.of(); 29 : 30 69 : public SubmissionExecutor(boolean dryrun, ImmutableList<SubmissionListener> submissionListeners) { 31 69 : this.dryrun = dryrun; 32 69 : this.submissionListeners = submissionListeners; 33 69 : if (dryrun) { 34 0 : submissionListeners.forEach(SubmissionListener::setDryrun); 35 : } 36 69 : } 37 : 38 : /** 39 : * Set additional listeners. These can be set again in each try (or will be reused if not 40 : * overwritten). 41 : */ 42 : public void setAdditionalBatchUpdateListeners( 43 : ImmutableList<BatchUpdateListener> additionalListeners) { 44 53 : this.additionalListeners = additionalListeners; 45 53 : } 46 : 47 : /** Execute the batch updates, reporting to all the Submission and BatchUpdateListeners. */ 48 : public void execute(Collection<BatchUpdate> updates) throws RestApiException, UpdateException { 49 69 : submissionListeners.forEach(l -> l.beforeBatchUpdates(updates)); 50 : 51 69 : ImmutableList<BatchUpdateListener> listeners = 52 : new ImmutableList.Builder<BatchUpdateListener>() 53 69 : .addAll(additionalListeners) 54 69 : .addAll( 55 69 : submissionListeners.stream() 56 69 : .map(l -> l.listensToBatchUpdates()) 57 69 : .filter(Optional::isPresent) 58 69 : .map(Optional::get) 59 69 : .collect(Collectors.toList())) 60 69 : .build(); 61 69 : BatchUpdate.execute(updates, listeners, dryrun); 62 69 : } 63 : 64 : /** 65 : * Caller invokes this when done with the submission (either because everything succeeded or gave 66 : * up retrying). 67 : */ 68 : public void afterExecutions(MergeOpRepoManager orm) { 69 69 : submissionListeners.forEach(l -> l.afterSubmission(orm)); 70 69 : } 71 : }