You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestProtocol.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (C) 2015, Google 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.transport;
  44. import java.net.URISyntaxException;
  45. import java.text.MessageFormat;
  46. import java.util.Collections;
  47. import java.util.EnumSet;
  48. import java.util.HashMap;
  49. import java.util.Set;
  50. import org.eclipse.jgit.errors.NotSupportedException;
  51. import org.eclipse.jgit.errors.TransportException;
  52. import org.eclipse.jgit.internal.JGitText;
  53. import org.eclipse.jgit.lib.Repository;
  54. import org.eclipse.jgit.transport.BasePackFetchConnection.FetchConfig;
  55. import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
  56. import org.eclipse.jgit.transport.resolver.UploadPackFactory;
  57. /**
  58. * Protocol for transport between manually-specified repositories in tests.
  59. * <p>
  60. * Remote repositories are registered using
  61. * {@link #register(Object, Repository)}, after which they can be accessed using
  62. * the returned URI. As this class provides both the client side (the protocol)
  63. * and the server side, the caller is responsible for setting up and passing the
  64. * connection context, whatever form that may take.
  65. * <p>
  66. * Unlike the other built-in protocols, which are automatically-registered
  67. * singletons, callers are expected to register/unregister specific protocol
  68. * instances on demand with
  69. * {@link org.eclipse.jgit.transport.Transport#register(TransportProtocol)}.
  70. *
  71. * @param <C>
  72. * the connection type
  73. * @since 4.0
  74. */
  75. public class TestProtocol<C> extends TransportProtocol {
  76. private static final String SCHEME = "test"; //$NON-NLS-1$
  77. private static FetchConfig fetchConfig;
  78. private class Handle {
  79. final C req;
  80. final Repository remote;
  81. Handle(C req, Repository remote) {
  82. this.req = req;
  83. this.remote = remote;
  84. }
  85. }
  86. final UploadPackFactory<C> uploadPackFactory;
  87. final ReceivePackFactory<C> receivePackFactory;
  88. private final HashMap<URIish, Handle> handles;
  89. /**
  90. * Constructor for TestProtocol.
  91. *
  92. * @param uploadPackFactory
  93. * factory for creating
  94. * {@link org.eclipse.jgit.transport.UploadPack} used by all
  95. * connections from this protocol instance.
  96. * @param receivePackFactory
  97. * factory for creating
  98. * {@link org.eclipse.jgit.transport.ReceivePack} used by all
  99. * connections from this protocol instance.
  100. */
  101. public TestProtocol(UploadPackFactory<C> uploadPackFactory,
  102. ReceivePackFactory<C> receivePackFactory) {
  103. this.uploadPackFactory = uploadPackFactory;
  104. this.receivePackFactory = receivePackFactory;
  105. this.handles = new HashMap<>();
  106. }
  107. /** {@inheritDoc} */
  108. @Override
  109. public String getName() {
  110. return JGitText.get().transportProtoTest;
  111. }
  112. /** {@inheritDoc} */
  113. @Override
  114. public Set<String> getSchemes() {
  115. return Collections.singleton(SCHEME);
  116. }
  117. /** {@inheritDoc} */
  118. @Override
  119. public Transport open(URIish uri, Repository local, String remoteName)
  120. throws NotSupportedException, TransportException {
  121. Handle h = handles.get(uri);
  122. if (h == null) {
  123. throw new NotSupportedException(MessageFormat.format(
  124. JGitText.get().URINotSupported, uri));
  125. }
  126. return new TransportInternal(local, uri, h);
  127. }
  128. /** {@inheritDoc} */
  129. @Override
  130. public Set<URIishField> getRequiredFields() {
  131. return EnumSet.of(URIishField.HOST, URIishField.PATH);
  132. }
  133. /** {@inheritDoc} */
  134. @Override
  135. public Set<URIishField> getOptionalFields() {
  136. return Collections.emptySet();
  137. }
  138. static void setFetchConfig(FetchConfig c) {
  139. fetchConfig = c;
  140. }
  141. /**
  142. * Register a repository connection over the internal test protocol.
  143. *
  144. * @param req
  145. * connection context. This instance is reused for all connections
  146. * made using this protocol; if it is stateful and usable only for
  147. * one connection, the same repository should be registered
  148. * multiple times.
  149. * @param remote
  150. * remote repository to connect to.
  151. * @return a URI that can be used to connect to this repository for both fetch
  152. * and push.
  153. */
  154. public synchronized URIish register(C req, Repository remote) {
  155. URIish uri;
  156. try {
  157. int n = handles.size();
  158. uri = new URIish(SCHEME + "://test/conn" + n); //$NON-NLS-1$
  159. } catch (URISyntaxException e) {
  160. throw new IllegalStateException();
  161. }
  162. handles.put(uri, new Handle(req, remote));
  163. return uri;
  164. }
  165. private class TransportInternal extends Transport implements PackTransport {
  166. private final Handle handle;
  167. TransportInternal(Repository local, URIish uri, Handle handle) {
  168. super(local, uri);
  169. this.handle = handle;
  170. }
  171. @Override
  172. public FetchConnection openFetch() throws NotSupportedException,
  173. TransportException {
  174. handle.remote.incrementOpen();
  175. return new InternalFetchConnection<C>(this, uploadPackFactory,
  176. handle.req, handle.remote) {
  177. @Override
  178. FetchConfig getFetchConfig() {
  179. return fetchConfig != null ? fetchConfig
  180. : super.getFetchConfig();
  181. }
  182. };
  183. }
  184. @Override
  185. public PushConnection openPush() throws NotSupportedException,
  186. TransportException {
  187. handle.remote.incrementOpen();
  188. return new InternalPushConnection<>(
  189. this, receivePackFactory, handle.req, handle.remote);
  190. }
  191. @Override
  192. public void close() {
  193. // Resources must be established per-connection.
  194. }
  195. }
  196. }