com.sap.cloud.mt.subscription.SqLiteFileResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of multi-tenant-subscription Show documentation
Show all versions of multi-tenant-subscription Show documentation
Spring Boot Enablement Parent
/**************************************************************************
* (C) 2019-2022 SAP SE or an SAP affiliate company. All rights reserved. *
**************************************************************************/
package com.sap.cloud.mt.subscription;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileVisitOption;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SqLiteFileResolver {
private static final Logger logger = LoggerFactory.getLogger(SqLiteFileResolver.class);
private static final Pattern SQLITE_FILE_NAME = Pattern.compile("sqlite[_|-]([\\w-]+)\\.db");
private static final Pattern SQLITE_ALTERNATIVE_FILE_NAME = Pattern.compile("db[_|-]([\\w-]+)\\.sqlite");
private final List directories = Arrays.asList("mtx/sidecar/gen", "mtx/sidecar/", "../mtx/sidecar/", "../mtx/sidecar/gen", ".", "../");
private final Path root;
public SqLiteFileResolver(Path root) {
this.root = root;
}
public Map get() {
for (String variant : directories) {
Path path = root.resolve(variant);
Map result = find(path);
if (!result.isEmpty()) {
if (logger.isDebugEnabled()) {
logger.debug("Picked up tenants: {}", String.join(", ", result.keySet()));
}
return result;
}
}
logger.debug("No tenant databases found.");
return Collections.emptyMap();
}
private static Map find(Path root) {
try (Stream stream = Files.find(root, 1,
(path, attributes) -> attributes.isRegularFile() && isFileUsable(path.toFile()),
FileVisitOption.FOLLOW_LINKS)) {
List potentialFiles = stream.toList();
if (!potentialFiles.isEmpty()) {
Map result = new HashMap<>();
potentialFiles.forEach(item -> {
ifMatch(item.getFileName().toString(), SQLITE_ALTERNATIVE_FILE_NAME,
matcher -> result.putIfAbsent(matcher.group(1), item));
ifMatch(item.getFileName().toString(), SQLITE_FILE_NAME,
matcher -> result.putIfAbsent(matcher.group(1), item));
});
logger.debug("Found {} tenant databases at {}", result.size(), root);
return Collections.unmodifiableMap(result);
}
} catch (NoSuchFileException e) {
logger.debug("Skipped missing path at {}", e.getFile());
} catch (IOException e) {
logger.error("There was an error during file access. No tenants will be returned", e);
}
return Collections.emptyMap();
}
private static boolean isFileUsable(File file) {
return file.exists() && file.canRead() && file.canWrite() && (
SQLITE_FILE_NAME.matcher(file.getName()).matches() ||
SQLITE_ALTERNATIVE_FILE_NAME.matcher(file.getName()).matches() );
}
private static void ifMatch(String text, Pattern pattern, Consumer action) {
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
action.accept(matcher);
}
}
}