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 6.5KB

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