Skip to content

Commit

Permalink
Merge pull request #2 from mgeiss/develop
Browse files Browse the repository at this point in the history
created DelegatingTenantContextExecutor to assure tenant inheritance …
  • Loading branch information
crain authored Apr 19, 2017
2 parents 723d58a + 2e4f5c3 commit 0b71ce2
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 2 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
buildscript {
repositories {
jcenter();
jcenter()
}
dependencies {
classpath 'io.spring.gradle:dependency-management-plugin:0.6.0.RELEASE'
Expand All @@ -15,6 +15,7 @@ group 'io.mifos.core'
version '0.1.0-BUILD-SNAPSHOT'

ext.versions = [
frameworklang : '0.1.0-BUILD-SNAPSHOT',
springcontext : '4.3.3.RELEASE',
springsecurity : '4.2.2.RELEASE',
springboot : '1.4.1.RELEASE',
Expand All @@ -40,6 +41,7 @@ repositories {

dependencies {
compile(
[group: 'io.mifos.core', name: 'lang', version: versions.frameworklang],
[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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.mifos.core.async.config;

import io.mifos.core.async.core.DelegatingTenantContextExecutor;
import io.mifos.core.async.util.AsyncConstants;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler;
Expand Down Expand Up @@ -53,7 +54,8 @@ public Executor getAsyncExecutor() {
executor.setThreadNamePrefix(
this.env.getProperty(AsyncConstants.THREAD_NAME_PROP, AsyncConstants.THREAD_NAME_DEFAULT));
executor.initialize();
return new DelegatingSecurityContextAsyncTaskExecutor(executor);

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

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.mifos.core.async.core;

import io.mifos.core.lang.TenantContextHolder;

import java.util.concurrent.Callable;

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

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

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

@Override
public V call() throws Exception {
try {
TenantContextHolder.clear();
TenantContextHolder.setIdentifier(this.tenantIdentifier);
return this.delegate.call();
} finally {
TenantContextHolder.clear();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package io.mifos.core.async.core;

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 {

private final DelegatingSecurityContextAsyncTaskExecutor delegate;

public DelegatingTenantContextExecutor(final DelegatingSecurityContextAsyncTaskExecutor delegate) {
super();
this.delegate = delegate;
}

@Override
public void execute(final Runnable task, final long startTimeout) {
final Runnable taskWrapper = this.wrap(task);
this.delegate.execute(taskWrapper, startTimeout);
}

@Override
public Future<?> submit(final Runnable task) {
final Runnable taskWrapper = this.wrap(task);
return this.delegate.submit(taskWrapper);
}

@Override
public <T> Future<T> submit(final Callable<T> task) {
final Callable<T> taskWrapper = this.wrap(task);
return this.delegate.submit(taskWrapper);
}

@Override
public void execute(final Runnable task) {
final Runnable taskWrapper = this.wrap(task);
this.delegate.execute(taskWrapper);
}

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

private <T> Callable<T> wrap(final Callable<T> task) {
return new DelegatingTenantContextCallable<>(task, TenantContextHolder.checkedGetIdentifier());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.mifos.core.async.core;

import io.mifos.core.lang.TenantContextHolder;

public class DelegatingTenantContextRunnable implements Runnable {

private final Runnable delegate;
private final String tenantIdentifier;

DelegatingTenantContextRunnable(final Runnable delegate, final String tenantIdentifier) {
super();
this.delegate = delegate;
this.tenantIdentifier = tenantIdentifier;
}

@Override
public void run() {
try {
TenantContextHolder.clear();
TenantContextHolder.setIdentifier(this.tenantIdentifier);
this.delegate.run();
} finally {
TenantContextHolder.clear();
}
}
}

0 comments on commit 0b71ce2

Please sign in to comment.