/
DefaultHonoConfig.java
180 lines (155 loc) · 6.4 KB
/
DefaultHonoConfig.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
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.connectivity.service.config;
import static org.eclipse.ditto.base.model.common.ConditionChecker.checkNotNull;
import java.net.URI;
import java.net.URISyntaxException;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiFunction;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.connectivity.model.UserPasswordCredentials;
import org.eclipse.ditto.internal.utils.config.ConfigWithFallback;
import org.eclipse.ditto.internal.utils.config.DittoConfigError;
import org.eclipse.ditto.internal.utils.config.ScopedConfig;
import com.typesafe.config.Config;
import org.apache.pekko.actor.ActorSystem;
/**
* Default implementation for {@link HonoConfig}.
*/
@Immutable
public final class DefaultHonoConfig implements HonoConfig {
private final URI baseUri;
private final boolean validateCertificates;
private final String trustedCertificates;
private final SaslMechanism saslMechanism;
private final Set<URI> bootstrapServerUris;
private final UserPasswordCredentials credentials;
/**
* Constructs a {@code DefaultHonoConfig} for the specified ActorSystem.
*
* @param actorSystem the actor system that provides the overall core config.
* @throws NullPointerException if {@code actorSystem} is {@code null}.
*/
public DefaultHonoConfig(final ActorSystem actorSystem) {
this(ConfigWithFallback.newInstance(checkNotNull(actorSystem, "actorSystem").settings().config(),
PREFIX,
HonoConfigValue.values()));
}
private DefaultHonoConfig(final ScopedConfig scopedConfig) {
baseUri = getBaseUriOrThrow(scopedConfig);
validateCertificates = scopedConfig.getBoolean(HonoConfigValue.VALIDATE_CERTIFICATES.getConfigPath());
trustedCertificates = scopedConfig.getString(HonoConfigValue.TRUSTED_CERTIFICATES.getConfigPath());
saslMechanism = scopedConfig.getEnum(SaslMechanism.class, HonoConfigValue.SASL_MECHANISM.getConfigPath());
bootstrapServerUris = Collections.unmodifiableSet(getBootstrapServerUrisOrThrow(scopedConfig));
credentials = UserPasswordCredentials.newInstance(
scopedConfig.getString(HonoConfigValue.USERNAME.getConfigPath()),
scopedConfig.getString(HonoConfigValue.PASSWORD.getConfigPath())
);
}
private static URI getBaseUriOrThrow(final Config scopedConfig) {
final var configPath = HonoConfigValue.BASE_URI.getConfigPath();
try {
return new URI(scopedConfig.getString(configPath));
} catch (final URISyntaxException e) {
throw new DittoConfigError(
MessageFormat.format("The string value at <{0}> is not a {1}: {2}",
configPath,
URI.class.getSimpleName(),
e.getMessage()),
e
);
}
}
private static Set<URI> getBootstrapServerUrisOrThrow(final Config scopedConfig) {
final var configPath = HonoConfigValue.BOOTSTRAP_SERVERS.getConfigPath();
final BiFunction<Integer, String, URI> getUriOrThrow = (index, uriString) -> {
try {
return new URI(uriString.trim());
} catch (final URISyntaxException e) {
throw new DittoConfigError(
MessageFormat.format("The string at index <{0}> for key <{1}> is not a valid URI: {2}",
index,
configPath,
e.getMessage()),
e
);
}
};
final var bootstrapServersString = scopedConfig.getString(configPath);
final var bootstrapServerUriStrings = bootstrapServersString.split(",");
final Set<URI> result = new LinkedHashSet<>(bootstrapServerUriStrings.length);
for (var i = 0; i < bootstrapServerUriStrings.length; i++) {
result.add(getUriOrThrow.apply(i, bootstrapServerUriStrings[i]));
}
return result;
}
@Override
public URI getBaseUri() {
return baseUri;
}
@Override
public boolean isValidateCertificates() {
return validateCertificates;
}
@Override
public String getTrustedCertificates() {
return trustedCertificates;
}
@Override
public SaslMechanism getSaslMechanism() {
return saslMechanism;
}
@Override
public Set<URI> getBootstrapServerUris() {
return bootstrapServerUris;
}
@Override
public UserPasswordCredentials getUserPasswordCredentials() {
return credentials;
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final var that = (DefaultHonoConfig) o;
return Objects.equals(baseUri, that.baseUri)
&& Objects.equals(validateCertificates, that.validateCertificates)
&& Objects.equals(trustedCertificates, that.trustedCertificates)
&& Objects.equals(saslMechanism, that.saslMechanism)
&& Objects.equals(bootstrapServerUris, that.bootstrapServerUris)
&& Objects.equals(credentials, that.credentials);
}
@Override
public int hashCode() {
return Objects.hash(baseUri, validateCertificates, trustedCertificates, saslMechanism, bootstrapServerUris,
credentials);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"baseUri=" + baseUri +
", validateCertificates=" + validateCertificates +
", trustedCertificates=hash:" + Objects.hash(trustedCertificates) +
", saslMechanism=" + saslMechanism +
", bootstrapServers=" + bootstrapServerUris +
"]";
}
}