From 4ef818f7404de7013bf9cb27fbccff88eebe8b58 Mon Sep 17 00:00:00 2001 From: Andrew Azores Date: Wed, 25 Nov 2020 13:10:20 -0500 Subject: [PATCH] Do not exit early from platform detection Use single method exit point to ensure post-selection logic (currently, client starting) is performed for both automatic and manual platform selection --- .../containerjfr/platform/PlatformModule.java | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/redhat/rhjmc/containerjfr/platform/PlatformModule.java b/src/main/java/com/redhat/rhjmc/containerjfr/platform/PlatformModule.java index 26c68cafae..105a173ff5 100644 --- a/src/main/java/com/redhat/rhjmc/containerjfr/platform/PlatformModule.java +++ b/src/main/java/com/redhat/rhjmc/containerjfr/platform/PlatformModule.java @@ -106,26 +106,33 @@ static AuthManager provideAuthManager( @Singleton static PlatformDetectionStrategy providePlatformStrategy( Logger logger, Set> strategies, Environment env) { + PlatformDetectionStrategy strat = null; if (env.hasEnv(PLATFORM_STRATEGY_ENV_VAR)) { String platform = env.getEnv(PLATFORM_STRATEGY_ENV_VAR); logger.info( String.format( "Selecting configured PlatformDetectionStrategy \"%s\"", platform)); - for (PlatformDetectionStrategy strat : strategies) { - if (Objects.equals(platform, strat.getClass().getCanonicalName())) { - return strat; + for (PlatformDetectionStrategy s : strategies) { + if (Objects.equals(platform, s.getClass().getCanonicalName())) { + strat = s; + break; } } - throw new RuntimeException( - String.format("Selected PlatformDetectionStrategy \"%s\" not found", platform)); + if (strat == null) { + throw new RuntimeException( + String.format( + "Selected PlatformDetectionStrategy \"%s\" not found", platform)); + } + } + if (strat == null) { + strat = + strategies.stream() + // reverse sort, higher priorities should be earlier in the stream + .sorted((a, b) -> Integer.compare(b.getPriority(), a.getPriority())) + .filter(PlatformDetectionStrategy::isAvailable) + .findFirst() + .orElseThrow(); } - PlatformDetectionStrategy strat = - strategies.stream() - // reverse sort, higher priorities should be earlier in the stream - .sorted((a, b) -> Integer.compare(b.getPriority(), a.getPriority())) - .filter(PlatformDetectionStrategy::isAvailable) - .findFirst() - .orElseThrow(); strat.getPlatformClient().start(); return strat; }