1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
/*
* Copyright (C) 2015, Google Inc. and others
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0 which is available at
* https://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.eclipse.jgit.transport;
import java.net.URISyntaxException;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Set;
import org.eclipse.jgit.errors.NotSupportedException;
import org.eclipse.jgit.errors.TransportException;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.BasePackFetchConnection.FetchConfig;
import org.eclipse.jgit.transport.resolver.ReceivePackFactory;
import org.eclipse.jgit.transport.resolver.UploadPackFactory;
/**
* Protocol for transport between manually-specified repositories in tests.
* <p>
* Remote repositories are registered using
* {@link #register(Object, Repository)}, after which they can be accessed using
* the returned URI. As this class provides both the client side (the protocol)
* and the server side, the caller is responsible for setting up and passing the
* connection context, whatever form that may take.
* <p>
* Unlike the other built-in protocols, which are automatically-registered
* singletons, callers are expected to register/unregister specific protocol
* instances on demand with
* {@link org.eclipse.jgit.transport.Transport#register(TransportProtocol)}.
*
* @param <C>
* the connection type
* @since 4.0
*/
public class TestProtocol<C> extends TransportProtocol {
private static final String SCHEME = "test"; //$NON-NLS-1$
private static FetchConfig fetchConfig;
private class Handle {
final C req;
final Repository remote;
Handle(C req, Repository remote) {
this.req = req;
this.remote = remote;
}
}
final UploadPackFactory<C> uploadPackFactory;
final ReceivePackFactory<C> receivePackFactory;
private final HashMap<URIish, Handle> handles;
/**
* Constructor for TestProtocol.
*
* @param uploadPackFactory
* factory for creating
* {@link org.eclipse.jgit.transport.UploadPack} used by all
* connections from this protocol instance.
* @param receivePackFactory
* factory for creating
* {@link org.eclipse.jgit.transport.ReceivePack} used by all
* connections from this protocol instance.
*/
public TestProtocol(UploadPackFactory<C> uploadPackFactory,
ReceivePackFactory<C> receivePackFactory) {
this.uploadPackFactory = uploadPackFactory;
this.receivePackFactory = receivePackFactory;
this.handles = new HashMap<>();
}
@Override
public String getName() {
return JGitText.get().transportProtoTest;
}
@Override
public Set<String> getSchemes() {
return Collections.singleton(SCHEME);
}
@Override
public Transport open(URIish uri, Repository local, String remoteName)
throws NotSupportedException, TransportException {
Handle h = handles.get(uri);
if (h == null) {
throw new NotSupportedException(MessageFormat.format(
JGitText.get().URINotSupported, uri));
}
return new TransportInternal(local, uri, h);
}
@Override
public Set<URIishField> getRequiredFields() {
return EnumSet.of(URIishField.HOST, URIishField.PATH);
}
@Override
public Set<URIishField> getOptionalFields() {
return Collections.emptySet();
}
static void setFetchConfig(FetchConfig c) {
fetchConfig = c;
}
/**
* Register a repository connection over the internal test protocol.
*
* @param req
* connection context. This instance is reused for all connections
* made using this protocol; if it is stateful and usable only for
* one connection, the same repository should be registered
* multiple times.
* @param remote
* remote repository to connect to.
* @return a URI that can be used to connect to this repository for both fetch
* and push.
*/
public synchronized URIish register(C req, Repository remote) {
URIish uri;
try {
int n = handles.size();
uri = new URIish(SCHEME + "://test/conn" + n); //$NON-NLS-1$
} catch (URISyntaxException e) {
throw new IllegalStateException(e);
}
handles.put(uri, new Handle(req, remote));
return uri;
}
private class TransportInternal extends Transport implements PackTransport {
private final Handle handle;
TransportInternal(Repository local, URIish uri, Handle handle) {
super(local, uri);
this.handle = handle;
}
@Override
public FetchConnection openFetch() throws NotSupportedException,
TransportException {
handle.remote.incrementOpen();
return new InternalFetchConnection<>(this, uploadPackFactory,
handle.req, handle.remote) {
@Override
FetchConfig getFetchConfig() {
return fetchConfig != null ? fetchConfig
: super.getFetchConfig();
}
};
}
@Override
public PushConnection openPush() throws NotSupportedException,
TransportException {
handle.remote.incrementOpen();
return new InternalPushConnection<>(
this, receivePackFactory, handle.req, handle.remote);
}
@Override
public void close() {
// Resources must be established per-connection.
}
}
}
|