-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEtcd3DiscoveryContainerConfig.java
242 lines (195 loc) · 8.69 KB
/
Etcd3DiscoveryContainerConfig.java
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*******************************************************************************
* Copyright (c) 2021 Composent, Inc. All rights reserved. This
* program and the accompanying materials are made available under the terms of
* the Eclipse Public License v1.0 which accompanies this distribution, and is
* available at http://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Scott Lewis - initial API and implementation
******************************************************************************/
package org.eclipse.ecf.provider.etcd3.container;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.UUID;
import org.eclipse.ecf.core.identity.IDCreateException;
import org.eclipse.ecf.core.identity.IDFactory;
import org.eclipse.ecf.discovery.DiscoveryContainerConfig;
import org.eclipse.ecf.discovery.identity.ServiceIDFactory;
import org.eclipse.ecf.provider.etcd3.Activator;
import org.eclipse.ecf.provider.etcd3.identity.Etcd3Namespace;
import org.eclipse.ecf.provider.etcd3.identity.Etcd3ServiceID;
import io.grpc.Channel;
import io.grpc.Grpc;
import io.grpc.ManagedChannelBuilder;
import io.grpc.ChannelCredentials;
import org.osgi.util.tracker.ServiceTracker;
public class Etcd3DiscoveryContainerConfig extends DiscoveryContainerConfig {
public static final String ETCD_DISABLED_PROP = Etcd3DiscoveryContainerInstantiator.NAME + ".disabled";
public static final String ETCD_TARGETID_PROTOCOL_PROP = Etcd3DiscoveryContainerInstantiator.NAME + ".protocol"; //$NON-NLS-1$
public static final String ETCD_TARGETID_PROTOCOL_DEFAULT = System.getProperty(ETCD_TARGETID_PROTOCOL_PROP, "http"); //$NON-NLS-1$
public static final String ETCD_TARGETID_HOSTNAME_PROP = Etcd3DiscoveryContainerInstantiator.NAME + ".hostname"; //$NON-NLS-1$
public static final String ETCD_TARGETID_HOSTNAME_DEFAULT = System.getProperty(ETCD_TARGETID_HOSTNAME_PROP,
"127.0.0.1"); //$NON-NLS-1$
public static final String ETCD_TARGETID_PORT_PROP = Etcd3DiscoveryContainerInstantiator.NAME + ".port"; //$NON-NLS-1$
public static final Integer ETCD_TARGETID_PORT_DEFAULT = Integer.getInteger(ETCD_TARGETID_PORT_PROP, 2379);
public static final String ETCD_TARGETID_PROP = Etcd3DiscoveryContainerInstantiator.NAME + ".targetid"; //$NON-NLS-1$
public static final String ETCD_TARGETID_DEFAULT = System.getProperty(ETCD_TARGETID_PROP);
public static final String ETCD_SESSIONID_PROP = Etcd3DiscoveryContainerInstantiator.NAME + ".sessionid"; //$NON-NLS-1$
public static final String ETCD_SESSIONID_DEFAULT = System.getProperty(ETCD_SESSIONID_PROP);
public static final String ETCD_TTL_PROP = Etcd3DiscoveryContainerInstantiator.NAME + ".ttl"; //$NON-NLS-1$
public static final Integer ETCD_TTL_DEFAULT = Integer.getInteger(ETCD_TTL_PROP, 30);
public static final String ETCD_CONTAINERID_PROP = Etcd3DiscoveryContainerInstantiator.NAME + ".containerId"; //$NON-NLS-1$
public static final String ETCD_CONTAINERID_DEFAULT = System.getProperty(ETCD_CONTAINERID_PROP,
Etcd3DiscoveryContainer.class.getName());
public static final String ETCD_KEYPREFIX_PROP = Etcd3DiscoveryContainerInstantiator.NAME + ".keyPrefix"; //$NON-NLS-1$
public static final String ETCD_KEYPREFIX_DEFAULT = System.getProperty(ETCD_KEYPREFIX_PROP,
Etcd3DiscoveryContainer.class.getName());
public static final String ETCD_KEEPALIVE_UPDATE_TIME_PROP = Etcd3DiscoveryContainerInstantiator.NAME
+ ".keepAliveUpdateTime"; //$NON-NLS-1$
public static final Integer ETCD_KEEPALIVE_UPDATE_TIME_DEFAULT = Integer.getInteger(ETCD_KEEPALIVE_UPDATE_TIME_PROP,
5);
public static final String ETCD_USEPLAINTEXT_PROP = Etcd3DiscoveryContainerInstantiator.NAME + ".usePlaintext";;
public static final Boolean ETCD_USEPLAINTEXT_DEFAULT = Boolean
.valueOf(System.getProperty(ETCD_USEPLAINTEXT_PROP, "false"));
public static final String ETCD_RETRY_PROP = Etcd3DiscoveryContainerInstantiator.NAME + ".retry";
public static final Boolean ETCD_RETRY_DEFAULT = Boolean.valueOf(System.getProperty(ETCD_RETRY_PROP, "false"));
private Etcd3ServiceID targetID;
private String sessionId = ETCD_SESSIONID_DEFAULT == null ? UUID.randomUUID().toString() : ETCD_SESSIONID_DEFAULT;
private long ttl = ETCD_TTL_DEFAULT.longValue();
private String keyPrefix = ETCD_KEYPREFIX_DEFAULT;
private int keepAliveUpdateTime = ETCD_KEEPALIVE_UPDATE_TIME_DEFAULT.intValue();
private boolean usePlaintext = ETCD_USEPLAINTEXT_DEFAULT;
private ChannelCredentials channelCredentials = null;
public static class Builder {
private String protocol = ETCD_TARGETID_PROTOCOL_DEFAULT;
private String hostname = ETCD_TARGETID_HOSTNAME_DEFAULT;
private int port = ETCD_TARGETID_PORT_DEFAULT;
public Builder setProtocol(String protocol) {
this.protocol = protocol;
return this;
}
public Builder setHostname(String hostname) {
this.hostname = hostname;
return this;
}
public Builder setPort(int port) {
this.port = port;
return this;
}
public Builder setTtl(long ttl) {
this.config.ttl = ttl;
return this;
}
public Builder setKeyPrefix(String keyPrefix) {
this.config.keyPrefix = keyPrefix;
return this;
}
public Builder setKeepAliveUpdateTime(int keepAliveUpdateTime) {
this.config.keepAliveUpdateTime = keepAliveUpdateTime;
return this;
}
public Builder setSessionId(String sessionId) {
this.config.sessionId = sessionId;
return this;
}
public Builder setUsePlaintext() {
this.config.usePlaintext = true;
return this;
}
public Builder setChannelCredentials(ChannelCredentials credentials) {
if (credentials != null) {
this.config.usePlaintext = false;
}
this.config.channelCredentials = credentials;
return this;
}
private Etcd3DiscoveryContainerConfig config;
private Builder(String containerId) {
config = new Etcd3DiscoveryContainerConfig(containerId);
}
public Etcd3DiscoveryContainerConfig build() throws IDCreateException {
URI uri = null;
try {
uri = new URI(this.protocol, null, this.hostname, this.port, null, null, null);
} catch (URISyntaxException e) {
throw new IDCreateException(
"Cannot create uri from protocol=" + protocol + ",hostname=" + hostname + ",port=" + port);
}
config.setTargetID((Etcd3ServiceID) IDFactory.getDefault().createID(Etcd3Namespace.NAME, new Object[] {
ServiceIDFactory.getDefault().createServiceTypeID(Etcd3Namespace.INSTANCE, Etcd3Namespace.SCHEME),
uri }));
return config;
}
}
public static Etcd3DiscoveryContainerConfig.Builder newBuilder() {
return new Builder(ETCD_CONTAINERID_DEFAULT);
}
public static Etcd3DiscoveryContainerConfig.Builder newBuilder(String containerId) {
return new Builder(containerId);
}
private Etcd3DiscoveryContainerConfig(String containerId) {
super(IDFactory.getDefault().createStringID(containerId));
}
private void setTargetID(Etcd3ServiceID aTargetId) {
this.targetID = aTargetId;
}
public Etcd3ServiceID getTargetID() {
return targetID;
}
public String getSessionId() {
return sessionId;
}
public long getTTL() {
return this.ttl;
}
public void setTTL(long ttl) {
this.ttl = ttl;
}
public String getKeyPrefix() {
return this.keyPrefix;
}
public void setKeyPrefix(String keyPrefix) {
this.keyPrefix = keyPrefix;
}
public ChannelCredentials getChannelCredentials() {
return this.channelCredentials;
}
public boolean usePlaintext() {
return this.usePlaintext;
}
public String getSessionKey() {
return String.join("/", getKeyPrefix(), getSessionId());
}
public int getKeepAliveUpdateTime() {
return this.keepAliveUpdateTime;
}
public URI getTargetLocation() {
return getTargetID().getLocation();
}
protected ManagedChannelBuilder<?> createAndConfigureManagedChannelBuilder() {
Activator.getDefault();
ServiceTracker<ManagedChannelBuilderConfigurer, ManagedChannelBuilderConfigurer> st = new ServiceTracker<ManagedChannelBuilderConfigurer, ManagedChannelBuilderConfigurer>(
Activator.getContext(), ManagedChannelBuilderConfigurer.class, null);
st.open();
ManagedChannelBuilderConfigurer configurer = st.getService();
st.close();
return (configurer != null) ? configurer.createAndConfigureManagedChannelBuilder(this) : null;
}
@SuppressWarnings("rawtypes")
protected ManagedChannelBuilder createManagedChannelBuilder() {
ManagedChannelBuilder mcb = createAndConfigureManagedChannelBuilder();
if (mcb == null) {
URI uri = getTargetLocation();
ChannelCredentials channelCreds = getChannelCredentials();
mcb = (channelCreds == null) ? ManagedChannelBuilder.forAddress(uri.getHost(), uri.getPort())
: Grpc.newChannelBuilderForAddress(uri.getHost(), uri.getPort(), channelCreds);
if (usePlaintext()) {
mcb.usePlaintext();
}
mcb.enableRetry();
}
return mcb;
}
public Channel createChannel() {
return createManagedChannelBuilder().build();
}
}