Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Merge pull request #2 from mgeiss/develop
created DelegatingTenantContextExecutor to assure tenant inheritance …
- Loading branch information
Showing
5 changed files
with
110 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -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(); | ||
} | ||
} | ||
} |