|
|
@ -21,26 +21,28 @@ package org.wso2.carbon.policy.mgt.core.cache.impl;
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
|
|
|
|
import org.wso2.carbon.policy.mgt.common.Policy;
|
|
|
|
import org.wso2.carbon.policy.mgt.common.Policy;
|
|
|
|
import org.wso2.carbon.policy.mgt.common.PolicyManagementException;
|
|
|
|
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.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class PolicyCacheManagerImpl implements PolicyCacheManager {
|
|
|
|
public class PolicyCacheManagerImpl implements PolicyCacheManager {
|
|
|
|
|
|
|
|
|
|
|
|
private static final Log log = LogFactory.getLog(PolicyCacheManagerImpl.class);
|
|
|
|
private static final Log log = LogFactory.getLog(PolicyCacheManagerImpl.class);
|
|
|
|
|
|
|
|
|
|
|
|
private static HashMap<Integer, HashMap<Integer, Policy>> tenantedPolicyMap = new HashMap<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static PolicyCacheManagerImpl policyCacheManager;
|
|
|
|
private static PolicyCacheManagerImpl policyCacheManager;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static Cache<Integer, List<Policy>> getPolicyListCache() {
|
|
|
|
|
|
|
|
return PolicyManagerUtil.getPolicyListCache(PolicyManagementConstants.DM_CACHE_LIST);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private PolicyCacheManagerImpl() {
|
|
|
|
private PolicyCacheManagerImpl() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -57,43 +59,36 @@ 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();
|
|
|
|
|
|
|
|
if (map.isEmpty()) {
|
|
|
|
|
|
|
|
for (Policy policy : policies) {
|
|
|
|
|
|
|
|
map.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();
|
|
|
|
|
|
|
|
map.clear();
|
|
|
|
Cache<Integer, List<Policy>> lCache = getPolicyListCache();
|
|
|
|
if (map.isEmpty()) {
|
|
|
|
lCache.removeAll();
|
|
|
|
for (Policy policy : policies) {
|
|
|
|
lCache.put(1, policies);
|
|
|
|
map.put(policy.getId(), policy);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public List<Policy> getAllPolicies() throws PolicyManagementException {
|
|
|
|
public List<Policy> getAllPolicies() throws PolicyManagementException {
|
|
|
|
HashMap<Integer, Policy> map = this.getTenantRelatedMap();
|
|
|
|
|
|
|
|
if (map.isEmpty()) {
|
|
|
|
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()) {
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
log.debug("No of policies stored in the cache .. : " + map.size());
|
|
|
|
List<Policy> cachedPolicy = lCache.get(1);
|
|
|
|
|
|
|
|
for (Policy policy : cachedPolicy) {
|
|
|
|
Set<Integer> keySet = map.keySet();
|
|
|
|
log.debug("Policy id in cache .. : " + policy.getId() + " policy name : " + policy.
|
|
|
|
for (Integer x : keySet) {
|
|
|
|
getPolicyName() + " Activated : " + policy.isActive());
|
|
|
|
log.debug("Policy id in maps .. : " + map.get(x).getId() + " policy name : " + map.get(x).
|
|
|
|
|
|
|
|
getPolicyName() + " Activated : " + map.get(x).isActive());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return new ArrayList<>(map.values());
|
|
|
|
return lCache.get(1);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
@ -105,59 +100,97 @@ public class PolicyCacheManagerImpl implements PolicyCacheManager {
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void removeAllPolicies() {
|
|
|
|
public void removeAllPolicies() {
|
|
|
|
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();
|
|
|
|
|
|
|
|
if (!map.containsKey(policy.getId())) {
|
|
|
|
Cache<Integer, List<Policy>> lCache = getPolicyListCache();
|
|
|
|
map.put(policy.getId(), policy);
|
|
|
|
if (lCache.containsKey(1)) {
|
|
|
|
} else {
|
|
|
|
List<Policy> cachedPolicy = lCache.get(1);
|
|
|
|
log.warn("Policy id (" + policy.getId() + ") already exist in the map. hence not attempted to store.");
|
|
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
if (map.containsKey(policy.getId())) {
|
|
|
|
Cache<Integer, List<Policy>> lCache = getPolicyListCache();
|
|
|
|
map.remove(policy.getId());
|
|
|
|
if (lCache.containsKey(1)) {
|
|
|
|
map.put(policy.getId(), policy);
|
|
|
|
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();
|
|
|
|
|
|
|
|
if (map.containsKey(policyId)) {
|
|
|
|
Cache<Integer, List<Policy>> lCache = getPolicyListCache();
|
|
|
|
this.removePolicy(policyId);
|
|
|
|
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();
|
|
|
|
|
|
|
|
if (map.containsKey(policyId)) {
|
|
|
|
Cache<Integer, List<Policy>> lCache = getPolicyListCache();
|
|
|
|
map.remove(policyId);
|
|
|
|
if (lCache.containsKey(1)) {
|
|
|
|
} else {
|
|
|
|
List<Policy> cachedPolicy = lCache.get(1);
|
|
|
|
log.warn("Policy id (" + policyId + ") does not exist in the cache. Hence not removed.");
|
|
|
|
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();
|
|
|
|
|
|
|
|
if (!map.containsKey(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
|
|
|
@ -175,13 +208,4 @@ public class PolicyCacheManagerImpl implements PolicyCacheManager {
|
|
|
|
return 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private HashMap<Integer, Policy> getTenantRelatedMap(){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
|
|
|
|
|
|
|
|
if(!tenantedPolicyMap.containsKey(tenantId)){
|
|
|
|
|
|
|
|
HashMap<Integer, Policy> policyMap = new HashMap<>();
|
|
|
|
|
|
|
|
tenantedPolicyMap.put(tenantId, policyMap);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return tenantedPolicyMap.get(tenantId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|