Enable customization of properties used to create JCache CacheManager

This commit is contained in:
Andy Wilkinson 2024-01-31 10:41:30 +00:00
parent ca6ba2e7cf
commit 622c9ed882
3 changed files with 21 additions and 16 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -38,21 +38,26 @@ import org.springframework.core.io.Resource;
class HazelcastJCacheCustomizationConfiguration {
@Bean
HazelcastPropertiesCustomizer hazelcastPropertiesCustomizer(ObjectProvider<HazelcastInstance> hazelcastInstance) {
return new HazelcastPropertiesCustomizer(hazelcastInstance.getIfUnique());
HazelcastPropertiesCustomizer hazelcastPropertiesCustomizer(ObjectProvider<HazelcastInstance> hazelcastInstance,
CacheProperties cacheProperties) {
return new HazelcastPropertiesCustomizer(hazelcastInstance.getIfUnique(), cacheProperties);
}
static class HazelcastPropertiesCustomizer implements JCachePropertiesCustomizer {
private final HazelcastInstance hazelcastInstance;
HazelcastPropertiesCustomizer(HazelcastInstance hazelcastInstance) {
private final CacheProperties cacheProperties;
HazelcastPropertiesCustomizer(HazelcastInstance hazelcastInstance, CacheProperties cacheProperties) {
this.hazelcastInstance = hazelcastInstance;
this.cacheProperties = cacheProperties;
}
@Override
public void customize(CacheProperties cacheProperties, Properties properties) {
Resource configLocation = cacheProperties.resolveConfigLocation(cacheProperties.getJcache().getConfig());
public void customize(Properties properties) {
Resource configLocation = this.cacheProperties
.resolveConfigLocation(this.cacheProperties.getJcache().getConfig());
if (configLocation != null) {
// Hazelcast does not use the URI as a mean to specify a custom config.
properties.setProperty("hazelcast.config.location", toUri(configLocation).toString());

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2023 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -95,7 +95,7 @@ class JCacheCacheConfiguration implements BeanClassLoaderAware {
private CacheManager createCacheManager(CacheProperties cacheProperties,
ObjectProvider<JCachePropertiesCustomizer> cachePropertiesCustomizers) throws IOException {
CachingProvider cachingProvider = getCachingProvider(cacheProperties.getJcache().getProvider());
Properties properties = createCacheManagerProperties(cachePropertiesCustomizers, cacheProperties);
Properties properties = createCacheManagerProperties(cachePropertiesCustomizers);
Resource configLocation = cacheProperties.resolveConfigLocation(cacheProperties.getJcache().getConfig());
if (configLocation != null) {
return cachingProvider.getCacheManager(configLocation.getURI(), this.beanClassLoader, properties);
@ -111,10 +111,9 @@ class JCacheCacheConfiguration implements BeanClassLoaderAware {
}
private Properties createCacheManagerProperties(
ObjectProvider<JCachePropertiesCustomizer> cachePropertiesCustomizers, CacheProperties cacheProperties) {
ObjectProvider<JCachePropertiesCustomizer> cachePropertiesCustomizers) {
Properties properties = new Properties();
cachePropertiesCustomizers.orderedStream()
.forEach((customizer) -> customizer.customize(cacheProperties, properties));
cachePropertiesCustomizers.orderedStream().forEach((customizer) -> customizer.customize(properties));
return properties;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -25,16 +25,17 @@ import javax.cache.spi.CachingProvider;
* Callback interface that can be implemented by beans wishing to customize the properties
* used by the {@link CachingProvider} to create the {@link CacheManager}.
*
* @see CachingProvider#getCacheManager(java.net.URI, ClassLoader, Properties)
* @author Stephane Nicoll
* @since 3.3.0
*/
interface JCachePropertiesCustomizer {
public interface JCachePropertiesCustomizer {
/**
* Customize the properties.
* @param cacheProperties the cache properties
* @param properties the current properties
* @see CachingProvider#getCacheManager(java.net.URI, ClassLoader, Properties)
*
*/
void customize(CacheProperties cacheProperties, Properties properties);
void customize(Properties properties);
}