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