fineract-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From my...@apache.org
Subject [fineract-cn-async] 10/13: added UserContext to context delegating
Date Mon, 22 Jan 2018 15:09:38 GMT
This is an automated email from the ASF dual-hosted git repository.

myrle pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/fineract-cn-async.git

commit 1b95bd735447b208fb68f37a8b336ecaa1c96092
Author: mgeiss <mgeiss@mifos.org>
AuthorDate: Wed Jun 14 11:32:29 2017 +0200

    added UserContext to context delegating
---
 build.gradle                                       |  2 +
 .../config/AsyncProcessingJavaConfiguration.java   |  4 +-
 ...allable.java => DelegatingContextCallable.java} | 17 +++++++--
 ...xecutor.java => DelegatingContextExecutor.java} | 21 +++++++----
 .../core/async/core/DelegatingContextRunnable.java | 44 ++++++++++++++++++++++
 .../core/DelegatingTenantContextRunnable.java      | 33 ----------------
 6 files changed, 75 insertions(+), 46 deletions(-)

diff --git a/build.gradle b/build.gradle
index 6a14e06..deb397d 100644
--- a/build.gradle
+++ b/build.gradle
@@ -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]
diff --git a/src/main/java/io/mifos/core/async/config/AsyncProcessingJavaConfiguration.java
b/src/main/java/io/mifos/core/async/config/AsyncProcessingJavaConfiguration.java
index 272f37f..b15e4cd 100644
--- a/src/main/java/io/mifos/core/async/config/AsyncProcessingJavaConfiguration.java
+++ b/src/main/java/io/mifos/core/async/config/AsyncProcessingJavaConfiguration.java
@@ -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 class AsyncProcessingJavaConfiguration
         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
diff --git a/src/main/java/io/mifos/core/async/core/DelegatingTenantContextCallable.java b/src/main/java/io/mifos/core/async/core/DelegatingContextCallable.java
similarity index 51%
rename from src/main/java/io/mifos/core/async/core/DelegatingTenantContextCallable.java
rename to src/main/java/io/mifos/core/async/core/DelegatingContextCallable.java
index 139c1ff..2a7d050 100644
--- a/src/main/java/io/mifos/core/async/core/DelegatingTenantContextCallable.java
+++ b/src/main/java/io/mifos/core/async/core/DelegatingContextCallable.java
@@ -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 class DelegatingTenantContextCallable<V> implements Callable<V>
{
       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();
     }
   }
 }
diff --git a/src/main/java/io/mifos/core/async/core/DelegatingTenantContextExecutor.java b/src/main/java/io/mifos/core/async/core/DelegatingContextExecutor.java
similarity index 59%
rename from src/main/java/io/mifos/core/async/core/DelegatingTenantContextExecutor.java
rename to src/main/java/io/mifos/core/async/core/DelegatingContextExecutor.java
index 1061822..316fc1b 100644
--- a/src/main/java/io/mifos/core/async/core/DelegatingTenantContextExecutor.java
+++ b/src/main/java/io/mifos/core/async/core/DelegatingContextExecutor.java
@@ -1,5 +1,6 @@
 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;
@@ -7,11 +8,11 @@ import org.springframework.security.task.DelegatingSecurityContextAsyncTaskExecu
 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 class DelegatingTenantContextExecutor implements AsyncTaskExecutor
{
   }
 
   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);
   }
 }
diff --git a/src/main/java/io/mifos/core/async/core/DelegatingContextRunnable.java b/src/main/java/io/mifos/core/async/core/DelegatingContextRunnable.java
new file mode 100644
index 0000000..51d0e34
--- /dev/null
+++ b/src/main/java/io/mifos/core/async/core/DelegatingContextRunnable.java
@@ -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();;
+    }
+  }
+}
diff --git a/src/main/java/io/mifos/core/async/core/DelegatingTenantContextRunnable.java b/src/main/java/io/mifos/core/async/core/DelegatingTenantContextRunnable.java
deleted file mode 100644
index 37d8ba6..0000000
--- a/src/main/java/io/mifos/core/async/core/DelegatingTenantContextRunnable.java
+++ /dev/null
@@ -1,33 +0,0 @@
-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) {
-    this.delegate = delegate;
-    this.tenantIdentifier = null;
-  }
-
-  DelegatingTenantContextRunnable(final Runnable delegate, final String tenantIdentifier)
{
-    super();
-    this.delegate = delegate;
-    this.tenantIdentifier = tenantIdentifier;
-  }
-
-  @Override
-  public void run() {
-    try {
-      TenantContextHolder.clear();
-      if(this.tenantIdentifier != null) {
-        TenantContextHolder.setIdentifier(this.tenantIdentifier);
-      }
-      this.delegate.run();
-    } finally {
-      TenantContextHolder.clear();
-    }
-  }
-}

-- 
To stop receiving notification emails like this one, please contact
myrle@apache.org.

Mime
View raw message