Skip to content

Commit

Permalink
Merge pull request #28091 from radcortez/fix-27760
Browse files Browse the repository at this point in the history
Support multiple profiles
  • Loading branch information
radcortez authored Nov 30, 2022
2 parents 7810646 + 5cf686e commit d4e7ef7
Show file tree
Hide file tree
Showing 14 changed files with 145 additions and 109 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 @@ -191,8 +191,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
18 changes: 12 additions & 6 deletions core/runtime/src/main/java/io/quarkus/runtime/ConfigConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@
/**
* We don't really use this, because these are configurations for the config itself, so it causes a chicken / egg
* problem, but we have it so the configurations can be properly documented.
*
* <br>
* Relocation of the Config configurations to the Quarkus namespace is done in
* {@link io.quarkus.runtime.configuration.ConfigUtils#configBuilder}.
*/
@ConfigRoot(name = ConfigItem.PARENT, phase = ConfigPhase.RUN_TIME)
public class ConfigConfig {
/**
* Additional config locations to be loaded with the Config. The configuration support multiple locations
* separated by a comma and each must represent a valid {@link java.net.URI}.
* Profile that will be active when Quarkus launches.
*/
@ConfigItem(name = "config.locations")
public Optional<List<URI>> locations;
@ConfigItem(name = "profile", defaultValue = "prod")
public Optional<String> profile;

/**
* Accepts a single configuration profile name. If a configuration property cannot be found in the current active
Expand All @@ -31,10 +30,17 @@ public class ConfigConfig {
@ConfigItem(name = "config.profile.parent")
public Optional<String> profileParent;

/**
* Additional config locations to be loaded with the Config. The configuration support multiple locations
* separated by a comma and each must represent a valid {@link java.net.URI}.
*/
@ConfigItem(name = "config.locations")
public Optional<List<URI>> locations;

/**
* A property that allows accessing a generated UUID.
* It generates that UUID at startup time. So it changes between two starts including in dev mode.
*
* <br>
* Access this generated UUID using expressions: `${quarkus.uuid}`.
*/
@ConfigItem(name = "uuid")
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,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 @@ -178,6 +191,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 @@ -269,6 +283,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,12 @@ public static void setRuntimeDefaultProfile(final String profile) {
}

//NOTE: changes made here must be replicated in BootstrapProfile

/**
* @deprecated This method is not suited to multiple profiles, because it returns a single string. Please use
* {@link ConfigUtils#getProfiles()} instead, which returns a List of profiles.
*/
@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 d4e7ef7

Please sign in to comment.