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