diff --git a/src/main/java/io/mifos/core/async/core/DelegatingContextCallable.java b/src/main/java/io/mifos/core/async/core/DelegatingContextCallable.java index 2a7d050..9af1bf3 100644 --- a/src/main/java/io/mifos/core/async/core/DelegatingContextCallable.java +++ b/src/main/java/io/mifos/core/async/core/DelegatingContextCallable.java @@ -4,39 +4,33 @@ import io.mifos.core.api.util.UserContextHolder; import io.mifos.core.lang.TenantContextHolder; +import java.util.Optional; import java.util.concurrent.Callable; +@SuppressWarnings("OptionalUsedAsFieldOrParameterType") public class DelegatingContextCallable implements Callable { private final Callable delegate; - private final String tenantIdentifier; - private final UserContext userContext; - - DelegatingContextCallable(Callable delegate) { - this.delegate = delegate; - this.tenantIdentifier = null; - this.userContext = null; - } + private final Optional optionalTenantIdentifier; + private final Optional optionalUserContext; DelegatingContextCallable(final Callable delegate, final String tenantIdentifier, final UserContext userContext) { super(); this.delegate = delegate; - this.tenantIdentifier = tenantIdentifier; - this.userContext = userContext; + this.optionalTenantIdentifier = Optional.ofNullable(tenantIdentifier); + this.optionalUserContext = Optional.ofNullable(userContext); } @Override public V call() throws Exception { try { TenantContextHolder.clear(); - if(this.tenantIdentifier != null) { - TenantContextHolder.setIdentifier(this.tenantIdentifier); - } + optionalTenantIdentifier.ifPresent(TenantContextHolder::setIdentifier); + UserContextHolder.clear(); - if (this.userContext != null) { - UserContextHolder.setUserContext(this.userContext); - } + optionalUserContext.ifPresent(UserContextHolder::setUserContext); + return this.delegate.call(); } finally { TenantContextHolder.clear(); diff --git a/src/main/java/io/mifos/core/async/core/DelegatingContextExecutor.java b/src/main/java/io/mifos/core/async/core/DelegatingContextExecutor.java index 316fc1b..2ea9f77 100644 --- a/src/main/java/io/mifos/core/async/core/DelegatingContextExecutor.java +++ b/src/main/java/io/mifos/core/async/core/DelegatingContextExecutor.java @@ -1,5 +1,6 @@ package io.mifos.core.async.core; +import io.mifos.core.api.util.UserContext; import io.mifos.core.api.util.UserContextHolder; import io.mifos.core.lang.TenantContextHolder; import org.springframework.core.task.AsyncTaskExecutor; @@ -42,20 +43,14 @@ public void execute(final Runnable task) { } private Runnable wrap(final Runnable task) { - if(TenantContextHolder.identifier().isPresent() - || UserContextHolder.getUserContext().isPresent()) { - return new DelegatingContextRunnable(task, TenantContextHolder.checkedGetIdentifier(), - UserContextHolder.getUserContext().get()); - } - return new DelegatingContextRunnable(task); + final String tenantIdentifier = TenantContextHolder.identifier().orElse(null); + final UserContext userContext = UserContextHolder.getUserContext().orElse(null); + return new DelegatingContextRunnable(task, tenantIdentifier, userContext); } private Callable wrap(final Callable task) { - if(TenantContextHolder.identifier().isPresent() - || UserContextHolder.getUserContext().isPresent()) { - return new DelegatingContextCallable<>(task, TenantContextHolder.checkedGetIdentifier(), - UserContextHolder.getUserContext().get()); - } - return new DelegatingContextCallable<>(task); + final String tenantIdentifier = TenantContextHolder.identifier().orElse(null); + final UserContext userContext = UserContextHolder.getUserContext().orElse(null); + return new DelegatingContextCallable<>(task, tenantIdentifier, userContext); } } diff --git a/src/main/java/io/mifos/core/async/core/DelegatingContextRunnable.java b/src/main/java/io/mifos/core/async/core/DelegatingContextRunnable.java index 51d0e34..ae5b620 100644 --- a/src/main/java/io/mifos/core/async/core/DelegatingContextRunnable.java +++ b/src/main/java/io/mifos/core/async/core/DelegatingContextRunnable.java @@ -4,41 +4,36 @@ import io.mifos.core.api.util.UserContextHolder; import io.mifos.core.lang.TenantContextHolder; +import java.util.Optional; + +@SuppressWarnings("OptionalUsedAsFieldOrParameterType") public class DelegatingContextRunnable implements Runnable { private final Runnable delegate; - private final String tenantIdentifier; - private final UserContext userContext; - - DelegatingContextRunnable(final Runnable delegate) { - this.delegate = delegate; - this.tenantIdentifier = null; - this.userContext = null; - } + private final Optional optionalTenantIdentifier; + private final Optional optionalUserContext; DelegatingContextRunnable(final Runnable delegate, final String tenantIdentifier, final UserContext userContext) { super(); this.delegate = delegate; - this.tenantIdentifier = tenantIdentifier; - this.userContext = userContext; + this.optionalTenantIdentifier = Optional.ofNullable(tenantIdentifier); + this.optionalUserContext = Optional.ofNullable(userContext); } @Override public void run() { try { TenantContextHolder.clear(); - if(this.tenantIdentifier != null) { - TenantContextHolder.setIdentifier(this.tenantIdentifier); - } + optionalTenantIdentifier.ifPresent(TenantContextHolder::setIdentifier); + UserContextHolder.clear(); - if (this.userContext != null) { - UserContextHolder.setUserContext(this.userContext); - } + optionalUserContext.ifPresent(UserContextHolder::setUserContext); + this.delegate.run(); } finally { TenantContextHolder.clear(); - UserContextHolder.clear();; + UserContextHolder.clear(); } } }