View Javadoc
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  
11  package org.eclipse.jgit.transport;
12  
13  import java.io.IOException;
14  import java.util.List;
15  
16  /**
17   * {@link org.eclipse.jgit.transport.AdvertiseRefsHook} that delegates to a list
18   * of other hooks.
19   * <p>
20   * Hooks are run in the order passed to the constructor. A hook may inspect or
21   * modify the results of the previous hooks in the chain by calling
22   * {@link org.eclipse.jgit.transport.UploadPack#getAdvertisedRefs()}, or
23   * {@link org.eclipse.jgit.transport.ReceivePack#getAdvertisedRefs()} or
24   * {@link org.eclipse.jgit.transport.ReceivePack#getAdvertisedObjects()}.
25   */
26  public class AdvertiseRefsHookChain implements AdvertiseRefsHook {
27  	private final AdvertiseRefsHook[] hooks;
28  	private final int count;
29  
30  	/**
31  	 * Create a new hook chaining the given hooks together.
32  	 *
33  	 * @param hooks
34  	 *            hooks to execute, in order.
35  	 * @return a new hook chain of the given hooks.
36  	 */
37  	public static AdvertiseRefsHook newChain(List<? extends AdvertiseRefsHook> hooks) {
38  		AdvertiseRefsHook[] newHooks = new AdvertiseRefsHook[hooks.size()];
39  		int i = 0;
40  		for (AdvertiseRefsHook hook : hooks)
41  			if (hook != AdvertiseRefsHook.DEFAULT)
42  				newHooks[i++] = hook;
43  		switch (i) {
44  		case 0:
45  			return AdvertiseRefsHook.DEFAULT;
46  		case 1:
47  			return newHooks[0];
48  		default:
49  			return new AdvertiseRefsHookChain(newHooks, i);
50  		}
51  	}
52  
53  	/**
54  	 * {@inheritDoc}
55  	 */
56  	@Override
57  	public void advertiseRefs(ReceivePack rp)
58  			throws IOException {
59  		for (int i = 0; i < count; i++)
60  			hooks[i].advertiseRefs(rp);
61  	}
62  
63  	/** {@inheritDoc} */
64  	@Override
65  	public void advertiseRefs(UploadPack rp)
66  			throws ServiceMayNotContinueException {
67  		for (int i = 0; i < count; i++)
68  			hooks[i].advertiseRefs(rp);
69  	}
70  
71  	private AdvertiseRefsHookChain(AdvertiseRefsHook[] hooks, int count) {
72  		this.hooks = hooks;
73  		this.count = count;
74  	}
75  }