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.5KB

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