Add unified application path resolver

This commit is contained in:
张家训
2026-08-03 14:53:15 +08:00
parent f1c8232480
commit 931b964226
2 changed files with 132 additions and 0 deletions
@@ -0,0 +1,68 @@
package com.example.workutils.system.support;
import java.nio.file.Path;
import java.util.Arrays;
/** Resolves every WorkUtils runtime path from one stable application home. */
public final class ApplicationPaths {
public static final String HOME_PROPERTY = "workutils.home";
public static final String HOME_ENV = "WORKUTILS_HOME";
private final Path home;
private ApplicationPaths(Path home) {
this.home = home.toAbsolutePath().normalize();
}
public static ApplicationPaths current() {
return resolve(System.getProperty(HOME_PROPERTY), System.getenv(HOME_ENV),
System.getProperty("user.dir"));
}
public static ApplicationPaths of(Path home) {
if (home == null) {
throw new IllegalArgumentException("Application home must not be null");
}
return new ApplicationPaths(home);
}
static ApplicationPaths resolve(String property, String environment, String userDirectory) {
String selected = firstNonBlank(property, environment, userDirectory);
if (selected == null) {
throw new IllegalStateException("Application home is unavailable");
}
return new ApplicationPaths(Path.of(selected.trim()));
}
public Path home() {
return home;
}
public Path resolve(String first, String... more) {
return home.resolve(Path.of(first, more)).normalize();
}
public Path resolveConfigured(String configured, String... fallbackSegments) {
Path value;
if (configured == null || configured.isBlank()) {
if (fallbackSegments.length == 0) {
throw new IllegalArgumentException("A fallback path is required");
}
value = Path.of(fallbackSegments[0],
Arrays.copyOfRange(fallbackSegments, 1, fallbackSegments.length));
} else {
value = Path.of(configured.trim());
}
return (value.isAbsolute() ? value : home.resolve(value)).normalize();
}
private static String firstNonBlank(String... values) {
for (String value : values) {
if (value != null && !value.isBlank()) {
return value;
}
}
return null;
}
}