diff --git a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/context/impl/ContextualDataStorage.java b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/context/impl/ContextualDataStorage.java new file mode 100644 index 000000000..295b92f4d --- /dev/null +++ b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/context/impl/ContextualDataStorage.java @@ -0,0 +1,25 @@ +/* Hibernate, Relational Persistence for Idiomatic Java + * + * SPDX-License-Identifier: Apache-2.0 + * Copyright: Red Hat Inc. and Hibernate Authors + */ +package org.hibernate.reactive.context.impl; + +import java.util.concurrent.ConcurrentMap; + +import io.vertx.core.impl.VertxBuilder; +import io.vertx.core.spi.VertxServiceProvider; +import io.vertx.core.spi.context.storage.ContextLocal; + +/** + * SPI Implementation for {@link ContextLocal} storage. + */ +public class ContextualDataStorage implements VertxServiceProvider { + + @SuppressWarnings("rawtypes") + static ContextLocal CONTEXTUAL_DATA_KEY = ContextLocal.registerLocal( ConcurrentMap.class ); + + @Override + public void init(VertxBuilder vertxBuilder) { + } +} diff --git a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/context/impl/VertxContext.java b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/context/impl/VertxContext.java index fef172e66..937dc3b1b 100644 --- a/hibernate-reactive-core/src/main/java/org/hibernate/reactive/context/impl/VertxContext.java +++ b/hibernate-reactive-core/src/main/java/org/hibernate/reactive/context/impl/VertxContext.java @@ -6,9 +6,12 @@ package org.hibernate.reactive.context.impl; import java.lang.invoke.MethodHandles; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import io.vertx.core.Vertx; import io.vertx.core.impl.ContextInternal; +import io.vertx.core.spi.context.storage.AccessMode; import org.hibernate.reactive.context.Context; import org.hibernate.reactive.logging.impl.Log; @@ -39,7 +42,7 @@ public void put(Key key, T instance) { final ContextInternal context = ContextInternal.current(); if ( context != null ) { if ( trace ) LOG.tracef( "Putting key,value in context: [%1$s, %2$s]", key, instance ); - context.localContextData().put( key, instance ); + VertxContext.contextualDataMap( context ).put( key, instance ); } else { if ( trace ) LOG.tracef( "Context is null for key,value: [%1$s, %2$s]", key, instance ); @@ -51,8 +54,7 @@ public void put(Key key, T instance) { public T get(Key key) { final ContextInternal context = ContextInternal.current(); if ( context != null ) { - @SuppressWarnings("unchecked") - T local = (T) context.localContextData().get( key ); + T local = VertxContext.contextualDataMap( context ).get( key ); if ( trace ) LOG.tracef( "Getting value %2$s from context for key %1$s", key, local ); return local; } @@ -66,7 +68,7 @@ public T get(Key key) { public void remove(Key key) { final ContextInternal context = ContextInternal.current(); if ( context != null ) { - boolean removed = context.localContextData().remove( key ) != null; + boolean removed = contextualDataMap( context ).remove( key ) != null; if ( trace ) LOG.tracef( "Key %s removed from context: %s", key, removed ); } else { @@ -93,4 +95,12 @@ public void execute(Runnable runnable) { } } + @SuppressWarnings({ "unchecked" }) + private static ConcurrentMap, T> contextualDataMap(ContextInternal vertxContext) { + return vertxContext.getLocal( + ContextualDataStorage.CONTEXTUAL_DATA_KEY, + AccessMode.CONCURRENT, + ConcurrentHashMap::new + ); + } } diff --git a/hibernate-reactive-core/src/main/resources/META-INF/services/io.vertx.core.spi.VertxServiceProvider b/hibernate-reactive-core/src/main/resources/META-INF/services/io.vertx.core.spi.VertxServiceProvider new file mode 100644 index 000000000..80ffcb31c --- /dev/null +++ b/hibernate-reactive-core/src/main/resources/META-INF/services/io.vertx.core.spi.VertxServiceProvider @@ -0,0 +1 @@ +org.hibernate.reactive.context.impl.ContextualDataStorage \ No newline at end of file