View Javadoc
1   /*
2    * Copyright (C) 2011, GitHub Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  package org.eclipse.jgit.api;
44  
45  import org.eclipse.jgit.lib.Repository;
46  import org.eclipse.jgit.transport.CredentialsProvider;
47  import org.eclipse.jgit.transport.Transport;
48  
49  /**
50   * Base class for commands that use a {@link Transport} during execution.
51   * <p>
52   * This class provides standard configuration of a transport for options such as
53   * a {@link CredentialsProvider}, a timeout, and a
54   * {@link TransportConfigCallback}.
55   *
56   * @param <C>
57   * @param <T>
58   */
59  public abstract class TransportCommand<C extends GitCommand, T> extends
60  		GitCommand<T> {
61  
62  	/**
63  	 * Configured credentials provider
64  	 */
65  	protected CredentialsProvider credentialsProvider;
66  
67  	/**
68  	 * Configured transport timeout
69  	 */
70  	protected int timeout;
71  
72  	/**
73  	 * Configured callback for transport configuration
74  	 */
75  	protected TransportConfigCallback transportConfigCallback;
76  
77  	/**
78  	 * @param repo
79  	 */
80  	protected TransportCommand(final Repository repo) {
81  		super(repo);
82  	}
83  
84  	/**
85  	 * @param credentialsProvider
86  	 *            the {@link CredentialsProvider} to use
87  	 * @return {@code this}
88  	 */
89  	public C setCredentialsProvider(
90  			final CredentialsProvider credentialsProvider) {
91  		this.credentialsProvider = credentialsProvider;
92  		return self();
93  	}
94  
95  	/**
96  	 * @param timeout
97  	 *            the timeout used for the transport step
98  	 * @return {@code this}
99  	 */
100 	public C setTimeout(int timeout) {
101 		this.timeout = timeout;
102 		return self();
103 	}
104 
105 	/**
106 	 * @param transportConfigCallback
107 	 *            if set, the callback will be invoked after the
108 	 *            {@link Transport} has created, but before the
109 	 *            {@link Transport} is used. The callback can use this
110 	 *            opportunity to set additional type-specific configuration on
111 	 *            the {@link Transport} instance.
112 	 * @return {@code this}
113 	 */
114 	public C setTransportConfigCallback(
115 			final TransportConfigCallback transportConfigCallback) {
116 		this.transportConfigCallback = transportConfigCallback;
117 		return self();
118 	}
119 
120 	/** @return {@code this} */
121 	@SuppressWarnings("unchecked")
122 	protected final C self() {
123 		return (C) this;
124 	}
125 
126 	/**
127 	 * Configure transport with credentials provider, timeout, and config
128 	 * callback
129 	 *
130 	 * @param transport
131 	 * @return {@code this}
132 	 */
133 	protected C configure(final Transport transport) {
134 		if (credentialsProvider != null)
135 			transport.setCredentialsProvider(credentialsProvider);
136 		transport.setTimeout(timeout);
137 		if (transportConfigCallback != null)
138 			transportConfigCallback.configure(transport);
139 		return self();
140 	}
141 
142 	/**
143 	 * Configure a child command with the current configuration set in
144 	 * {@code this} command
145 	 *
146 	 * @param childCommand
147 	 * @return {@code this}
148 	 */
149 	protected C configure(final TransportCommand childCommand) {
150 		childCommand.setCredentialsProvider(credentialsProvider);
151 		childCommand.setTimeout(timeout);
152 		childCommand.setTransportConfigCallback(transportConfigCallback);
153 		return self();
154 	}
155 }