package com.ulisesbocchio.jasyptspringboot.caching;

import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyFilter;
import com.ulisesbocchio.jasyptspringboot.EncryptablePropertyResolver;
import com.ulisesbocchio.jasyptspringboot.EncryptablePropertySource;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.core.env.PropertySource;
import org.springframework.util.Assert;

public class CachingDelegateEncryptablePropertySource<T> extends PropertySource<T> implements EncryptablePropertySource<T> {
    private final PropertySource<T> delegate;
    private final EncryptablePropertyResolver resolver;
    private final EncryptablePropertyFilter filter;
    private final ConcurrentMapCache cache;

    public CachingDelegateEncryptablePropertySource(PropertySource<T> delegate, EncryptablePropertyResolver resolver, EncryptablePropertyFilter filter) {
        super(delegate.getName(), delegate.getSource());
        Assert.notNull(delegate, "PropertySource delegate cannot be null");
        Assert.notNull(resolver, "EncryptablePropertyResolver cannot be null");
        Assert.notNull(filter, "EncryptablePropertyFilter cannot be null");
        this.delegate = delegate;
        this.resolver = resolver;
        this.filter = filter;
        this.cache = new ConcurrentMapCache("encryptablePropertiesCache");
    }

    @Override
    public PropertySource<T> getDelegate() {
        return delegate;
    }

    @Override
    public Object getProperty(String name) {
        return cache.get(name, () -> getProperty(resolver, filter, delegate, name));
    }

    @Override
    public void refresh() {
        cache.clear();
    }
}

@@ -266,63 +266,47 @@
 ort 
-org.springframework.cache.concurrent.ConcurrentMapCache
+java.util.HashMap;%0Aimport java.util.Map
 ;%0Aim
@@ -691,32 +691,27 @@
 ate 
-final ConcurrentMapCache
+Map%3CString, Object%3E
  cac
@@ -1285,55 +1285,18 @@
 new 
-ConcurrentMapCache(%22encryptablePropertiesCache%22
+HashMap%3C%3E(
 );%0A 
@@ -1463,84 +1463,387 @@
     
-return cache.get(name, () -%3E getProperty(resolver, filter, delegate, name));
+// Can be called recursively, so, we cannot use computeIfAbsent.%0A        synchronized (this) %7B%0A            if (cache.containsKey(name)) %7B%0A                return cache.get(name);%0A            %7D else %7B%0A                Object resolved = getProperty(resolver, filter, delegate, name);%0A                cache.put(name, resolved);%0A                return resolved;%0A            %7D%0A        %7D
 %0A   

Switch to HashMap to avoid concurrency issues.