From 2ef37b8aa8ca415114be6bf6b364c1673166e0d0 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Thu, 7 May 2015 16:31:39 +0530 Subject: [PATCH] Fixing issues related to apis to call and get effective policy --- .../mgt/core/dao/impl/FeatureDAOImpl.java | 3 +- .../mgt/core/dao/impl/PolicyDAOImpl.java | 9 +++--- .../core/impl/PolicyInformationPointImpl.java | 5 +++ .../PolicyManagementServiceComponent.java | 5 ++- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 31 ++++++++++++------- .../policy/mgt/core/PolicyDAOTestCase.java | 4 +-- .../mgt/core/util/DeviceTypeCreator.java | 2 +- .../policy/mgt/core/util/ProfileCreator.java | 2 +- .../src/test/resources/testdbconfig.xml | 4 +-- .../decision/point/SimpleEvaluationImpl.java | 11 +++++-- 10 files changed, 47 insertions(+), 29 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java index 276cf3a2ca..59485bd155 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/FeatureDAOImpl.java @@ -303,7 +303,7 @@ public class FeatureDAOImpl implements FeatureDAO { try { conn = this.getConnection(); String query = "SELECT PF.ID AS ID, PF.FEATURE_ID FEATURE_ID, F.NAME NAME, F.CODE CODE, " + - "F.EVALUATION_RULE RULE, F.CONTENT AS CONTENT FROM DM_PROFILE_FEATURES AS PF " + + "F.EVALUATION_RULE RULE, PF.CONTENT AS CONTENT, PF.PROFILE_ID PROFILE_ID FROM DM_PROFILE_FEATURES AS PF " + "JOIN DM_FEATURES AS F ON F.ID = PF.FEATURE_ID"; stmt = conn.prepareStatement(query); resultSet = stmt.executeQuery(); @@ -320,6 +320,7 @@ public class FeatureDAOImpl implements FeatureDAO { profileFeature.setFeature(feature); profileFeature.setId(resultSet.getInt("ID")); profileFeature.setContent(resultSet.getObject("CONTENT")); + profileFeature.setProfileId(resultSet.getInt("PROFILE_ID")); featureList.add(profileFeature); } 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 b57841dd4d..a475279e99 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 @@ -536,7 +536,7 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setTimestamp(4, currentTimestamp); stmt.setTimestamp(5, currentTimestamp); - stmt.executeQuery(); + stmt.executeUpdate(); } catch (SQLException e) { String msg = "Error occurred while adding the evaluated feature list to device."; @@ -562,7 +562,7 @@ public class PolicyDAOImpl implements PolicyDAO { stmt.setBoolean(2, true); stmt.setInt(3, deviceId); - stmt.executeQuery(); + stmt.executeUpdate(); } catch (SQLException e) { String msg = "Error occurred while updating applied policy to device (" + deviceId + ")"; @@ -583,14 +583,15 @@ public class PolicyDAOImpl implements PolicyDAO { Timestamp currentTimestamp = new Timestamp(Calendar.getInstance().getTime().getTime()); try { conn = this.getConnection(); - String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET POLICY_ID = ?, POLICY_CONTENT = ? UPDATED_TIME = ?, APPLIED = ? WHERE DEVICE_ID = ?"; + String query = "UPDATE DM_DEVICE_POLICY_APPLIED SET POLICY_ID = ?, POLICY_CONTENT = ?, UPDATED_TIME = ?, " + + "APPLIED = ? WHERE DEVICE_ID = ?"; stmt = conn.prepareStatement(query); stmt.setInt(1, policyId); stmt.setObject(2, profileFeatures); stmt.setTimestamp(3, currentTimestamp); stmt.setBoolean(4, false); stmt.setInt(5, deviceId); - stmt.executeQuery(); + stmt.executeUpdate(); } catch (SQLException e) { String msg = "Error occurred while updating the evaluated feature list to device."; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java index 75b212a389..12cd31c126 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyInformationPointImpl.java @@ -61,6 +61,7 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint { DeviceType deviceType = new DeviceType(); deviceType.setName(deviceIdentifier.getType()); + deviceManagementService = getDeviceManagementService(); try { device = deviceManagementService.getDevice(deviceIdentifier); @@ -146,4 +147,8 @@ public class PolicyInformationPointImpl implements PolicyInformationPoint { return finalPolicies; } + private DeviceManagementService getDeviceManagementService() { + return PolicyManagementDataHolder.getInstance().getDeviceManagementService(); + } + } 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 dc65f1a587..fcf9247d03 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 @@ -23,13 +23,12 @@ import org.apache.commons.logging.LogFactory; import org.osgi.service.component.ComponentContext; import org.wso2.carbon.device.mgt.core.service.DeviceManagementService; import org.wso2.carbon.policy.mgt.common.PolicyEvaluationPoint; -import org.wso2.carbon.policy.mgt.common.PolicyInformationPoint; import org.wso2.carbon.policy.mgt.core.PolicyManagerService; +import org.wso2.carbon.policy.mgt.core.PolicyManagerServiceImpl; import org.wso2.carbon.policy.mgt.core.config.PolicyConfigurationManager; import org.wso2.carbon.policy.mgt.core.config.PolicyManagementConfig; import org.wso2.carbon.policy.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagementDAOFactory; -import org.wso2.carbon.policy.mgt.core.service.PolicyManagementService; import org.wso2.carbon.user.core.service.RealmService; /** @@ -67,7 +66,7 @@ public class PolicyManagementServiceComponent { PolicyManagementDAOFactory.init(dsConfig); componentContext.getBundleContext().registerService( - PolicyManagerService.class.getName(), new PolicyManagementService(), null); + PolicyManagerService.class.getName(), new PolicyManagerServiceImpl(), null); } catch (Throwable t) { log.error("Error occurred while initializing the Policy management core.", t); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java index 63ddc5ff0a..89cd3eefae 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/PolicyManagerImpl.java @@ -27,12 +27,10 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.device.mgt.core.dto.Device; import org.wso2.carbon.device.mgt.core.dto.DeviceType; -import org.wso2.carbon.policy.mgt.common.Policy; -import org.wso2.carbon.policy.mgt.common.PolicyManagementException; -import org.wso2.carbon.policy.mgt.common.Profile; -import org.wso2.carbon.policy.mgt.common.ProfileFeature; +import org.wso2.carbon.policy.mgt.common.*; import org.wso2.carbon.policy.mgt.core.dao.*; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; +import org.wso2.carbon.policy.mgt.core.mgt.ProfileManager; import java.util.ArrayList; import java.util.List; @@ -44,6 +42,7 @@ public class PolicyManagerImpl implements PolicyManager { private FeatureDAO featureDAO; private DeviceDAO deviceDAO; private DeviceTypeDAO deviceTypeDAO; + private ProfileManager profileManager; private static Log log = LogFactory.getLog(PolicyManagerImpl.class); public PolicyManagerImpl() { @@ -52,6 +51,7 @@ public class PolicyManagerImpl implements PolicyManager { this.featureDAO = PolicyManagementDAOFactory.getFeatureDAO(); this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); + this.profileManager = new ProfileManagerImpl(); } @Override @@ -463,13 +463,16 @@ public class PolicyManagerImpl implements PolicyManager { List policies = new ArrayList(); try { - DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName); - List profileList = profileDAO.getProfilesOfDeviceType(deviceType); +// DeviceType deviceType = deviceTypeDAO.getDeviceType(deviceTypeName); + + List profileList = profileManager.getProfilesOfDeviceType(deviceTypeName); List allPolicies = policyDAO.getAllPolicies(); + for (Profile profile : profileList) { for (Policy policy : allPolicies) { if (policy.getProfileId() == profile.getProfileId()) { + policy.setProfile(profile); policies.add(policy); } } @@ -479,12 +482,16 @@ public class PolicyManagerImpl implements PolicyManager { String msg = "Error occurred while getting all the policies."; log.error(msg, e); throw new PolicyManagementException(msg, e); - } catch (ProfileManagerDAOException e) { - String msg = "Error occurred while getting the profiles related to device type (" + deviceTypeName + ")"; - log.error(msg, e); - throw new PolicyManagementException(msg, e); - } catch (DeviceManagementDAOException e) { - String msg = "Error occurred while getting device type object related to (" + deviceTypeName + ")"; +// } catch (ProfileManagerDAOException e) { +// String msg = "Error occurred while getting the profiles related to device type (" + deviceTypeName + ")"; +// log.error(msg, e); +// throw new PolicyManagementException(msg, e); +// } catch (DeviceManagementDAOException e) { +// String msg = "Error occurred while getting device type object related to (" + deviceTypeName + ")"; +// log.error(msg, e); +// throw new PolicyManagementException(msg, e); + } catch (ProfileManagementException e) { + String msg = "Error occurred while getting all the profile features."; log.error(msg, e); throw new PolicyManagementException(msg, e); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java index 12312d12de..7e0621e89a 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/PolicyDAOTestCase.java @@ -235,7 +235,7 @@ public class PolicyDAOTestCase { List deviceIdentifierList = new ArrayList(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setId(device.getDeviceIdentificationId()); - deviceIdentifier.setType("ANDROID"); + deviceIdentifier.setType("android"); deviceIdentifierList.add(deviceIdentifier); policyManager.addPolicyToDevice(deviceIdentifierList, policy); @@ -266,7 +266,7 @@ public class PolicyDAOTestCase { public void getDeviceTypeRelatedPolicy() throws PolicyManagementException { PolicyAdministratorPoint policyAdministratorPoint = new PolicyAdministratorPointImpl(); - List policyList = policyAdministratorPoint.getPoliciesOfDeviceType("ANDROID"); + List policyList = policyAdministratorPoint.getPoliciesOfDeviceType("android"); log.debug("----------Device type related policy---------"); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/DeviceTypeCreator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/DeviceTypeCreator.java index e8e2188ddd..5dc9e981e2 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/DeviceTypeCreator.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/DeviceTypeCreator.java @@ -25,7 +25,7 @@ public class DeviceTypeCreator { public static DeviceType getDeviceType(){ DeviceType deviceType = new DeviceType(); - deviceType.setName("ANDROID"); + deviceType.setName("android"); deviceType.setId(1); return deviceType; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileCreator.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileCreator.java index 0095f8d987..332e464422 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileCreator.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/util/ProfileCreator.java @@ -32,7 +32,7 @@ public class ProfileCreator { DeviceType deviceType = new DeviceType(); deviceType.setId(1); - deviceType.setName("ANDROID"); + deviceType.setName("android"); profile.setProfileFeaturesList(ProfileFeatureCreator.getProfileFeature(features)); profile.setProfileName("Test Profile"); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testdbconfig.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testdbconfig.xml index 6241a0aefe..936272480e 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testdbconfig.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testdbconfig.xml @@ -19,10 +19,10 @@ - jdbc:mysql://10.100.0.47:3306/WSO2CDM + jdbc:mysql://localhost:3306/WSO2CDM com.mysql.jdbc.Driver root - root + jdbc:h2:mem:WSO2_TEST_DB;DB_CLOSE_ON_EXIT=FALSE;LOCK_TIMEOUT=60000 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 26ee12c15c..bd15caec36 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 @@ -36,15 +36,16 @@ public class SimpleEvaluationImpl implements SimpleEvaluation { private PolicyManagerService policyManagerService; private List policyList = new ArrayList(); - public SimpleEvaluationImpl() { - policyManagerService = PolicyDecisionPointDataHolder.getInstance().getPolicyManagerService(); - } +// public SimpleEvaluationImpl() { +// policyManagerService = PolicyDecisionPointDataHolder.getInstance().getPolicyManagerService(); +// } @Override public Policy getEffectivePolicy(DeviceIdentifier deviceIdentifier) throws PolicyEvaluationException { Policy policy = new Policy(); PolicyAdministratorPoint policyAdministratorPoint; PolicyInformationPoint policyInformationPoint; + policyManagerService = getPolicyManagerService(); try { if (policyManagerService != null) { @@ -74,4 +75,8 @@ public class SimpleEvaluationImpl implements SimpleEvaluation { public void sortPolicies() throws PolicyEvaluationException { Collections.sort(policyList); } + + private PolicyManagerService getPolicyManagerService(){ + return PolicyDecisionPointDataHolder.getInstance().getPolicyManagerService(); + } }