Skip to content

Commit

Permalink
Proper commit of recent changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tjquinno committed Nov 5, 2024
1 parent 286d4bb commit 512b888
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* 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).
* <p>
* 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 <em>are not</em> 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 <T> 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 <T> Optional<T> getConfigValue(Config config, Class<T> type, String currentKey, String deprecatedKey) {
Optional<T> deprecatedConfig = config.getOptionalValue(deprecatedKey, type);
Optional<T> 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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
*
Expand All @@ -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.
*
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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<Builder> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 512b888

Please sign in to comment.