Skip to content
Permalink
Browse files
Merge pull request #6 from mgeiss/develop
fixed possible NoSuchElementException when retrieving user context
  • Loading branch information
mgeiss committed Jun 16, 2017
2 parents cdcdf2a + 91d3ac1 commit a28cd1ed14b3a70d0a5f3ccfba2710c6623bf0bf
Showing 3 changed files with 29 additions and 45 deletions.
@@ -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();
@@ -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 <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);
}
}
@@ -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 a28cd1e

Please sign in to comment.