Skip to content

Commit

Permalink
Initial work to support multiple profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Sep 20, 2022
1 parent 9f17bbb commit 8bba86d
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
import io.quarkus.runtime.configuration.ConfigurationException;
import io.quarkus.runtime.configuration.HyphenateEnumConverter;
import io.quarkus.runtime.configuration.NameIterator;
import io.quarkus.runtime.configuration.ProfileManager;
import io.quarkus.runtime.configuration.PropertiesUtil;
import io.quarkus.runtime.configuration.QuarkusConfigFactory;
import io.quarkus.runtime.configuration.RuntimeConfigSource;
Expand Down Expand Up @@ -210,9 +209,6 @@ public final class RunTimeConfigurationGenerator {
static final MethodDescriptor OPT_IS_PRESENT = MethodDescriptor.ofMethod(Optional.class, "isPresent", boolean.class);
static final MethodDescriptor OPT_OF = MethodDescriptor.ofMethod(Optional.class, "of", Optional.class, Object.class);

static final MethodDescriptor PM_SET_RUNTIME_DEFAULT_PROFILE = MethodDescriptor.ofMethod(ProfileManager.class,
"setRuntimeDefaultProfile", void.class, String.class);

static final MethodDescriptor SB_NEW = MethodDescriptor.ofConstructor(StringBuilder.class);
static final MethodDescriptor SB_NEW_STR = MethodDescriptor.ofConstructor(StringBuilder.class, String.class);
static final MethodDescriptor SB_APPEND_STRING = MethodDescriptor.ofMethod(StringBuilder.class, "append",
Expand Down Expand Up @@ -365,7 +361,6 @@ public static final class GenerateOperation implements AutoCloseable {
cc.getFieldCreator(C_UNKNOWN_RUNTIME).setModifiers(Opcodes.ACC_STATIC | Opcodes.ACC_FINAL);
clinit.writeStaticField(C_UNKNOWN_RUNTIME, clinit.newInstance(AL_NEW));

clinit.invokeStaticMethod(PM_SET_RUNTIME_DEFAULT_PROFILE, clinit.load(ProfileManager.getActiveProfile()));
clinitNameBuilder = clinit.newInstance(SB_NEW);

// static field containing the instance of the class - is set when createBootstrapConfig is run
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
package io.quarkus.deployment.steps;

import org.eclipse.microprofile.config.ConfigProvider;

import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.LaunchModeBuildItem;
import io.quarkus.deployment.builditem.RunTimeConfigurationDefaultBuildItem;
import io.quarkus.runtime.LaunchMode;

public class ProfileBuildStep {
@BuildStep
RunTimeConfigurationDefaultBuildItem defaultProfile(LaunchModeBuildItem launchModeBuildItem) {
return new RunTimeConfigurationDefaultBuildItem("quarkus.profile",
getProfileValue(launchModeBuildItem.getLaunchMode()));
}

private String getProfileValue(LaunchMode launchMode) {
if (launchMode == LaunchMode.DEVELOPMENT) {
return "dev";
} else if (launchMode == LaunchMode.TEST) {
return "test";
}
return "prod";
return new RunTimeConfigurationDefaultBuildItem(launchModeBuildItem.getLaunchMode().getProfileKey(),
ConfigProvider.getConfig().getConfigValue(launchModeBuildItem.getLaunchMode().getProfileKey()).getValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

import io.quarkus.bootstrap.logging.InitialConfigurator;
import io.quarkus.bootstrap.runner.RunnerClassLoader;
import io.quarkus.runtime.configuration.ConfigUtils;
import io.quarkus.runtime.configuration.ConfigurationException;
import io.quarkus.runtime.configuration.ProfileManager;
import io.quarkus.runtime.graal.DiagnosticPrinter;
import sun.misc.Signal;
import sun.misc.SignalHandler;
Expand Down Expand Up @@ -192,8 +192,9 @@ public static void run(Application application, Class<? extends QuarkusApplicati
} else if (rootCause instanceof ConfigurationException) {
System.err.println(rootCause.getMessage());
} else {
// If it is not a ConfigurationException it should be safe to call ConfigProvider.getConfig here
applicationLogger.errorv(rootCause, "Failed to start application (with profile {0})",
ProfileManager.getActiveProfile());
ConfigUtils.getProfiles());
ensureConsoleLogsDrained();
}
}
Expand Down
14 changes: 10 additions & 4 deletions core/runtime/src/main/java/io/quarkus/runtime/LaunchMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ public enum LaunchMode {
* A normal production build. At the moment this can be both native image or
* JVM mode, but eventually these will likely be split
*/
NORMAL("prod"),
NORMAL("prod", "quarkus.profile"),
/**
* quarkus:dev or an IDE launch (when we support IDE launch)
*/
DEVELOPMENT("dev"),
DEVELOPMENT("dev", "quarkus.profile"),
/**
* a test run
*/
TEST("test");
TEST("test", "quarkus.test.profile");

public boolean isDevOrTest() {
return this != NORMAL;
Expand All @@ -30,15 +30,21 @@ public static boolean isRemoteDev() {
}

private final String defaultProfile;
private final String profileKey;

LaunchMode(String defaultProfile) {
LaunchMode(final String defaultProfile, final String profileKey) {
this.defaultProfile = defaultProfile;
this.profileKey = profileKey;
}

public String getDefaultProfile() {
return defaultProfile;
}

public String getProfileKey() {
return profileKey;
}

/**
*
* @return The current launch mode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,20 @@ public static SmallRyeConfigBuilder configBuilder(final boolean runTime, final b

public static SmallRyeConfigBuilder emptyConfigBuilder() {
SmallRyeConfigBuilder builder = new SmallRyeConfigBuilder();
builder.withDefaultValue(SMALLRYE_CONFIG_PROFILE, ProfileManager.getActiveProfile());
LaunchMode launchMode = ProfileManager.getLaunchMode();
builder.withDefaultValue(launchMode.getProfileKey(), launchMode.getDefaultProfile());

builder.withInterceptorFactories(new ConfigSourceInterceptorFactory() {
@Override
public ConfigSourceInterceptor getInterceptor(final ConfigSourceInterceptorContext context) {
return new RelocateConfigSourceInterceptor(Map.of(SMALLRYE_CONFIG_PROFILE, launchMode.getProfileKey()));
}

@Override
public OptionalInt getPriority() {
return OptionalInt.of(Priorities.LIBRARY + 200 - 10);
}
});

builder.withInterceptorFactories(new ConfigSourceInterceptorFactory() {
@Override
Expand Down Expand Up @@ -170,6 +183,7 @@ public OptionalInt getPriority() {
@Override
public ConfigSourceInterceptor getInterceptor(final ConfigSourceInterceptorContext context) {
Map<String, String> fallbacks = new HashMap<>();
fallbacks.put("quarkus.profile", SMALLRYE_CONFIG_PROFILE);
fallbacks.put("quarkus.config.locations", SMALLRYE_CONFIG_LOCATIONS);
fallbacks.put("quarkus.config.profile.parent", SMALLRYE_CONFIG_PROFILE_PARENT);
return new FallbackConfigSourceInterceptor(fallbacks);
Expand Down Expand Up @@ -258,6 +272,14 @@ public static void addMapping(SmallRyeConfigBuilder builder, String mappingClass
}
}

public static List<String> getProfiles() {
return ConfigProvider.getConfig().unwrap(SmallRyeConfig.class).getProfiles();
}

public static boolean isProfileActive(final String profile) {
return getProfiles().contains(profile);
}

/**
* Checks if a property is present in the current Configuration.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public static void setRuntimeDefaultProfile(final String profile) {
}

//NOTE: changes made here must be replicated in BootstrapProfile
@Deprecated
public static String getActiveProfile() {
if (launchMode == LaunchMode.TEST) {
String profile = System.getProperty(QUARKUS_TEST_PROFILE_PROP);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,7 @@ public SmallRyeConfig getConfigFor(final SmallRyeConfigProviderResolver configPr
//TODO: this code path is only hit when start fails in dev mode very early in the process
//the recovery code will fail without this as it cannot read any properties such as
//the HTTP port or logging info
return configProviderResolver.getBuilder().forClassLoader(classLoader)
.addDefaultSources()
.addDefaultInterceptors()
.addDiscoveredSources()
.addDiscoveredConverters()
.addDiscoveredInterceptors()
.withProfile(ProfileManager.getActiveProfile())
.build();
return ConfigUtils.emptyConfigBuilder().addDefaultSources().addDiscoveredSources().build();
}
return config;
}
Expand Down
Loading

0 comments on commit 8bba86d

Please sign in to comment.