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.

TransportProtocol.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (C) 2011, 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.util.Collections;
  45. import java.util.EnumSet;
  46. import java.util.Set;
  47. import org.eclipse.jgit.errors.NotSupportedException;
  48. import org.eclipse.jgit.errors.TransportException;
  49. import org.eclipse.jgit.lib.Repository;
  50. /**
  51. * Describes a way to connect to another Git repository.
  52. * <p>
  53. * Implementations of this class are typically immutable singletons held by
  54. * static class members, for example:
  55. *
  56. * <pre>
  57. * package com.example.my_transport;
  58. *
  59. * class MyTransport extends Transport {
  60. * public static final TransportProtocol PROTO = new TransportProtocol() {
  61. * public String getName() {
  62. * return &quot;My Protocol&quot;;
  63. * }
  64. * };
  65. * }
  66. * </pre>
  67. *
  68. * <p>
  69. * Applications may register additional protocols for use by JGit by calling
  70. * {@link Transport#register(TransportProtocol)}. Because that API holds onto
  71. * the protocol object by a WeakReference, applications must ensure their own
  72. * ClassLoader retains the TransportProtocol for the life of the application.
  73. * Using a static singleton pattern as above will ensure the protocol is valid
  74. * so long as the ClassLoader that defines it remains valid.
  75. * <p>
  76. * Applications may automatically register additional protocols by filling in
  77. * the names of their TransportProtocol defining classes using the services file
  78. * {@code META-INF/services/org.eclipse.jgit.transport.Transport}. For each
  79. * class name listed in the services file, any static fields of type
  80. * {@code TransportProtocol} will be automatically registered. For the above
  81. * example the string {@code com.example.my_transport.MyTransport} should be
  82. * listed in the file, as that is the name of the class that defines the static
  83. * PROTO singleton.
  84. */
  85. public abstract class TransportProtocol {
  86. /** Fields within a {@link URIish} that a transport uses. */
  87. public static enum URIishField {
  88. /** the user field */
  89. USER,
  90. /** the pass (aka password) field */
  91. PASS,
  92. /** the host field */
  93. HOST,
  94. /** the port field */
  95. PORT,
  96. /** the path field */
  97. PATH,
  98. }
  99. /** @return text name of the protocol suitable for display to a user. */
  100. public abstract String getName();
  101. /** @return immutable set of schemes supported by this protocol. */
  102. public Set<String> getSchemes() {
  103. return Collections.emptySet();
  104. }
  105. /** @return immutable set of URIishFields that must be filled in. */
  106. public Set<URIishField> getRequiredFields() {
  107. return Collections.unmodifiableSet(EnumSet.of(URIishField.PATH));
  108. }
  109. /** @return immutable set of URIishFields that may be filled in. */
  110. public Set<URIishField> getOptionalFields() {
  111. return Collections.emptySet();
  112. }
  113. /** @return if a port is supported, the default port, else -1. */
  114. public int getDefaultPort() {
  115. return -1;
  116. }
  117. /**
  118. * Determine if this protocol can handle a particular URI.
  119. * <p>
  120. * Implementations should try to avoid looking at the local filesystem, but
  121. * may look at implementation specific configuration options in the remote
  122. * block of {@code local.getConfig()} using {@code remoteName} if the name
  123. * is non-null.
  124. * <p>
  125. * The default implementation of this method matches the scheme against
  126. * {@link #getSchemes()}, required fields against
  127. * {@link #getRequiredFields()}, and optional fields against
  128. * {@link #getOptionalFields()}, returning true only if all of the fields
  129. * match the specification.
  130. *
  131. * @param uri
  132. * address of the Git repository; never null.
  133. * @return true if this protocol can handle this URI; false otherwise.
  134. */
  135. public boolean canHandle(URIish uri) {
  136. return canHandle(uri, null, null);
  137. }
  138. /**
  139. * Determine if this protocol can handle a particular URI.
  140. * <p>
  141. * Implementations should try to avoid looking at the local filesystem, but
  142. * may look at implementation specific configuration options in the remote
  143. * block of {@code local.getConfig()} using {@code remoteName} if the name
  144. * is non-null.
  145. * <p>
  146. * The default implementation of this method matches the scheme against
  147. * {@link #getSchemes()}, required fields against
  148. * {@link #getRequiredFields()}, and optional fields against
  149. * {@link #getOptionalFields()}, returning true only if all of the fields
  150. * match the specification.
  151. *
  152. * @param uri
  153. * address of the Git repository; never null.
  154. * @param local
  155. * the local repository that will communicate with the other Git
  156. * repository. May be null if the caller is only asking about a
  157. * specific URI and does not have a local Repository.
  158. * @param remoteName
  159. * name of the remote, if the remote as configured in
  160. * {@code local}; otherwise null.
  161. * @return true if this protocol can handle this URI; false otherwise.
  162. */
  163. public boolean canHandle(URIish uri, Repository local, String remoteName) {
  164. if (!getSchemes().isEmpty() && !getSchemes().contains(uri.getScheme()))
  165. return false;
  166. for (URIishField field : getRequiredFields()) {
  167. switch (field) {
  168. case USER:
  169. if (uri.getUser() == null || uri.getUser().length() == 0)
  170. return false;
  171. break;
  172. case PASS:
  173. if (uri.getPass() == null || uri.getPass().length() == 0)
  174. return false;
  175. break;
  176. case HOST:
  177. if (uri.getHost() == null || uri.getHost().length() == 0)
  178. return false;
  179. break;
  180. case PORT:
  181. if (uri.getPort() <= 0)
  182. return false;
  183. break;
  184. case PATH:
  185. if (uri.getPath() == null || uri.getPath().length() == 0)
  186. return false;
  187. break;
  188. default:
  189. return false;
  190. }
  191. }
  192. Set<URIishField> canHave = EnumSet.copyOf(getRequiredFields());
  193. canHave.addAll(getOptionalFields());
  194. if (uri.getUser() != null && !canHave.contains(URIishField.USER))
  195. return false;
  196. if (uri.getPass() != null && !canHave.contains(URIishField.PASS))
  197. return false;
  198. if (uri.getHost() != null && !canHave.contains(URIishField.HOST))
  199. return false;
  200. if (uri.getPort() > 0 && !canHave.contains(URIishField.PORT))
  201. return false;
  202. if (uri.getPath() != null && !canHave.contains(URIishField.PATH))
  203. return false;
  204. return true;
  205. }
  206. /**
  207. * Open a Transport instance to the other repository.
  208. * <p>
  209. * Implementations should avoid making remote connections until an operation
  210. * on the returned Transport is invoked, however they may fail fast here if
  211. * they know a connection is impossible, such as when using the local
  212. * filesystem and the target path does not exist.
  213. * <p>
  214. * Implementations may access implementation-specific configuration options
  215. * within {@code local.getConfig()} using the remote block named by the
  216. * {@code remoteName}, if the name is non-null.
  217. *
  218. * @param uri
  219. * address of the Git repository.
  220. * @param local
  221. * the local repository that will communicate with the other Git
  222. * repository.
  223. * @param remoteName
  224. * name of the remote, if the remote as configured in
  225. * {@code local}; otherwise null.
  226. * @return the transport.
  227. * @throws NotSupportedException
  228. * this protocol does not support the URI.
  229. * @throws TransportException
  230. * the transport cannot open this URI.
  231. */
  232. public abstract Transport open(URIish uri, Repository local,
  233. String remoteName)
  234. throws NotSupportedException, TransportException;
  235. }