1
0

Optimierung: StartConfigurationValidator strukturell vereinfacht

This commit is contained in:
2026-04-05 20:54:42 +02:00
parent 9ddb32912c
commit 8278a16bbb

View File

@@ -79,26 +79,15 @@ public class StartConfigurationValidator {
public void validate(StartConfiguration config) { public void validate(StartConfiguration config) {
List<String> errors = new ArrayList<>(); List<String> errors = new ArrayList<>();
// Mandatory string/path presence checks // Mandatory fields and required paths
validateSourceFolder(config.sourceFolder(), errors); validateMandatoryFields(config, errors);
validateTargetFolder(config.targetFolder(), errors);
validateSqliteFile(config.sqliteFile(), errors);
validateApiBaseUrl(config.apiBaseUrl(), errors);
validateApiModel(config.apiModel(), errors);
validatePromptTemplateFile(config.promptTemplateFile(), errors);
// Numeric validation // Numeric constraints
validateApiTimeoutSeconds(config.apiTimeoutSeconds(), errors); validateNumericConstraints(config, errors);
validateMaxRetriesTransient(config.maxRetriesTransient(), errors);
validateMaxPages(config.maxPages(), errors);
validateMaxTextCharacters(config.maxTextCharacters(), errors);
// Path relationship validation // Path relationships and optional paths
validateSourceAndTargetNotSame(config.sourceFolder(), config.targetFolder(), errors); validateSourceAndTargetNotSame(config.sourceFolder(), config.targetFolder(), errors);
validateOptionalPaths(config, errors);
// Optional path validations (only if present)
validateRuntimeLockFile(config.runtimeLockFile(), errors);
validateLogDirectory(config.logDirectory(), errors);
if (!errors.isEmpty()) { if (!errors.isEmpty()) {
String errorMessage = "Invalid startup configuration:\n" + String.join("\n", errors); String errorMessage = "Invalid startup configuration:\n" + String.join("\n", errors);
@@ -108,6 +97,27 @@ public class StartConfigurationValidator {
LOG.info("Configuration validation successful."); LOG.info("Configuration validation successful.");
} }
private void validateMandatoryFields(StartConfiguration config, List<String> errors) {
validateSourceFolder(config.sourceFolder(), errors);
validateTargetFolder(config.targetFolder(), errors);
validateSqliteFile(config.sqliteFile(), errors);
validateApiBaseUrl(config.apiBaseUrl(), errors);
validateApiModel(config.apiModel(), errors);
validatePromptTemplateFile(config.promptTemplateFile(), errors);
}
private void validateNumericConstraints(StartConfiguration config, List<String> errors) {
validateApiTimeoutSeconds(config.apiTimeoutSeconds(), errors);
validateMaxRetriesTransient(config.maxRetriesTransient(), errors);
validateMaxPages(config.maxPages(), errors);
validateMaxTextCharacters(config.maxTextCharacters(), errors);
}
private void validateOptionalPaths(StartConfiguration config, List<String> errors) {
validateRuntimeLockFile(config.runtimeLockFile(), errors);
validateLogDirectory(config.logDirectory(), errors);
}
private void validateSourceFolder(Path sourceFolder, List<String> errors) { private void validateSourceFolder(Path sourceFolder, List<String> errors) {
if (sourceFolder == null) { if (sourceFolder == null) {
errors.add("- source.folder: must not be null"); errors.add("- source.folder: must not be null");
@@ -120,30 +130,11 @@ public class StartConfigurationValidator {
} }
private void validateTargetFolder(Path targetFolder, List<String> errors) { private void validateTargetFolder(Path targetFolder, List<String> errors) {
if (targetFolder == null) { validateRequiredExistingDirectory(targetFolder, "target.folder", errors);
errors.add("- target.folder: must not be null");
return;
}
if (!Files.exists(targetFolder)) {
errors.add("- target.folder: path does not exist: " + targetFolder);
} else if (!Files.isDirectory(targetFolder)) {
errors.add("- target.folder: path is not a directory: " + targetFolder);
}
} }
private void validateSqliteFile(Path sqliteFile, List<String> errors) { private void validateSqliteFile(Path sqliteFile, List<String> errors) {
if (sqliteFile == null) { validateRequiredFileParentDirectory(sqliteFile, "sqlite.file", errors);
errors.add("- sqlite.file: must not be null");
return;
}
Path parent = sqliteFile.getParent();
if (parent == null) {
errors.add("- sqlite.file: has no parent directory: " + sqliteFile);
} else if (!Files.exists(parent)) {
errors.add("- sqlite.file: parent directory does not exist: " + parent);
} else if (!Files.isDirectory(parent)) {
errors.add("- sqlite.file: parent is not a directory: " + parent);
}
} }
private void validateApiBaseUrl(java.net.URI apiBaseUrl, List<String> errors) { private void validateApiBaseUrl(java.net.URI apiBaseUrl, List<String> errors) {
@@ -192,15 +183,7 @@ public class StartConfigurationValidator {
} }
private void validatePromptTemplateFile(Path promptTemplateFile, List<String> errors) { private void validatePromptTemplateFile(Path promptTemplateFile, List<String> errors) {
if (promptTemplateFile == null) { validateRequiredRegularFile(promptTemplateFile, "prompt.template.file", errors);
errors.add("- prompt.template.file: must not be null");
return;
}
if (!Files.exists(promptTemplateFile)) {
errors.add("- prompt.template.file: path does not exist: " + promptTemplateFile);
} else if (!Files.isRegularFile(promptTemplateFile)) {
errors.add("- prompt.template.file: path is not a regular file: " + promptTemplateFile);
}
} }
private void validateSourceAndTargetNotSame(Path sourceFolder, Path targetFolder, List<String> errors) { private void validateSourceAndTargetNotSame(Path sourceFolder, Path targetFolder, List<String> errors) {
@@ -219,23 +202,89 @@ public class StartConfigurationValidator {
} }
private void validateRuntimeLockFile(Path runtimeLockFile, List<String> errors) { private void validateRuntimeLockFile(Path runtimeLockFile, List<String> errors) {
if (runtimeLockFile != null && !runtimeLockFile.toString().isBlank()) { validateOptionalFileParentDirectory(runtimeLockFile, "runtime.lock.file", errors);
Path parent = runtimeLockFile.getParent(); }
private void validateLogDirectory(Path logDirectory, List<String> errors) {
validateOptionalExistingDirectory(logDirectory, "log.directory", errors);
}
// === Helper methods for common validation patterns ===
/**
* Validates that a required path is not null, exists, and is a directory.
*/
private void validateRequiredExistingDirectory(Path path, String fieldName, List<String> errors) {
if (path == null) {
errors.add("- " + fieldName + ": must not be null");
return;
}
if (!Files.exists(path)) {
errors.add("- " + fieldName + ": path does not exist: " + path);
} else if (!Files.isDirectory(path)) {
errors.add("- " + fieldName + ": path is not a directory: " + path);
}
}
/**
* Validates that a required file path is not null and its parent directory exists and is a directory.
*/
private void validateRequiredFileParentDirectory(Path filePath, String fieldName, List<String> errors) {
if (filePath == null) {
errors.add("- " + fieldName + ": must not be null");
return;
}
Path parent = filePath.getParent();
if (parent == null) {
errors.add("- " + fieldName + ": has no parent directory: " + filePath);
} else if (!Files.exists(parent)) {
errors.add("- " + fieldName + ": parent directory does not exist: " + parent);
} else if (!Files.isDirectory(parent)) {
errors.add("- " + fieldName + ": parent is not a directory: " + parent);
}
}
/**
* Validates that a required file path is not null, exists, and is a regular file.
*/
private void validateRequiredRegularFile(Path filePath, String fieldName, List<String> errors) {
if (filePath == null) {
errors.add("- " + fieldName + ": must not be null");
return;
}
if (!Files.exists(filePath)) {
errors.add("- " + fieldName + ": path does not exist: " + filePath);
} else if (!Files.isRegularFile(filePath)) {
errors.add("- " + fieldName + ": path is not a regular file: " + filePath);
}
}
/**
* Validates that an optional file path, if present and non-blank, has a parent directory
* that exists and is a directory.
*/
private void validateOptionalFileParentDirectory(Path filePath, String fieldName, List<String> errors) {
if (filePath != null && !filePath.toString().isBlank()) {
Path parent = filePath.getParent();
if (parent != null) { if (parent != null) {
if (!Files.exists(parent)) { if (!Files.exists(parent)) {
errors.add("- runtime.lock.file: parent directory does not exist: " + parent); errors.add("- " + fieldName + ": parent directory does not exist: " + parent);
} else if (!Files.isDirectory(parent)) { } else if (!Files.isDirectory(parent)) {
errors.add("- runtime.lock.file: parent is not a directory: " + parent); errors.add("- " + fieldName + ": parent is not a directory: " + parent);
} }
} }
} }
} }
private void validateLogDirectory(Path logDirectory, List<String> errors) { /**
if (logDirectory != null && !logDirectory.toString().isBlank()) { * Validates that an optional directory path, if present and non-blank, either does not exist
if (Files.exists(logDirectory)) { * or exists and is a directory.
if (!Files.isDirectory(logDirectory)) { */
errors.add("- log.directory: exists but is not a directory: " + logDirectory); private void validateOptionalExistingDirectory(Path directoryPath, String fieldName, List<String> errors) {
if (directoryPath != null && !directoryPath.toString().isBlank()) {
if (Files.exists(directoryPath)) {
if (!Files.isDirectory(directoryPath)) {
errors.add("- " + fieldName + ": exists but is not a directory: " + directoryPath);
} }
} }
// If it doesn't exist yet, that's acceptable - we don't auto-create // If it doesn't exist yet, that's acceptable - we don't auto-create