Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1519b51

Browse files
committed
API to store real Runtime values
1 parentadbf1ed commit1519b51

File tree

3 files changed

+134
-0
lines changed

3 files changed

+134
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
packageio.quarkus.runtime;
2+
3+
importjava.util.Map;
4+
importjava.util.Objects;
5+
importjava.util.concurrent.ConcurrentHashMap;
6+
7+
/**
8+
* Provides a low-level API for registering and accessing runtime values generated by Quarkus or Quarkus
9+
* extensions. Typically, such values describe a fully configured application, known only at application startup,
10+
* such as the HTTP port.
11+
* <p>
12+
* These values should not be exposed by the Config mechanism directly, as that would require mutating the Config
13+
* system, which should be immutable, and cause hard-to-debug issues. Instead, RuntimeValues should be preferred for
14+
* exposing such values.
15+
*/
16+
publicclassRuntimeValues {
17+
privatestaticfinalMap<String,Object>values =newConcurrentHashMap<>();
18+
19+
/**
20+
* Registers a value with a specified key.
21+
*
22+
* @param key key with which the specified value is to be associated
23+
* @param value value to be associated with the specified key
24+
* @return the value associated with the specified key
25+
* @throws IllegalArgumentException if the specified key already has an associated value
26+
*/
27+
@SuppressWarnings("unchecked")
28+
publicstatic <T>Tregister(finalRuntimeKey<T>key,finalTvalue) {
29+
ObjectmapValue =values.putIfAbsent(key.key(),value);
30+
if (mapValue !=null && !mapValue.equals(value)) {
31+
thrownewIllegalArgumentException("Key already registered " +key.key() +" with value " +mapValue);
32+
}
33+
return (T)mapValue;
34+
}
35+
36+
/**
37+
* Returns the value to which the specified key is mapped.
38+
*
39+
* @param key the key whose associated value is to be returned
40+
* @return the value to which the specified key is mapped
41+
* @throws java.lang.IllegalArgumentException if the specified key has no associated value
42+
*/
43+
@SuppressWarnings("unchecked")
44+
publicstatic <T>Tget(finalRuntimeKey<T>key) {
45+
Tt = (T)values.get(key.key());
46+
if (t ==null) {
47+
thrownewIllegalArgumentException("Key " +key.key() +" not found");
48+
}
49+
returnt;
50+
}
51+
52+
/**
53+
* Returns the value to which the specified key is mapped.
54+
*
55+
* @param key the key whose associated value is to be returned
56+
* @param defaultValue the default mapping of the key
57+
* @return the value to which the specified key is mapped, or {@code defaultValue} if the key has no value
58+
*/
59+
@SuppressWarnings("unchecked")
60+
publicstatic <T>Tget(finalRuntimeKey<T>key,finalTdefaultValue) {
61+
return (T)values.getOrDefault(key.key(),defaultValue);
62+
}
63+
64+
staticStringgetValue(finalStringpropertyName) {
65+
Objectvalue =values.get(propertyName);
66+
// TODO - We may require to convert this to the expected config string
67+
returnvalue !=null ?value.toString() :null;
68+
}
69+
70+
publicstaticfinalclassRuntimeKey<T> {
71+
privatefinalStringkey;
72+
73+
RuntimeKey(Stringkey) {
74+
this.key =key;
75+
}
76+
77+
publicStringkey() {
78+
returnkey;
79+
}
80+
81+
@Override
82+
publicbooleanequals(Objecto) {
83+
if (!(oinstanceofRuntimeKey<?>that))
84+
returnfalse;
85+
returnObjects.equals(key,that.key);
86+
}
87+
88+
@Override
89+
publicinthashCode() {
90+
returnObjects.hashCode(key);
91+
}
92+
93+
publicstaticRuntimeKey<String>key(finalStringkey) {
94+
returnnewRuntimeKey<>(key);
95+
}
96+
97+
publicstaticRuntimeKey<Integer>intKey(finalStringkey) {
98+
returnnewRuntimeKey<>(key);
99+
}
100+
}
101+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
packageio.quarkus.runtime;
2+
3+
importjava.util.Set;
4+
5+
importio.smallrye.config.common.AbstractConfigSource;
6+
7+
/**
8+
* A <code>ConfigSource</code> to bridge between <code>Config</code> and {@link RuntimeValues}.
9+
* <p>
10+
* While {@link RuntimeValues} shouldn't be exposed as <code>Config</code>, this is intended to
11+
* work as a temporary compatibility layer, since until the introduction of {@link RuntimeValues},
12+
* the norm was to use <code>Config</code> to relay this kind of information.
13+
* <p>
14+
* This should be kept until we decide on an alternate solution in the discussion
15+
* <a href="https://github.com/quarkusio/quarkus/discussions/46915">#46915</a>.
16+
*/
17+
publicclassRuntimeValuesConfigSourceextendsAbstractConfigSource {
18+
publicRuntimeValuesConfigSource() {
19+
super(RuntimeValuesConfigSource.class.getName(),Integer.MAX_VALUE -10);
20+
}
21+
22+
@Override
23+
publicSet<String>getPropertyNames() {
24+
returnSet.of();
25+
}
26+
27+
@Override
28+
publicStringgetValue(StringpropertyName) {
29+
returnRuntimeValues.getValue(propertyName);
30+
}
31+
}

‎core/runtime/src/main/java/io/quarkus/runtime/configuration/RuntimeConfigBuilder.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
importorg.eclipse.microprofile.config.spi.ConfigSource;
77

8+
importio.quarkus.runtime.RuntimeValuesConfigSource;
89
importio.smallrye.config.SmallRyeConfigBuilder;
910
importio.smallrye.config.SmallRyeConfigBuilderCustomizer;
1011

@@ -15,6 +16,7 @@ public class RuntimeConfigBuilder implements SmallRyeConfigBuilderCustomizer {
1516
@Override
1617
publicvoidconfigBuilder(finalSmallRyeConfigBuilderbuilder) {
1718
newQuarkusConfigBuilderCustomizer().configBuilder(builder);
19+
builder.withSources(newRuntimeValuesConfigSource());
1820
builder.withSources(newUuidConfigSource());
1921

2022
builder.forClassLoader(Thread.currentThread().getContextClassLoader())

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp