Skip to content

Commit 8a86546

Browse files
committed
Allowing different nested tenants
1 parent 1af97c8 commit 8a86546

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66
}
77

88
group = "com.github.enablingflow"
9-
version = "0.0.25"
9+
version = "0.0.26"
1010

1111
repositories {
1212
mavenCentral()

src/main/java/com/github/enablingflow/springbootdatamongomultitenant/MultiTenantContext.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.github.enablingflow.springbootdatamongomultitenant;
22

3+
import java.util.LinkedList;
34
import java.util.concurrent.Callable;
45

56
public class MultiTenantContext<T> {
67
private static final ThreadLocal<Boolean> asRoot = new ThreadLocal<>();
7-
private static final ThreadLocal<Object> asTenant = new ThreadLocal<>();
8+
private static final ThreadLocal<LinkedList<Object>> asTenant = new ThreadLocal<>();
89
private static final ThreadLocal<Object> tenant = new ThreadLocal<>();
910

1011
public T get() {
@@ -47,32 +48,27 @@ public <T> T performAsRoot(Callable<T> callable) throws Exception {
4748
}
4849

4950
public void performAsTenant(T tenant, ThrowingRunnable runnable) throws Exception {
50-
if (asTenant.get() != null && asTenant.get().equals(tenant)) {
51-
runnable.run();
52-
return;
53-
} else if (asTenant.get() != null && !asTenant.get().equals(tenant)) {
54-
throw new IllegalStateException("Cannot change tenant");
51+
if (asTenant.get() == null) {
52+
asTenant.set(new LinkedList<>());
5553
}
56-
asTenant.set(tenant);
54+
asTenant.get().add(tenant);
5755
try {
5856
runnable.run();
5957
} finally {
60-
asTenant.remove();
58+
asTenant.get().pollLast();
6159
}
6260
}
6361

6462
public <T> T performAsTenant(String tenant, Callable<T> callable) throws Exception {
65-
if (asTenant.get() != null && asTenant.get().equals(tenant)) {
66-
return callable.call();
67-
} else if (asTenant.get() != null && !asTenant.get().equals(tenant)) {
68-
throw new IllegalStateException("Cannot change tenant");
63+
if (asTenant.get() == null) {
64+
asTenant.set(new LinkedList<>());
6965
}
70-
asTenant.set(tenant);
66+
asTenant.get().add(tenant);
7167
T result;
7268
try {
7369
result = callable.call();
7470
} finally {
75-
asTenant.remove();
71+
asTenant.get().pollLast();
7672
}
7773
return result;
7874
}
@@ -82,10 +78,10 @@ public boolean isRoot() {
8278
}
8379

8480
public boolean hasScopedTenant() {
85-
return asTenant.get() != null;
81+
return asTenant.get() != null && !asTenant.get().isEmpty();
8682
}
8783

8884
public T getScopedTenant() {
89-
return (T) asTenant.get();
85+
return (T) asTenant.get().getLast();
9086
}
9187
}

src/test/java/com/github/enablingflow/springbootdatamongomultitenant/MultiTenantTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,11 @@ void itDoesNotLoseTheContextWhenNestingRootExecutions() throws Exception {
202202

203203
@Test
204204
@Order(15)
205-
void itDoesNotLoseTheContextWhenNestingAsTenantExecutions() throws Exception {
205+
void itAllowsToNestTenantContext() throws Exception {
206206
tenants.performAsTenant("tenantA", () -> {
207207
assertThat(tenants.getScopedTenant()).isEqualTo("tenantA");
208-
tenants.performAsTenant("tenantA", () -> {
209-
assertThat(tenants.getScopedTenant()).isEqualTo("tenantA");
208+
tenants.performAsTenant("tenantB", () -> {
209+
assertThat(tenants.getScopedTenant()).isEqualTo("tenantB");
210210
});
211211
assertThat(tenants.getScopedTenant()).isEqualTo("tenantA");
212212
});

0 commit comments

Comments
 (0)