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

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