-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
665 additions
and
617 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
core/deployment/src/test/java/io/quarkus/deployment/pkg/BuiltInTypeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package io.quarkus.deployment.pkg; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.EnumSource; | ||
|
||
public class BuiltInTypeTest { | ||
|
||
@ParameterizedTest | ||
@EnumSource(PackageConfig.BuiltInType.class) | ||
void packageTypeConversion(PackageConfig.BuiltInType packageType) { | ||
assertThat(PackageConfig.BuiltInType.fromString(packageType.toString())).isSameAs(packageType); | ||
} | ||
|
||
@Test | ||
void invalidPackageType() { | ||
assertThatIllegalArgumentException().isThrownBy(() -> PackageConfig.BuiltInType.fromString("not-a-package-type")) | ||
.withMessage("Unknown Quarkus package type 'not-a-package-type'"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...pplication-plugin/src/main/java/io/quarkus/gradle/tasks/CombinedConfigSourceProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.quarkus.gradle.tasks; | ||
|
||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
|
||
import org.eclipse.microprofile.config.spi.ConfigSource; | ||
import org.eclipse.microprofile.config.spi.ConfigSourceProvider; | ||
|
||
import io.smallrye.config.AbstractLocationConfigSourceLoader; | ||
import io.smallrye.config.PropertiesConfigSource; | ||
import io.smallrye.config.source.yaml.YamlConfigSource; | ||
|
||
final class CombinedConfigSourceProvider extends AbstractLocationConfigSourceLoader implements ConfigSourceProvider { | ||
private final Consumer<URL> sourceUrls; | ||
private final int ordinal; | ||
|
||
CombinedConfigSourceProvider(Consumer<URL> sourceUrls, int ordinal) { | ||
this.sourceUrls = sourceUrls; | ||
this.ordinal = ordinal; | ||
} | ||
|
||
@Override | ||
protected String[] getFileExtensions() { | ||
return new String[] { | ||
"application.yaml", | ||
"application.yml", | ||
"application.properties" | ||
}; | ||
} | ||
|
||
@Override | ||
protected ConfigSource loadConfigSource(final URL url, final int ordinal) throws IOException { | ||
sourceUrls.accept(url); | ||
return url.getPath().endsWith(".properties") ? new PropertiesConfigSource(url, ordinal) | ||
: new YamlConfigSource(url, ordinal); | ||
} | ||
|
||
@Override | ||
public List<ConfigSource> getConfigSources(final ClassLoader classLoader) { | ||
// Note: | ||
return loadConfigSources(new String[] { "application.properties", "application.yaml", "application.yml" }, ordinal, | ||
classLoader); | ||
} | ||
} |
196 changes: 196 additions & 0 deletions
196
...adle/gradle-application-plugin/src/main/java/io/quarkus/gradle/tasks/EffectiveConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
package io.quarkus.gradle.tasks; | ||
|
||
import static java.util.Collections.*; | ||
|
||
import java.io.File; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import java.util.function.Consumer; | ||
|
||
import org.eclipse.microprofile.config.spi.ConfigSource; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
|
||
import io.quarkus.deployment.configuration.ClassLoadingConfig; | ||
import io.quarkus.deployment.pkg.PackageConfig; | ||
import io.quarkus.gradle.dsl.Manifest; | ||
import io.quarkus.runtime.configuration.ConfigInstantiator; | ||
import io.quarkus.runtime.configuration.ConfigUtils; | ||
import io.smallrye.config.EnvConfigSource; | ||
import io.smallrye.config.PropertiesConfigSource; | ||
import io.smallrye.config.SmallRyeConfig; | ||
import io.smallrye.config.common.utils.ConfigSourceUtil; | ||
|
||
/** | ||
* Helper that bundles the various sources of config options for the Gradle plugin: system environment, system properties, | ||
* quarkus build properties (on the Quarkus extension), Gradle project properties, application properties and "forced" | ||
* properties (on the Gradle task). | ||
* | ||
* <p> | ||
* Eventually used to construct a map with the <em>effective</em> config options from all the sources above and expose | ||
* the Quarkus config objects like {@link PackageConfig}, {@link ClassLoadingConfig} and the underlying {@link SmallRyeConfig}. | ||
*/ | ||
final class EffectiveConfig { | ||
private final SmallRyeConfig config; | ||
private final PackageConfig packageConfig; | ||
private final ClassLoadingConfig classLoadingConfig; | ||
private final Manifest manifest = new Manifest(); | ||
private final Map<String, String> fullConfig; | ||
private final List<URL> applicationPropsSources; | ||
|
||
private EffectiveConfig(Builder builder) { | ||
List<ConfigSource> configSources = new ArrayList<>(); | ||
// TODO add io.quarkus.runtime.configuration.DefaultsConfigSource ? | ||
// TODO leverage io.quarkus.runtime.configuration.ProfileManager ? | ||
|
||
// Effective "ordinals" for the config sources: | ||
// 700 -> forcedProperties | ||
// 600 -> System.getProperties() | ||
// 500 -> System.getenv() | ||
// 480 -> quarkusBuildProperties | ||
// 300 -> projectProperties | ||
// 0-250 -> application.(properties|yaml|yml) | ||
|
||
applicationPropsSources = new ArrayList<>(); | ||
|
||
configSources.add(new PropertiesConfigSource(builder.forcedProperties, "forcedProperties", 700)); | ||
configSources.add(new PropertiesConfigSource(ConfigSourceUtil.propertiesToMap(System.getProperties()), | ||
"System.getProperties()", 600)); | ||
configSources.add(new EnvConfigSource(500) { | ||
}); | ||
configSources.add(new PropertiesConfigSource(builder.buildProperties, "quarkusBuildProperties", 400)); | ||
configSources.add(new PropertiesConfigSource(builder.projectProperties, "projectProperties", 300)); | ||
|
||
configSourcesForApplicationProperties(builder.sourceDirectories, applicationPropsSources::add, configSources::add); | ||
config = buildConfig(builder.profile, configSources); | ||
|
||
packageConfig = new PackageConfig(); | ||
classLoadingConfig = new ClassLoadingConfig(); | ||
|
||
ConfigInstantiator.handleObject(classLoadingConfig, config); | ||
ConfigInstantiator.handleObject(packageConfig, config); | ||
|
||
// populate the Gradle Manifest object | ||
manifest.attributes(packageConfig.manifest.attributes); | ||
packageConfig.manifest.manifestSections.forEach((section, attribs) -> manifest.attributes(attribs, section)); | ||
|
||
this.fullConfig = generateFullConfigMap(config); | ||
} | ||
|
||
@VisibleForTesting | ||
static Map<String, String> generateFullConfigMap(SmallRyeConfig config) { | ||
Map<String, String> map = new HashMap<>(); | ||
config.getPropertyNames().forEach(property -> { | ||
String v = config.getConfigValue(property).getValue(); | ||
if (v != null) { | ||
map.put(property, v); | ||
} | ||
}); | ||
Map<String, String> fullConfig = unmodifiableMap(map); | ||
return fullConfig; | ||
} | ||
|
||
@VisibleForTesting | ||
static SmallRyeConfig buildConfig(String profile, List<ConfigSource> configSources) { | ||
return ConfigUtils.emptyConfigBuilder() | ||
.withSources(configSources) | ||
.withProfile(profile) | ||
.build(); | ||
} | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
public PackageConfig.BuiltInType packageType() { | ||
return PackageConfig.BuiltInType.fromString(packageConfig.type); | ||
} | ||
|
||
public Map<String, String> configMap() { | ||
return fullConfig; | ||
} | ||
|
||
public List<URL> applicationPropsSources() { | ||
return applicationPropsSources; | ||
} | ||
|
||
public SmallRyeConfig config() { | ||
return config; | ||
} | ||
|
||
public PackageConfig packageConfig() { | ||
return packageConfig; | ||
} | ||
|
||
public ClassLoadingConfig classLoadingConfig() { | ||
return classLoadingConfig; | ||
} | ||
|
||
static void configSourcesForApplicationProperties(Set<File> sourceDirectories, Consumer<URL> sourceUrls, | ||
Consumer<ConfigSource> configSourceConsumer) { | ||
URL[] resourceUrls = sourceDirectories.stream().map(File::toURI) | ||
.map(u -> { | ||
try { | ||
return u.toURL(); | ||
} catch (MalformedURLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
}) | ||
.toArray(URL[]::new); | ||
|
||
for (URL resourceUrl : resourceUrls) { | ||
URLClassLoader classLoader = new URLClassLoader(new URL[] { resourceUrl }); | ||
CombinedConfigSourceProvider configSourceProvider = new CombinedConfigSourceProvider(sourceUrls, 250); | ||
configSourceProvider.getConfigSources(classLoader).forEach(configSourceConsumer); | ||
} | ||
} | ||
|
||
static final class Builder { | ||
private Map<String, String> buildProperties = emptyMap(); | ||
private Map<String, String> projectProperties = emptyMap(); | ||
private Map<String, String> forcedProperties = emptyMap(); | ||
private Set<File> sourceDirectories = emptySet(); | ||
private String profile = "prod"; | ||
|
||
EffectiveConfig build() { | ||
return new EffectiveConfig(this); | ||
} | ||
|
||
public Builder withForcedProperties(Map<String, String> forcedProperties) { | ||
this.forcedProperties = forcedProperties; | ||
return this; | ||
} | ||
|
||
public Builder withBuildProperties(Map<String, String> buildProperties) { | ||
this.buildProperties = buildProperties; | ||
return this; | ||
} | ||
|
||
public Builder withProjectProperties(Map<String, ?> projectProperties) { | ||
Map<String, String> target = new HashMap<>(); | ||
projectProperties.forEach((k, v) -> { | ||
if (v != null) { | ||
target.put(k, v.toString()); | ||
} | ||
}); | ||
this.projectProperties = target; | ||
return this; | ||
} | ||
|
||
public Builder withSourceDirectories(Set<File> sourceDirectories) { | ||
this.sourceDirectories = sourceDirectories; | ||
return this; | ||
} | ||
|
||
public Builder withProfile(String profile) { | ||
this.profile = profile; | ||
return this; | ||
} | ||
} | ||
} |
Oops, something went wrong.