• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

TAKETODAY / today-infrastructure / 16688256371

02 Aug 2025 01:13AM UTC coverage: 81.778% (-0.006%) from 81.784%
16688256371

push

github

TAKETODAY
:white_check_mark:

59512 of 77725 branches covered (76.57%)

Branch coverage included in aggregate %.

140809 of 167233 relevant lines covered (84.2%)

3.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.18
today-context/src/main/java/infra/cache/annotation/AbstractCachingConfiguration.java
1
/*
2
 * Copyright 2017 - 2025 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17

18
package infra.cache.annotation;
19

20
import java.util.function.Function;
21
import java.util.function.Supplier;
22

23
import infra.beans.BeansException;
24
import infra.beans.factory.BeanFactory;
25
import infra.beans.factory.BeanFactoryAware;
26
import infra.beans.factory.annotation.DisableDependencyInjection;
27
import infra.cache.CacheManager;
28
import infra.cache.interceptor.CacheErrorHandler;
29
import infra.cache.interceptor.CacheResolver;
30
import infra.cache.interceptor.KeyGenerator;
31
import infra.context.annotation.Configuration;
32
import infra.context.annotation.ImportAware;
33
import infra.core.annotation.AnnotationAttributes;
34
import infra.core.type.AnnotationMetadata;
35
import infra.lang.Nullable;
36
import infra.util.ObjectUtils;
37
import infra.util.function.SingletonSupplier;
38

39
/**
40
 * Abstract base {@code @Configuration} class providing common structure
41
 * for enabling Infra annotation-driven cache management capability.
42
 *
43
 * @author Chris Beams
44
 * @author Stephane Nicoll
45
 * @author Juergen Hoeller
46
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
47
 * @see EnableCaching
48
 * @since 4.0
49
 */
50
@DisableDependencyInjection
51
@Configuration(proxyBeanMethods = false)
52
public abstract class AbstractCachingConfiguration implements ImportAware, BeanFactoryAware {
3✔
53

54
  @Nullable
55
  protected AnnotationAttributes enableCaching;
56

57
  @Nullable
58
  protected Supplier<CacheManager> cacheManager;
59

60
  @Nullable
61
  protected Supplier<CacheResolver> cacheResolver;
62

63
  @Nullable
64
  protected Supplier<KeyGenerator> keyGenerator;
65

66
  @Nullable
67
  protected Supplier<CacheErrorHandler> errorHandler;
68

69
  @Override
70
  public void setImportMetadata(AnnotationMetadata importMetadata) {
71
    this.enableCaching = AnnotationAttributes.fromMap(
5✔
72
            importMetadata.getAnnotationAttributes(EnableCaching.class));
1✔
73
    if (this.enableCaching == null) {
3!
74
      throw new IllegalArgumentException(
×
75
              "@EnableCaching is not present on importing class " + importMetadata.getClassName());
×
76
    }
77
  }
1✔
78

79
  @Override
80
  public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
81
    useCachingConfigurer(new CachingConfigurerSupplier(() -> {
7✔
82
      var candidates = beanFactory.getBeanNamesForType(CachingConfigurer.class);
4✔
83
      if (ObjectUtils.isEmpty(candidates)) {
3✔
84
        return null;
2✔
85
      }
86
      if (candidates.length > 1) {
4✔
87
        throw new IllegalStateException(candidates.length + " implementations of " +
7✔
88
                "CachingConfigurer were found when only 1 was expected. " +
89
                "Refactor the configuration such that CachingConfigurer is " +
90
                "implemented only once or not at all.");
91
      }
92
      return beanFactory.getBean(candidates[0], CachingConfigurer.class);
8✔
93
    }));
94
  }
1✔
95

96
  /**
97
   * Extract the configuration from the nominated {@link CachingConfigurer}.
98
   */
99
  protected void useCachingConfigurer(CachingConfigurerSupplier supplier) {
100
    this.cacheManager = supplier.adapt(CachingConfigurer::cacheManager);
5✔
101
    this.cacheResolver = supplier.adapt(CachingConfigurer::cacheResolver);
5✔
102
    this.keyGenerator = supplier.adapt(CachingConfigurer::keyGenerator);
5✔
103
    this.errorHandler = supplier.adapt(CachingConfigurer::errorHandler);
5✔
104
  }
1✔
105

106
  protected static class CachingConfigurerSupplier {
107

108
    private final Supplier<CachingConfigurer> supplier;
109

110
    public CachingConfigurerSupplier(Supplier<CachingConfigurer> supplier) {
2✔
111
      this.supplier = SingletonSupplier.from(supplier);
4✔
112
    }
1✔
113

114
    /**
115
     * Adapt the {@link CachingConfigurer} supplier to another supplier
116
     * provided by the specified mapping function. If the underlying
117
     * {@link CachingConfigurer} is {@code null}, {@code null} is returned
118
     * and the mapping function is not invoked.
119
     *
120
     * @param provider the provider to use to adapt the supplier
121
     * @param <T> the type of the supplier
122
     * @return another supplier mapped by the specified function
123
     */
124
    @Nullable
125
    public <T> Supplier<T> adapt(Function<CachingConfigurer, T> provider) {
126
      return () -> {
4✔
127
        CachingConfigurer cachingConfigurer = this.supplier.get();
5✔
128
        return (cachingConfigurer != null ? provider.apply(cachingConfigurer) : null);
8✔
129
      };
130
    }
131

132
  }
133

134
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc