1 /*
2 * Copyright (C) 2011, GitHub 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.api;
11
12 import org.eclipse.jgit.lib.Repository;
13 import org.eclipse.jgit.transport.CredentialsProvider;
14 import org.eclipse.jgit.transport.Transport;
15
16 /**
17 * Base class for commands that use a
18 * {@link org.eclipse.jgit.transport.Transport} during execution.
19 * <p>
20 * This class provides standard configuration of a transport for options such as
21 * a {@link org.eclipse.jgit.transport.CredentialsProvider}, a timeout, and a
22 * {@link org.eclipse.jgit.api.TransportConfigCallback}.
23 *
24 * @param <C>
25 * @param <T>
26 */
27 public abstract class TransportCommand<C extends GitCommand, T> extends
28 GitCommand<T> {
29
30 /**
31 * Configured credentials provider
32 */
33 protected CredentialsProvider credentialsProvider;
34
35 /**
36 * Configured transport timeout
37 */
38 protected int timeout;
39
40 /**
41 * Configured callback for transport configuration
42 */
43 protected TransportConfigCallback transportConfigCallback;
44
45 /**
46 * <p>Constructor for TransportCommand.</p>
47 *
48 * @param repo a {@link org.eclipse.jgit.lib.Repository} object.
49 */
50 protected TransportCommand(Repository repo) {
51 super(repo);
52 setCredentialsProvider(CredentialsProvider.getDefault());
53 }
54
55 /**
56 * Set the <code>credentialsProvider</code>.
57 *
58 * @param credentialsProvider
59 * the {@link org.eclipse.jgit.transport.CredentialsProvider} to
60 * use
61 * @return {@code this}
62 */
63 public C setCredentialsProvider(
64 final CredentialsProvider credentialsProvider) {
65 this.credentialsProvider = credentialsProvider;
66 return self();
67 }
68
69 /**
70 * Set <code>timeout</code>.
71 *
72 * @param timeout
73 * the timeout (in seconds) used for the transport step
74 * @return {@code this}
75 */
76 public C setTimeout(int timeout) {
77 this.timeout = timeout;
78 return self();
79 }
80
81 /**
82 * Set the <code>TransportConfigCallback</code>.
83 *
84 * @param transportConfigCallback
85 * if set, the callback will be invoked after the
86 * {@link org.eclipse.jgit.transport.Transport} has created, but
87 * before the {@link org.eclipse.jgit.transport.Transport} is
88 * used. The callback can use this opportunity to set additional
89 * type-specific configuration on the
90 * {@link org.eclipse.jgit.transport.Transport} instance.
91 * @return {@code this}
92 */
93 public C setTransportConfigCallback(
94 final TransportConfigCallback transportConfigCallback) {
95 this.transportConfigCallback = transportConfigCallback;
96 return self();
97 }
98
99 /**
100 * Return this command cast to {@code C}
101 *
102 * @return {@code this} cast to {@code C}
103 */
104 @SuppressWarnings("unchecked")
105 protected final C self() {
106 return (C) this;
107 }
108
109 /**
110 * Configure transport with credentials provider, timeout, and config
111 * callback
112 *
113 * @param transport
114 * a {@link org.eclipse.jgit.transport.Transport} object.
115 * @return {@code this}
116 */
117 protected C configure(Transport transport) {
118 if (credentialsProvider != null)
119 transport.setCredentialsProvider(credentialsProvider);
120 transport.setTimeout(timeout);
121 if (transportConfigCallback != null)
122 transportConfigCallback.configure(transport);
123 return self();
124 }
125
126 /**
127 * Configure a child command with the current configuration set in
128 * {@code this} command
129 *
130 * @param childCommand
131 * a {@link org.eclipse.jgit.api.TransportCommand} object.
132 * @return {@code this}
133 */
134 protected C configure(TransportCommand childCommand) {
135 childCommand.setCredentialsProvider(credentialsProvider);
136 childCommand.setTimeout(timeout);
137 childCommand.setTransportConfigCallback(transportConfigCallback);
138 return self();
139 }
140 }