-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
AbstractDiscoveryStrategy.java
218 lines (194 loc) · 8.69 KB
/
AbstractDiscoveryStrategy.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
/*
* Copyright (c) 2008-2021, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.spi.discovery;
import com.hazelcast.cluster.Member;
import com.hazelcast.config.properties.PropertyDefinition;
import com.hazelcast.logging.ILogger;
import com.hazelcast.spi.partitiongroup.PartitionGroupStrategy;
import com.hazelcast.internal.util.StringUtil;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import static java.util.Collections.unmodifiableMap;
/**
* An common abstract superclass for {@link DiscoveryStrategy} implementations,
* offering convenient access to configuration properties (which may be overridden
* on the system's environment or JVM properties), as well as a {@link ILogger} instance.
*
* @since 3.6
*/
public abstract class AbstractDiscoveryStrategy implements DiscoveryStrategy {
private final ILogger logger;
private final Map<String, Comparable> properties;
public AbstractDiscoveryStrategy(ILogger logger, Map<String, Comparable> properties) {
this.logger = logger;
this.properties = unmodifiableMap(properties);
}
@Override
public void destroy() {
}
@Override
public void start() {
}
@Override
public PartitionGroupStrategy getPartitionGroupStrategy() {
return null;
}
@Override
public PartitionGroupStrategy getPartitionGroupStrategy(Collection<? extends Member> allMembers) {
return null;
}
@Override
public Map<String, String> discoverLocalMetadata() {
return Collections.emptyMap();
}
/**
* Returns an immutable copy of the configuration properties.
*
* @return the configuration properties
*/
protected Map<String, Comparable> getProperties() {
return properties;
}
/**
* Returns a {@link ILogger} instance bound to the current class.
*
* @return a ILogger instance
*/
protected ILogger getLogger() {
return logger;
}
/**
* Returns the value of the requested {@link PropertyDefinition} if available in the
* declarative or programmatic configuration (XML or Config API), otherwise it will
* return <code>null</code>.
* <p>
* <b>This method overload won't do environment or JVM property lookup.</b> A call to
* this overload is equivalent to {@link #getOrNull(String, PropertyDefinition)}
* with <code>null</code> passed as the first parameter.
*
* @param property the PropertyDefinition to lookup
* @param <T> the type of the property, must be compatible with the conversion
* result of {@link PropertyDefinition#typeConverter()}
* @return the value of the given property if available in the configuration, otherwise null
*/
protected <T extends Comparable> T getOrNull(PropertyDefinition property) {
return getOrDefault(property, null);
}
/**
* Returns the value of the requested {@link PropertyDefinition} if available in the
* declarative or programmatic configuration (XML or Config API), can be found in the
* system's environment, or passed as a JVM property. Otherwise it will return <code>null</code>.
* <p>
* This overload will resolve the requested property in the following order, whereas the
* higher priority is from top to bottom:
* <ul>
* <li>{@link System#getProperty(String)}: JVM properties</li>
* <li>{@link System#getenv(String)}: System environment</li>
* <li>Configuration properties of this {@link DiscoveryStrategy}</li>
* </ul>
* To resolve JVM properties or the system environment the property's key is prefixed with
* given <code>prefix</code>, therefore a prefix of <i>com.hazelcast.discovery</i> and a property
* key of <i>hostname</i> will result in a property lookup of <i>com.hazelcast.discovery.hostname</i>
* in the system environment and JVM properties.
*
* @param prefix the property key prefix for environment and JVM properties lookup
* @param property the PropertyDefinition to lookup
* @param <T> the type of the property, must be compatible with the conversion
* result of {@link PropertyDefinition#typeConverter()}
* @return the value of the given property if available in the configuration, system environment
* or JVM properties, otherwise null
*/
protected <T extends Comparable> T getOrNull(String prefix, PropertyDefinition property) {
return getOrDefault(prefix, property, null);
}
/**
* Returns the value of the requested {@link PropertyDefinition} if available in the
* declarative or programmatic configuration (XML or Config API), otherwise it will
* return the given <code>defaultValue</code>.
* <p>
* <b>This method overload won't do environment or JVM property lookup.</b> A call to
* this overload is equivalent to {@link #getOrDefault(String, PropertyDefinition, Comparable)}
* with <code>null</code> passed as the first parameter.
*
* @param property the PropertyDefinition to lookup
* @param <T> the type of the property, must be compatible with the conversion
* result of {@link PropertyDefinition#typeConverter()}
* @return the value of the given property if available in the configuration, otherwise the
* given default value
*/
protected <T extends Comparable> T getOrDefault(PropertyDefinition property, T defaultValue) {
return getOrDefault(null, property, defaultValue);
}
/**
* Returns the value of the requested {@link PropertyDefinition} if available in the
* declarative or programmatic configuration (XML or Config API), can be found in the
* system's environment, or passed as a JVM property. otherwise it will return the given
* <code>defaultValue</code>.
* <p>
* This overload will resolve the requested property in the following order, whereas the
* higher priority is from top to bottom:
* <ul>
* <li>{@link System#getProperty(String)}: JVM properties</li>
* <li>{@link System#getenv(String)}: System environment</li>
* <li>Configuration properties of this {@link DiscoveryStrategy}</li>
* </ul>
* To resolve JVM properties or the system environment the property's key is prefixed with
* given <code>prefix</code>, therefore a prefix of <i>com.hazelcast.discovery</i> and a property
* key of <i>hostname</i> will result in a property lookup of <i>com.hazelcast.discovery.hostname</i>
* in the system environment and JVM properties.
*
* @param prefix the property key prefix for environment and JVM properties lookup
* @param property the PropertyDefinition to lookup
* @param <T> the type of the property, must be compatible with the conversion
* result of {@link PropertyDefinition#typeConverter()}
* @return the value of the given property if available in the configuration, system environment
* or JVM properties, otherwise the given default value
*/
protected <T extends Comparable> T getOrDefault(String prefix, PropertyDefinition property, T defaultValue) {
if (property == null) {
return defaultValue;
}
Comparable value = readProperty(prefix, property);
if (value == null) {
value = properties.get(property.key());
}
if (value == null) {
return defaultValue;
}
return (T) value;
}
private Comparable readProperty(String prefix, PropertyDefinition property) {
if (prefix != null) {
String p = getProperty(prefix, property);
String v = System.getProperty(p);
if (StringUtil.isNullOrEmpty(v)) {
v = System.getenv(p);
}
if (!StringUtil.isNullOrEmpty(v)) {
return property.typeConverter().convert(v);
}
}
return null;
}
private String getProperty(String prefix, PropertyDefinition property) {
StringBuilder sb = new StringBuilder(prefix);
if (prefix.charAt(prefix.length() - 1) != '.') {
sb.append('.');
}
return sb.append(property.key()).toString();
}
}