This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git

commit 11427c11b9be4fae8ccc641882dbf432ade3e017
Author: Guillaume Nodet <gno...@gmail.com>
AuthorDate: Wed May 15 18:31:31 2024 +0200

    Do not synchronize on non final fields
---
 .../processor/idempotent/FileIdempotentRepository.java    | 15 ++++++++-------
 .../processor/idempotent/MemoryIdempotentRepository.java  |  9 +++++----
 .../support/processor/state/FileStateRepository.java      | 10 ++++++----
 3 files changed, 19 insertions(+), 15 deletions(-)

diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/processor/idempotent/FileIdempotentRepository.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/processor/idempotent/FileIdempotentRepository.java
index 30d0d203df8..818a8d95c58 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/processor/idempotent/FileIdempotentRepository.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/processor/idempotent/FileIdempotentRepository.java
@@ -64,6 +64,7 @@ public class FileIdempotentRepository extends ServiceSupport 
implements Idempote
 
     private final AtomicBoolean init = new AtomicBoolean();
     private Map<String, Object> cache;
+    private final Object cacheAndStoreLock = new Object();
 
     @Metadata(description = "The maximum size of the 1st-level in-memory 
cache", defaultValue = "1000")
     private int cacheSize;
@@ -80,9 +81,9 @@ public class FileIdempotentRepository extends ServiceSupport 
implements Idempote
     public FileIdempotentRepository() {
     }
 
-    public FileIdempotentRepository(File fileStore, Map<String, Object> set) {
+    public FileIdempotentRepository(File fileStore, Map<String, Object> cache) 
{
         this.fileStore = fileStore;
-        this.cache = set;
+        this.cache = cache;
     }
 
     /**
@@ -133,7 +134,7 @@ public class FileIdempotentRepository extends 
ServiceSupport implements Idempote
     @Override
     @ManagedOperation(description = "Adds the key to the store")
     public boolean add(String key) {
-        synchronized (cache) {
+        synchronized (cacheAndStoreLock) {
             if (cache.containsKey(key)) {
                 return false;
             } else {
@@ -165,7 +166,7 @@ public class FileIdempotentRepository extends 
ServiceSupport implements Idempote
     @Override
     @ManagedOperation(description = "Does the store contain the given key")
     public boolean contains(String key) {
-        synchronized (cache) {
+        synchronized (cacheAndStoreLock) {
             // check 1st-level first and then fallback to check the actual file
             return cache.containsKey(key) || containsStore(key);
         }
@@ -175,7 +176,7 @@ public class FileIdempotentRepository extends 
ServiceSupport implements Idempote
     @ManagedOperation(description = "Remove the key from the store")
     public boolean remove(String key) {
         boolean answer;
-        synchronized (cache) {
+        synchronized (cacheAndStoreLock) {
             answer = cache.remove(key) != null;
             // remove from file cache also
             removeFromStore(key);
@@ -192,7 +193,7 @@ public class FileIdempotentRepository extends 
ServiceSupport implements Idempote
     @Override
     @ManagedOperation(description = "Clear the store (danger this removes all 
entries)")
     public void clear() {
-        synchronized (cache) {
+        synchronized (cacheAndStoreLock) {
             cache.clear();
             if (cache instanceof LRUCache<String, Object> lruCache) {
                 lruCache.cleanUp();
@@ -279,7 +280,7 @@ public class FileIdempotentRepository extends 
ServiceSupport implements Idempote
      */
     @ManagedOperation(description = "Reset and reloads the file store")
     public synchronized void reset() throws IOException {
-        synchronized (cache) {
+        synchronized (cacheAndStoreLock) {
             // run the cleanup task first
             if (cache instanceof LRUCache<String, Object> lruCache) {
                 lruCache.cleanUp();
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/processor/idempotent/MemoryIdempotentRepository.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/processor/idempotent/MemoryIdempotentRepository.java
index cc1f9522619..e39c08d38f3 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/processor/idempotent/MemoryIdempotentRepository.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/processor/idempotent/MemoryIdempotentRepository.java
@@ -44,6 +44,7 @@ public class MemoryIdempotentRepository extends 
ServiceSupport implements Idempo
     private static final int MAX_CACHE_SIZE = 1000;
 
     private Map<String, Object> cache;
+    private final Object cacheAndStoreLock = new Object();
 
     @Metadata(description = "Maximum elements that can be stored in-memory", 
defaultValue = "" + MAX_CACHE_SIZE)
     private int cacheSize;
@@ -88,7 +89,7 @@ public class MemoryIdempotentRepository extends 
ServiceSupport implements Idempo
     @Override
     @ManagedOperation(description = "Adds the key to the store")
     public boolean add(String key) {
-        synchronized (cache) {
+        synchronized (cacheAndStoreLock) {
             if (cache.containsKey(key)) {
                 return false;
             } else {
@@ -101,7 +102,7 @@ public class MemoryIdempotentRepository extends 
ServiceSupport implements Idempo
     @Override
     @ManagedOperation(description = "Does the store contain the given key")
     public boolean contains(String key) {
-        synchronized (cache) {
+        synchronized (cacheAndStoreLock) {
             return cache.containsKey(key);
         }
     }
@@ -109,7 +110,7 @@ public class MemoryIdempotentRepository extends 
ServiceSupport implements Idempo
     @Override
     @ManagedOperation(description = "Remove the key from the store")
     public boolean remove(String key) {
-        synchronized (cache) {
+        synchronized (cacheAndStoreLock) {
             return cache.remove(key) != null;
         }
     }
@@ -123,7 +124,7 @@ public class MemoryIdempotentRepository extends 
ServiceSupport implements Idempo
     @Override
     @ManagedOperation(description = "Clear the store")
     public void clear() {
-        synchronized (cache) {
+        synchronized (cacheAndStoreLock) {
             cache.clear();
         }
     }
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/processor/state/FileStateRepository.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/processor/state/FileStateRepository.java
index 1c4aa0f5148..d6b372380e9 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/processor/state/FileStateRepository.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/processor/state/FileStateRepository.java
@@ -21,6 +21,7 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Objects;
 import java.util.concurrent.atomic.AtomicBoolean;
 
 import org.apache.camel.RuntimeCamelException;
@@ -48,6 +49,7 @@ public class FileStateRepository extends ServiceSupport 
implements StateReposito
     private static final String KEY_VALUE_DELIMITER = "=";
     private final AtomicBoolean init = new AtomicBoolean();
     private Map<String, String> cache;
+    private final Object cacheAndStoreLock = new Object();
     private File fileStore;
     private long maxFileStoreSize = 1024 * 1000L; // 1mb store file
 
@@ -106,7 +108,7 @@ public class FileStateRepository extends ServiceSupport 
implements StateReposito
         if (value.contains(STORE_DELIMITER)) {
             throw new IllegalArgumentException("Value " + value + " contains 
illegal character: <newline>");
         }
-        synchronized (cache) {
+        synchronized (cacheAndStoreLock) {
             cache.put(key, value);
             if (fileStore.length() < maxFileStoreSize) {
                 // just append to store
@@ -121,7 +123,7 @@ public class FileStateRepository extends ServiceSupport 
implements StateReposito
     @Override
     @ManagedOperation(description = "Gets the value of the given key from 
store")
     public String getState(String key) {
-        synchronized (cache) {
+        synchronized (cacheAndStoreLock) {
             return cache.get(key);
         }
     }
@@ -131,7 +133,7 @@ public class FileStateRepository extends ServiceSupport 
implements StateReposito
      */
     @ManagedOperation(description = "Reset and reloads the file store")
     public synchronized void reset() throws IOException {
-        synchronized (cache) {
+        synchronized (cacheAndStoreLock) {
             // trunk and clear, before we reload the store
             trunkStore();
             cache.clear();
@@ -272,7 +274,7 @@ public class FileStateRepository extends ServiceSupport 
implements StateReposito
     }
 
     public void setCache(Map<String, String> cache) {
-        this.cache = cache;
+        this.cache = Objects.requireNonNull(cache, "cache");
     }
 
     @ManagedAttribute(description = "The maximum file size for the file store 
in bytes")

Reply via email to