-
Notifications
You must be signed in to change notification settings - Fork 564
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
4.x: Global instance handling change (and relevant changes to testing) #9193
Open
tomas-langer
wants to merge
17
commits into
helidon-io:main
Choose a base branch
from
tomas-langer:9186-context-value
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 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
4a881bd
Global instances - to hold all instances that must be shared globally…
tomas-langer 7499a1d
Fixed ConfigProvider not to handle Java service loader mappers.
tomas-langer 69d40b0
Introduced web server service (for core registry) to allow setup that…
tomas-langer f99237c
New testing module that
tomas-langer 18201bf
Refactored webserver and metrics tests to use the new testing module.
tomas-langer 78deefe
Fix discovered when copying the concept of this class to global insta…
tomas-langer 78870e7
javadoc fix
tomas-langer d2c51f8
Refactored back to use Context (with a slightly better handling of it).
tomas-langer 4b69c25
Renaming annotations (and fixed javadoc) to align with current MP ann…
tomas-langer 213eb55
Updates to global instances through Helidon context for MP and to fix…
tomas-langer 6583cc6
Checkstyle, copyright, and javadoc fixes
tomas-langer 88bf9f6
Javadoc fix.
tomas-langer 75f02b1
Improve web server builder updates by supporting more than one implem…
tomas-langer 5d7a683
Renamed context value.
tomas-langer 535c8fe
Javadoc update.
tomas-langer 554fd43
Update ContextSingleton.java use singleton in constant name
tomas-langer e93c8a9
Fix typo in testing/testing/src/main/java/io/helidon/testing/TestRegi…
tomas-langer 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
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
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
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
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
184 changes: 184 additions & 0 deletions
184
common/context/src/main/java/io/helidon/common/context/ContextSingleton.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,184 @@ | ||
/* | ||
* Copyright (c) 2024 Oracle and/or its affiliates. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.helidon.common.context; | ||
|
||
import java.util.NoSuchElementException; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.concurrent.locks.ReentrantReadWriteLock; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* A context based "singleton" value holder that provides an indirection mechanism for static values. | ||
* "Singleton" values are stored and resolved from a context classified with {@value #STATIC_CONTEXT_CLASSIFIER} | ||
* in the {@link io.helidon.common.context.Contexts#context()} current context}, | ||
* or the {@link io.helidon.common.context.Contexts#globalContext() global context if none is defined}. | ||
* | ||
* @param <T> type of the value stored in context | ||
*/ | ||
public final class ContextSingleton<T> { | ||
/** | ||
* Classifier used to register a context that is to serve as the static context. | ||
*/ | ||
public static final String STATIC_CONTEXT_CLASSIFIER = "helidon-singleton-context"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ContextSingleton.CLASSIFIER is better than ContextSingleton.STATIC_CONTEXT_CLASSIFIER At the very least we should not use "static", so either |
||
|
||
private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); | ||
private final ContextSingletonClassifier<T> classifier; | ||
private final Supplier<T> supplier; | ||
|
||
private ContextSingleton(ContextSingletonClassifier<T> qualifier, Supplier<T> supplier) { | ||
this.classifier = qualifier; | ||
this.supplier = supplier; | ||
} | ||
|
||
/** | ||
* Create a new context value. | ||
* | ||
* @param ownerClass type owning this context singleton | ||
* @param clazz type of the value | ||
* @param <T> type of the value | ||
* @return a new context value with nothing set. | ||
*/ | ||
public static <T> ContextSingleton<T> create(Class<?> ownerClass, Class<T> clazz) { | ||
Objects.requireNonNull(ownerClass); | ||
Objects.requireNonNull(clazz); | ||
|
||
return new ContextSingleton<>(new ContextSingletonClassifier<>(ownerClass, clazz), null); | ||
} | ||
|
||
/** | ||
* Create a new context value with a supplier of instances for each context. | ||
* | ||
* @param ownerClass type owning this context singleton | ||
* @param clazz type of the value | ||
* @param value value supplier | ||
* @param <T> type of the value | ||
* @return a new context value | ||
*/ | ||
public static <T> ContextSingleton<T> create(Class<?> ownerClass, Class<T> clazz, Supplier<T> value) { | ||
Objects.requireNonNull(ownerClass); | ||
Objects.requireNonNull(clazz); | ||
Objects.requireNonNull(value); | ||
|
||
return new ContextSingleton<>(new ContextSingletonClassifier<>(ownerClass, clazz), value); | ||
} | ||
|
||
/** | ||
* Set the value in current context. | ||
* | ||
* @param value value to use | ||
*/ | ||
public void set(T value) { | ||
lock.writeLock().lock(); | ||
try { | ||
context().register(classifier, value); | ||
} finally { | ||
lock.writeLock().unlock(); | ||
} | ||
} | ||
|
||
/** | ||
* Get the current value in this context, or call the provided supplier to get the value and register | ||
* it, if not yet registered. | ||
* | ||
* @return current value (or the new one obtained from supplier) | ||
* @throws NoSuchElementException in case there is no value, and no supplier | ||
* @see #set(Object) | ||
*/ | ||
public T get() throws NoSuchElementException { | ||
return get(this.supplier); | ||
} | ||
|
||
/** | ||
* Get the current value in this context, or call the provided supplier to get the value and register | ||
* it, if not yet registered. | ||
* | ||
* @param supplier supplier to call if the current value is not present | ||
* @return current value (or the new one obtained from supplier) | ||
* @throws NoSuchElementException in case there is no value, and the supplier returned {@code null} | ||
* @see #set(Object) | ||
*/ | ||
public T get(Supplier<T> supplier) { | ||
var current = current(); | ||
if (current.isPresent()) { | ||
return current.get(); | ||
} | ||
lock.writeLock().lock(); | ||
try { | ||
current = current(); | ||
if (current.isPresent()) { | ||
return current.get(); | ||
} | ||
if (supplier != null) { | ||
var value = supplier.get(); | ||
set(value); | ||
return value; | ||
} | ||
throw new NoSuchElementException("There is no value available in the current context, " | ||
+ "and supplier was not provided when creating this instance for: " | ||
+ " " + classifier); | ||
} finally { | ||
lock.writeLock().unlock(); | ||
} | ||
} | ||
|
||
/** | ||
* Current value as an {@link java.util.Optional}. | ||
* | ||
* @return current value, or empty if not configured | ||
*/ | ||
public Optional<T> value() { | ||
return current(); | ||
} | ||
|
||
/** | ||
* Whether there is a value in current context. | ||
* | ||
* @return {@code true} if there is a value | ||
*/ | ||
public boolean isPresent() { | ||
return current().isPresent(); | ||
} | ||
|
||
private static Context context() { | ||
Context globalContext = Contexts.globalContext(); | ||
|
||
// this is the context we expect to get (and set global instances) | ||
return Contexts.context() | ||
.orElse(globalContext) | ||
.get(STATIC_CONTEXT_CLASSIFIER, Context.class) | ||
.orElse(globalContext); | ||
} | ||
|
||
private Optional<T> current() { | ||
lock.readLock().lock(); | ||
try { | ||
return context().get(classifier, classifier.valueType()); | ||
} finally { | ||
lock.readLock().unlock(); | ||
} | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
private record ContextSingletonClassifier<T>(Class<ContextSingleton> contextSingleton, | ||
Class<?> ownerClass, | ||
Class<T> valueType) { | ||
private ContextSingletonClassifier(Class<?> ownerClass, Class<T> valueType) { | ||
this(ContextSingleton.class, ownerClass, valueType); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
common/context/src/main/java/io/helidon/common/context/Contexts.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
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
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.