diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/claim/ClaimInvalidationCache.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/claim/ClaimInvalidationCache.java index edf73d05d81..f0cd52e859c 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/claim/ClaimInvalidationCache.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/claim/ClaimInvalidationCache.java @@ -17,6 +17,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.base.ServerConfiguration; +import org.wso2.carbon.caching.impl.CachingConstants; import javax.cache.Cache; import javax.cache.CacheManager; @@ -31,6 +33,10 @@ public class ClaimInvalidationCache { private String INVALIDATE_CACHE_KEY = "Invalidate.Cache.Key"; private int myHashCode; + /** + * This boolean is used to flag the cache, once the claims are loaded to the cache. + */ + private boolean isAlreadyInitialize; private ClaimInvalidationCache() { } @@ -64,6 +70,12 @@ public boolean isInvalid() { return true; } } + if (ServerConfiguration.getInstance().getFirstProperty(CachingConstants.FORCE_LOCAL_CACHE) != null && + ServerConfiguration.getInstance().getFirstProperty(CachingConstants.FORCE_LOCAL_CACHE).equals("true") && + hashCode == null && this.isAlreadyInitialize) { + updateCache(INVALIDATE_CACHE_KEY, myHashCode); + return true; + } return false; } @@ -116,6 +128,25 @@ private void clearCacheEntry(String key) { cache.remove(key); } } + + public void setIsAlreadyInitialize(Boolean bool) { + this.isAlreadyInitialize = bool; + } + + /** + * Update the cache entry without clearing. + * + * @param key Key which cache entry is indexed. + * @param entry Actual object where cache entry is placed. + */ + private void updateCache(String key, Integer entry) { + Cache cache = getClaimCache(); + if (cache != null) { + cache.put(key, entry); + } else { + log.debug("Error while updating the claim cache. ClaimCache is null"); + } + } } diff --git a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/claim/DefaultClaimManager.java b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/claim/DefaultClaimManager.java index d0b79fa01b7..d9131b505de 100644 --- a/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/claim/DefaultClaimManager.java +++ b/core/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/claim/DefaultClaimManager.java @@ -17,6 +17,8 @@ */ package org.wso2.carbon.user.core.claim; +import org.wso2.carbon.base.ServerConfiguration; +import org.wso2.carbon.caching.impl.CachingConstants; import org.wso2.carbon.user.core.UserStoreException; import org.wso2.carbon.user.core.claim.builder.ClaimBuilder; import org.wso2.carbon.user.core.claim.builder.ClaimBuilderException; @@ -67,6 +69,10 @@ public DefaultClaimManager(Map claimMapping, DataSource da this.claimMapping = new ConcurrentHashMap(); this.claimMapping.putAll(claimMapping); this.claimCache.invalidateCache(); + if (ServerConfiguration.getInstance().getFirstProperty(CachingConstants.FORCE_LOCAL_CACHE) != null && + ServerConfiguration.getInstance().getFirstProperty(CachingConstants.FORCE_LOCAL_CACHE).equals("true")) { + this.claimCache.setIsAlreadyInitialize(true); + } } }