Skip to content
Permalink
Browse files
Add null checks for tenant identifier
  • Loading branch information
crain committed Apr 19, 2017
1 parent 0b71ce2 commit 25dd7affcb0fb36f7fefa8420c48e7eba83b57af
Showing 3 changed files with 24 additions and 4 deletions.
@@ -9,6 +9,11 @@
private final Callable<V> delegate;
private final String tenantIdentifier;

DelegatingTenantContextCallable(Callable<V> delegate) {
this.delegate = delegate;
this.tenantIdentifier = null;
}

DelegatingTenantContextCallable(final Callable<V> delegate, final String tenantIdentifier) {
super();
this.delegate = delegate;
@@ -19,7 +24,9 @@
public V call() throws Exception {
try {
TenantContextHolder.clear();
TenantContextHolder.setIdentifier(this.tenantIdentifier);
if(this.tenantIdentifier != null) {
TenantContextHolder.setIdentifier(this.tenantIdentifier);
}
return this.delegate.call();
} finally {
TenantContextHolder.clear();
@@ -41,10 +41,16 @@ public void execute(final Runnable task) {
}

private Runnable wrap(final Runnable task) {
return new DelegatingTenantContextRunnable(task, TenantContextHolder.checkedGetIdentifier());
if(TenantContextHolder.identifier().isPresent()) {
return new DelegatingTenantContextRunnable(task, TenantContextHolder.checkedGetIdentifier());
}
return new DelegatingTenantContextRunnable(task);
}

private <T> Callable<T> wrap(final Callable<T> task) {
return new DelegatingTenantContextCallable<>(task, TenantContextHolder.checkedGetIdentifier());
if(TenantContextHolder.identifier().isPresent()) {
return new DelegatingTenantContextCallable<>(task, TenantContextHolder.checkedGetIdentifier());
}
return new DelegatingTenantContextCallable<>(task);
}
}
@@ -7,6 +7,11 @@ public class DelegatingTenantContextRunnable implements Runnable {
private final Runnable delegate;
private final String tenantIdentifier;

DelegatingTenantContextRunnable(final Runnable delegate) {
this.delegate = delegate;
this.tenantIdentifier = null;
}

DelegatingTenantContextRunnable(final Runnable delegate, final String tenantIdentifier) {
super();
this.delegate = delegate;
@@ -17,7 +22,9 @@ public class DelegatingTenantContextRunnable implements Runnable {
public void run() {
try {
TenantContextHolder.clear();
TenantContextHolder.setIdentifier(this.tenantIdentifier);
if(this.tenantIdentifier != null) {
TenantContextHolder.setIdentifier(this.tenantIdentifier);
}
this.delegate.run();
} finally {
TenantContextHolder.clear();

0 comments on commit 25dd7af

Please sign in to comment.