-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Propagate SecurityContext into @transactional methods. (#1979)
Closes #1944.
- Loading branch information
1 parent
8360fe3
commit d75b4b6
Showing
6 changed files
with
147 additions
and
7 deletions.
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
41 changes: 41 additions & 0 deletions
41
src/main/java/org/springframework/data/couchbase/transaction/CouchbaseResourceOwner.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,41 @@ | ||
package org.springframework.data.couchbase.transaction; | ||
|
||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.Optional; | ||
|
||
import com.couchbase.client.core.annotation.Stability.Internal; | ||
|
||
@Internal | ||
public class CouchbaseResourceOwner { | ||
private static final ThreadLocal<CouchbaseResourceHolder> marker = new ThreadLocal(); | ||
|
||
public CouchbaseResourceOwner() {} | ||
|
||
public static void set(CouchbaseResourceHolder toInject) { | ||
if (marker.get() != null) { | ||
throw new IllegalStateException( | ||
"Trying to set resource holder when already inside a transaction - likely an internal bug, please report it"); | ||
} else { | ||
marker.set(toInject); | ||
} | ||
} | ||
|
||
public static void clear() { | ||
marker.remove(); | ||
} | ||
|
||
public static Mono<Optional<CouchbaseResourceHolder>> get() { | ||
return Mono.deferContextual((ctx) -> { | ||
CouchbaseResourceHolder fromThreadLocal = marker.get(); | ||
CouchbaseResourceHolder fromReactive = ctx.hasKey(CouchbaseResourceHolder.class) | ||
? ctx.get(CouchbaseResourceHolder.class) | ||
: null; | ||
if (fromThreadLocal != null) { | ||
return Mono.just(Optional.of(fromThreadLocal)); | ||
} else { | ||
return fromReactive != null ? Mono.just(Optional.of(fromReactive)) : Mono.just(Optional.empty()); | ||
} | ||
}); | ||
} | ||
} |
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