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