Skip to content

Commit

Permalink
Remove GlobalServiceRegistry context qualifier constants (#9827)
Browse files Browse the repository at this point in the history
Remove the following (unreleased) constants to minimize temporary API surface of 4.2.0
- GlobalServiceRegistry.STATIC_CONTEXT_CLASSIFIER
- GlobalServiceRegistry.CONTEXT_CLASSIFIER
  • Loading branch information
romain-grecourt authored Feb 26, 2025
1 parent 2de63a8 commit 43ce223
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import io.helidon.microprofile.testing.HelidonTestInfo.MethodInfo;
import io.helidon.microprofile.testing.HelidonTestScope;
import io.helidon.microprofile.testing.Proxies;
import io.helidon.service.registry.GlobalServiceRegistry;
import io.helidon.service.registry.ServiceRegistry;
import io.helidon.service.registry.ServiceRegistryManager;

Expand Down Expand Up @@ -302,10 +301,10 @@ private Context staticContext(Class<?> testClass) {
.build();

// self-register, so this context is used even if the current context is some child of it
context.register(GlobalServiceRegistry.STATIC_CONTEXT_CLASSIFIER, context);
context.register("helidon-registry-static-context", context);

// supply registry
context.supply(GlobalServiceRegistry.CONTEXT_QUALIFIER, ServiceRegistry.class,
context.supply("helidon-registry", ServiceRegistry.class,
() -> ServiceRegistryManager.create().registry());

return context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void firstTest() {
assertThat(INSTANCES.size(), is(1));
assertThat(INSTANCES.iterator().next(),
is(not(System.identityHashCode(Contexts.globalContext()
.get(GlobalServiceRegistry.CONTEXT_QUALIFIER, ServiceRegistry.class)
.get("helidon-registry", ServiceRegistry.class)
.orElse(null)))));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void firstTest() {
assertThat(INSTANCES.size(), is(1));
assertThat(INSTANCES.iterator().next(),
is(not(System.identityHashCode(Contexts.globalContext()
.get(GlobalServiceRegistry.CONTEXT_QUALIFIER, ServiceRegistry.class)
.get("helidon-registry", ServiceRegistry.class)
.orElse(null)))));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,36 +25,9 @@
import io.helidon.common.context.Contexts;

/**
* Represents an application wide service registry.
* <p>
* The registry instance is shared through {@link io.helidon.common.context.Context}, using the current context
* to obtain a context with classifier {@link #STATIC_CONTEXT_CLASSIFIER}. If such a context is found, the
* registry instance is obtained/stored there. If the context does not exist, the
* {@link io.helidon.common.context.Contexts#globalContext()} is used to store the value.
* <p>
* The first option is designed for testing, to make sure the global registry is only "global" for a single test class.
* The second option is the default for application runtime, where we intend to share the registry as a proper, static
* singleton instance.
* <p>
* Helidon testing libraries support this approach and correctly configure appropriate context for each execution.
* Application wide service registry backed by {@link io.helidon.common.context.Context}.
*/
public final class GlobalServiceRegistry {
/**
* Classifier used to register a context that is to serve as the context that holds the
* global registry instance.
* <p>
* This is to allow testing in parallel, where we need the global registry instance restricted to a single test.
* <p>
* In normal application runtime we use {@link io.helidon.common.context.Contexts#globalContext()}.
*/
public static final Object STATIC_CONTEXT_CLASSIFIER = new Object();

/**
* Classifier used to register the global service registry.
*
* @see #STATIC_CONTEXT_CLASSIFIER
*/
public static final Object CONTEXT_QUALIFIER = new Object();

private static final ReadWriteLock RW_LOCK = new ReentrantReadWriteLock();

Expand Down Expand Up @@ -137,7 +110,7 @@ public static ServiceRegistry registry(Supplier<ServiceRegistry> registrySupplie
public static ServiceRegistry registry(ServiceRegistry newGlobalRegistry) {
RW_LOCK.writeLock().lock();
try {
context().register(CONTEXT_QUALIFIER, newGlobalRegistry);
context().register("helidon-registry", newGlobalRegistry);
} finally {
RW_LOCK.writeLock().unlock();
}
Expand All @@ -150,14 +123,14 @@ private static Context context() {
// this is the context we expect to get (and set global instances)
return Contexts.context()
.orElse(globalContext)
.get(STATIC_CONTEXT_CLASSIFIER, Context.class)
.get("helidon-registry-static-context", Context.class)
.orElse(globalContext);
}

private static Optional<ServiceRegistry> current() {
RW_LOCK.readLock().lock();
try {
return context().get(CONTEXT_QUALIFIER, ServiceRegistry.class);
return context().get("helidon-registry", ServiceRegistry.class);
} finally {
RW_LOCK.readLock().unlock();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ protected void initStaticContext(ExtensionContext.Store store, ExtensionContext
.build();

// self-register, so this context is used even if the current context is some child of it
context.register(GlobalServiceRegistry.STATIC_CONTEXT_CLASSIFIER, context);
context.register("helidon-registry-static-context", context);

// supply registry
context.supply(GlobalServiceRegistry.CONTEXT_QUALIFIER, ServiceRegistry.class, () -> {
context.supply("helidon-registry", ServiceRegistry.class, () -> {
var manager = ServiceRegistryManager.create();
var registry = manager.registry();
store.put(ServiceRegistryManager.class, (CloseableResource) manager::shutdown);
Expand Down

0 comments on commit 43ce223

Please sign in to comment.