Polish
This commit is contained in:
@@ -37,10 +37,8 @@ class HazelcastClientInstanceConfiguration {
|
||||
@Bean
|
||||
HazelcastInstance hazelcastInstance(HazelcastConnectionDetails hazelcastConnectionDetails) {
|
||||
ClientConfig config = hazelcastConnectionDetails.getClientConfig();
|
||||
if (StringUtils.hasText(config.getInstanceName())) {
|
||||
return HazelcastClient.getOrCreateHazelcastClient(config);
|
||||
}
|
||||
return HazelcastClient.newHazelcastClient(config);
|
||||
return (!StringUtils.hasText(config.getInstanceName())) ? HazelcastClient.newHazelcastClient(config)
|
||||
: HazelcastClient.getOrCreateHazelcastClient(config);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -55,14 +55,16 @@ class PropertiesHazelcastConnectionDetails implements HazelcastConnectionDetails
|
||||
try {
|
||||
URL configUrl = configLocation.getURL();
|
||||
String configFileName = configUrl.getPath().toLowerCase();
|
||||
if (configFileName.endsWith(".yaml") || configFileName.endsWith(".yml")) {
|
||||
return new YamlClientConfigBuilder(configUrl).build();
|
||||
}
|
||||
return new XmlClientConfigBuilder(configUrl).build();
|
||||
return (!isYaml(configFileName)) ? new XmlClientConfigBuilder(configUrl).build()
|
||||
: new YamlClientConfigBuilder(configUrl).build();
|
||||
}
|
||||
catch (IOException ex) {
|
||||
throw new UncheckedIOException("Failed to load Hazelcast config", ex);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isYaml(String configFileName) {
|
||||
return configFileName.endsWith(".yaml") || configFileName.endsWith(".yml");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -65,36 +65,6 @@ public final class Binding {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the container destination path.
|
||||
* @return the container destination path
|
||||
*/
|
||||
String getContainerDestinationPath() {
|
||||
List<String> parts = split(this.value, ':', '\\');
|
||||
// Format is <host>:<container>:[<options>]
|
||||
Assert.state(parts.size() >= 2, () -> "Expected 2 or more parts, but found %d".formatted(parts.size()));
|
||||
return parts.get(1);
|
||||
}
|
||||
|
||||
private List<String> split(String input, char delimiter, char notFollowedBy) {
|
||||
Assert.state(notFollowedBy != '\0', "notFollowedBy must not be the null terminator");
|
||||
List<String> parts = new ArrayList<>();
|
||||
StringBuilder accumulator = new StringBuilder();
|
||||
for (int i = 0; i < input.length(); i++) {
|
||||
char c = input.charAt(i);
|
||||
char nextChar = (i + 1 < input.length()) ? input.charAt(i + 1) : '\0';
|
||||
if (c == delimiter && nextChar != notFollowedBy) {
|
||||
parts.add(accumulator.toString());
|
||||
accumulator.setLength(0);
|
||||
}
|
||||
else {
|
||||
accumulator.append(c);
|
||||
}
|
||||
}
|
||||
parts.add(accumulator.toString());
|
||||
return parts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the binding uses a sensitive container path.
|
||||
* @return whether the binding uses a sensitive container path
|
||||
@@ -104,6 +74,35 @@ public final class Binding {
|
||||
return SENSITIVE_CONTAINER_PATHS.contains(getContainerDestinationPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the container destination path.
|
||||
* @return the container destination path
|
||||
*/
|
||||
String getContainerDestinationPath() {
|
||||
List<String> parts = getParts();
|
||||
Assert.state(parts.size() >= 2, () -> "Expected 2 or more parts, but found %d".formatted(parts.size()));
|
||||
return parts.get(1);
|
||||
}
|
||||
|
||||
private List<String> getParts() {
|
||||
// Format is <host>:<container>:[<options>]
|
||||
List<String> parts = new ArrayList<>();
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
for (int i = 0; i < this.value.length(); i++) {
|
||||
char ch = this.value.charAt(i);
|
||||
char nextChar = (i + 1 < this.value.length()) ? this.value.charAt(i + 1) : '\0';
|
||||
if (ch == ':' && nextChar != '\\') {
|
||||
parts.add(buffer.toString());
|
||||
buffer.setLength(0);
|
||||
}
|
||||
else {
|
||||
buffer.append(ch);
|
||||
}
|
||||
}
|
||||
parts.add(buffer.toString());
|
||||
return parts;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link Binding} with the specified value containing a host source,
|
||||
* container destination, and options.
|
||||
|
||||
@@ -107,14 +107,14 @@ class BindingTests {
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(textBlock = """
|
||||
/cnb, true
|
||||
/layers, true
|
||||
/workspace, true
|
||||
/something, false
|
||||
c:\\cnb, true
|
||||
c:\\layers, true
|
||||
c:\\workspace, true
|
||||
c:\\something, false
|
||||
/cnb, true
|
||||
/layers, true
|
||||
/workspace, true
|
||||
/something, false
|
||||
c:\\cnb, true
|
||||
c:\\layers, true
|
||||
c:\\workspace, true
|
||||
c:\\something, false
|
||||
""")
|
||||
void shouldDetectSensitiveContainerPaths(String containerPath, boolean sensitive) {
|
||||
Binding binding = Binding.from("/host", containerPath);
|
||||
|
||||
Reference in New Issue
Block a user