-
Notifications
You must be signed in to change notification settings - Fork 39
/
ConfigUtils.java
106 lines (91 loc) · 3.96 KB
/
ConfigUtils.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
package com.getindata.connectors.http.internal.utils;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.flink.util.StringUtils;
import com.getindata.connectors.http.internal.config.ConfigException;
import com.getindata.connectors.http.internal.config.HttpConnectorConfigConstants;
@NoArgsConstructor(access = AccessLevel.NONE)
public final class ConfigUtils {
private static final String PROPERTY_NAME_DELIMITER = ".";
/**
* Convert properties that name starts with given {@code keyPrefix} to Map.
* Values for this property will be cast to {@code valueClazz} type.
*
* @param properties properties to extract keys from.
* @param keyPrefix prefix used to match property name with.
* @param valueClazz type to cast property values to.
* @param <T> type of the elements of a returned map.
* @return Map of propertyName to propertyValue.
*/
public static <T> Map<String, T> propertiesToMap(
Properties properties,
String keyPrefix,
Class<T> valueClazz) {
Map<String, T> map = new HashMap<>();
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
if (entry.getKey() instanceof String) {
String key = (String) entry.getKey();
if (key.startsWith(keyPrefix)) {
tryAddToConfigMap(properties, valueClazz, map, key);
}
} else {
throw new ConfigException(
entry.getKey().toString(),
entry.getValue(), "Key must be a string."
);
}
}
return map;
}
/**
* A utility method to extract last element from property name. This method assumes property to
* be in format as <b>{@code this.is.my.property.name}</b>, using "dot" as a delimiter. For this
* example the returned value would be <b>{@code name}</b>.
*
* @param propertyKey Property name to extract the last element from.
* @return property last element or the property name if {@code propertyKey} parameter had no
* dot delimiter.
* @throws ConfigException when invalid property such as null, empty, blank, ended with dot was
* used.
*/
public static String extractPropertyLastElement(String propertyKey) {
if (StringUtils.isNullOrWhitespaceOnly(propertyKey)) {
throw new ConfigException("Provided a property name that is null, empty or blank.");
}
if (!propertyKey.contains(PROPERTY_NAME_DELIMITER)) {
return propertyKey;
}
int delimiterLastIndex = propertyKey.lastIndexOf(PROPERTY_NAME_DELIMITER);
if (delimiterLastIndex == propertyKey.length() - 1) {
throw new ConfigException(
String.format(
"Invalid property - %s. Property name should not end with property delimiter.",
propertyKey)
);
}
return propertyKey.substring(delimiterLastIndex + 1);
}
private static <T> void tryAddToConfigMap(
Properties properties,
Class<T> clazz, Map<String, T> map,
String key) {
try {
map.put(key, clazz.cast(properties.get(key)));
} catch (ClassCastException e) {
throw new ConfigException(
String.format("Unable to cast value for property %s to type %s", key,
clazz), e);
}
}
public static Properties getHttpConnectorProperties(Map<String, String> tableOptions) {
final Properties httpProperties = new Properties();
tableOptions.entrySet().stream()
.filter(entry ->
entry.getKey().startsWith(HttpConnectorConfigConstants.GID_CONNECTOR_HTTP))
.forEach(entry -> httpProperties.put(entry.getKey(), entry.getValue()));
return httpProperties;
}
}