TransportCommand.java

  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. import org.eclipse.jgit.lib.Repository;
  12. import org.eclipse.jgit.transport.CredentialsProvider;
  13. import org.eclipse.jgit.transport.Transport;

  14. /**
  15.  * Base class for commands that use a
  16.  * {@link org.eclipse.jgit.transport.Transport} during execution.
  17.  * <p>
  18.  * This class provides standard configuration of a transport for options such as
  19.  * a {@link org.eclipse.jgit.transport.CredentialsProvider}, a timeout, and a
  20.  * {@link org.eclipse.jgit.api.TransportConfigCallback}.
  21.  *
  22.  * @param <C>
  23.  * @param <T>
  24.  */
  25. public abstract class TransportCommand<C extends GitCommand, T> extends
  26.         GitCommand<T> {

  27.     /**
  28.      * Configured credentials provider
  29.      */
  30.     protected CredentialsProvider credentialsProvider;

  31.     /**
  32.      * Configured transport timeout
  33.      */
  34.     protected int timeout;

  35.     /**
  36.      * Configured callback for transport configuration
  37.      */
  38.     protected TransportConfigCallback transportConfigCallback;

  39.     /**
  40.      * <p>Constructor for TransportCommand.</p>
  41.      *
  42.      * @param repo a {@link org.eclipse.jgit.lib.Repository} object.
  43.      */
  44.     protected TransportCommand(Repository repo) {
  45.         super(repo);
  46.         setCredentialsProvider(CredentialsProvider.getDefault());
  47.     }

  48.     /**
  49.      * Set the <code>credentialsProvider</code>.
  50.      *
  51.      * @param credentialsProvider
  52.      *            the {@link org.eclipse.jgit.transport.CredentialsProvider} to
  53.      *            use
  54.      * @return {@code this}
  55.      */
  56.     public C setCredentialsProvider(
  57.             final CredentialsProvider credentialsProvider) {
  58.         this.credentialsProvider = credentialsProvider;
  59.         return self();
  60.     }

  61.     /**
  62.      * Set <code>timeout</code>.
  63.      *
  64.      * @param timeout
  65.      *            the timeout (in seconds) used for the transport step
  66.      * @return {@code this}
  67.      */
  68.     public C setTimeout(int timeout) {
  69.         this.timeout = timeout;
  70.         return self();
  71.     }

  72.     /**
  73.      * Set the <code>TransportConfigCallback</code>.
  74.      *
  75.      * @param transportConfigCallback
  76.      *            if set, the callback will be invoked after the
  77.      *            {@link org.eclipse.jgit.transport.Transport} has created, but
  78.      *            before the {@link org.eclipse.jgit.transport.Transport} is
  79.      *            used. The callback can use this opportunity to set additional
  80.      *            type-specific configuration on the
  81.      *            {@link org.eclipse.jgit.transport.Transport} instance.
  82.      * @return {@code this}
  83.      */
  84.     public C setTransportConfigCallback(
  85.             final TransportConfigCallback transportConfigCallback) {
  86.         this.transportConfigCallback = transportConfigCallback;
  87.         return self();
  88.     }

  89.     /**
  90.      * Return this command cast to {@code C}
  91.      *
  92.      * @return {@code this} cast to {@code C}
  93.      */
  94.     @SuppressWarnings("unchecked")
  95.     protected final C self() {
  96.         return (C) this;
  97.     }

  98.     /**
  99.      * Configure transport with credentials provider, timeout, and config
  100.      * callback
  101.      *
  102.      * @param transport
  103.      *            a {@link org.eclipse.jgit.transport.Transport} object.
  104.      * @return {@code this}
  105.      */
  106.     protected C configure(Transport transport) {
  107.         if (credentialsProvider != null)
  108.             transport.setCredentialsProvider(credentialsProvider);
  109.         transport.setTimeout(timeout);
  110.         if (transportConfigCallback != null)
  111.             transportConfigCallback.configure(transport);
  112.         return self();
  113.     }

  114.     /**
  115.      * Configure a child command with the current configuration set in
  116.      * {@code this} command
  117.      *
  118.      * @param childCommand
  119.      *            a {@link org.eclipse.jgit.api.TransportCommand} object.
  120.      * @return {@code this}
  121.      */
  122.     protected C configure(TransportCommand childCommand) {
  123.         childCommand.setCredentialsProvider(credentialsProvider);
  124.         childCommand.setTimeout(timeout);
  125.         childCommand.setTransportConfigCallback(transportConfigCallback);
  126.         return self();
  127.     }
  128. }