Skip to content

Commit

Permalink
fixed possible NoSuchElementException when retrieving user context
Browse files Browse the repository at this point in the history
  • Loading branch information
mgeiss committed Jun 16, 2017
1 parent 1b95bd7 commit 91d3ac1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<V> implements Callable<V> {

private final Callable<V> delegate;
private final String tenantIdentifier;
private final UserContext userContext;

DelegatingContextCallable(Callable<V> delegate) {
this.delegate = delegate;
this.tenantIdentifier = null;
this.userContext = null;
}
private final Optional<String> optionalTenantIdentifier;
private final Optional<UserContext> optionalUserContext;

DelegatingContextCallable(final Callable<V> 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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 <T> Callable<T> wrap(final Callable<T> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> optionalTenantIdentifier;
private final Optional<UserContext> 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();
}
}
}

0 comments on commit 91d3ac1

Please sign in to comment.