TestProtocol.java

  1. /*
  2.  * Copyright (C) 2015, 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. package org.eclipse.jgit.transport;

  11. import java.net.URISyntaxException;
  12. import java.text.MessageFormat;
  13. import java.util.Collections;
  14. import java.util.EnumSet;
  15. import java.util.HashMap;
  16. import java.util.Set;

  17. import org.eclipse.jgit.errors.NotSupportedException;
  18. import org.eclipse.jgit.errors.TransportException;
  19. import org.eclipse.jgit.internal.JGitText;
  20. import org.eclipse.jgit.lib.Repository;
  21. import org.eclipse.jgit.transport.BasePackFetchConnection.FetchConfig;
  22. import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
  23. import org.eclipse.jgit.transport.resolver.UploadPackFactory;

  24. /**
  25.  * Protocol for transport between manually-specified repositories in tests.
  26.  * <p>
  27.  * Remote repositories are registered using
  28.  * {@link #register(Object, Repository)}, after which they can be accessed using
  29.  * the returned URI. As this class provides both the client side (the protocol)
  30.  * and the server side, the caller is responsible for setting up and passing the
  31.  * connection context, whatever form that may take.
  32.  * <p>
  33.  * Unlike the other built-in protocols, which are automatically-registered
  34.  * singletons, callers are expected to register/unregister specific protocol
  35.  * instances on demand with
  36.  * {@link org.eclipse.jgit.transport.Transport#register(TransportProtocol)}.
  37.  *
  38.  * @param <C>
  39.  *            the connection type
  40.  * @since 4.0
  41.  */
  42. public class TestProtocol<C> extends TransportProtocol {
  43.     private static final String SCHEME = "test"; //$NON-NLS-1$

  44.     private static FetchConfig fetchConfig;

  45.     private class Handle {
  46.         final C req;
  47.         final Repository remote;

  48.         Handle(C req, Repository remote) {
  49.             this.req = req;
  50.             this.remote = remote;
  51.         }
  52.     }

  53.     final UploadPackFactory<C> uploadPackFactory;
  54.     final ReceivePackFactory<C> receivePackFactory;
  55.     private final HashMap<URIish, Handle> handles;

  56.     /**
  57.      * Constructor for TestProtocol.
  58.      *
  59.      * @param uploadPackFactory
  60.      *            factory for creating
  61.      *            {@link org.eclipse.jgit.transport.UploadPack} used by all
  62.      *            connections from this protocol instance.
  63.      * @param receivePackFactory
  64.      *            factory for creating
  65.      *            {@link org.eclipse.jgit.transport.ReceivePack} used by all
  66.      *            connections from this protocol instance.
  67.      */
  68.     public TestProtocol(UploadPackFactory<C> uploadPackFactory,
  69.             ReceivePackFactory<C> receivePackFactory) {
  70.         this.uploadPackFactory = uploadPackFactory;
  71.         this.receivePackFactory = receivePackFactory;
  72.         this.handles = new HashMap<>();
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public String getName() {
  77.         return JGitText.get().transportProtoTest;
  78.     }

  79.     /** {@inheritDoc} */
  80.     @Override
  81.     public Set<String> getSchemes() {
  82.         return Collections.singleton(SCHEME);
  83.     }

  84.     /** {@inheritDoc} */
  85.     @Override
  86.     public Transport open(URIish uri, Repository local, String remoteName)
  87.             throws NotSupportedException, TransportException {
  88.         Handle h = handles.get(uri);
  89.         if (h == null) {
  90.             throw new NotSupportedException(MessageFormat.format(
  91.                     JGitText.get().URINotSupported, uri));
  92.         }
  93.         return new TransportInternal(local, uri, h);
  94.     }

  95.     /** {@inheritDoc} */
  96.     @Override
  97.     public Set<URIishField> getRequiredFields() {
  98.         return EnumSet.of(URIishField.HOST, URIishField.PATH);
  99.     }

  100.     /** {@inheritDoc} */
  101.     @Override
  102.     public Set<URIishField> getOptionalFields() {
  103.         return Collections.emptySet();
  104.     }

  105.     static void setFetchConfig(FetchConfig c) {
  106.         fetchConfig = c;
  107.     }

  108.     /**
  109.      * Register a repository connection over the internal test protocol.
  110.      *
  111.      * @param req
  112.      *            connection context. This instance is reused for all connections
  113.      *            made using this protocol; if it is stateful and usable only for
  114.      *            one connection, the same repository should be registered
  115.      *            multiple times.
  116.      * @param remote
  117.      *            remote repository to connect to.
  118.      * @return a URI that can be used to connect to this repository for both fetch
  119.      *         and push.
  120.      */
  121.     public synchronized URIish register(C req, Repository remote) {
  122.         URIish uri;
  123.         try {
  124.             int n = handles.size();
  125.             uri = new URIish(SCHEME + "://test/conn" + n); //$NON-NLS-1$
  126.         } catch (URISyntaxException e) {
  127.             throw new IllegalStateException(e);
  128.         }
  129.         handles.put(uri, new Handle(req, remote));
  130.         return uri;
  131.     }

  132.     private class TransportInternal extends Transport implements PackTransport {
  133.         private final Handle handle;

  134.         TransportInternal(Repository local, URIish uri, Handle handle) {
  135.             super(local, uri);
  136.             this.handle = handle;
  137.         }

  138.         @Override
  139.         public FetchConnection openFetch() throws NotSupportedException,
  140.                 TransportException {
  141.             handle.remote.incrementOpen();
  142.             return new InternalFetchConnection<C>(this, uploadPackFactory,
  143.                     handle.req, handle.remote) {
  144.                 @Override
  145.                 FetchConfig getFetchConfig() {
  146.                     return fetchConfig != null ? fetchConfig
  147.                             : super.getFetchConfig();
  148.                 }
  149.             };
  150.         }

  151.         @Override
  152.         public PushConnection openPush() throws NotSupportedException,
  153.                 TransportException {
  154.             handle.remote.incrementOpen();
  155.             return new InternalPushConnection<>(
  156.                     this, receivePackFactory, handle.req, handle.remote);
  157.         }

  158.         @Override
  159.         public void close() {
  160.             // Resources must be established per-connection.
  161.         }
  162.     }
  163. }