From 1025e9a4259e8fbdaf6798796f01884b43aaad1a Mon Sep 17 00:00:00 2001 From: Dulitha Wijewantha Date: Fri, 16 Oct 2015 21:05:51 +0530 Subject: [PATCH 1/4] Fixes to user api --- .../carbon/policy/mgt/core/dao/PolicyDAO.java | 7 ++++ .../mgt/core/dao/impl/PolicyDAOImpl.java | 42 ++++++++++++++----- .../mgt/core/mgt/impl/PolicyManagerImpl.java | 5 ++- .../policy/mgt/core/PolicyDAOTestCase.java | 1 - 4 files changed, 42 insertions(+), 13 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java index d684525148..9fa1259b31 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/PolicyDAO.java @@ -41,6 +41,13 @@ public interface PolicyDAO { */ Policy addPolicyToRole(List roleNames, Policy policy) throws PolicyManagerDAOException; + /** + * This method is used to add/update the users associated with the policy. + * @param usernameList - List of the users that needs to be applied + * @param policy - policy object with the current role list + * @return + * @throws PolicyManagerDAOException + */ Policy addPolicyToUser(List usernameList, Policy policy) throws PolicyManagerDAOException; Policy addPolicyToDevice(List devices, Policy policy) throws PolicyManagerDAOException; 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 b33b71289e..7df06e8fe6 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 @@ -110,23 +110,45 @@ public class PolicyDAOImpl implements PolicyDAO { } @Override - public Policy addPolicyToUser(List usernameList, Policy policy) throws PolicyManagerDAOException { + public Policy addPolicyToUser(List usersToAdd, Policy policy) throws PolicyManagerDAOException { Connection conn; - PreparedStatement stmt = null; + PreparedStatement insertStmt = null; + PreparedStatement deleteStmt = null; + final List currentUsers = policy.getUsers(); + + SetReferenceTransformer transformer = new SetReferenceTransformer(); + + transformer.transform(currentUsers, usersToAdd); + usersToAdd = transformer.getObjectsToAdd(); + List usersToDelete = transformer.getObjectsToRemove(); try { conn = this.getConnection(); - String query = "INSERT INTO DM_USER_POLICY (POLICY_ID, USERNAME) VALUES (?, ?)"; - stmt = conn.prepareStatement(query); - for (String username : usernameList) { - stmt.setInt(1, policy.getId()); - stmt.setString(2, username); - stmt.addBatch(); + if (usersToAdd.size() > 0){ + String query = "INSERT INTO DM_USER_POLICY (POLICY_ID, USERNAME) VALUES (?, ?)"; + insertStmt = conn.prepareStatement(query); + for (String username : usersToAdd) { + insertStmt.setInt(1, policy.getId()); + insertStmt.setString(2, username); + insertStmt.addBatch(); + } + insertStmt.executeBatch(); } - stmt.executeBatch(); + if (usersToDelete.size() > 0){ + String deleteQuery = "DELETE FROM DM_USER_POLICY WHERE USERNAME=? AND POLICY_ID=?"; + deleteStmt = conn.prepareStatement(deleteQuery); + for (String username : usersToDelete) { + deleteStmt.setString(1, username); + deleteStmt.setInt(2, policy.getId()); + deleteStmt.addBatch(); + } + deleteStmt.executeBatch(); + } + } catch (SQLException e) { throw new PolicyManagerDAOException("Error occurred while adding the user name with policy to database", e); } finally { - PolicyManagementDAOUtil.cleanupResources(stmt, null); + PolicyManagementDAOUtil.cleanupResources(insertStmt, null); + PolicyManagementDAOUtil.cleanupResources(deleteStmt, null); } return policy; } 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 7474651d0d..263817802b 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 @@ -471,17 +471,18 @@ public class PolicyManagerImpl implements PolicyManager { Policy policy; List deviceList; List roleNames; - + List userNames; try { PolicyManagementDAOFactory.openConnection(); policy = policyDAO.getPolicy(policyId); roleNames = policyDAO.getPolicyAppliedRoles(policyId); + userNames = policyDAO.getPolicyAppliedUsers(policyId); Profile profile = profileDAO.getProfile(policy.getProfileId()); policy.setProfile(profile); policy.setRoles(roleNames); - + policy.setUsers(userNames); } catch (PolicyManagerDAOException e) { throw new PolicyManagementException("Error occurred while getting the policy related to policy ID (" + 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 730e02071c..b17fda051c 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 @@ -311,7 +311,6 @@ public class PolicyDAOTestCase extends BasePolicyManagementDAOTest { policy = pap.addPolicy(policy); pap.activatePolicy(policy.getId()); List users = new ArrayList<>(); - log.debug(policy.getRoles().size()); users.add("Udara"); users.add("Dileesha"); policy.setUsers(users); From 5962694ec2f920cb00aaa1ab31fa01509db39e8d Mon Sep 17 00:00:00 2001 From: Dulitha Wijewantha Date: Fri, 16 Oct 2015 23:37:58 +0530 Subject: [PATCH 2/4] Adding annotation for the description --- .../src/main/java/org/wso2/carbon/policy/mgt/common/Policy.java | 1 + 1 file changed, 1 insertion(+) 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 4299549f96..c5b8ed303d 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 @@ -171,6 +171,7 @@ public class Policy implements Comparable, Serializable { this.updated = updated; } + @XmlElement public String getDescription() { return description; } From 8a8a152d363c4d63bfd548c2d88128ad50087df4 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Fri, 16 Oct 2015 23:47:16 +0530 Subject: [PATCH 3/4] Adding the policy task config --- .../mgt/core/dao/impl/PolicyDAOImpl.java | 4 ++ .../PolicyManagementServiceComponent.java | 5 ++- .../mgt/core/util/PolicyManagerUtil.java | 44 ++++++++++++++++++- 3 files changed, 50 insertions(+), 3 deletions(-) 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 8513540ea4..7f87b93a7b 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 @@ -744,6 +744,8 @@ public class PolicyDAOImpl implements PolicyDAO { policy.setProfileId(resultSet.getInt("PROFILE_ID")); policy.setCompliance(resultSet.getString("COMPLIANCE")); policy.setDescription(resultSet.getString("DESCRIPTION")); + policy.setUpdated(PolicyManagerUtil.convertIntToBoolean(resultSet.getInt("UPDATED"))); + policy.setActive(PolicyManagerUtil.convertIntToBoolean(resultSet.getInt("ACTIVE"))); } return policy; @@ -778,6 +780,8 @@ public class PolicyDAOImpl implements PolicyDAO { policy.setPriorityId(resultSet.getInt("PRIORITY")); policy.setCompliance(resultSet.getString("COMPLIANCE")); policy.setDescription(resultSet.getString("DESCRIPTION")); + policy.setUpdated(PolicyManagerUtil.convertIntToBoolean(resultSet.getInt("UPDATED"))); + policy.setActive(PolicyManagerUtil.convertIntToBoolean(resultSet.getInt("ACTIVE"))); } return policy; } catch (SQLException e) { 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 1b6f10c162..c7fb6a3251 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 @@ -35,6 +35,7 @@ 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.task.TaskScheduleService; import org.wso2.carbon.policy.mgt.core.task.TaskScheduleServiceImpl; +import org.wso2.carbon.policy.mgt.core.util.PolicyManagerUtil; import org.wso2.carbon.user.core.service.RealmService; /** @@ -86,11 +87,13 @@ public class PolicyManagementServiceComponent { componentContext.getBundleContext().registerService( PolicyManagerService.class.getName(), new PolicyManagerServiceImpl(), null); + + PolicyConfiguration policyConfiguration = DeviceConfigurationManager.getInstance().getDeviceManagementConfig(). getDeviceManagementConfigRepository().getPolicyConfiguration(); if(policyConfiguration.getMonitoringEnable()) { TaskScheduleService taskScheduleService = new TaskScheduleServiceImpl(); - taskScheduleService.startTask(policyConfiguration.getMonitoringFrequency()); + taskScheduleService.startTask(PolicyManagerUtil.getMonitoringFequency()); } } catch (Throwable t) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index a9d292c732..502740018b 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -22,7 +22,14 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.dom.Document; import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationEntry; +import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException; +import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfiguration; +import org.wso2.carbon.device.mgt.common.configuration.mgt.TenantConfigurationManagementService; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; +import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; +import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration; +import org.wso2.carbon.device.mgt.core.config.tenant.TenantConfigurationManagementServiceImpl; import org.wso2.carbon.device.mgt.core.operation.mgt.PolicyOperation; import org.wso2.carbon.device.mgt.core.operation.mgt.ProfileOperation; import org.wso2.carbon.policy.mgt.common.Policy; @@ -47,6 +54,10 @@ public class PolicyManagerUtil { private static final Log log = LogFactory.getLog(PolicyManagerUtil.class); + public static final String GENERAL_CONFIG_RESOURCE_PATH = "general"; + public static final String MONITORING_FREQUENCY = "notifierFrequency"; + + public static Document convertToDocument(File file) throws PolicyManagementException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); @@ -156,13 +167,13 @@ public class PolicyManagerUtil { // } - public static Cache getPolicyCache(String name){ + public static Cache getPolicyCache(String name) { CacheManager manager = getCacheManager(); return (manager != null) ? manager.getCache(name) : Caching.getCacheManager().getCache(name); } - public static Cache> getPolicyListCache(String name){ + public static Cache> getPolicyListCache(String name) { CacheManager manager = getCacheManager(); return (manager != null) ? manager.>getCache(name) : Caching.getCacheManager().>getCache(name); @@ -182,4 +193,33 @@ public class PolicyManagerUtil { } return deviceHashMap; } + + + public static int getMonitoringFequency() { + + TenantConfigurationManagementService configMgtService = new TenantConfigurationManagementServiceImpl(); + TenantConfiguration tenantConfiguration = null; + int monitoringFrequency = 0; + try { + tenantConfiguration = configMgtService.getConfiguration(GENERAL_CONFIG_RESOURCE_PATH); + List configuration = tenantConfiguration.getConfiguration(); + + for (ConfigurationEntry cEntry : configuration) { + if (cEntry.getName().equalsIgnoreCase(MONITORING_FREQUENCY)) { + monitoringFrequency = (int) cEntry.getValue(); + } + } + + } catch (ConfigurationManagementException e) { + log.error("Error while getting the configurations from registry.", e); + } + + if (monitoringFrequency == 0) { + PolicyConfiguration policyConfiguration = DeviceConfigurationManager.getInstance(). + getDeviceManagementConfig().getDeviceManagementConfigRepository().getPolicyConfiguration(); + monitoringFrequency = policyConfiguration.getMonitoringFrequency(); + } + + return monitoringFrequency; + } } From d15eb10636ece6e60dd8bc80cf7507013ca3bb69 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Sat, 17 Oct 2015 17:08:12 +0530 Subject: [PATCH 4/4] Adding the null check to monitiring frequncy and fixing the servlet-api issue which is caused a compilation error --- .../policy/mgt/core/util/PolicyManagerUtil.java | 8 +++++--- pom.xml | 12 ++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java index 502740018b..990cb24875 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/util/PolicyManagerUtil.java @@ -204,9 +204,11 @@ public class PolicyManagerUtil { tenantConfiguration = configMgtService.getConfiguration(GENERAL_CONFIG_RESOURCE_PATH); List configuration = tenantConfiguration.getConfiguration(); - for (ConfigurationEntry cEntry : configuration) { - if (cEntry.getName().equalsIgnoreCase(MONITORING_FREQUENCY)) { - monitoringFrequency = (int) cEntry.getValue(); + if (configuration != null && !configuration.isEmpty()) { + for (ConfigurationEntry cEntry : configuration) { + if (cEntry.getName().equalsIgnoreCase(MONITORING_FREQUENCY)) { + monitoringFrequency = (int) cEntry.getValue(); + } } } diff --git a/pom.xml b/pom.xml index 12b522c5bb..770dffd2c4 100644 --- a/pom.xml +++ b/pom.xml @@ -312,6 +312,10 @@ commons-pool.wso2 commons-pool + + javax.servlet + servlet-api + @@ -386,6 +390,10 @@ org.wso2.carbon.registry org.wso2.carbon.registry.extensions + + javax.servlet + servlet-api + @@ -808,6 +816,10 @@ org.wso2.carbon.registry org.wso2.carbon.registry.ws.client + + javax.servlet + servlet-api +