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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 { class HazelcastJCacheCustomizationConfiguration {
@Bean @Bean
HazelcastPropertiesCustomizer hazelcastPropertiesCustomizer(ObjectProvider<HazelcastInstance> hazelcastInstance) { HazelcastPropertiesCustomizer hazelcastPropertiesCustomizer(ObjectProvider<HazelcastInstance> hazelcastInstance,
return new HazelcastPropertiesCustomizer(hazelcastInstance.getIfUnique()); CacheProperties cacheProperties) {
return new HazelcastPropertiesCustomizer(hazelcastInstance.getIfUnique(), cacheProperties);
} }
static class HazelcastPropertiesCustomizer implements JCachePropertiesCustomizer { static class HazelcastPropertiesCustomizer implements JCachePropertiesCustomizer {
private final HazelcastInstance hazelcastInstance; private final HazelcastInstance hazelcastInstance;
HazelcastPropertiesCustomizer(HazelcastInstance hazelcastInstance) { private final CacheProperties cacheProperties;
HazelcastPropertiesCustomizer(HazelcastInstance hazelcastInstance, CacheProperties cacheProperties) {
this.hazelcastInstance = hazelcastInstance; this.hazelcastInstance = hazelcastInstance;
this.cacheProperties = cacheProperties;
} }
@Override @Override
public void customize(CacheProperties cacheProperties, Properties properties) { public void customize(Properties properties) {
Resource configLocation = cacheProperties.resolveConfigLocation(cacheProperties.getJcache().getConfig()); Resource configLocation = this.cacheProperties
.resolveConfigLocation(this.cacheProperties.getJcache().getConfig());
if (configLocation != null) { if (configLocation != null) {
// Hazelcast does not use the URI as a mean to specify a custom config. // Hazelcast does not use the URI as a mean to specify a custom config.
properties.setProperty("hazelcast.config.location", toUri(configLocation).toString()); 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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, private CacheManager createCacheManager(CacheProperties cacheProperties,
ObjectProvider<JCachePropertiesCustomizer> cachePropertiesCustomizers) throws IOException { ObjectProvider<JCachePropertiesCustomizer> cachePropertiesCustomizers) throws IOException {
CachingProvider cachingProvider = getCachingProvider(cacheProperties.getJcache().getProvider()); CachingProvider cachingProvider = getCachingProvider(cacheProperties.getJcache().getProvider());
Properties properties = createCacheManagerProperties(cachePropertiesCustomizers, cacheProperties); Properties properties = createCacheManagerProperties(cachePropertiesCustomizers);
Resource configLocation = cacheProperties.resolveConfigLocation(cacheProperties.getJcache().getConfig()); Resource configLocation = cacheProperties.resolveConfigLocation(cacheProperties.getJcache().getConfig());
if (configLocation != null) { if (configLocation != null) {
return cachingProvider.getCacheManager(configLocation.getURI(), this.beanClassLoader, properties); return cachingProvider.getCacheManager(configLocation.getURI(), this.beanClassLoader, properties);
@ -111,10 +111,9 @@ class JCacheCacheConfiguration implements BeanClassLoaderAware {
} }
private Properties createCacheManagerProperties( private Properties createCacheManagerProperties(
ObjectProvider<JCachePropertiesCustomizer> cachePropertiesCustomizers, CacheProperties cacheProperties) { ObjectProvider<JCachePropertiesCustomizer> cachePropertiesCustomizers) {
Properties properties = new Properties(); Properties properties = new Properties();
cachePropertiesCustomizers.orderedStream() cachePropertiesCustomizers.orderedStream().forEach((customizer) -> customizer.customize(properties));
.forEach((customizer) -> customizer.customize(cacheProperties, properties));
return 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 * Callback interface that can be implemented by beans wishing to customize the properties
* used by the {@link CachingProvider} to create the {@link CacheManager}. * used by the {@link CachingProvider} to create the {@link CacheManager}.
* *
* @see CachingProvider#getCacheManager(java.net.URI, ClassLoader, Properties)
* @author Stephane Nicoll * @author Stephane Nicoll
* @since 3.3.0
*/ */
interface JCachePropertiesCustomizer { public interface JCachePropertiesCustomizer {
/** /**
* Customize the properties. * Customize the properties.
* @param cacheProperties the cache properties
* @param properties the current properties * @param properties the current properties
* @see CachingProvider#getCacheManager(java.net.URI, ClassLoader, Properties) *
*/ */
void customize(CacheProperties cacheProperties, Properties properties); void customize(Properties properties);
} }