From 90ff502b218347f84e93f29e613957a021069b22 Mon Sep 17 00:00:00 2001 From: Geeth Munasinghe Date: Fri, 10 Apr 2015 20:10:50 +0530 Subject: [PATCH] Adding the final changes of the policy implementation, Added the simple policy evaluation implementation --- .../wso2/carbon/policy/mgt/common/Policy.java | 27 ++++- .../common}/PolicyEvaluationException.java | 2 +- .../mgt/common/PolicyEvaluationPoint.java | 4 +- .../mgt/core/dao/impl/PolicyDAOImpl.java | 29 ++++- .../internal/PolicyManagementDataHolder.java | 18 +++ .../PolicyManagementServiceComponent.java | 28 ++++- .../test/resources/sql/CreateMySqlTestDB.sql | 10 +- .../pom.xml | 9 ++ .../point/PolicyEvaluationServiceImpl.java | 18 ++- .../decision/point/SimpleEvaluation.java | 7 +- .../decision/point/SimpleEvaluationImpl.java | 36 ++++-- .../PolicyDecisionPointDataHolder.java | 53 +++++++++ .../PolicyEvaluationServiceComponent.java | 109 ++++++++++++++++++ .../pom.xml | 4 +- pom.xml | 5 + 15 files changed, 333 insertions(+), 26 deletions(-) rename components/policy-mgt/{org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point => org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common}/PolicyEvaluationException.java (96%) create mode 100644 components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyDecisionPointDataHolder.java create mode 100644 components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyEvaluationServiceComponent.java diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java index b2b80acbb8..afe7b4abec 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java @@ -22,13 +22,15 @@ package org.wso2.carbon.policy.mgt.common; import org.wso2.carbon.device.mgt.core.dto.Device; import java.sql.Date; +import java.util.Comparator; import java.util.List; import java.util.Map; /** * This class will be the used to create policy object with relevant information for evaluating. */ -public class Policy { +public class Policy implements Comparable { + private int id; // Identifier of the policy. private int priorityId; // Priority of the policies. This will be used only for simple evaluation. private Profile profile; // Profile id @@ -186,4 +188,27 @@ public class Policy { public void setTenantId(int tenantId) { this.tenantId = tenantId; } + + + /* static final Comparator PRIORITY_ORDER = + new Comparator() { + public int compare(Policy p1, Policy p2) { + int dateCmp = new Integer(p2.getId()).compareTo(new Integer(p1.getId())); + if (dateCmp != 0) + return dateCmp; + + return (p1.getId() < p2.getId() ? -1 : + (p1.getId() == p2.getId() ? 0 : 1)); + } + };*/ + + @Override + public int compareTo(Policy o) { + if (this.priorityId == o.priorityId) + return 0; + else if ((this.priorityId) > o.priorityId) + return 1; + else + return -1; + } } \ No newline at end of file diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationException.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyEvaluationException.java similarity index 96% rename from components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationException.java rename to components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyEvaluationException.java index b779b6ab6c..3a0535c225 100644 --- a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationException.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyEvaluationException.java @@ -16,7 +16,7 @@ * under the License. */ -package org.wso2.carbon.simple.policy.decision.point; +package org.wso2.carbon.policy.mgt.common; public class PolicyEvaluationException extends Exception { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyEvaluationPoint.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyEvaluationPoint.java index 944e3aa620..0ee56812fe 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyEvaluationPoint.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.common/src/main/java/org/wso2/carbon/policy/mgt/common/PolicyEvaluationPoint.java @@ -34,7 +34,7 @@ public interface PolicyEvaluationPoint { * @param deviceIdentifier device information. * @return returns the effective policy. */ - Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier); + Policy getEffectivePolicies(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException; /** @@ -42,5 +42,5 @@ public interface PolicyEvaluationPoint { * @param deviceIdentifier device information. * @return returns the effective feature set. */ - List getEffectiveFeatures(DeviceIdentifier deviceIdentifier); + List getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException ; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java index 74a986e19e..536c7d535c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java @@ -641,12 +641,13 @@ public class PolicyDAOImpl implements PolicyDAO { int tenantId = -1234; try { conn = this.getConnection(); - String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID) VALUES (?, ?, ?)"; + String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY) VALUES (?, ?, ?)"; stmt = conn.prepareStatement(query, stmt.RETURN_GENERATED_KEYS); stmt.setString(1, policy.getPolicyName()); stmt.setInt(2, policy.getProfile().getProfileId()); stmt.setInt(3, tenantId); + stmt.setInt(4, readHighestPriorityOfPolicies()); int affectedRows = stmt.executeUpdate(); @@ -715,4 +716,30 @@ public class PolicyDAOImpl implements PolicyDAO { return deviceTypeId; } + + private int readHighestPriorityOfPolicies() throws PolicyManagerDAOException { + + Connection conn = null; + PreparedStatement stmt = null; + ResultSet resultSet = null; + try { + conn = this.getConnection(); + String query = "SELECT MAX(PRIORITY) PRIORITY FROM DM_POLICY;"; + stmt = conn.prepareStatement(query); + + resultSet = stmt.executeQuery(); + + while (resultSet.next()) { + return resultSet.getInt("PRIORITY"); + } + } catch (SQLException e) { + String msg = "Error occurred while reading the highest priority of the policies."; + log.error(msg, e); + throw new PolicyManagerDAOException(msg, e); + } finally { + PolicyManagementDAOUtil.cleanupResources(conn, stmt, resultSet); + } + return 0; + } + } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementDataHolder.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementDataHolder.java index 71457a6898..6269f62153 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementDataHolder.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementDataHolder.java @@ -18,6 +18,8 @@ package org.wso2.carbon.policy.mgt.core.internal; +import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; +import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint; import org.wso2.carbon.user.core.service.RealmService; import org.wso2.carbon.user.core.tenant.TenantManager; @@ -25,6 +27,8 @@ public class PolicyManagementDataHolder { private RealmService realmService; private TenantManager tenantManager; + private PolicyEvaluationPoint policyEvaluationPoint; + private PolicyInformationPoint policyInformationPoint; private static PolicyManagementDataHolder thisInstance = new PolicyManagementDataHolder(); private PolicyManagementDataHolder() {} @@ -53,5 +57,19 @@ public class PolicyManagementDataHolder { return tenantManager; } + public PolicyEvaluationPoint getPolicyEvaluationPoint() { + return policyEvaluationPoint; + } + + public void setPolicyEvaluationPoint(PolicyEvaluationPoint policyEvaluationPoint) { + this.policyEvaluationPoint = policyEvaluationPoint; + } + public PolicyInformationPoint getPolicyInformationPoint() { + return policyInformationPoint; + } + + public void setPolicyInformationPoint(PolicyInformationPoint policyInformationPoint) { + this.policyInformationPoint = policyInformationPoint; + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java index f7fce484b1..e0d2cea037 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/internal/PolicyManagementServiceComponent.java @@ -21,6 +21,7 @@ package org.wso2.carbon.policy.mgt.core.internal; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint; import org.wso2.carbon.policy.mgt.core.PolicyManager; import org.wso2.carbon.policy.mgt.core.config.PolicyConfigurationManager; @@ -44,6 +45,12 @@ import org.wso2.carbon.user.core.service.RealmService; * policy="dynamic" * bind="setPIPService" * unbind="unsetPIPService" + * @scr.reference name="org.wso2.carbon.devicemgt.simple.policy.evaluation.manager" + * interface="org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint" + * cardinality="1..1" + * policy="dynamic" + * bind="setPEPService" + * unbind="unsetPEPService" */ public class PolicyManagementServiceComponent { @@ -94,16 +101,33 @@ public class PolicyManagementServiceComponent { } - protected void setPIPService(PolicyInformationPoint policyInformationService) { + protected void setPIPService(PolicyInformationPoint pipService) { + if (log.isDebugEnabled()) { + log.debug("Setting Policy Information Service"); + } + PolicyManagementDataHolder.getInstance().setPolicyInformationPoint(pipService); + } + + protected void unsetPIPService(PolicyInformationPoint pipService) { + if (log.isDebugEnabled()) { + log.debug("Unsetting Policy Information Service"); + } + PolicyManagementDataHolder.getInstance().setPolicyInformationPoint(null); + } + + + protected void setPEPService(PolicyEvaluationPoint pepService) { if (log.isDebugEnabled()) { log.debug("Setting Policy Information Service"); } + PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint(pepService); } - protected void unsetPIPService(PolicyInformationPoint policyInformationService) { + protected void unsetPEPService(PolicyEvaluationPoint pepService) { if (log.isDebugEnabled()) { log.debug("Unsetting Policy Information Service"); } + PolicyManagementDataHolder.getInstance().setPolicyEvaluationPoint(null); } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql index cca3f4321a..a192fd4cce 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/sql/CreateMySqlTestDB.sql @@ -79,6 +79,7 @@ CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_POLICY` ( `NAME` VARCHAR(45) NULL DEFAULT NULL , `TENANT_ID` INT(11) NOT NULL , `PROFILE_ID` INT(11) NOT NULL , + `PRIORITY` INT NOT NULL , PRIMARY KEY (`ID`) , INDEX `FK_DM_PROFILE_DM_POLICY` (`PROFILE_ID` ASC) , CONSTRAINT `FK_DM_PROFILE_DM_POLICY` @@ -151,9 +152,16 @@ CREATE TABLE IF NOT EXISTS `WSO2CDM`.`DM_FEATURES` ( `ID` INT(11) NOT NULL AUTO_INCREMENT , `NAME` VARCHAR(256) NOT NULL , `CODE` VARCHAR(45) NULL DEFAULT NULL , + `DEVICE_TYPE_ID` INT NOT NULL , `DESCRIPTION` TEXT NULL DEFAULT NULL , `EVALUVATION_RULE` VARCHAR(60) NOT NULL , - PRIMARY KEY (`ID`) ) + PRIMARY KEY (`ID`) , + INDEX `DM_FEATURES_DEVICE_TYPE` (`DEVICE_TYPE_ID` ASC) , + CONSTRAINT `DM_FEATURES_DEVICE_TYPE` + FOREIGN KEY (`DEVICE_TYPE_ID` ) + REFERENCES `WSO2CDM`.`DM_DEVICE_TYPE` (`ID` ) + ON DELETE NO ACTION + ON UPDATE NO ACTION) ENGINE = InnoDB DEFAULT CHARACTER SET = latin1; diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/pom.xml b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/pom.xml index 58a7644503..d0a9d031ab 100644 --- a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/pom.xml +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/pom.xml @@ -34,6 +34,15 @@ ${carbon.device.mgt.version} Simple Policy Decision Point Bundle org.wso2.carbon.simple.policy.decision.point.internal + + org.osgi.framework, + org.osgi.service.component, + org.apache.commons.logging, + org.wso2.carbon.policy.mgt.common.*, + org.wso2.carbon.policy.mgt.core.*, + org.wso2.carbon.user.core.*, + org.wso2.carbon.device.mgt.common.* + org.wso2.carbon.simple.policy.decision.point.* diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationServiceImpl.java b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationServiceImpl.java index b66d5e143f..4fbacba173 100644 --- a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationServiceImpl.java +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/PolicyEvaluationServiceImpl.java @@ -19,20 +19,28 @@ package org.wso2.carbon.simple.policy.decision.point; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.policy.mgt.common.Feature; import org.wso2.carbon.policy.mgt.common.Policy; +import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; +import org.wso2.carbon.policy.mgt.common.ProfileFeature; import java.util.List; public class PolicyEvaluationServiceImpl implements PolicyEvaluationPoint { + + private SimpleEvaluationImpl evaluation; + + public PolicyEvaluationServiceImpl() { + evaluation = new SimpleEvaluationImpl(); + } + @Override - public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) { - return null; + public Policy getEffectivePolicies(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException { + return evaluation.getEffectivePolicy(deviceIdentifier); } @Override - public List getEffectiveFeatures(DeviceIdentifier deviceIdentifier) { - return null; + public List getEffectiveFeatures(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException { + return evaluation.getEffectivePolicy(deviceIdentifier).getProfile().getProfileFeaturesList(); } } diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluation.java b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluation.java index 8e4a55ab9d..2019f3f632 100644 --- a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluation.java +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluation.java @@ -19,13 +19,14 @@ package org.wso2.carbon.simple.policy.decision.point; -import org.wso2.carbon.policy.mgt.common.PIPDevice; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.policy.mgt.common.Policy; +import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException; public interface SimpleEvaluation { - void sortPolicy(Policy policy) throws PolicyEvaluationException; + void sortPolicy() throws PolicyEvaluationException; - Policy getEffectivePolicy(PIPDevice pipDevice) throws PolicyEvaluationException; + Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException; } diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java index f1697fc4ec..eb5b504b2e 100644 --- a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/SimpleEvaluationImpl.java @@ -20,28 +20,48 @@ package org.wso2.carbon.simple.policy.decision.point; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.policy.mgt.common.PIPDevice; import org.wso2.carbon.policy.mgt.common.Policy; -import org.wso2.carbon.policy.mgt.core.dao.impl.PolicyDAOImpl; +import org.wso2.carbon.policy.mgt.common.PolicyEvaluationException; +import org.wso2.carbon.policy.mgt.common.PolicyManagementException; +import org.wso2.carbon.policy.mgt.core.PolicyManager; +import org.wso2.carbon.simple.policy.decision.point.internal.PolicyDecisionPointDataHolder; + +import java.util.Collections; +import java.util.List; public class SimpleEvaluationImpl implements SimpleEvaluation { private static final Log log = LogFactory.getLog(SimpleEvaluationImpl.class); - - PolicyDAOImpl policyDAO; - + private PolicyManager policyManager; + private List policyList; public SimpleEvaluationImpl() { - policyDAO = new PolicyDAOImpl(); + policyManager = PolicyDecisionPointDataHolder.getInstance().getPolicyManager(); } @Override - public void sortPolicy(Policy policy) throws PolicyEvaluationException { + public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException { + try { + if (policyManager == null && policyList == null) { + PIPDevice pipDevice = policyManager.getPIP().getDeviceData(deviceIdentifier); + policyList = policyManager.getPIP().getRelatedPolicies(pipDevice); + } + sortPolicy(); + } catch (PolicyManagementException e) { + String msg = "Error occurred when retrieving the policy related data from policy management service."; + log.error(msg, e); + throw new PolicyEvaluationException(msg, e); + } + + return policyList.get(0); } + @Override - public Policy getEffectivePolicy(PIPDevice pipDevice) throws PolicyEvaluationException { - return null; + public void sortPolicy() throws PolicyEvaluationException { + Collections.sort(policyList); } } diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyDecisionPointDataHolder.java b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyDecisionPointDataHolder.java new file mode 100644 index 0000000000..30ba8466f2 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyDecisionPointDataHolder.java @@ -0,0 +1,53 @@ +/* +* Copyright (c) 2015 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you under the Apache License, +* Version 2.0 (the "License"); you may not use this file except +* in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +*/ + +package org.wso2.carbon.simple.policy.decision.point.internal; + +import org.wso2.carbon.policy.mgt.core.PolicyManager; +import org.wso2.carbon.user.core.service.RealmService; + +public class PolicyDecisionPointDataHolder { + + private RealmService realmService; + private PolicyManager policyManager; + + private static PolicyDecisionPointDataHolder dataHolder = new PolicyDecisionPointDataHolder(); + + private PolicyDecisionPointDataHolder() { + } + + public static PolicyDecisionPointDataHolder getInstance() { + return dataHolder; + } + + public RealmService getRealmService() { + return realmService; + } + + public void setRealmService(RealmService realmService) { + this.realmService = realmService; + } + + public PolicyManager getPolicyManager() { + return policyManager; + } + + public void setPolicyManager(PolicyManager policyManager) { + this.policyManager = policyManager; + } +} diff --git a/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyEvaluationServiceComponent.java b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyEvaluationServiceComponent.java new file mode 100644 index 0000000000..8d95706152 --- /dev/null +++ b/components/policy-mgt/org.wso2.carbon.simple.policy.decision.point/src/main/java/org/wso2/carbon/simple/policy/decision/point/internal/PolicyEvaluationServiceComponent.java @@ -0,0 +1,109 @@ +/* +* Copyright (c) 2015 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. +* +* WSO2 Inc. licenses this file to you under the Apache License, +* Version 2.0 (the "License"); you may not use this file except +* in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, +* software distributed under the License is distributed on an +* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +* KIND, either express or implied. See the License for the +* specific language governing permissions and limitations +* under the License. +*/ + +package org.wso2.carbon.simple.policy.decision.point.internal; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; +import org.wso2.carbon.policy.mgt.core.PolicyManager; +import org.wso2.carbon.simple.policy.decision.point.PolicyEvaluationServiceImpl; +import org.wso2.carbon.user.core.service.RealmService; + +/** + * @scr.component name="org.wso2.carbon.simple.policy.decision.PolicyEvaluationServiceComponent" immediate="true" + * @scr.reference name="user.realmservice.default" + * interface="org.wso2.carbon.user.core.service.RealmService" + * cardinality="1..1" + * policy="dynamic" + * bind="setRealmService" + * unbind="unsetRealmService" + * @scr.reference name="org.wso2.carbon.devicemgt.policy.manager" + * interface="org.wso2.carbon.policy.mgt.core.PolicyManager" + * cardinality="0..1" + * policy="dynamic" + * bind="setPolicyManagerService" + * unbind="unsetPolicyManagerService" + */ + +public class PolicyEvaluationServiceComponent { + + private static Log log = LogFactory.getLog(PolicyEvaluationServiceComponent.class); + + protected void activate(ComponentContext componentContext) { + if (log.isDebugEnabled()) { + log.debug("Activating the simple policy evaluation bundle."); + } + + try { + componentContext.getBundleContext().registerService(PolicyEvaluationPoint.class.getName(), + new PolicyEvaluationServiceImpl(), null); + } catch (Throwable t) { + log.error("Error occurred while initializing the simple policy evaluation bundle"); + } + } + + protected void deactivate(ComponentContext componentContext) { + if (log.isDebugEnabled()) { + log.debug("De-activating the simple policy evaluation bundle."); + } + } + + /** + * Sets Realm Service + * + * @param realmService An instance of RealmService + */ + protected void setRealmService(RealmService realmService) { + + if (log.isDebugEnabled()) { + log.debug("Setting Realm Service"); + } + PolicyDecisionPointDataHolder.getInstance().setRealmService(realmService); + } + + /** + * Unsets Realm Service + * + * @param realmService An instance of RealmService + */ + protected void unsetRealmService(RealmService realmService) { + if (log.isDebugEnabled()) { + log.debug("Unsetting Realm Service"); + } + PolicyDecisionPointDataHolder.getInstance().setRealmService(null); + } + + + protected void setPolicyManagerService(PolicyManager policyManagerService) { + if (log.isDebugEnabled()) { + log.debug("Unsetting PolicyManager Service"); + } + PolicyDecisionPointDataHolder.getInstance().setPolicyManager(policyManagerService); + } + + + protected void unsetPolicyManagerService(PolicyManager policyManagerService) { + if (log.isDebugEnabled()) { + log.debug("Unsetting PolicyManager Service"); + } + PolicyDecisionPointDataHolder.getInstance().setPolicyManager(null); + } + +} diff --git a/features/policy-mgt/org.wso2.carbon.policy.mgt.server.feature/pom.xml b/features/policy-mgt/org.wso2.carbon.policy.mgt.server.feature/pom.xml index 9e0f8ba3bb..f641eeac4c 100644 --- a/features/policy-mgt/org.wso2.carbon.policy.mgt.server.feature/pom.xml +++ b/features/policy-mgt/org.wso2.carbon.policy.mgt.server.feature/pom.xml @@ -47,7 +47,7 @@ org.wso2.carbon.devicemgt - org.wso2.carbon.complex.policy.decision.point + org.wso2.carbon.simple.policy.decision.point org.wso2.carbon.devicemgt @@ -110,7 +110,7 @@ org.wso2.carbon.devicemgt:org.wso2.carbon.policy.mgt.common:${carbon.device.mgt.version} - org.wso2.carbon.devicemgt:org.wso2.carbon.complex.policy.decision.point:${carbon.device.mgt.version} + org.wso2.carbon.devicemgt:org.wso2.carbon.simple.policy.decision.point:${carbon.device.mgt.version} org.wso2.carbon.devicemgt:org.wso2.carbon.policy.information.point:${carbon.device.mgt.version} diff --git a/pom.xml b/pom.xml index 7039e14dd3..79dce48675 100644 --- a/pom.xml +++ b/pom.xml @@ -141,6 +141,11 @@ org.wso2.carbon.complex.policy.decision.point ${carbon.device.mgt.version} + + org.wso2.carbon.devicemgt + org.wso2.carbon.simple.policy.decision.point + ${carbon.device.mgt.version} + org.wso2.carbon.devicemgt org.wso2.carbon.policy.information.point