Skip to content
Permalink
Browse files
Merge pull request #5 from mgeiss/develop
added UserContext to context delegating
  • Loading branch information
mgeiss committed Jun 14, 2017
2 parents d645c62 + 1b95bd7 commit cdcdf2a927498526779a80b5eeb4b9930581eb84
Showing 6 changed files with 75 additions and 46 deletions.
@@ -16,6 +16,7 @@ version '0.1.0-BUILD-SNAPSHOT'

ext.versions = [
frameworklang : '0.1.0-BUILD-SNAPSHOT',
frameworkapi : '0.1.0-BUILD-SNAPSHOT',
springcontext : '4.3.3.RELEASE',
springsecurity : '4.2.2.RELEASE',
springboot : '1.4.1.RELEASE',
@@ -42,6 +43,7 @@ repositories {
dependencies {
compile(
[group: 'io.mifos.core', name: 'lang', version: versions.frameworklang],
[group: 'io.mifos.core', name: 'api', version: versions.frameworkapi],
[group: 'org.springframework', name: 'spring-context', version: versions.springcontext],
[group: 'org.springframework.security', name: 'spring-security-web', version: versions.springsecurity],
[group: 'com.google.code.findbugs', name: 'jsr305', version: versions.findbugs]
@@ -15,7 +15,7 @@
*/
package io.mifos.core.async.config;

import io.mifos.core.async.core.DelegatingTenantContextExecutor;
import io.mifos.core.async.core.DelegatingContextExecutor;
import io.mifos.core.async.util.AsyncConstants;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
@@ -55,7 +55,7 @@ public Executor getAsyncExecutor() {
this.env.getProperty(AsyncConstants.THREAD_NAME_PROP, AsyncConstants.THREAD_NAME_DEFAULT));
executor.initialize();

return new DelegatingTenantContextExecutor(new DelegatingSecurityContextAsyncTaskExecutor(executor));
return new DelegatingContextExecutor(new DelegatingSecurityContextAsyncTaskExecutor(executor));
}

@Override
@@ -1,23 +1,29 @@
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 java.util.concurrent.Callable;

public class DelegatingTenantContextCallable<V> implements Callable<V> {
public class DelegatingContextCallable<V> implements Callable<V> {

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

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

DelegatingTenantContextCallable(final Callable<V> delegate, final String tenantIdentifier) {
DelegatingContextCallable(final Callable<V> delegate, final String tenantIdentifier,
final UserContext userContext) {
super();
this.delegate = delegate;
this.tenantIdentifier = tenantIdentifier;
this.userContext = userContext;
}

@Override
@@ -27,9 +33,14 @@ public V call() throws Exception {
if(this.tenantIdentifier != null) {
TenantContextHolder.setIdentifier(this.tenantIdentifier);
}
UserContextHolder.clear();
if (this.userContext != null) {
UserContextHolder.setUserContext(this.userContext);
}
return this.delegate.call();
} finally {
TenantContextHolder.clear();
UserContextHolder.clear();
}
}
}
@@ -1,17 +1,18 @@
package io.mifos.core.async.core;

import io.mifos.core.api.util.UserContextHolder;
import io.mifos.core.lang.TenantContextHolder;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.security.task.DelegatingSecurityContextAsyncTaskExecutor;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

public class DelegatingTenantContextExecutor implements AsyncTaskExecutor {
public class DelegatingContextExecutor implements AsyncTaskExecutor {

private final DelegatingSecurityContextAsyncTaskExecutor delegate;

public DelegatingTenantContextExecutor(final DelegatingSecurityContextAsyncTaskExecutor delegate) {
public DelegatingContextExecutor(final DelegatingSecurityContextAsyncTaskExecutor delegate) {
super();
this.delegate = delegate;
}
@@ -41,16 +42,20 @@ public void execute(final Runnable task) {
}

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

private <T> Callable<T> wrap(final Callable<T> task) {
if(TenantContextHolder.identifier().isPresent()) {
return new DelegatingTenantContextCallable<>(task, TenantContextHolder.checkedGetIdentifier());
if(TenantContextHolder.identifier().isPresent()
|| UserContextHolder.getUserContext().isPresent()) {
return new DelegatingContextCallable<>(task, TenantContextHolder.checkedGetIdentifier(),
UserContextHolder.getUserContext().get());
}
return new DelegatingTenantContextCallable<>(task);
return new DelegatingContextCallable<>(task);
}
}
@@ -0,0 +1,44 @@
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;

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;
}

DelegatingContextRunnable(final Runnable delegate, final String tenantIdentifier,
final UserContext userContext) {
super();
this.delegate = delegate;
this.tenantIdentifier = tenantIdentifier;
this.userContext = userContext;
}

@Override
public void run() {
try {
TenantContextHolder.clear();
if(this.tenantIdentifier != null) {
TenantContextHolder.setIdentifier(this.tenantIdentifier);
}
UserContextHolder.clear();
if (this.userContext != null) {
UserContextHolder.setUserContext(this.userContext);
}
this.delegate.run();
} finally {
TenantContextHolder.clear();
UserContextHolder.clear();;
}
}
}

This file was deleted.

0 comments on commit cdcdf2a

Please sign in to comment.