-
Notifications
You must be signed in to change notification settings - Fork 215
/
MqttSpecificConfig.java
178 lines (154 loc) · 6.06 KB
/
MqttSpecificConfig.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
/*
* Copyright (c) 2019 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.services.connectivity.messaging.mqtt;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.model.connectivity.Connection;
import org.eclipse.ditto.services.connectivity.config.MqttConfig;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigException;
import com.typesafe.config.ConfigFactory;
/**
* Class providing access to MQTT specific configuration.
*/
@Immutable
public final class MqttSpecificConfig {
private static final String CLEAN_SESSION = "cleanSession";
private static final String RECONNECT_FOR_REDELIVERY = "reconnectForRedelivery";
private static final String SEPARATE_PUBLISHER_CLIENT = "separatePublisherClient";
private static final String CLIENT_ID = "clientId";
private static final String PUBLISHER_ID = "publisherId";
private static final String KEEP_ALIVE_INTERVAL = "keepAlive";
private static final String RECONNECT_FOR_REDELIVERY_DELAY = "reconnectForRedeliveryDelay";
private final Config specificConfig;
private MqttSpecificConfig(final Config specificConfig) {
this.specificConfig = specificConfig;
}
/**
* Creates a new instance of MqttSpecificConfig based on the {@code specificConfig} of the passed
* {@code connection}.
*
* @param connection the Connection to extract the {@code specificConfig} map from.
* @param mqttConfig the mqtt config to create the default config from.
* @return the new MqttSpecificConfig instance
*/
public static MqttSpecificConfig fromConnection(final Connection connection, final MqttConfig mqttConfig) {
final Map<String, Object> defaultConfig = toDefaultConfig(mqttConfig);
final Config config = ConfigFactory.parseMap(connection.getSpecificConfig())
.withFallback(ConfigFactory.parseMap(defaultConfig));
return new MqttSpecificConfig(config);
}
private static Map<String, Object> toDefaultConfig(final MqttConfig mqttConfig) {
final Map<String, Object> defaultMap = new HashMap<>();
defaultMap.put(RECONNECT_FOR_REDELIVERY, mqttConfig.shouldReconnectForRedelivery());
defaultMap.put(SEPARATE_PUBLISHER_CLIENT, mqttConfig.shouldUseSeparatePublisherClient());
defaultMap.put(RECONNECT_FOR_REDELIVERY_DELAY, mqttConfig.getReconnectForRedeliveryDelay());
return defaultMap;
}
/**
* @return whether subscriber CONN messages should set clean-session or clean-start flag to true.
* Default to the negation of "reconnectForRedelivery" (if reconnect for redelivery then persistent session,
* otherwise clean-session or clean-start.)
*/
public boolean cleanSession() {
if (specificConfig.hasPath(CLEAN_SESSION)) {
return getSafely(() -> specificConfig.getBoolean(CLEAN_SESSION), false);
} else {
return !reconnectForRedelivery();
}
}
/**
* @return whether reconnect-for-redelivery behavior is activated.
*/
public boolean reconnectForRedelivery() {
return specificConfig.getBoolean(RECONNECT_FOR_REDELIVERY);
}
/**
* @return whether to use a separate client for publisher actors so that reconnect-for-redelivery
* does not disrupt the publisher.
*/
public boolean separatePublisherClient() {
return specificConfig.getBoolean(SEPARATE_PUBLISHER_CLIENT);
}
/**
* @return how long to wait before reconnect a consumer client for redelivery.
*/
public Duration getReconnectForDeliveryDelay() {
return specificConfig.getDuration(RECONNECT_FOR_REDELIVERY_DELAY);
}
/**
* @return the optional clientId which should be used by the MQTT client when connecting to the MQTT broker.
*/
public Optional<String> getMqttClientId() {
return getStringOptional(CLIENT_ID);
}
/**
* @return the optional publisherId which should be used as the client ID of the publisher actor.
*/
public Optional<String> getMqttPublisherId() {
return getStringOptional(PUBLISHER_ID);
}
/**
* @return the interval between keep alive pings.
*/
public Optional<Duration> getKeepAliveInterval() {
return getDurationOptional(KEEP_ALIVE_INTERVAL);
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final MqttSpecificConfig that = (MqttSpecificConfig) o;
return Objects.equals(specificConfig, that.specificConfig);
}
@Override
public int hashCode() {
return Objects.hash(specificConfig);
}
@Override
public String toString() {
return getClass().getSimpleName() + " [" +
"specificConfig=" + specificConfig +
"]";
}
private Optional<String> getStringOptional(final String key) {
if (specificConfig.hasPath(key)) {
return Optional.of(specificConfig.getString(key));
} else {
return Optional.empty();
}
}
private Optional<Duration> getDurationOptional(final String key) {
if (specificConfig.hasPath(key)) {
return Optional.of(specificConfig.getDuration(key));
} else {
return Optional.empty();
}
}
private static <T> T getSafely(Supplier<T> supplier, final T defaultValue) {
try {
return supplier.get();
} catch (final ConfigException e) {
return defaultValue;
}
}
}