Skip to content

Commit

Permalink
fix: Fix disabling EhCache (#3276)
Browse files Browse the repository at this point in the history
  • Loading branch information
pj892031 authored Jan 18, 2024
1 parent aa8b316 commit 3c18920
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@

package org.zowe.apiml.gateway.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.cache.jcache.JCacheManagerFactoryBean;
import org.springframework.cache.support.NoOpCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
Expand All @@ -29,9 +32,9 @@
/**
* Spring configuration to use EhCache. This context is using from application and also from tests.
*/
@Slf4j
@EnableCaching
@Configuration
@ConditionalOnProperty(value = "apiml.caching.enabled", havingValue = "true", matchIfMissing = true)
public class CacheConfig {

public static final String COMPOSITE_KEY_GENERATOR = "compositeKeyGenerator";
Expand All @@ -40,30 +43,47 @@ public class CacheConfig {
private static final String EHCACHE_STORAGE_ENV_PARAM_NAME = "ehcache.disk.store.dir";
private static final String APIML_CACHE_STORAGE_LOCATION_ENV_PARAM_NAME = "apiml.cache.storage.location";

@Value("${apiml.caching.enabled:true}")
private boolean cacheEnabled;

@PostConstruct
public void afterPropertiesSet() {
if (System.getProperty(EHCACHE_STORAGE_ENV_PARAM_NAME) == null) {
String location = System.getProperty(APIML_CACHE_STORAGE_LOCATION_ENV_PARAM_NAME);
if (location == null) location = System.getProperty("user.dir");
if (cacheEnabled) {
if (System.getProperty(EHCACHE_STORAGE_ENV_PARAM_NAME) == null) {
String location = System.getProperty(APIML_CACHE_STORAGE_LOCATION_ENV_PARAM_NAME);
if (location == null) location = System.getProperty("user.dir");

System.setProperty(EHCACHE_STORAGE_ENV_PARAM_NAME, location);
System.setProperty(EHCACHE_STORAGE_ENV_PARAM_NAME, location);
}
} else {
log.warn("Gateway Service is running in NoOp Cache mode. Do not use in production. " +
"To enable caching set configuration property apiml.caching.enabled to true."
);
}
}

@Bean
@ConditionalOnProperty(value = "apiml.caching.enabled", havingValue = "true", matchIfMissing = true)
public JCacheManagerFactoryBean cacheManagerFactoryBean() throws IOException {
JCacheManagerFactoryBean jCacheManagerFactoryBean = new JCacheManagerFactoryBean();
jCacheManagerFactoryBean.setCacheManagerUri(new ClassPathResource("ehcache.xml").getURI());
return jCacheManagerFactoryBean;
}

@Bean
@Bean("cacheManager")
@ConditionalOnProperty(value = "apiml.caching.enabled", havingValue = "true", matchIfMissing = true)
public CacheManager cacheManager() throws IOException {
final JCacheCacheManager jCacheCacheManager = new JCacheCacheManager();
jCacheCacheManager.setCacheManager(cacheManagerFactoryBean().getObject());
return jCacheCacheManager;
}

@ConditionalOnProperty(value = "apiml.caching.enabled", havingValue = "false")
@Bean("cacheManager")
public CacheManager cacheManagerNoOp() {
return new NoOpCacheManager();
}

@Bean(CacheConfig.COMPOSITE_KEY_GENERATOR)
public KeyGenerator getCompositeKeyGenerator() {
return new CompositeKeyGenerator();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,54 @@

package org.zowe.apiml.gateway.config;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cache.CacheManager;
import org.springframework.cache.jcache.JCacheCacheManager;
import org.springframework.cache.support.NoOpCacheManager;
import org.springframework.test.context.ActiveProfiles;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = CacheConfig.class)
@ActiveProfiles("test")

class CacheConfigTest {

@Autowired
private CacheManager cacheManager;
@Nested
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = CacheConfig.class)
@ActiveProfiles("test")
class EnabledCache {

@Autowired
private CacheManager cacheManager;

@Test
void testCacheManagerIsRealImplementation() {
assertNotNull(cacheManager);
assertTrue(cacheManager instanceof JCacheCacheManager);
}

}

@Nested
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = CacheConfig.class, properties = {
"apiml.caching.enabled=false"
})
@ActiveProfiles("test")
class DisabledCache {


@Autowired
private CacheManager cacheManager;

@Test
void testDisabledCacheManager() {
assertNotNull(cacheManager);
assertTrue(cacheManager instanceof NoOpCacheManager);
}

@Test
void testCacheManagerIsRealImplementation() {
assertNotNull(cacheManager);
assertTrue(cacheManager instanceof JCacheCacheManager);
}

}

This file was deleted.

0 comments on commit 3c18920

Please sign in to comment.