-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
AbstractDomConfigProcessor.java
422 lines (383 loc) · 19.7 KB
/
AbstractDomConfigProcessor.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/*
* 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.internal.config;
import com.hazelcast.config.AbstractFactoryWithPropertiesConfig;
import com.hazelcast.config.ClassFilter;
import com.hazelcast.config.GlobalSerializerConfig;
import com.hazelcast.config.InstanceTrackingConfig;
import com.hazelcast.config.InvalidConfigurationException;
import com.hazelcast.config.JavaSerializationFilterConfig;
import com.hazelcast.config.LoginModuleConfig;
import com.hazelcast.config.NativeMemoryConfig;
import com.hazelcast.config.PersistentMemoryConfig;
import com.hazelcast.config.PersistentMemoryDirectoryConfig;
import com.hazelcast.config.PersistentMemoryMode;
import com.hazelcast.config.SSLConfig;
import com.hazelcast.config.SerializationConfig;
import com.hazelcast.config.SerializerConfig;
import com.hazelcast.config.SocketInterceptorConfig;
import com.hazelcast.config.security.JaasAuthenticationConfig;
import com.hazelcast.config.security.RealmConfig;
import com.hazelcast.internal.util.StringUtil;
import com.hazelcast.memory.MemorySize;
import com.hazelcast.memory.MemoryUnit;
import org.w3c.dom.Node;
import java.nio.ByteOrder;
import java.util.HashSet;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import static com.hazelcast.internal.config.DomConfigHelper.childElements;
import static com.hazelcast.internal.config.DomConfigHelper.cleanNodeName;
import static com.hazelcast.internal.config.DomConfigHelper.getBooleanValue;
import static com.hazelcast.internal.config.DomConfigHelper.getIntegerValue;
import static com.hazelcast.internal.util.StringUtil.isNullOrEmptyAfterTrim;
import static com.hazelcast.internal.util.StringUtil.upperCaseInternal;
/**
* Base class of the config processors working from W3C DOM objects
*/
public abstract class AbstractDomConfigProcessor implements DomConfigProcessor {
/**
* Set collecting already seen elements. Used to detect duplicates in
* the configurations.
*/
protected final Set<String> occurrenceSet = new HashSet<>();
/**
* Enables strict parsing mode in which config entries are parsed as-is
* When disabled, dashes are ignored
*/
protected final boolean strict;
/**
* Tells whether the traversed DOM is a level 3 one
*/
final boolean domLevel3;
protected AbstractDomConfigProcessor(boolean domLevel3) {
this.domLevel3 = domLevel3;
this.strict = true;
}
protected AbstractDomConfigProcessor(boolean domLevel3, boolean strict) {
this.domLevel3 = domLevel3;
this.strict = strict;
}
protected String getTextContent(Node node) {
return DomConfigHelper.getTextContent(node, domLevel3).trim();
}
protected String getAttribute(Node node, String attName) {
if (strict) {
return DomConfigHelper.getAttribute(node, attName, domLevel3);
} else {
String value = DomConfigHelper.getAttribute(node, attName, domLevel3);
return value != null
? value
: DomConfigHelper.getAttribute(node, attName.replace("-", ""), domLevel3);
}
}
protected Node getNamedItemNode(final Node node, String attrName) {
if (strict) {
return node.getAttributes().getNamedItem(attrName);
} else {
Node attrNode = node.getAttributes().getNamedItem(attrName);
return attrNode != null
? attrNode
: node.getAttributes().getNamedItem(attrName.replace("-", ""));
}
}
protected boolean matches(String config1, String config2) {
return strict
? config1 != null && config1.equals(config2)
: ConfigUtils.matches(config1, config2);
}
protected void fillProperties(Node node, Map<String, Comparable> properties) {
DomConfigHelper.fillProperties(node, properties, domLevel3);
}
protected void fillProperties(Node node, Properties properties) {
DomConfigHelper.fillProperties(node, properties, domLevel3);
}
protected SocketInterceptorConfig parseSocketInterceptorConfig(final Node node) {
SocketInterceptorConfig socketInterceptorConfig = new SocketInterceptorConfig();
final Node enabledNode = getNamedItemNode(node, "enabled");
final boolean enabled = enabledNode != null && getBooleanValue(getTextContent(enabledNode).trim());
socketInterceptorConfig.setEnabled(enabled);
for (Node n : childElements(node)) {
final String nodeName = cleanNodeName(n);
if (matches("class-name", nodeName)) {
socketInterceptorConfig.setClassName(getTextContent(n).trim());
} else if (matches("properties", nodeName)) {
fillProperties(n, socketInterceptorConfig.getProperties());
}
}
return socketInterceptorConfig;
}
@SuppressWarnings({"checkstyle:cyclomaticcomplexity"})
protected SerializationConfig parseSerialization(final Node node) {
SerializationConfig serializationConfig = new SerializationConfig();
for (Node child : childElements(node)) {
final String name = cleanNodeName(child);
if (matches("portable-version", name)) {
String value = getTextContent(child);
serializationConfig.setPortableVersion(getIntegerValue(name, value));
} else if (matches("check-class-def-errors", name)) {
String value = getTextContent(child);
serializationConfig.setCheckClassDefErrors(getBooleanValue(value));
} else if (matches("use-native-byte-order", name)) {
serializationConfig.setUseNativeByteOrder(getBooleanValue(getTextContent(child)));
} else if (matches("byte-order", name)) {
String value = getTextContent(child);
ByteOrder byteOrder = null;
if (ByteOrder.BIG_ENDIAN.toString().equals(value)) {
byteOrder = ByteOrder.BIG_ENDIAN;
} else if (ByteOrder.LITTLE_ENDIAN.toString().equals(value)) {
byteOrder = ByteOrder.LITTLE_ENDIAN;
}
serializationConfig.setByteOrder(byteOrder != null ? byteOrder : ByteOrder.BIG_ENDIAN);
} else if (matches("enable-compression", name)) {
serializationConfig.setEnableCompression(getBooleanValue(getTextContent(child)));
} else if (matches("enable-shared-object", name)) {
serializationConfig.setEnableSharedObject(getBooleanValue(getTextContent(child)));
} else if (matches("allow-unsafe", name)) {
serializationConfig.setAllowUnsafe(getBooleanValue(getTextContent(child)));
} else if (matches("allow-override-default-serializers", name)) {
serializationConfig.setAllowOverrideDefaultSerializers(getBooleanValue(getTextContent(child)));
} else if (matches("data-serializable-factories", name)) {
fillDataSerializableFactories(child, serializationConfig);
} else if (matches("portable-factories", name)) {
fillPortableFactories(child, serializationConfig);
} else if (matches("serializers", name)) {
fillSerializers(child, serializationConfig);
} else if (matches("java-serialization-filter", name)) {
fillJavaSerializationFilter(child, serializationConfig);
}
}
return serializationConfig;
}
protected void fillDataSerializableFactories(Node node, SerializationConfig serializationConfig) {
for (Node child : childElements(node)) {
final String name = cleanNodeName(child);
if (matches("data-serializable-factory", name)) {
final String value = getTextContent(child);
final Node factoryIdNode = getNamedItemNode(child, "factory-id");
if (factoryIdNode == null) {
throw new IllegalArgumentException(
"'factory-id' attribute of 'data-serializable-factory' is required!");
}
int factoryId = Integer.parseInt(getTextContent(factoryIdNode));
serializationConfig.addDataSerializableFactoryClass(factoryId, value);
}
}
}
protected void fillPortableFactories(Node node, SerializationConfig serializationConfig) {
for (Node child : childElements(node)) {
final String name = cleanNodeName(child);
if (matches("portable-factory", name)) {
final String value = getTextContent(child);
final Node factoryIdNode = getNamedItemNode(child, "factory-id");
if (factoryIdNode == null) {
throw new IllegalArgumentException("'factory-id' attribute of 'portable-factory' is required!");
}
int factoryId = Integer.parseInt(getTextContent(factoryIdNode));
serializationConfig.addPortableFactoryClass(factoryId, value);
}
}
}
protected void fillSerializers(final Node node, SerializationConfig serializationConfig) {
for (Node child : childElements(node)) {
final String name = cleanNodeName(child);
final String value = getTextContent(child);
if (matches("serializer", name)) {
SerializerConfig serializerConfig = new SerializerConfig();
final String typeClassName = getAttribute(child, "type-class");
final String className = getAttribute(child, "class-name");
serializerConfig.setTypeClassName(typeClassName);
serializerConfig.setClassName(className);
serializationConfig.addSerializerConfig(serializerConfig);
} else if (matches("global-serializer", name)) {
GlobalSerializerConfig globalSerializerConfig = new GlobalSerializerConfig();
globalSerializerConfig.setClassName(value);
String attrValue = getAttribute(child, "override-java-serialization");
boolean overrideJavaSerialization = attrValue != null && getBooleanValue(attrValue.trim());
globalSerializerConfig.setOverrideJavaSerialization(overrideJavaSerialization);
serializationConfig.setGlobalSerializerConfig(globalSerializerConfig);
}
}
}
protected void fillJavaSerializationFilter(final Node node, SerializationConfig serializationConfig) {
JavaSerializationFilterConfig filterConfig = new JavaSerializationFilterConfig();
serializationConfig.setJavaSerializationFilterConfig(filterConfig);
Node defaultsDisabledNode = getNamedItemNode(node, "defaults-disabled");
boolean defaultsDisabled = defaultsDisabledNode != null && getBooleanValue(getTextContent(defaultsDisabledNode));
filterConfig.setDefaultsDisabled(defaultsDisabled);
for (Node child : childElements(node)) {
final String name = cleanNodeName(child);
if (matches("blacklist", name)) {
ClassFilter list = parseClassFilterList(child);
filterConfig.setBlacklist(list);
} else if (matches("whitelist", name)) {
ClassFilter list = parseClassFilterList(child);
filterConfig.setWhitelist(list);
}
}
}
protected ClassFilter parseClassFilterList(Node node) {
ClassFilter list = new ClassFilter();
for (Node child : childElements(node)) {
final String name = cleanNodeName(child);
if (matches("class", name)) {
list.addClasses(getTextContent(child));
} else if (matches("package", name)) {
list.addPackages(getTextContent(child));
} else if (matches("prefix", name)) {
list.addPrefixes(getTextContent(child));
}
}
return list;
}
protected SSLConfig parseSslConfig(Node node) {
return fillFactoryWithPropertiesConfig(node, new SSLConfig());
}
protected <T extends AbstractFactoryWithPropertiesConfig<?>> T fillFactoryWithPropertiesConfig(Node node, T factoryConfig) {
Node enabledNode = getNamedItemNode(node, "enabled");
boolean enabled = enabledNode != null && getBooleanValue(getTextContent(enabledNode));
factoryConfig.setEnabled(enabled);
for (Node n : childElements(node)) {
String nodeName = cleanNodeName(n);
if (matches("factory-class-name", nodeName)) {
factoryConfig.setFactoryClassName(getTextContent(n));
} else if (matches("properties", nodeName)) {
fillProperties(n, factoryConfig.getProperties());
}
}
return factoryConfig;
}
protected void fillNativeMemoryConfig(Node node, NativeMemoryConfig nativeMemoryConfig) {
final Node enabledNode = getNamedItemNode(node, "enabled");
final boolean enabled = enabledNode != null && getBooleanValue(getTextContent(enabledNode).trim());
nativeMemoryConfig.setEnabled(enabled);
final Node allocTypeNode = getNamedItemNode(node, "allocator-type");
final String allocType = getTextContent(allocTypeNode);
if (allocType != null && !"".equals(allocType)) {
nativeMemoryConfig.setAllocatorType(
NativeMemoryConfig.MemoryAllocatorType.valueOf(upperCaseInternal(allocType)));
}
for (Node n : childElements(node)) {
final String nodeName = cleanNodeName(n);
if (matches("size", nodeName)) {
final String value = getTextContent(getNamedItemNode(n, "value"));
final MemoryUnit unit = MemoryUnit.valueOf(getTextContent(getNamedItemNode(n, "unit")));
MemorySize memorySize = new MemorySize(Long.parseLong(value), unit);
nativeMemoryConfig.setSize(memorySize);
} else if (matches("min-block-size", nodeName)) {
String value = getTextContent(n);
nativeMemoryConfig.setMinBlockSize(Integer.parseInt(value));
} else if (matches("page-size", nodeName)) {
String value = getTextContent(n);
nativeMemoryConfig.setPageSize(Integer.parseInt(value));
} else if (matches("metadata-space-percentage", nodeName)) {
String value = getTextContent(n);
nativeMemoryConfig.setMetadataSpacePercentage(Float.parseFloat(value));
} else if (matches("persistent-memory-directory", nodeName)) {
PersistentMemoryConfig pmemConfig = nativeMemoryConfig.getPersistentMemoryConfig();
pmemConfig.addDirectoryConfig(new PersistentMemoryDirectoryConfig(getTextContent(n).trim()));
// we enable the persistent memory configuration for legacy reasons
pmemConfig.setEnabled(true);
} else if (matches("persistent-memory", nodeName)) {
handlePersistentMemoryConfig(nativeMemoryConfig.getPersistentMemoryConfig(), n);
}
}
}
private void handlePersistentMemoryConfig(PersistentMemoryConfig persistentMemoryConfig, Node node) {
Node enabledNode = getNamedItemNode(node, "enabled");
if (enabledNode != null) {
boolean enabled = getBooleanValue(getTextContent(enabledNode));
persistentMemoryConfig.setEnabled(enabled);
}
final Node modeNode = getNamedItemNode(node, "mode");
final String modeStr = getTextContent(modeNode);
PersistentMemoryMode mode = PersistentMemoryMode.MOUNTED;
if (!StringUtil.isNullOrEmptyAfterTrim(modeStr)) {
try {
mode = PersistentMemoryMode.valueOf(modeStr);
persistentMemoryConfig.setMode(mode);
} catch (Exception ex) {
throw new InvalidConfigurationException("Invalid 'mode' for 'persistent-memory': " + modeStr);
}
}
for (Node parent : childElements(node)) {
final String nodeName = cleanNodeName(parent);
if (matches("directories", nodeName)) {
if (PersistentMemoryMode.SYSTEM_MEMORY == mode) {
throw new InvalidConfigurationException("Directories for 'persistent-memory' should only be"
+ " defined if the 'mode' is set to '" + PersistentMemoryMode.MOUNTED.name() + "'");
}
for (Node dirNode : childElements(parent)) {
handlePersistentMemoryDirectory(persistentMemoryConfig, dirNode);
}
}
}
}
protected void handlePersistentMemoryDirectory(PersistentMemoryConfig persistentMemoryConfig, Node dirNode) {
final String childNodeName = cleanNodeName(dirNode);
if (matches("directory", childNodeName)) {
Node numaNodeIdNode = getNamedItemNode(dirNode, "numa-node");
int numaNodeId = numaNodeIdNode != null
? getIntegerValue("numa-node", getTextContent(numaNodeIdNode))
: -1;
String directory = getTextContent(dirNode).trim();
persistentMemoryConfig.addDirectoryConfig(new PersistentMemoryDirectoryConfig(directory, numaNodeId));
}
}
protected void handleJaasAuthentication(RealmConfig realmConfig, Node node) {
JaasAuthenticationConfig jaasAuthenticationConfig = new JaasAuthenticationConfig();
for (Node child : childElements(node)) {
String nodeName = cleanNodeName(child);
if (matches("login-module", nodeName)) {
jaasAuthenticationConfig.addLoginModuleConfig(handleLoginModule(child));
}
}
realmConfig.setJaasAuthenticationConfig(jaasAuthenticationConfig);
}
protected LoginModuleConfig handleLoginModule(Node node) {
Node classNameNode = getNamedItemNode(node, "class-name");
String className = getTextContent(classNameNode);
Node usageNode = getNamedItemNode(node, "usage");
LoginModuleConfig.LoginModuleUsage usage =
usageNode != null ? LoginModuleConfig.LoginModuleUsage.get(getTextContent(usageNode))
: LoginModuleConfig.LoginModuleUsage.REQUIRED;
LoginModuleConfig moduleConfig = new LoginModuleConfig(className, usage);
for (Node child : childElements(node)) {
String nodeName = cleanNodeName(child);
if (matches("properties", nodeName)) {
fillProperties(child, moduleConfig.getProperties());
break;
}
}
return moduleConfig;
}
protected void handleInstanceTracking(Node node, InstanceTrackingConfig trackingConfig) {
Node attrEnabled = getNamedItemNode(node, "enabled");
String textContent = getTextContent(attrEnabled);
if (!isNullOrEmptyAfterTrim(textContent)) {
trackingConfig.setEnabled(getBooleanValue(textContent));
}
for (Node n : childElements(node)) {
final String name = cleanNodeName(n);
if (matches("file-name", name)) {
trackingConfig.setFileName(getTextContent(n));
} else if (matches("format-pattern", name)) {
trackingConfig.setFormatPattern(getTextContent(n));
}
}
}
}