Fixing the cache manager issues

revert-70aa11f8
geethkokila 9 years ago
parent e1da84ec55
commit 5ee8c0f065

@ -71,7 +71,8 @@
org.wso2.carbon.user.api.*, org.wso2.carbon.user.api.*,
org.wso2.carbon.device.mgt.core.*, org.wso2.carbon.device.mgt.core.*,
org.wso2.carbon.device.mgt.common.*, org.wso2.carbon.device.mgt.common.*,
org.wso2.carbon.ntask.* org.wso2.carbon.ntask.*,
org.wso2.carbon.caching.*
</Import-Package> </Import-Package>
<Export-Package> <Export-Package>
!org.wso2.carbon.policy.mgt.core.internal, !org.wso2.carbon.policy.mgt.core.internal,

@ -27,11 +27,13 @@ import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
import org.wso2.carbon.policy.mgt.core.cache.PolicyCacheManager; import org.wso2.carbon.policy.mgt.core.cache.PolicyCacheManager;
import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager;
import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl; import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl;
import org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants;
import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil;
import java.util.ArrayList; import javax.cache.Cache;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set;
public class PolicyCacheManagerImpl implements PolicyCacheManager { public class PolicyCacheManagerImpl implements PolicyCacheManager {
@ -41,6 +43,14 @@ public class PolicyCacheManagerImpl implements PolicyCacheManager {
private static PolicyCacheManagerImpl policyCacheManager; private static PolicyCacheManagerImpl policyCacheManager;
// private static Cache<Integer, Policy> getPolicyCache() {
// return PolicyManagerUtil.getPolicyCache(PolicyManagementConstants.DM_CACHE);
// }
private static Cache<Integer, List<Policy>> getPolicyListCache() {
return PolicyManagerUtil.getPolicyListCache(PolicyManagementConstants.DM_CACHE_LIST);
}
private PolicyCacheManagerImpl() { private PolicyCacheManagerImpl() {
} }
@ -57,43 +67,87 @@ public class PolicyCacheManagerImpl implements PolicyCacheManager {
@Override @Override
public void addAllPolicies(List<Policy> policies) { public void addAllPolicies(List<Policy> policies) {
HashMap<Integer, Policy> map = this.getTenantRelatedMap(); // HashMap<Integer, Policy> map = this.getTenantRelatedMap();
if (map.isEmpty()) {
for (Policy policy : policies) {
map.put(policy.getId(), policy);
}
}
// Cache<Integer, Policy> cache = getPolicyCache();
// if (cache.isEmpty()) {
// for (Policy policy : policies) {
// cache.put(policy.getId(), policy);
// }
// }
Cache<Integer, List<Policy>> lCache = getPolicyListCache();
lCache.put(1, policies);
} }
@Override @Override
public void updateAllPolicies(List<Policy> policies) { public void updateAllPolicies(List<Policy> policies) {
HashMap<Integer, Policy> map = this.getTenantRelatedMap(); //// HashMap<Integer, Policy> map = this.getTenantRelatedMap();
map.clear(); // Cache<Integer, Policy> cache = getPolicyCache();
if (map.isEmpty()) { // cache.removeAll();
for (Policy policy : policies) { //// map.clear();
map.put(policy.getId(), policy); //// if (map.isEmpty()) {
} // for (Policy policy : policies) {
} //// map.put(policy.getId(), policy);
// cache.put(policy.getId(), policy);
// }
//// }
Cache<Integer, List<Policy>> lCache = getPolicyListCache();
lCache.removeAll();
lCache.put(1, policies);
} }
@Override @Override
public List<Policy> getAllPolicies() throws PolicyManagementException { public List<Policy> getAllPolicies() throws PolicyManagementException {
HashMap<Integer, Policy> map = this.getTenantRelatedMap(); // // HashMap<Integer, Policy> map = this.getTenantRelatedMap();
if (map.isEmpty()) { // Cache<Integer, Policy> cache = getPolicyCache();
// Iterator iterator = cache.iterator();
//// iterator.hasNext()
// if (!iterator.hasNext()) {
// PolicyManager policyManager = new PolicyManagerImpl();
// this.addAllPolicies(policyManager.getPolicies());
// }
// if (log.isDebugEnabled()) {
// //log.debug("No of policies stored in the cache .. : " + map.size());
//
// //Set<Integer> keySet = map.keySet();
// Iterator iterator2 = cache.iterator();
// while (iterator2.hasNext()) {
// org.wso2.carbon.caching.impl.CacheEntry thisEntry = (org.wso2.carbon.caching.impl.CacheEntry)
// iterator2.next();
// log.debug("Policy id in maps .. : " + thisEntry.getKey() + " policy name : " + ((Policy) thisEntry
// .getValue()).
// getPolicyName() + " Activated : " + ((Policy) thisEntry.getValue()).isActive());
// }
// }
//
// List<Policy> policies = new ArrayList<>();
// while (iterator.hasNext()){
// CacheEntry thisEntry = (CacheEntry) iterator.next();
// policies.add((Policy) thisEntry.getValue());
// }
//
// return policies;
//// return new ArrayList<>(map.values());
Cache<Integer, List<Policy>> lCache = getPolicyListCache();
if (!lCache.containsKey(1)) {
PolicyManager policyManager = new PolicyManagerImpl(); PolicyManager policyManager = new PolicyManagerImpl();
this.addAllPolicies(policyManager.getPolicies()); this.addAllPolicies(policyManager.getPolicies());
} }
if (log.isDebugEnabled()) {
log.debug("No of policies stored in the cache .. : " + map.size());
Set<Integer> keySet = map.keySet(); if (log.isDebugEnabled()) {
for (Integer x : keySet) { List<Policy> cachedPolicy = lCache.get(1);
log.debug("Policy id in maps .. : " + map.get(x).getId() + " policy name : " + map.get(x). for (Policy policy : cachedPolicy) {
getPolicyName() + " Activated : " + map.get(x).isActive()); log.debug("Policy id in cache .. : " + policy.getId() + " policy name : " + policy.
getPolicyName() + " Activated : " + policy.isActive());
} }
} }
return new ArrayList<>(map.values());
return lCache.get(1);
} }
@Override @Override
@ -105,59 +159,136 @@ public class PolicyCacheManagerImpl implements PolicyCacheManager {
@Override @Override
public void removeAllPolicies() { public void removeAllPolicies() {
HashMap<Integer, Policy> map = this.getTenantRelatedMap(); // Cache<Integer, Policy> cache = getPolicyCache();
map.clear(); // cache.removeAll();
//HashMap<Integer, Policy> map = this.getTenantRelatedMap();
//map.clear();
Cache<Integer, List<Policy>> lCache = getPolicyListCache();
lCache.removeAll();
} }
@Override @Override
public void addPolicy(Policy policy) { public void addPolicy(Policy policy) {
HashMap<Integer, Policy> map = this.getTenantRelatedMap(); //HashMap<Integer, Policy> map = this.getTenantRelatedMap();
if (!map.containsKey(policy.getId())) { // Cache<Integer, Policy> cache = getPolicyCache();
map.put(policy.getId(), policy); // if (!cache.containsKey(policy.getId())) {
} else { // cache.put(policy.getId(), policy);
log.warn("Policy id (" + policy.getId() + ") already exist in the map. hence not attempted to store."); // } else {
// log.warn("Policy id (" + policy.getId() + ") already exist in the map. hence not attempted to store.");
// }
Cache<Integer, List<Policy>> lCache = getPolicyListCache();
if (lCache.containsKey(1)) {
List<Policy> cachedPolicy = lCache.get(1);
for (Policy pol : cachedPolicy) {
if (pol.getId() == policy.getId()) {
return;
} }
} }
cachedPolicy.add(policy);
}
}
@Override @Override
public void updatePolicy(Policy policy) { public void updatePolicy(Policy policy) {
HashMap<Integer, Policy> map = this.getTenantRelatedMap(); // HashMap<Integer, Policy> map = this.getTenantRelatedMap();
if (map.containsKey(policy.getId())) { // Cache<Integer, Policy> cache = getPolicyCache();
map.remove(policy.getId()); // if (cache.containsKey(policy.getId())) {
map.put(policy.getId(), policy); // cache.remove(policy.getId());
// cache.put(policy.getId(), policy);
// }
Cache<Integer, List<Policy>> lCache = getPolicyListCache();
if (lCache.containsKey(1)) {
List<Policy> cachedPolicy = lCache.get(1);
Iterator iterator = cachedPolicy.iterator();
while (iterator.hasNext()) {
Policy pol = (Policy) iterator.next();
if (pol.getId() == policy.getId()) {
iterator.remove();
break;
}
} }
cachedPolicy.add(policy);
lCache.replace(1, cachedPolicy);
}
} }
@Override @Override
public void updatePolicy(int policyId) throws PolicyManagementException { public void updatePolicy(int policyId) throws PolicyManagementException {
HashMap<Integer, Policy> map = this.getTenantRelatedMap(); // HashMap<Integer, Policy> map = this.getTenantRelatedMap();
if (map.containsKey(policyId)) { // Cache<Integer, Policy> cache = getPolicyCache();
this.removePolicy(policyId); // if (cache.containsKey(policyId)) {
} // this.removePolicy(policyId);
// }
// PolicyManager policyManager = new PolicyManagerImpl();
// Policy policy = policyManager.getPolicy(policyId);
// cache.put(policyId, policy);
Cache<Integer, List<Policy>> lCache = getPolicyListCache();
if (lCache.containsKey(1)) {
PolicyManager policyManager = new PolicyManagerImpl(); PolicyManager policyManager = new PolicyManagerImpl();
Policy policy = policyManager.getPolicy(policyId); Policy policy = policyManager.getPolicy(policyId);
map.put(policyId, policy); this.updatePolicy(policy);
}
} }
@Override @Override
public void removePolicy(int policyId) { public void removePolicy(int policyId) {
HashMap<Integer, Policy> map = this.getTenantRelatedMap(); // HashMap<Integer, Policy> map = this.getTenantRelatedMap();
if (map.containsKey(policyId)) { // Cache<Integer, Policy> cache = getPolicyCache();
map.remove(policyId); // if (cache.containsKey(policyId)) {
} else { // cache.remove(policyId);
log.warn("Policy id (" + policyId + ") does not exist in the cache. Hence not removed."); // } else {
// log.warn("Policy id (" + policyId + ") does not exist in the cache. Hence not removed.");
// }
Cache<Integer, List<Policy>> lCache = getPolicyListCache();
if (lCache.containsKey(1)) {
List<Policy> cachedPolicy = lCache.get(1);
Iterator iterator = cachedPolicy.iterator();
while (iterator.hasNext()) {
Policy pol = (Policy) iterator.next();
if (pol.getId() == policyId) {
iterator.remove();
break;
}
}
lCache.replace(1, cachedPolicy);
} }
} }
@Override @Override
public Policy getPolicy(int policyId) throws PolicyManagementException { public Policy getPolicy(int policyId) throws PolicyManagementException {
HashMap<Integer, Policy> map = this.getTenantRelatedMap(); //HashMap<Integer, Policy> map = this.getTenantRelatedMap();
if (!map.containsKey(policyId)) { // Cache<Integer, Policy> cache = getPolicyCache();
// if (!cache.containsKey(policyId)) {
// this.removeAllPolicies();
// this.getAllPolicies();
// }
// return cache.get(policyId);
Cache<Integer, List<Policy>> lCache = getPolicyListCache();
if (!lCache.containsKey(1)) {
this.removeAllPolicies(); this.removeAllPolicies();
this.getAllPolicies(); this.getAllPolicies();
} }
return map.get(policyId);
Policy policy = null;
List<Policy> cachedPolicy = lCache.get(1);
Iterator iterator = cachedPolicy.iterator();
while (iterator.hasNext()) {
Policy pol = (Policy) iterator.next();
if (pol.getId() == policyId) {
policy = pol;
}
}
return policy;
} }
@Override @Override

@ -39,6 +39,7 @@ public final class PolicyManagementConstants {
public static final String DM_CACHE_MANAGER = "DM_CACHE_MANAGER"; public static final String DM_CACHE_MANAGER = "DM_CACHE_MANAGER";
public static final String DM_CACHE = "DM_CACHE"; public static final String DM_CACHE = "DM_CACHE";
public static final String DM_CACHE_LIST = "DM_CACHE_LIST";
public static final String DELEGATION_TASK_TYPE = "DELEGATION__TASK"; public static final String DELEGATION_TASK_TYPE = "DELEGATION__TASK";

@ -31,6 +31,10 @@ import org.wso2.carbon.policy.mgt.common.ProfileFeature;
import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig;
import org.wso2.carbon.policy.mgt.core.config.datasource.JNDILookupDefinition; import org.wso2.carbon.policy.mgt.core.config.datasource.JNDILookupDefinition;
import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil; import org.wso2.carbon.policy.mgt.core.dao.util.PolicyManagementDAOUtil;
import org.wso2.carbon.registry.api.GhostResource;
import org.wso2.carbon.registry.core.RegistryConstants;
import org.wso2.carbon.registry.core.caching.RegistryCacheEntry;
import org.wso2.carbon.registry.core.caching.RegistryCacheKey;
import javax.cache.Cache; import javax.cache.Cache;
import javax.cache.CacheManager; import javax.cache.CacheManager;
@ -152,10 +156,28 @@ public class PolicyManagerUtil {
} }
public static Cache getCacheManagerImpl() { // public static Cache getCacheManagerImpl() {
return Caching.getCacheManagerFactory() // return Caching.getCacheManagerFactory()
.getCacheManager(PolicyManagementConstants.DM_CACHE_MANAGER).getCache(PolicyManagementConstants // .getCacheManager(PolicyManagementConstants.DM_CACHE_MANAGER).getCache(PolicyManagementConstants
.DM_CACHE); // .DM_CACHE);
// }
public static Cache<Integer, Policy> getPolicyCache(String name){
CacheManager manager = getCacheManager();
return (manager != null) ? manager.<Integer, Policy>getCache(name) :
Caching.getCacheManager().<Integer, Policy>getCache(name);
}
public static Cache<Integer, List<Policy>> getPolicyListCache(String name){
CacheManager manager = getCacheManager();
return (manager != null) ? manager.<Integer, List<Policy>>getCache(name) :
Caching.getCacheManager().<Integer, List<Policy>>getCache(name);
}
private static CacheManager getCacheManager() {
return Caching.getCacheManagerFactory().getCacheManager(
PolicyManagementConstants.DM_CACHE_MANAGER);
} }

Loading…
Cancel
Save