Skip to content

Commit aa38dc6

Browse files
authored
Merge pull request quarkusio#49846 from radcortez/fix-49775
Use RestClientsBuildTimeConfig to access build time configuration related to the REST Client
2 parents ef0f9e2 + 593d38b commit aa38dc6

File tree

17 files changed

+295
-406
lines changed

17 files changed

+295
-406
lines changed

core/deployment/src/main/java/io/quarkus/deployment/steps/ConfigGenerationBuildStep.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,17 +408,23 @@ public void checkForBuildTimeConfigChange(
408408

409409
Map<String, ConfigValue> values = new TreeMap<>();
410410
BuildTimeConfigurationReader.ReadResult readResult = configItem.getReadResult();
411-
for (final Map.Entry<String, ConfigValue> entry : readResult.getAllBuildTimeValues().entrySet()) {
411+
for (Map.Entry<String, ConfigValue> entry : readResult.getAllBuildTimeValues().entrySet()) {
412412
if (excludedConfigKeys.contains(entry.getKey())) {
413413
continue;
414414
}
415+
if (readResult.getRunTimeValues().containsKey(entry.getKey())) {
416+
continue;
417+
}
415418
values.putIfAbsent(entry.getKey(), entry.getValue());
416419
}
417420

418421
for (Map.Entry<String, ConfigValue> entry : readResult.getBuildTimeRunTimeValues().entrySet()) {
419422
if (excludedConfigKeys.contains(entry.getKey())) {
420423
continue;
421424
}
425+
if (readResult.getRunTimeValues().containsKey(entry.getKey())) {
426+
continue;
427+
}
422428
values.put(entry.getKey(), entry.getValue());
423429
}
424430

extensions/resteasy-classic/rest-client-config/deployment/src/main/java/io/quarkus/restclient/config/deployment/RestClientConfigUtils.java

Lines changed: 1 addition & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
package io.quarkus.restclient.config.deployment;
22

3-
import static io.quarkus.restclient.config.Constants.GLOBAL_REST_SCOPE_FORMAT;
4-
import static io.quarkus.restclient.config.Constants.MP_REST_SCOPE_FORMAT;
5-
import static io.quarkus.restclient.config.Constants.QUARKUS_REST_SCOPE_FORMAT;
6-
73
import java.util.ArrayList;
84
import java.util.List;
9-
import java.util.Optional;
10-
11-
import org.eclipse.microprofile.config.Config;
12-
import org.jboss.jandex.ClassInfo;
135

146
import io.quarkus.deployment.GeneratedClassGizmoAdaptor;
157
import io.quarkus.deployment.annotations.BuildProducer;
@@ -27,42 +19,7 @@
2719
public final class RestClientConfigUtils {
2820

2921
private RestClientConfigUtils() {
30-
}
31-
32-
public static Optional<String> findConfiguredScope(Config config, ClassInfo restClientInterface,
33-
Optional<String> configKeyOptional) {
34-
Optional<String> scopeConfig;
35-
36-
// quarkus style config; fully qualified class name
37-
scopeConfig = config.getOptionalValue(
38-
String.format(QUARKUS_REST_SCOPE_FORMAT, '"' + restClientInterface.name().toString() + '"'),
39-
String.class);
40-
if (scopeConfig.isEmpty()) { // microprofile style config; fully qualified class name
41-
scopeConfig = config.getOptionalValue(
42-
String.format(MP_REST_SCOPE_FORMAT, restClientInterface.name().toString()),
43-
String.class);
44-
}
45-
if (scopeConfig.isEmpty() && configKeyOptional.isPresent()) { // quarkus style config; configKey
46-
scopeConfig = config.getOptionalValue(String.format(QUARKUS_REST_SCOPE_FORMAT, configKeyOptional.get()),
47-
String.class);
48-
}
49-
if (scopeConfig.isEmpty() && configKeyOptional.isPresent()) { // quarkus style config; quoted configKey
50-
scopeConfig = config.getOptionalValue(String.format(QUARKUS_REST_SCOPE_FORMAT, '"' + configKeyOptional.get() + '"'),
51-
String.class);
52-
}
53-
if (scopeConfig.isEmpty() && configKeyOptional.isPresent()) { // microprofile style config; configKey
54-
scopeConfig = config.getOptionalValue(String.format(MP_REST_SCOPE_FORMAT, configKeyOptional.get()), String.class);
55-
}
56-
if (scopeConfig.isEmpty()) { // quarkus style config; short class name
57-
scopeConfig = config.getOptionalValue(
58-
String.format(QUARKUS_REST_SCOPE_FORMAT, restClientInterface.simpleName()),
59-
String.class);
60-
}
61-
return scopeConfig;
62-
}
63-
64-
public static Optional<String> getDefaultScope(Config config) {
65-
return config.getOptionalValue(GLOBAL_REST_SCOPE_FORMAT, String.class);
22+
throw new UnsupportedOperationException();
6623
}
6724

6825
public static void generateRestClientConfigBuilder(
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package io.quarkus.restclient.config.deployment;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.HashSet;
6+
import java.util.List;
7+
import java.util.Optional;
8+
import java.util.Set;
9+
10+
import org.eclipse.microprofile.config.ConfigProvider;
11+
import org.eclipse.microprofile.config.spi.ConfigSource;
12+
import org.jboss.jandex.ClassInfo;
13+
import org.jboss.jandex.DotName;
14+
15+
import io.quarkus.arc.processor.BuiltinScope;
16+
import io.quarkus.builder.item.SimpleBuildItem;
17+
import io.quarkus.deployment.Capabilities;
18+
import io.quarkus.deployment.Capability;
19+
import io.quarkus.restclient.config.AbstractRestClientConfigBuilder;
20+
import io.quarkus.restclient.config.RegisteredRestClient;
21+
import io.quarkus.restclient.config.RestClientKeysProvider;
22+
import io.quarkus.restclient.config.RestClientsBuildTimeConfig;
23+
import io.smallrye.config.ConfigValue;
24+
import io.smallrye.config.DefaultValuesConfigSource;
25+
import io.smallrye.config.SmallRyeConfig;
26+
import io.smallrye.config.SmallRyeConfigBuilder;
27+
import io.smallrye.config.SmallRyeConfigBuilderCustomizer;
28+
29+
/**
30+
* Provides a new {@link RestClientsBuildTimeConfig} with the discovered registered REST Clients configuration
31+
* only. This should be preferred once REST Clients are discovered and validated to keep only the required
32+
* configuration.
33+
* <p>
34+
* This has to be done manually, because the {@link RestClientsBuildTimeConfig} is marked for
35+
* {@link io.quarkus.runtime.annotations.ConfigPhase#BUILD_TIME}, and the REST Clients are not known when the
36+
* configuration starts (before build steps execution).
37+
*
38+
* @see io.quarkus.restclient.config.AbstractRestClientConfigBuilder
39+
*/
40+
public final class RestClientsBuildTimeConfigBuildItem extends SimpleBuildItem {
41+
private final List<RegisteredRestClient> restClients;
42+
private final SmallRyeConfig config;
43+
private final RestClientsBuildTimeConfig restClientsBuildTimeConfig;
44+
45+
public RestClientsBuildTimeConfigBuildItem(final List<RegisteredRestClient> restClients) {
46+
this.restClients = Collections.unmodifiableList(restClients);
47+
this.config = new SmallRyeConfigBuilder()
48+
.withSources(new ConfigSource() {
49+
final SmallRyeConfig config = ConfigProvider.getConfig().unwrap(SmallRyeConfig.class);
50+
51+
@Override
52+
public Set<String> getPropertyNames() {
53+
Set<String> properties = new HashSet<>();
54+
config.getPropertyNames().forEach(properties::add);
55+
return properties;
56+
}
57+
58+
@Override
59+
public String getValue(final String propertyName) {
60+
ConfigValue configValue = config.getConfigValue(propertyName);
61+
if (configValue.getValue() != null
62+
&& !DefaultValuesConfigSource.NAME.equals(configValue.getConfigSourceName())) {
63+
return configValue.getValue();
64+
}
65+
return null;
66+
}
67+
68+
@Override
69+
public String getName() {
70+
return "SmallRye Config";
71+
}
72+
})
73+
.withCustomizers(new SmallRyeConfigBuilderCustomizer() {
74+
@Override
75+
public void configBuilder(final SmallRyeConfigBuilder builder) {
76+
new AbstractRestClientConfigBuilder() {
77+
@Override
78+
public List<RegisteredRestClient> getRestClients() {
79+
return restClients;
80+
}
81+
}.configBuilder(builder);
82+
}
83+
})
84+
.withMapping(RestClientsBuildTimeConfig.class)
85+
.withMappingIgnore("quarkus.**")
86+
.build();
87+
this.restClientsBuildTimeConfig = config.getConfigMapping(RestClientsBuildTimeConfig.class);
88+
RestClientKeysProvider.KEYS.clear();
89+
}
90+
91+
public List<RegisteredRestClient> getRestClients() {
92+
return restClients;
93+
}
94+
95+
public SmallRyeConfig getConfig() {
96+
return config;
97+
}
98+
99+
public RestClientsBuildTimeConfig getRestClientsBuildTimeConfig() {
100+
return restClientsBuildTimeConfig;
101+
}
102+
103+
public Optional<BuiltinScope> getScope(final Capabilities capabilities, final ClassInfo restClientInterface) {
104+
List<Optional<BuiltinScope>> discoveredScopes = new ArrayList<>();
105+
106+
// First config in the rest client service
107+
restClientsBuildTimeConfig.clients().get(restClientInterface.name().toString()).scope()
108+
.ifPresent(s -> discoveredScopes.add(Optional.of(builtinScopeFromName(DotName.createSimple(s)))));
109+
110+
// Second annotation in the rest client declaration
111+
Set<DotName> annotations = restClientInterface.annotationsMap().keySet();
112+
for (DotName annotationName : annotations) {
113+
BuiltinScope builtinScope = BuiltinScope.from(annotationName);
114+
if (builtinScope != null) {
115+
discoveredScopes.add(Optional.of(builtinScope));
116+
}
117+
}
118+
119+
// Third global config
120+
restClientsBuildTimeConfig.scope()
121+
.ifPresent(s -> discoveredScopes.add(Optional.of(builtinScopeFromName(DotName.createSimple(s)))));
122+
123+
Optional<BuiltinScope> scope = discoveredScopes.stream()
124+
.filter(Optional::isPresent)
125+
.map(Optional::get)
126+
.findFirst();
127+
if (scope.isPresent()) {
128+
if (scope.get().equals(BuiltinScope.SESSION)) {
129+
if (capabilities.isPresent(Capability.SERVLET)) {
130+
return scope;
131+
}
132+
} else {
133+
return scope;
134+
}
135+
}
136+
return Optional.empty();
137+
}
138+
139+
private static BuiltinScope builtinScopeFromName(DotName scopeName) {
140+
BuiltinScope scope = BuiltinScope.from(scopeName);
141+
if (scope == null) {
142+
for (BuiltinScope builtinScope : BuiltinScope.values()) {
143+
if (builtinScope.getName().withoutPackagePrefix().equalsIgnoreCase(scopeName.toString())) {
144+
scope = builtinScope;
145+
}
146+
}
147+
}
148+
return scope;
149+
}
150+
}

extensions/resteasy-classic/rest-client-config/runtime/src/main/java/io/quarkus/restclient/config/AbstractRestClientConfigBuilder.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
* property names provided by each source. This also applies to the REST Client, but since the names are known to
3030
* Quarkus, the REST Client configuration could be loaded even for sources that don't provide a list of property
3131
* names. To achieve such behaviour, we use {@link io.smallrye.config.WithKeys} and
32-
* {@link io.quarkus.restclient.config.RestClientsConfig.RestClientKeysProvider} to provide the REST Client keys.
32+
* {@link RestClientKeysProvider} to provide the REST Client keys.
3333
* <p>
3434
* The REST Client configuration looks up the following names in order:
3535
*
@@ -55,29 +55,17 @@
5555
public abstract class AbstractRestClientConfigBuilder implements ConfigBuilder {
5656
private static final String REST_CLIENT_PREFIX = "quarkus.rest-client.";
5757

58-
private final boolean runtime;
59-
60-
public AbstractRestClientConfigBuilder() {
61-
this.runtime = true;
62-
RestClientsConfig.RestClientKeysProvider.KEYS.clear();
63-
}
64-
65-
public AbstractRestClientConfigBuilder(boolean runtime) {
66-
this.runtime = runtime;
67-
}
68-
6958
@Override
7059
public SmallRyeConfigBuilder configBuilder(final SmallRyeConfigBuilder builder) {
60+
RestClientKeysProvider.KEYS.clear();
7161
List<RegisteredRestClient> restClients = getRestClients();
7262

7363
Map<String, String> quarkusFallbacks = new HashMap<>();
7464
Map<String, String> microProfileFallbacks = new HashMap<>();
7565
Map<String, List<String>> relocates = new HashMap<>();
7666

7767
for (RegisteredRestClient restClient : restClients) {
78-
if (runtime) {
79-
RestClientsConfig.RestClientKeysProvider.KEYS.add(restClient.getFullName());
80-
}
68+
RestClientKeysProvider.KEYS.add(restClient.getFullName());
8169

8270
String quotedFullName = "\"" + restClient.getFullName() + "\"";
8371
String quotedSimpleName = "\"" + restClient.getSimpleName() + "\"";

extensions/resteasy-classic/rest-client-config/runtime/src/main/java/io/quarkus/restclient/config/Constants.java

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.quarkus.restclient.config;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.function.Supplier;
6+
7+
public class RestClientKeysProvider implements Supplier<Iterable<String>> {
8+
public static List<String> KEYS = new ArrayList<>();
9+
10+
@Override
11+
public Iterable<String> get() {
12+
return KEYS;
13+
}
14+
}

0 commit comments

Comments
 (0)