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