-
Notifications
You must be signed in to change notification settings - Fork 3k
API to store real Runtime values #51209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
radcortez
wants to merge
2
commits into
quarkusio:main
Choose a base branch
from
radcortez:runtime-values
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
core/runtime/src/main/java/io/quarkus/runtime/RuntimeValues.java
This file contains hidden or 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,101 @@ | ||
| package io.quarkus.runtime; | ||
|
|
||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
|
|
||
| /** | ||
| * Provides a low-level API for registering and accessing runtime values generated by Quarkus or Quarkus | ||
| * extensions. Typically, such values describe a fully configured application, known only at application startup, | ||
| * such as the HTTP port. | ||
| * <p> | ||
| * These values should not be exposed by the Config mechanism directly, as that would require mutating the Config | ||
| * system, which should be immutable, and cause hard-to-debug issues. Instead, RuntimeValues should be preferred for | ||
| * exposing such values. | ||
| */ | ||
| public class RuntimeValues { | ||
| private static final Map<String, Object> values = new ConcurrentHashMap<>(); | ||
|
|
||
| /** | ||
| * Registers a value with a specified key. | ||
| * | ||
| * @param key key with which the specified value is to be associated | ||
| * @param value value to be associated with the specified key | ||
| * @return the value associated with the specified key | ||
| * @throws IllegalArgumentException if the specified key already has an associated value | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| public static <T> T register(final RuntimeKey<T> key, final T value) { | ||
| Object mapValue = values.putIfAbsent(key.key(), value); | ||
| if (mapValue != null && !mapValue.equals(value)) { | ||
| throw new IllegalArgumentException("Key already registered " + key.key() + " with value " + mapValue); | ||
| } | ||
| return (T) mapValue; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the value to which the specified key is mapped. | ||
| * | ||
| * @param key the key whose associated value is to be returned | ||
| * @return the value to which the specified key is mapped | ||
| * @throws java.lang.IllegalArgumentException if the specified key has no associated value | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| public static <T> T get(final RuntimeKey<T> key) { | ||
| T t = (T) values.get(key.key()); | ||
| if (t == null) { | ||
| throw new IllegalArgumentException("Key " + key.key() + " not found"); | ||
| } | ||
| return t; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the value to which the specified key is mapped. | ||
| * | ||
| * @param key the key whose associated value is to be returned | ||
| * @param defaultValue the default mapping of the key | ||
| * @return the value to which the specified key is mapped, or {@code defaultValue} if the key has no value | ||
| */ | ||
| @SuppressWarnings("unchecked") | ||
| public static <T> T get(final RuntimeKey<T> key, final T defaultValue) { | ||
| return (T) values.getOrDefault(key.key(), defaultValue); | ||
| } | ||
|
|
||
| static String getValue(final String propertyName) { | ||
| Object value = values.get(propertyName); | ||
| // TODO - We may require to convert this to the expected config string | ||
| return value != null ? value.toString() : null; | ||
| } | ||
|
|
||
| public static final class RuntimeKey<T> { | ||
| private final String key; | ||
|
|
||
| RuntimeKey(String key) { | ||
| this.key = key; | ||
| } | ||
|
|
||
| public String key() { | ||
| return key; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (!(o instanceof RuntimeKey<?> that)) | ||
| return false; | ||
| return Objects.equals(key, that.key); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hashCode(key); | ||
| } | ||
|
|
||
| public static RuntimeKey<String> key(final String key) { | ||
| return new RuntimeKey<>(key); | ||
| } | ||
|
|
||
| public static RuntimeKey<Integer> intKey(final String key) { | ||
| return new RuntimeKey<>(key); | ||
| } | ||
| } | ||
| } |
31 changes: 31 additions & 0 deletions
31
core/runtime/src/main/java/io/quarkus/runtime/RuntimeValuesConfigSource.java
This file contains hidden or 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,31 @@ | ||
| package io.quarkus.runtime; | ||
|
|
||
| import java.util.Set; | ||
|
|
||
| import io.smallrye.config.common.AbstractConfigSource; | ||
|
|
||
| /** | ||
| * A <code>ConfigSource</code> to bridge between <code>Config</code> and {@link RuntimeValues}. | ||
| * <p> | ||
| * While {@link RuntimeValues} shouldn't be exposed as <code>Config</code>, this is intended to | ||
| * work as a temporary compatibility layer, since until the introduction of {@link RuntimeValues}, | ||
| * the norm was to use <code>Config</code> to relay this kind of information. | ||
| * <p> | ||
| * This should be kept until we decide on an alternate solution in the discussion | ||
| * <a href="https://github.com/quarkusio/quarkus/discussions/46915">#46915</a>. | ||
| */ | ||
| public class RuntimeValuesConfigSource extends AbstractConfigSource { | ||
| public RuntimeValuesConfigSource() { | ||
| super(RuntimeValuesConfigSource.class.getName(), Integer.MAX_VALUE - 10); | ||
| } | ||
|
|
||
| @Override | ||
| public Set<String> getPropertyNames() { | ||
| return Set.of(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getValue(String propertyName) { | ||
| return RuntimeValues.getValue(propertyName); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
27 changes: 27 additions & 0 deletions
27
extensions/vertx-http/deployment/src/test/java/io/quarkus/vertx/http/HttpServerTest.java
This file contains hidden or 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,27 @@ | ||
| package io.quarkus.vertx.http; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import jakarta.inject.Inject; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
|
||
| import io.quarkus.test.QuarkusUnitTest; | ||
| import io.quarkus.vertx.http.runtime.HttpServer; | ||
|
|
||
| public class HttpServerTest { | ||
| @RegisterExtension | ||
| static final QuarkusUnitTest CONFIG = new QuarkusUnitTest(); | ||
|
|
||
| @Inject | ||
| HttpServer webServer; | ||
|
|
||
| @Test | ||
| void ports() { | ||
| assertTrue(webServer.getPort() > 0); | ||
| assertEquals(-1, webServer.getSecurePort()); | ||
| assertEquals(-1, webServer.getManagementPort()); | ||
| } | ||
| } |
This file contains hidden or 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
74 changes: 74 additions & 0 deletions
74
extensions/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/HttpServer.java
This file contains hidden or 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,74 @@ | ||
| package io.quarkus.vertx.http.runtime; | ||
|
|
||
| import io.quarkus.runtime.RuntimeValues; | ||
| import io.quarkus.runtime.RuntimeValues.RuntimeKey; | ||
|
|
||
| public class HttpServer { | ||
| private final static HttpServer INSTANCE = new HttpServer(); | ||
|
|
||
| public static final RuntimeKey<Integer> HTTP_PORT = RuntimeKey.intKey("quarkus.http.port"); | ||
| public static final RuntimeKey<Integer> HTTP_TEST_PORT = RuntimeKey.intKey("quarkus.http.test-port"); | ||
| public static final RuntimeKey<Integer> HTTPS_PORT = RuntimeKey.intKey("quarkus.http.ssl-port"); | ||
| public static final RuntimeKey<Integer> HTTPS_TEST_PORT = RuntimeKey.intKey("quarkus.http.test-ssl-port"); | ||
| public static final RuntimeKey<Integer> MANAGEMENT_PORT = RuntimeKey.intKey("quarkus.management.port"); | ||
| public static final RuntimeKey<Integer> MANAGEMENT_TEST_PORT = RuntimeKey.intKey("quarkus.management.test-port"); | ||
|
|
||
| private HttpServer() { | ||
| } | ||
|
|
||
| /** | ||
| * Return the http port that Quarkus is listening on. | ||
| * | ||
| * @return the port or <code>-1</code> if Quarkus is not set to listen to the http port. | ||
| */ | ||
| public int getPort() { | ||
| // TODO - What if this method gets called before being set? Should this be an event instead? | ||
| return RuntimeValues.get(HTTP_PORT, -1); | ||
| } | ||
|
|
||
| static void setPort(int port) { | ||
| RuntimeValues.register(HTTP_PORT, port); | ||
| } | ||
|
|
||
| static void setTestPort(int testPort) { | ||
| RuntimeValues.register(HTTP_TEST_PORT, testPort); | ||
| } | ||
|
|
||
| /** | ||
| * Return the https port that Quarkus is listening on. | ||
| * | ||
| * @return the port or <code>-1</code> if Quarkus is not set to listen to the https port. | ||
| */ | ||
| public int getSecurePort() { | ||
| return RuntimeValues.get(HTTPS_PORT, -1); | ||
| } | ||
|
|
||
| static void setSecurePort(int port) { | ||
| RuntimeValues.register(HTTPS_PORT, port); | ||
| } | ||
|
|
||
| static void setSecureTestPort(int testPort) { | ||
| RuntimeValues.register(HTTPS_TEST_PORT, testPort); | ||
| } | ||
|
|
||
| /** | ||
| * Return the management http port that Quarkus is listening on. | ||
| * | ||
| * @return the port or <code>-1</code> if Quarkus is not set to listen to the management http port. | ||
| */ | ||
| public int getManagementPort() { | ||
| return RuntimeValues.get(MANAGEMENT_PORT, -1); | ||
| } | ||
|
|
||
| static void setManagementPort(int managementPort) { | ||
| RuntimeValues.register(MANAGEMENT_PORT, managementPort); | ||
| } | ||
|
|
||
| static void setManagementTestPort(int testPort) { | ||
| RuntimeValues.register(MANAGEMENT_TEST_PORT, testPort); | ||
| } | ||
|
|
||
| public static HttpServer instance() { | ||
| return INSTANCE; | ||
| } | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
...ns/vertx-http/runtime/src/main/java/io/quarkus/vertx/http/runtime/HttpServerProducer.java
This file contains hidden or 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,13 @@ | ||
| package io.quarkus.vertx.http.runtime; | ||
|
|
||
| import jakarta.enterprise.inject.Produces; | ||
| import jakarta.inject.Singleton; | ||
|
|
||
| @Singleton | ||
| public class HttpServerProducer { | ||
| @Produces | ||
| @Singleton | ||
| public HttpServer producer() { | ||
| return HttpServer.instance(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't we want to store the actual socket address(es) rather than just the port number?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is something we can add. This was just to showcase it with the port number.