You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AdvertiseRefsHookChain.java 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (C) 2012, 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.List;
  12. /**
  13. * {@link org.eclipse.jgit.transport.AdvertiseRefsHook} that delegates to a list
  14. * of other hooks.
  15. * <p>
  16. * Hooks are run in the order passed to the constructor. A hook may inspect or
  17. * modify the results of the previous hooks in the chain by calling
  18. * {@link org.eclipse.jgit.transport.UploadPack#getAdvertisedRefs()}, or
  19. * {@link org.eclipse.jgit.transport.ReceivePack#getAdvertisedRefs()} or
  20. * {@link org.eclipse.jgit.transport.ReceivePack#getAdvertisedObjects()}.
  21. */
  22. public class AdvertiseRefsHookChain implements AdvertiseRefsHook {
  23. private final AdvertiseRefsHook[] hooks;
  24. private final int count;
  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 AdvertiseRefsHook newChain(List<? extends AdvertiseRefsHook> hooks) {
  33. AdvertiseRefsHook[] newHooks = new AdvertiseRefsHook[hooks.size()];
  34. int i = 0;
  35. for (AdvertiseRefsHook hook : hooks)
  36. if (hook != AdvertiseRefsHook.DEFAULT)
  37. newHooks[i++] = hook;
  38. switch (i) {
  39. case 0:
  40. return AdvertiseRefsHook.DEFAULT;
  41. case 1:
  42. return newHooks[0];
  43. default:
  44. return new AdvertiseRefsHookChain(newHooks, i);
  45. }
  46. }
  47. /** {@inheritDoc} */
  48. @Override
  49. public void advertiseRefs(ReceivePack rp)
  50. throws ServiceMayNotContinueException {
  51. for (int i = 0; i < count; i++)
  52. hooks[i].advertiseRefs(rp);
  53. }
  54. /** {@inheritDoc} */
  55. @Override
  56. public void advertiseRefs(UploadPack rp)
  57. throws ServiceMayNotContinueException {
  58. for (int i = 0; i < count; i++)
  59. hooks[i].advertiseRefs(rp);
  60. }
  61. private AdvertiseRefsHookChain(AdvertiseRefsHook[] hooks, int count) {
  62. this.hooks = hooks;
  63. this.count = count;
  64. }
  65. }