fineract-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From my...@apache.org
Subject [fineract-cn-async] 12/13: fixed possible NoSuchElementException when retrieving user context
Date Mon, 22 Jan 2018 15:09:40 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 91d3ac16be246cc7c6b0e481d2e7d3635954a1be
Author: mgeiss <mgeiss@mifos.org>
AuthorDate: Fri Jun 16 11:28:57 2017 +0200

    fixed possible NoSuchElementException when retrieving user context
---
 .../core/async/core/DelegatingContextCallable.java | 26 ++++++++-----------
 .../core/async/core/DelegatingContextExecutor.java | 19 ++++++--------
 .../core/async/core/DelegatingContextRunnable.java | 29 +++++++++-------------
 3 files changed, 29 insertions(+), 45 deletions(-)

diff --git a/src/main/java/io/mifos/core/async/core/DelegatingContextCallable.java b/src/main/java/io/mifos/core/async/core/DelegatingContextCallable.java
index 2a7d050..9af1bf3 100644
--- a/src/main/java/io/mifos/core/async/core/DelegatingContextCallable.java
+++ b/src/main/java/io/mifos/core/async/core/DelegatingContextCallable.java
@@ -4,39 +4,33 @@ import io.mifos.core.api.util.UserContext;
 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();
diff --git a/src/main/java/io/mifos/core/async/core/DelegatingContextExecutor.java b/src/main/java/io/mifos/core/async/core/DelegatingContextExecutor.java
index 316fc1b..2ea9f77 100644
--- a/src/main/java/io/mifos/core/async/core/DelegatingContextExecutor.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.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 class DelegatingContextExecutor implements AsyncTaskExecutor {
   }
 
   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);
   }
 }
diff --git a/src/main/java/io/mifos/core/async/core/DelegatingContextRunnable.java b/src/main/java/io/mifos/core/async/core/DelegatingContextRunnable.java
index 51d0e34..ae5b620 100644
--- a/src/main/java/io/mifos/core/async/core/DelegatingContextRunnable.java
+++ b/src/main/java/io/mifos/core/async/core/DelegatingContextRunnable.java
@@ -4,41 +4,36 @@ import io.mifos.core.api.util.UserContext;
 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();
     }
   }
 }

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

Mime
View raw message