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  		setCredentialsProvider(CredentialsProvider.getDefault());
83  	}
84  
85  	/**
86  	 * @param credentialsProvider
87  	 *            the {@link CredentialsProvider} to use
88  	 * @return {@code this}
89  	 */
90  	public C setCredentialsProvider(
91  			final CredentialsProvider credentialsProvider) {
92  		this.credentialsProvider = credentialsProvider;
93  		return self();
94  	}
95  
96  	/**
97  	 * @param timeout
98  	 *            the timeout used for the transport step
99  	 * @return {@code this}
100 	 */
101 	public C setTimeout(int timeout) {
102 		this.timeout = timeout;
103 		return self();
104 	}
105 
106 	/**
107 	 * @param transportConfigCallback
108 	 *            if set, the callback will be invoked after the
109 	 *            {@link Transport} has created, but before the
110 	 *            {@link Transport} is used. The callback can use this
111 	 *            opportunity to set additional type-specific configuration on
112 	 *            the {@link Transport} instance.
113 	 * @return {@code this}
114 	 */
115 	public C setTransportConfigCallback(
116 			final TransportConfigCallback transportConfigCallback) {
117 		this.transportConfigCallback = transportConfigCallback;
118 		return self();
119 	}
120 
121 	/** @return {@code this} */
122 	@SuppressWarnings("unchecked")
123 	protected final C self() {
124 		return (C) this;
125 	}
126 
127 	/**
128 	 * Configure transport with credentials provider, timeout, and config
129 	 * callback
130 	 *
131 	 * @param transport
132 	 * @return {@code this}
133 	 */
134 	protected C configure(final Transport transport) {
135 		if (credentialsProvider != null)
136 			transport.setCredentialsProvider(credentialsProvider);
137 		transport.setTimeout(timeout);
138 		if (transportConfigCallback != null)
139 			transportConfigCallback.configure(transport);
140 		return self();
141 	}
142 
143 	/**
144 	 * Configure a child command with the current configuration set in
145 	 * {@code this} command
146 	 *
147 	 * @param childCommand
148 	 * @return {@code this}
149 	 */
150 	protected C configure(final TransportCommand childCommand) {
151 		childCommand.setCredentialsProvider(credentialsProvider);
152 		childCommand.setTimeout(timeout);
153 		childCommand.setTransportConfigCallback(transportConfigCallback);
154 		return self();
155 	}
156 }