package org.bremersee.security.authentication;

import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.EventListener;
import org.springframework.util.ClassUtils;

@ConditionalOnWebApplication(type = Type.ANY)
@Configuration
@ConditionalOnClass({
    CacheManager.class
})
@Slf4j
public class AccessTokenCacheAutoConfiguration {

  /**
   * Init.
   */
  @EventListener(ApplicationReadyEvent.class)
  public void init() {
    log.info("\n"
            + "*********************************************************************************\n"
            + "* {}\n"
            + "*********************************************************************************",
        ClassUtils.getUserClass(getClass()).getSimpleName());
  }

  /**
   * Creates access token cache.
   *
   * @param cacheManagerProvider the cache manager provider
   * @return the access token cache
   */
  @ConditionalOnMissingBean
  @Bean
  public AccessTokenCache accessTokenCache(ObjectProvider<CacheManager> cacheManagerProvider) {
    return Optional.ofNullable(cacheManagerProvider.getIfAvailable())
        .map(cacheManager -> {
          if (cacheManager.getCacheNames().contains(AccessTokenCache.CACHE_NAME)) {
            Cache cache = cacheManager.getCache(AccessTokenCache.CACHE_NAME);
            log.info("Creating access token cache with external cache {} from cache manager {}",
                cache, cacheManager);
            return cache;
          } else {
            log.info("Creating access token cache: a cache manager was found ({}), but it doesn't "
                    + "contain a cache with name {}. Using internal cache.",
                cacheManager, AccessTokenCache.CACHE_NAME);
            return null;
          }
        })
        .map(AccessTokenCacheImpl::new)
        .orElseGet(AccessTokenCacheImpl::new);
  }

}

@@ -41,16 +41,39 @@
 ation;%0A%0A
+import java.util.List;%0A
 import j
@@ -1518,24 +1518,17 @@
 eManager
-Provider
+s
  the cac
@@ -1541,17 +1541,9 @@
 ager
- provider
+s
 %0A   
@@ -1675,16 +1675,21 @@
 rovider%3C
+List%3C
 CacheMan
@@ -1693,16 +1693,17 @@
 Manager%3E
+%3E
  cacheMa
@@ -1707,24 +1707,17 @@
 eManager
-Provider
+s
 ) %7B%0A    
@@ -1755,24 +1755,17 @@
 eManager
-Provider
+s
 .getIfAv
@@ -1788,50 +1788,78 @@
    .
-map(cacheM
+flatMap(m
 anager
+s
  -%3E 
-%7B%0A          if (cacheM
+managers.stream()%0A            .filter(manager -%3E m
 anag
@@ -1915,21 +1915,74 @@
 E_NAME))
- %7B
 %0A  
+          .findFirst())%0A        .map(cacheManager -%3E %7B%0A
         
@@ -2051,34 +2051,32 @@
 AME);%0A          
- 
- 
 log.info(%22Creati
@@ -2154,26 +2154,24 @@
             
-  
 cache, cache
@@ -2186,26 +2186,24 @@
 ;%0A          
-  
 return cache
@@ -2208,301 +2208,8 @@
 he;%0A
-          %7D else %7B%0A            log.info(%22Creating access token cache: a cache manager was found (%7B%7D), but it doesn't %22%0A                    + %22contain a cache with name %7B%7D. Using internal cache.%22,%0A                cacheManager, AccessTokenCache.CACHE_NAME);%0A            return null;%0A          %7D%0A
     

Version 2.1.3-SNAPSHOT