From 16ccb51a3ac51e601b86b076873718343d0126ae Mon Sep 17 00:00:00 2001 From: Keith Woodlock Date: Thu, 19 Sep 2013 15:11:49 +0100 Subject: [PATCH] MIFOSX-259: ensure cache type persisted in database is used when platform starts up. --- .../cache/api/CacheApiResource.java | 3 ++- .../RuntimeDelegatingCacheManager.java | 22 +++++++++++-------- .../TenantAwareBasicAuthenticationFilter.java | 21 +++++++++++++++++- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/cache/api/CacheApiResource.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/cache/api/CacheApiResource.java index ef4ec4a83..8d6b2729b 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/cache/api/CacheApiResource.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/cache/api/CacheApiResource.java @@ -51,7 +51,8 @@ public class CacheApiResource { private final RuntimeDelegatingCacheManager cacheService; @Autowired - public CacheApiResource(final PlatformSecurityContext context, @Qualifier("runtimeDelegatingCacheManager") final RuntimeDelegatingCacheManager cacheService, + public CacheApiResource(final PlatformSecurityContext context, + @Qualifier("runtimeDelegatingCacheManager") final RuntimeDelegatingCacheManager cacheService, final DefaultToApiJsonSerializer toApiJsonSerializer, final ApiRequestParameterHelper apiRequestParameterHelper, final PortfolioCommandSourceWritePlatformService commandsSourceWritePlatformService) { this.context = context; diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/cache/service/RuntimeDelegatingCacheManager.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/cache/service/RuntimeDelegatingCacheManager.java index 54612b0ae..d70d02d47 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/cache/service/RuntimeDelegatingCacheManager.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/cache/service/RuntimeDelegatingCacheManager.java @@ -30,11 +30,12 @@ public class RuntimeDelegatingCacheManager implements CacheManager { private final EhCacheCacheManager ehcacheCacheManager; private final CacheManager noOpCacheManager = new NoOpCacheManager(); - private CacheManager currentCacheManager = this.noOpCacheManager; + private CacheManager currentCacheManager; @Autowired public RuntimeDelegatingCacheManager(final EhCacheCacheManager ehCacheCacheManager) { this.ehcacheCacheManager = ehCacheCacheManager; + this.currentCacheManager = this.noOpCacheManager; } @Override @@ -49,19 +50,22 @@ public class RuntimeDelegatingCacheManager implements CacheManager { public Collection retrieveAll() { - final boolean ehcacheEnabled = false; - final boolean noCacheEnabled = !ehcacheEnabled; - final boolean distributedCacheEnabled = false; + final boolean noCacheEnabled = this.currentCacheManager instanceof NoOpCacheManager; + final boolean ehcacheEnabled = this.currentCacheManager instanceof EhCacheCacheManager; + + // final boolean distributedCacheEnabled = false; final EnumOptionData noCacheType = CacheEnumerations.cacheType(CacheType.NO_CACHE); final EnumOptionData singleNodeCacheType = CacheEnumerations.cacheType(CacheType.SINGLE_NODE); - final EnumOptionData multiNodeCacheType = CacheEnumerations.cacheType(CacheType.MULTI_NODE); + // final EnumOptionData multiNodeCacheType = + // CacheEnumerations.cacheType(CacheType.MULTI_NODE); final CacheData noCache = CacheData.instance(noCacheType, noCacheEnabled); final CacheData singleNodeCache = CacheData.instance(singleNodeCacheType, ehcacheEnabled); - final CacheData distributedCache = CacheData.instance(multiNodeCacheType, distributedCacheEnabled); + // final CacheData distributedCache = + // CacheData.instance(multiNodeCacheType, distributedCacheEnabled); - final Collection caches = Arrays.asList(noCache, singleNodeCache, distributedCache); + final Collection caches = Arrays.asList(noCache, singleNodeCache); return caches; } @@ -78,15 +82,15 @@ public class RuntimeDelegatingCacheManager implements CacheManager { case NO_CACHE: if (!noCacheEnabled) { changes.put(CacheApiConstants.cacheTypeParameter, toCacheType.getValue()); - this.currentCacheManager = this.noOpCacheManager; } + this.currentCacheManager = this.noOpCacheManager; break; case SINGLE_NODE: if (!ehcacheEnabled) { changes.put(CacheApiConstants.cacheTypeParameter, toCacheType.getValue()); clearEhCache(); - this.currentCacheManager = this.ehcacheCacheManager; } + this.currentCacheManager = this.ehcacheCacheManager; break; case MULTI_NODE: if (!distributedCacheEnabled) { diff --git a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/security/filter/TenantAwareBasicAuthenticationFilter.java b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/security/filter/TenantAwareBasicAuthenticationFilter.java index 688ec6d61..10c0ef4e3 100644 --- a/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/security/filter/TenantAwareBasicAuthenticationFilter.java +++ b/mifosng-provider/src/main/java/org/mifosplatform/infrastructure/security/filter/TenantAwareBasicAuthenticationFilter.java @@ -15,6 +15,9 @@ import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.time.StopWatch; +import org.mifosplatform.infrastructure.cache.domain.CacheType; +import org.mifosplatform.infrastructure.cache.service.CacheWritePlatformService; +import org.mifosplatform.infrastructure.configuration.domain.ConfigurationDomainService; import org.mifosplatform.infrastructure.core.domain.MifosPlatformTenant; import org.mifosplatform.infrastructure.core.serialization.ToApiJsonSerializer; import org.mifosplatform.infrastructure.core.service.ThreadLocalContextUtil; @@ -48,10 +51,13 @@ import org.springframework.stereotype.Service; @Service(value = "basicAuthenticationProcessingFilter") public class TenantAwareBasicAuthenticationFilter extends BasicAuthenticationFilter { + private static boolean firstRequestProcessed = false; private final static Logger logger = LoggerFactory.getLogger(TenantAwareBasicAuthenticationFilter.class); private final BasicAuthTenantDetailsService basicAuthTenantDetailsService; private final ToApiJsonSerializer toApiJsonSerializer; + private final ConfigurationDomainService configurationDomainService; + private final CacheWritePlatformService cacheWritePlatformService; private final String tenantRequestHeader = "X-Mifos-Platform-TenantId"; private final boolean exceptionIfHeaderMissing = true; @@ -60,10 +66,13 @@ public class TenantAwareBasicAuthenticationFilter extends BasicAuthenticationFil public TenantAwareBasicAuthenticationFilter(final AuthenticationManager authenticationManager, final AuthenticationEntryPoint authenticationEntryPoint, final BasicAuthTenantDetailsService basicAuthTenantDetailsService, - final ToApiJsonSerializer toApiJsonSerializer) { + final ToApiJsonSerializer toApiJsonSerializer, + final ConfigurationDomainService configurationDomainService, final CacheWritePlatformService cacheWritePlatformService) { super(authenticationManager, authenticationEntryPoint); this.basicAuthTenantDetailsService = basicAuthTenantDetailsService; this.toApiJsonSerializer = toApiJsonSerializer; + this.configurationDomainService = configurationDomainService; + this.cacheWritePlatformService = cacheWritePlatformService; } @Override @@ -95,6 +104,16 @@ public class TenantAwareBasicAuthenticationFilter extends BasicAuthenticationFil final MifosPlatformTenant tenant = this.basicAuthTenantDetailsService.loadTenantById(tenantIdentifier); ThreadLocalContextUtil.setTenant(tenant); + + if (!firstRequestProcessed) { + final boolean ehcacheEnabled = this.configurationDomainService.isEhcacheEnabled(); + if (ehcacheEnabled) { + this.cacheWritePlatformService.switchToCache(CacheType.SINGLE_NODE); + } else { + this.cacheWritePlatformService.switchToCache(CacheType.NO_CACHE); + } + TenantAwareBasicAuthenticationFilter.firstRequestProcessed = true; + } } super.doFilter(req, res, chain);