/
DeclarativeConfigUtil.java
153 lines (132 loc) · 5.93 KB
/
DeclarativeConfigUtil.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
/*
* Copyright (c) 2008-2024, 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.core.HazelcastException;
import com.hazelcast.internal.util.StringUtil;
import java.util.Arrays;
import java.util.Collection;
import static java.util.Arrays.asList;
import static java.util.Collections.singletonList;
import static java.util.Collections.unmodifiableList;
/**
* Utility class for handling declarative configuration files.
*/
public final class DeclarativeConfigUtil {
/**
* System property used for defining the configuring file used for
* member configuration.
*/
public static final String SYSPROP_MEMBER_CONFIG = "hazelcast.config";
/**
* System property used for defining the configuring file used for
* client configuration.
*/
public static final String SYSPROP_CLIENT_CONFIG = "hazelcast.client.config";
/**
* System property used for defining the configuring file used for
* client failover configuration.
*/
public static final String SYSPROP_CLIENT_FAILOVER_CONFIG = "hazelcast.client.failover.config";
/**
* Array of accepted suffixes for XML configuration files.
*/
public static final Collection<String> XML_ACCEPTED_SUFFIXES = singletonList("xml");
/**
* List of accepted suffixes for XML configuration files.
*
* @see #XML_ACCEPTED_SUFFIXES
*/
public static final String XML_ACCEPTED_SUFFIXES_STRING = Arrays.toString(XML_ACCEPTED_SUFFIXES.toArray());
/**
* Array of accepted suffixes for YAML configuration files.
*/
public static final Collection<String> YAML_ACCEPTED_SUFFIXES = unmodifiableList(asList("yaml", "yml"));
/**
* List of accepted suffixes for YAML configuration files.
*
* @see #YAML_ACCEPTED_SUFFIXES
*/
public static final String YAML_ACCEPTED_SUFFIXES_STRING = Arrays.toString(YAML_ACCEPTED_SUFFIXES.toArray());
/**
* Array of the suffixes accepted in Hazelcast configuration files.
*/
public static final Collection<String> ALL_ACCEPTED_SUFFIXES = unmodifiableList(asList("xml", "yaml", "yml"));
/**
* The list of the suffixes accepted in Hazelcast configuration files.
*
* @see #ALL_ACCEPTED_SUFFIXES
*/
public static final String ALL_ACCEPTED_SUFFIXES_STRING = Arrays.toString(ALL_ACCEPTED_SUFFIXES.toArray());
private DeclarativeConfigUtil() {
}
/**
* Validates if the config file referenced in {@code propertyKey}
* has an accepted suffix. If the system property is not set, the
* validation passes without throwing exception.
*
* @param propertyKey The name of the system property to validate
* @throws HazelcastException If the suffix of the config file name
* is not in the accepted suffix list
*/
public static void validateSuffixInSystemProperty(String propertyKey) {
String configSystemProperty = System.getProperty(propertyKey);
if (configSystemProperty == null) {
return;
}
if (!isAcceptedSuffixConfigured(configSystemProperty, ALL_ACCEPTED_SUFFIXES)) {
throwUnacceptedSuffixInSystemProperty(propertyKey, configSystemProperty, ALL_ACCEPTED_SUFFIXES);
}
}
/**
* Throws {@link HazelcastException} unconditionally referring to that
* the configuration file referenced in the system property
* {@code propertyKey} has a suffix not in the accepted suffix list
* defined in {@code acceptedSuffixes}.
*
* @param propertyKey The name of the system property key
* holding the reference to the
* configuration file
* @param configResource The value of the system property
* @param acceptedSuffixes The list of the accepted suffixes
* @throws HazelcastException Thrown unconditionally with a message
* referring to the unaccepted suffix of
* the file referenced by {@code propertyKey}
*/
public static void throwUnacceptedSuffixInSystemProperty(String propertyKey, String configResource,
Collection<String> acceptedSuffixes) {
String message = String.format("The suffix of the resource \'%s\' referenced in \'%s\' is not in the list of accepted "
+ "suffixes: \'%s\'", configResource, propertyKey, Arrays.toString(acceptedSuffixes.toArray()));
throw new HazelcastException(message);
}
/**
* Checks if the suffix of the passed {@code configFile} is in the
* accepted suffixes list passed in {@code acceptedSuffixes}.
*
* @param configFile The configuration file to check
* @param acceptedSuffixes The list of the accepted suffixes
* @return {@code true} if the suffix of the configuration file is in
* the accepted list, {@code false} otherwise
*/
public static boolean isAcceptedSuffixConfigured(String configFile, Collection<String> acceptedSuffixes) {
String configFileLower = StringUtil.lowerCaseInternal(configFile);
int lastDotIndex = configFileLower.lastIndexOf('.');
if (lastDotIndex == -1) {
return false;
}
String configFileSuffix = configFileLower.substring(lastDotIndex + 1);
return acceptedSuffixes.contains(configFileSuffix);
}
}