Skip to content

Commit

Permalink
SAVE POINT / BROKEN
Browse files Browse the repository at this point in the history
  • Loading branch information
snazy committed Feb 17, 2023
1 parent 017bafa commit e3fd8a7
Show file tree
Hide file tree
Showing 21 changed files with 665 additions and 617 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ private BuiltInType(final String name) {
public String toString() {
return name;
}

public static BuiltInType fromString(String name) {
for (PackageConfig.BuiltInType type : values()) {
if (type.toString().equals(name)) {
return type;
}
}
throw new IllegalArgumentException("Unknown Quarkus package type '" + name + "'");
}
}

/**
Expand Down
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'");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public static <T> T handleObject(Supplier<T> supplier) {

public static void handleObject(Object o) {
final SmallRyeConfig config = (SmallRyeConfig) ConfigProvider.getConfig();
handleObject(o, config);
}

public static void handleObject(Object o, SmallRyeConfig config) {
final String clsNameSuffix = getClassNameSuffix(o);
if (clsNameSuffix == null) {
// unsupported object type
Expand Down Expand Up @@ -190,7 +194,11 @@ private static Converter<?> getConverterFor(Type type, SmallRyeConfig config) {
} else if (rawType == Optional.class) {
return Converters.newOptionalConverter(getConverterFor(typeOfParameter(type, 0), config));
} else if (rawType == List.class) {
return Converters.newCollectionConverter(getConverterFor(typeOfParameter(type, 0), config), ArrayList::new);
return Converters.newCollectionConverter(getConverterFor(typeOfParameter(type, 0), config),
ConfigUtils.listFactory());
} else if (rawType == Set.class) {
return Converters.newCollectionConverter(getConverterFor(typeOfParameter(type, 0), config),
ConfigUtils.setFactory());
} else {
return config.requireConverter(rawTypeOf(type));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ public void beforeTest(Test task) {
}

public String resolveBuildProperty(String propertyKey, Map<String, Object> taskSystemProps, String defaultValue) {
if (true) {
// TODO !!!
throw new UnsupportedOperationException("Delegate to EffectiveConfig");
}
Object v = taskSystemProps.get(propertyKey);
if (v instanceof String) {
return v.toString();
Expand Down
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);
}
}
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;
}
}
}

0 comments on commit e3fd8a7

Please sign in to comment.