diff --git a/config/config-mp/src/main/java/io/helidon/config/mp/DeprecatedMpConfig.java b/config/config-mp/src/main/java/io/helidon/config/mp/DeprecatedMpConfig.java index 08b4b0e0793..f20978cb7df 100644 --- a/config/config-mp/src/main/java/io/helidon/config/mp/DeprecatedMpConfig.java +++ b/config/config-mp/src/main/java/io/helidon/config/mp/DeprecatedMpConfig.java @@ -13,5 +13,62 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package io.helidon.config.mp;public class DeprecatedMpConfig { +package io.helidon.config.mp; + +import java.util.Optional; +import java.util.logging.Logger; + +import org.eclipse.microprofile.config.Config; + +/** + * A utility class to handle MicroProfile configuration properties that should no longer be used. + *

+ * For one major release, the property is retrieved through this class, to warn about the usage. + * In next major release, the deprecated property is removed (as is use of this class). + *

+ * This class is closely patterned after {@code io.helidon.config.DeprecatedConfig} and works whether the MP config object + * is proxied by CDI or not. That other class can be used in conjunction with + * {@link io.helidon.config.mp.MpConfig#toHelidonConfig(org.eclipse.microprofile.config.Config)} for MP + * config objects that are not injected but that does work for MP config objects proxied by CDI. This one does work for + * those use cases. + */ +public final class DeprecatedMpConfig { + private static final Logger LOGGER = Logger.getLogger(DeprecatedMpConfig.class.getName()); + + private DeprecatedMpConfig() { + } + + /** + * Get a value from config, attempting to read both the keys. + * Warning is logged if either the current key is not defined, or both the keys are defined. + * + * @param config configuration instance + * @param type type of the retrieved value + * @param currentKey key that should be used + * @param deprecatedKey key that should not be used + * @param type of the retrieved value + * @return config value of the current key if exists, or the deprecated key if it does not, an empty {@code Optional} + * otherwise + */ + public static Optional getConfigValue(Config config, Class type, String currentKey, String deprecatedKey) { + Optional deprecatedConfig = config.getOptionalValue(deprecatedKey, type); + Optional currentConfig = config.getOptionalValue(currentKey, type); + + if (deprecatedConfig.isPresent()) { + if (currentConfig.isPresent()) { + LOGGER.warning("You are using both a deprecated configuration and a current one. " + + "Deprecated key: \"" + deprecatedKey + "\", " + + "current key: \"" + currentKey + "\", " + + "only the current key will be used, and deprecated will be ignored."); + return currentConfig; + } else { + LOGGER.warning("You are using a deprecated configuration key. " + + "Deprecated key: \"" + deprecatedKey + "\", " + + "current key: \"" + currentKey + "\"."); + return deprecatedConfig; + } + } else { + return currentConfig; + } + } } diff --git a/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java b/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java index 478d276484f..7fecf2a9c50 100644 --- a/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java +++ b/health/health-checks/src/main/java/io/helidon/health/checks/DiskSpaceHealthCheck.java @@ -23,10 +23,10 @@ import java.nio.file.Paths; import java.util.Formatter; import java.util.Locale; +import java.util.function.Consumer; import io.helidon.config.Config; -import io.helidon.config.DeprecatedConfig; -import io.helidon.config.mp.MpConfig; +import io.helidon.config.mp.DeprecatedMpConfig; import io.helidon.health.HealthCheckException; import io.helidon.health.common.BuiltInHealthCheck; @@ -83,8 +83,6 @@ public class DiskSpaceHealthCheck implements HealthCheck { static final String CONFIG_KEY_DISKSPACE_PREFIX = "diskSpace"; static final String CONFIG_KEY_PATH_SUFFIX = "path"; - static final String CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX = "thresholdPercent"; - /** * Full configuration key for path, when configured through MicroProfile config. * @@ -94,7 +92,7 @@ public class DiskSpaceHealthCheck implements HealthCheck { public static final String CONFIG_KEY_PATH = HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX + "." + CONFIG_KEY_DISKSPACE_PREFIX + "." + CONFIG_KEY_PATH_SUFFIX; - + static final String CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX = "thresholdPercent"; /** * Full configuration key for threshold percent, when configured through Microprofile config. * @@ -133,9 +131,7 @@ public class DiskSpaceHealthCheck implements HealthCheck { @Inject DiskSpaceHealthCheck(org.eclipse.microprofile.config.Config mpConfig) { - this(builder().config(DeprecatedConfig.get(MpConfig.toHelidonConfig(mpConfig), - HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, - HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX))); + this(builder().update(applyConfig(mpConfig))); } private DiskSpaceHealthCheck(Builder builder) { @@ -186,30 +182,28 @@ public static DiskSpaceHealthCheck create() { return builder().build(); } -// private static FileStore path(Config config) { -// try { -// return Files.getFileStore( -// Path.of(DeprecatedConfig.get(MpConfig.toHelidonConfig(config), -// HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, -// HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX) -// .get(CONFIG_KEY_DISKSPACE_PREFIX) -// .get(CONFIG_KEY_PATH_SUFFIX) -// .asString() -// .orElse(DEFAULT_PATH))); -// } catch (IOException ex) { -// throw new RuntimeException(ex); -// } -// } -// -// private static double thresholdPercent(Config config) { -// return DeprecatedConfig.get(MpConfig.toHelidonConfig(config), -// HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, -// HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX) -// .get(CONFIG_KEY_DISKSPACE_PREFIX) -// .get(CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX) -// .asDouble() -// .orElse(DEFAULT_THRESHOLD); -// } + private static Consumer applyConfig(org.eclipse.microprofile.config.Config mpConfig) { + return builder -> { + DeprecatedMpConfig.getConfigValue(mpConfig, + Path.class, + configKey(HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, + CONFIG_KEY_PATH_SUFFIX), + configKey(HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, + CONFIG_KEY_PATH_SUFFIX)) + .ifPresent(builder::path); + DeprecatedMpConfig.getConfigValue(mpConfig, + Double.class, + configKey(HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, + CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX), + configKey(HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, + CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX)) + .ifPresent(builder::thresholdPercent); + }; + } + + private static String configKey(String prefix, String suffix) { + return prefix + "." + CONFIG_KEY_DISKSPACE_PREFIX + "." + suffix; + } @Override public HealthCheckResponse call() { diff --git a/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java b/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java index a589deb4e92..603dc4295a7 100644 --- a/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java +++ b/health/health-checks/src/main/java/io/helidon/health/checks/HeapMemoryHealthCheck.java @@ -19,13 +19,11 @@ import java.util.Formatter; import java.util.Locale; -import io.helidon.config.DeprecatedConfig; -import io.helidon.config.mp.MpConfig; +import io.helidon.config.mp.DeprecatedMpConfig; import io.helidon.health.common.BuiltInHealthCheck; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; -import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.health.HealthCheck; import org.eclipse.microprofile.health.HealthCheckResponse; import org.eclipse.microprofile.health.Liveness; @@ -78,22 +76,18 @@ public class HeapMemoryHealthCheck implements HealthCheck { + "." + CONFIG_KEY_HEAP_PREFIX + "." + CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX; - private final Runtime rt; private final double thresholdPercent; // this will be ignored if not within CDI @Inject - HeapMemoryHealthCheck( - Config config, - Runtime runtime) { - this(runtime, DeprecatedConfig.get(MpConfig.toHelidonConfig(config), - HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX, - HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX) - .get(CONFIG_KEY_HEAP_PREFIX) - .get(CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX) - .asDouble() - .orElse(98d)); + HeapMemoryHealthCheck(Runtime runtime, org.eclipse.microprofile.config.Config mpConfig) { + // Cannot use + this(runtime, DeprecatedMpConfig.getConfigValue(mpConfig, + Double.class, + thresholdPercentKey(HealthChecks.CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX), + thresholdPercentKey(HealthChecks.DEPRECATED_CONFIG_KEY_BUILT_IN_HEALTH_CHECKS_PREFIX)) + .orElse(DEFAULT_THRESHOLD)); } HeapMemoryHealthCheck(Runtime runtime, double thresholdPercent) { @@ -126,6 +120,12 @@ public static HeapMemoryHealthCheck create() { return builder().build(); } + private static String thresholdPercentKey(String prefix) { + return prefix + "." + + CONFIG_KEY_HEAP_PREFIX + "." + + CONFIG_KEY_THRESHOLD_PERCENT_SUFFIX; + } + @Override public HealthCheckResponse call() { //Formatter ensures that returned delimiter will be always the same