Skip to content

Commit b4ab6a4

Browse files
tsegismontDavideD
authored andcommitted
[#2176] Create custom Vert.x contextual data storage
Closes #2176 In Vert.x 5, the local context data map is deprecated. Hibernate Reactive can have its own Vert.x contextual data storage (requires creating a io.vertx.core.spi.context.storage.ContextLocal key).
1 parent 742ab0b commit b4ab6a4

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive.context.impl;
7+
8+
import java.util.concurrent.ConcurrentMap;
9+
10+
import io.vertx.core.impl.VertxBuilder;
11+
import io.vertx.core.spi.VertxServiceProvider;
12+
import io.vertx.core.spi.context.storage.ContextLocal;
13+
14+
/**
15+
* SPI Implementation for {@link ContextLocal} storage.
16+
*/
17+
public class ContextualDataStorage implements VertxServiceProvider {
18+
19+
@SuppressWarnings("rawtypes")
20+
static ContextLocal<ConcurrentMap> CONTEXTUAL_DATA_KEY = ContextLocal.registerLocal( ConcurrentMap.class );
21+
22+
@Override
23+
public void init(VertxBuilder vertxBuilder) {
24+
}
25+
}

hibernate-reactive-core/src/main/java/org/hibernate/reactive/context/impl/VertxContext.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
package org.hibernate.reactive.context.impl;
77

88
import java.lang.invoke.MethodHandles;
9+
import java.util.concurrent.ConcurrentHashMap;
10+
import java.util.concurrent.ConcurrentMap;
911

1012
import io.vertx.core.Vertx;
1113
import io.vertx.core.impl.ContextInternal;
14+
import io.vertx.core.spi.context.storage.AccessMode;
1215

1316
import org.hibernate.reactive.context.Context;
1417
import org.hibernate.reactive.logging.impl.Log;
@@ -39,7 +42,7 @@ public <T> void put(Key<T> key, T instance) {
3942
final ContextInternal context = ContextInternal.current();
4043
if ( context != null ) {
4144
if ( trace ) LOG.tracef( "Putting key,value in context: [%1$s, %2$s]", key, instance );
42-
context.localContextData().put( key, instance );
45+
VertxContext.<T>contextualDataMap( context ).put( key, instance );
4346
}
4447
else {
4548
if ( trace ) LOG.tracef( "Context is null for key,value: [%1$s, %2$s]", key, instance );
@@ -51,8 +54,7 @@ public <T> void put(Key<T> key, T instance) {
5154
public <T> T get(Key<T> key) {
5255
final ContextInternal context = ContextInternal.current();
5356
if ( context != null ) {
54-
@SuppressWarnings("unchecked")
55-
T local = (T) context.localContextData().get( key );
57+
T local = VertxContext.<T>contextualDataMap( context ).get( key );
5658
if ( trace ) LOG.tracef( "Getting value %2$s from context for key %1$s", key, local );
5759
return local;
5860
}
@@ -66,7 +68,7 @@ public <T> T get(Key<T> key) {
6668
public void remove(Key<?> key) {
6769
final ContextInternal context = ContextInternal.current();
6870
if ( context != null ) {
69-
boolean removed = context.localContextData().remove( key ) != null;
71+
boolean removed = contextualDataMap( context ).remove( key ) != null;
7072
if ( trace ) LOG.tracef( "Key %s removed from context: %s", key, removed );
7173
}
7274
else {
@@ -93,4 +95,12 @@ public void execute(Runnable runnable) {
9395
}
9496
}
9597

98+
@SuppressWarnings({ "unchecked" })
99+
private static <T> ConcurrentMap<Key<T>, T> contextualDataMap(ContextInternal vertxContext) {
100+
return vertxContext.getLocal(
101+
ContextualDataStorage.CONTEXTUAL_DATA_KEY,
102+
AccessMode.CONCURRENT,
103+
ConcurrentHashMap::new
104+
);
105+
}
96106
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.hibernate.reactive.context.impl.ContextualDataStorage

0 commit comments

Comments
 (0)