PreUploadHookChain.java

  1. /*
  2.  * Copyright (C) 2011, Google Inc. and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.transport;

  11. import java.util.Collection;
  12. import java.util.Collections;
  13. import java.util.List;
  14. import java.util.stream.Collectors;

  15. import org.eclipse.jgit.lib.ObjectId;

  16. /**
  17.  * {@link org.eclipse.jgit.transport.PreUploadHook} that delegates to a list of
  18.  * other hooks.
  19.  * <p>
  20.  * Hooks are run in the order passed to the constructor. If running a method on
  21.  * one hook throws an exception, execution of remaining hook methods is aborted.
  22.  */
  23. public class PreUploadHookChain implements PreUploadHook {
  24.     private final List<PreUploadHook> hooks;

  25.     /**
  26.      * Create a new hook chaining the given hooks together.
  27.      *
  28.      * @param hooks
  29.      *            hooks to execute, in order.
  30.      * @return a new hook chain of the given hooks.
  31.      */
  32.     public static PreUploadHook newChain(List<PreUploadHook> hooks) {
  33.         List<PreUploadHook> newHooks = hooks.stream()
  34.                 .filter(hook -> !hook.equals(PreUploadHook.NULL))
  35.                 .collect(Collectors.toList());

  36.         if (newHooks.isEmpty()) {
  37.             return PreUploadHook.NULL;
  38.         } else if (newHooks.size() == 1) {
  39.             return newHooks.get(0);
  40.         } else {
  41.             return new PreUploadHookChain(newHooks);
  42.         }
  43.     }

  44.     /** {@inheritDoc} */
  45.     @Override
  46.     public void onBeginNegotiateRound(UploadPack up,
  47.             Collection<? extends ObjectId> wants, int cntOffered)
  48.             throws ServiceMayNotContinueException {
  49.         for (PreUploadHook hook : hooks) {
  50.             hook.onBeginNegotiateRound(up, wants, cntOffered);
  51.         }
  52.     }

  53.     /** {@inheritDoc} */
  54.     @Override
  55.     public void onEndNegotiateRound(UploadPack up,
  56.             Collection<? extends ObjectId> wants, int cntCommon,
  57.             int cntNotFound, boolean ready)
  58.             throws ServiceMayNotContinueException {
  59.         for (PreUploadHook hook : hooks) {
  60.             hook.onEndNegotiateRound(up, wants, cntCommon, cntNotFound, ready);
  61.         }
  62.     }

  63.     /** {@inheritDoc} */
  64.     @Override
  65.     public void onSendPack(UploadPack up,
  66.             Collection<? extends ObjectId> wants,
  67.             Collection<? extends ObjectId> haves)
  68.             throws ServiceMayNotContinueException {
  69.         for (PreUploadHook hook : hooks) {
  70.             hook.onSendPack(up, wants, haves);
  71.         }
  72.     }

  73.     private PreUploadHookChain(List<PreUploadHook> hooks) {
  74.         this.hooks = Collections.unmodifiableList(hooks);
  75.     }
  76. }