From d25779774bb017c5be639668e43e6e36aff85e48 Mon Sep 17 00:00:00 2001 From: "tcdlpds@gmail.com" Date: Sat, 7 Nov 2020 00:31:02 +0530 Subject: [PATCH] Improve MDM policy functionality --- ...ApplicationManagementPublisherAPIImpl.java | 14 +-- .../impl/PolicyManagementServiceImpl.java | 10 +- .../policy/mgt/PolicyMonitoringManager.java | 1 - .../DeviceManagementProviderServiceImpl.java | 1 - .../mgt/DefaultPolicyMonitoringManager.java | 7 +- .../mgt/core/dao/impl/MonitoringDAOImpl.java | 13 +- .../mgt/core/dao/impl/PolicyDAOImpl.java | 115 +++--------------- .../mgt/core/enforcement/DelegationTask.java | 15 +-- .../PolicyEnforcementDelegatorImpl.java | 5 +- .../impl/PolicyAdministratorPointImpl.java | 58 +-------- .../mgt/core/impl/PolicyFilterImpl.java | 2 +- .../mgt/bean/UpdatedPolicyDeviceListBean.java | 2 - .../core/mgt/impl/MonitoringManagerImpl.java | 1 - .../mgt/core/mgt/impl/PolicyManagerImpl.java | 6 +- .../mgt/core/mgt/impl/ProfileManagerImpl.java | 31 ++--- .../core/task/TaskScheduleServiceImpl.java | 2 +- 16 files changed, 66 insertions(+), 217 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java index a7dc95940f4..2b53e65da7f 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.publisher.api/src/main/java/org/wso2/carbon/device/application/mgt/publisher/api/services/impl/ApplicationManagementPublisherAPIImpl.java @@ -334,7 +334,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem log.error(msg, e); return Response.status(Response.Status.BAD_REQUEST).entity(msg).build(); } catch (ApplicationManagementException e) { - String msg = "Error occurred while creating a costom application"; + String msg = "Error occurred while creating a custom application"; log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } catch (RequestValidatingException e) { @@ -716,7 +716,7 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem .changeLifecycleState(applicationUuid, lifecycleChanger); return Response.status(Response.Status.CREATED).entity(applicationRelease).build(); } catch (BadRequestException e) { - String msg = "Request payload contains invalid data, hence veryfy the request payload."; + String msg = "Request payload contains invalid data, hence verify the request payload."; log.error(msg, e); return Response.status(Response.Status.BAD_REQUEST).build(); } catch (ForbiddenException e) { @@ -1005,10 +1005,10 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem } if (attachmentList != null && !attachmentList.isEmpty()) { - Map scrrenshotData = new TreeMap<>(); + Map screenshotData = new TreeMap<>(); for (Attachment sc : attachmentList) { dataHandler = sc.getDataHandler(); - String screenshotrFileName = dataHandler.getName(); + String screenshotFileName = dataHandler.getName(); InputStream screenshotStream = dataHandler.getInputStream(); if (screenshotStream == null) { String msg = @@ -1017,16 +1017,16 @@ public class ApplicationManagementPublisherAPIImpl implements ApplicationManagem log.error(msg); throw new BadRequestException(msg); } - if (screenshotrFileName == null) { + if (screenshotFileName == null) { String msg = "Screenshot file name retrieving is failed for one screenshot. Hence can't proceed. " + "Please verify the screenshots."; log.error(msg); throw new BadRequestException(msg); } - scrrenshotData.put(screenshotrFileName, screenshotStream); + screenshotData.put(screenshotFileName, screenshotStream); } - applicationArtifact.setScreenshots(scrrenshotData); + applicationArtifact.setScreenshots(screenshotData); } return applicationArtifact; } catch (IOException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java index b92fa01e1d3..fa93c46c60c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/PolicyManagementServiceImpl.java @@ -136,6 +136,14 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { } } + /** + * GEt {@link Policy} from {@link PolicyWrapper} + * + * @param policyWrapper {@link PolicyWrapper} + * @return {@link Policy} + * @throws DeviceManagementException if error occurred while creating {@link Policy} object from + * {@link PolicyWrapper} + */ private Policy getPolicyFromWrapper(@Valid PolicyWrapper policyWrapper) throws DeviceManagementException { Policy policy = new Policy(); policy.setPolicyName(policyWrapper.getPolicyName()); @@ -151,7 +159,7 @@ public class PolicyManagementServiceImpl implements PolicyManagementService { policy.setPolicyPayloadVersion(policyWrapper.getPayloadVersion()); policy.setCorrectiveActions(policyWrapper.getCorrectiveActions()); //TODO iterates the device identifiers to create the object. need to implement a proper DAO layer here. - List devices = new ArrayList(); + List devices = new ArrayList<>(); List deviceIdentifiers = policyWrapper.getDeviceIdentifiers(); if (deviceIdentifiers != null) { for (DeviceIdentifier id : deviceIdentifiers) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/PolicyMonitoringManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/PolicyMonitoringManager.java index e4bd294e243..dce81f95caf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/PolicyMonitoringManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/policy/mgt/PolicyMonitoringManager.java @@ -19,7 +19,6 @@ package org.wso2.carbon.device.mgt.common.policy.mgt; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.NonComplianceData; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 8aa03908201..d453f88a5d7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -150,7 +150,6 @@ import java.io.IOException; import java.io.StringWriter; import java.sql.SQLException; import java.util.ArrayList; -import java.util.Arrays; import java.util.Calendar; import java.util.Collections; import java.util.Date; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/policy/mgt/DefaultPolicyMonitoringManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/policy/mgt/DefaultPolicyMonitoringManager.java index 8a0bc9c2929..2b44124c1a5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/policy/mgt/DefaultPolicyMonitoringManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/policy/mgt/DefaultPolicyMonitoringManager.java @@ -25,7 +25,6 @@ import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.ComplianceFeature; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.NonComplianceData; -import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; import java.util.ArrayList; import java.util.List; @@ -35,10 +34,10 @@ import java.util.List; */ public class DefaultPolicyMonitoringManager implements PolicyMonitoringManager { - private static Log log = LogFactory.getLog(DefaultPolicyMonitoringManager.class); + private static final Log log = LogFactory.getLog(DefaultPolicyMonitoringManager.class); + @Override - public NonComplianceData checkPolicyCompliance(DeviceIdentifier deviceIdentifier, Policy policy, Object response) - throws PolicyComplianceException { + public NonComplianceData checkPolicyCompliance(DeviceIdentifier deviceIdentifier, Policy policy, Object response) { if (log.isDebugEnabled()) { log.debug("Checking policy compliance status of device '" + deviceIdentifier.getId() + "'"); } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java index e9f8abcf774..25531d57578 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/MonitoringDAOImpl.java @@ -188,14 +188,6 @@ public class MonitoringDAOImpl implements MonitoringDAO { stmt.setInt(4, tenantId); stmt.setInt(5, enrolmentId); stmt.executeUpdate(); - -// generatedKeys = stmt.getGeneratedKeys(); -// if (generatedKeys.next()) { -// return generatedKeys.getInt(1); -// } else { -// return 0; -// } - } catch (SQLException e) { throw new MonitoringDAOException("Error occurred while deleting the none compliance to the database.", e); } finally { @@ -442,7 +434,7 @@ public class MonitoringDAOImpl implements MonitoringDAO { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; - List complianceFeatures = new ArrayList(); + List complianceFeatures = new ArrayList<>(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { @@ -488,8 +480,7 @@ public class MonitoringDAOImpl implements MonitoringDAO { } - private Connection getConnection() throws MonitoringDAOException { + private Connection getConnection() { return PolicyManagementDAOFactory.getConnection(); } - } 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 bf3635cd953..063b5c47a89 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 @@ -75,41 +75,13 @@ public class PolicyDAOImpl implements PolicyDAO { return persistPolicy(policy); } -// @Override -// public Policy addPolicyToDeviceType(String deviceType, Policy policy) throws PolicyManagerDAOException { -// Connection conn; -// PreparedStatement stmt = null; -// try { -// conn = this.getConnection(); -// String query = "INSERT INTO DM_DEVICE_TYPE_POLICY (DEVICE_TYPE_ID, POLICY_ID) VALUES (?, ?)"; -// stmt = conn.prepareStatement(query); -// stmt.setInt(1, getDeviceTypeId(deviceType)); -// stmt.setInt(2, policy.getId()); -// stmt.executeQuery(); -// } catch (SQLException e) { -// throw new PolicyManagerDAOException("Error occurred while adding the device type policy to database.", e); -// } finally { -// PolicyManagementDAOUtil.cleanupResources(stmt, null); -// } -// return policy; -// -// } - @Override public Policy addPolicyToRole(List rolesToAdd, Policy policy) throws PolicyManagerDAOException { Connection conn; PreparedStatement insertStmt = null; -// PreparedStatement deleteStmt = null; -// final List currentRoles = this.getPolicy(policy.getId()).getRoles(); -// -// SetReferenceTransformer transformer = new SetReferenceTransformer(); -// -// transformer.transform(currentRoles, rolesToAdd); -// rolesToAdd = transformer.getObjectsToAdd(); -// List rolesToDelete = transformer.getObjectsToRemove(); try { conn = this.getConnection(); - if (rolesToAdd.size() > 0) { + if (!rolesToAdd.isEmpty()) { String query = "INSERT INTO DM_ROLE_POLICY (ROLE_NAME, POLICY_ID) VALUES (?, ?)"; insertStmt = conn.prepareStatement(query); for (String role : rolesToAdd) { @@ -119,16 +91,6 @@ public class PolicyDAOImpl implements PolicyDAO { } insertStmt.executeBatch(); } -// if (rolesToDelete.size() > 0){ -// String deleteQuery = "DELETE FROM DM_ROLE_POLICY WHERE ROLE_NAME=? AND POLICY_ID=?"; -// deleteStmt = conn.prepareStatement(deleteQuery); -// for (String role : rolesToDelete) { -// deleteStmt.setString(1, role); -// deleteStmt.setInt(2, policy.getId()); -// deleteStmt.addBatch(); -// } -// deleteStmt.executeBatch(); -// } } catch (SQLException e) { throw new PolicyManagerDAOException("Error occurred while adding the role name with policy to database", e); } finally { @@ -146,14 +108,14 @@ public class PolicyDAOImpl implements PolicyDAO { final List currentRoles = previousPolicy.getRoles(); - SetReferenceTransformer transformer = new SetReferenceTransformer(); + SetReferenceTransformer transformer = new SetReferenceTransformer<>(); transformer.transform(currentRoles, rolesToAdd); rolesToAdd = transformer.getObjectsToAdd(); List rolesToDelete = transformer.getObjectsToRemove(); try { conn = this.getConnection(); - if (rolesToAdd.size() > 0) { + if (!rolesToAdd.isEmpty()) { String query = "INSERT INTO DM_ROLE_POLICY (ROLE_NAME, POLICY_ID) VALUES (?, ?)"; insertStmt = conn.prepareStatement(query); for (String role : rolesToAdd) { @@ -163,7 +125,7 @@ public class PolicyDAOImpl implements PolicyDAO { } insertStmt.executeBatch(); } - if (rolesToDelete.size() > 0) { + if (!rolesToDelete.isEmpty()) { String deleteQuery = "DELETE FROM DM_ROLE_POLICY WHERE ROLE_NAME=? AND POLICY_ID=?"; deleteStmt = conn.prepareStatement(deleteQuery); for (String role : rolesToDelete) { @@ -186,17 +148,9 @@ public class PolicyDAOImpl implements PolicyDAO { public Policy addPolicyToUser(List usersToAdd, Policy policy) throws PolicyManagerDAOException { Connection conn; PreparedStatement insertStmt = null; -// PreparedStatement deleteStmt = null; -// final List currentUsers = this.getPolicy(policy.getId()).getUsers(); -// -// SetReferenceTransformer transformer = new SetReferenceTransformer(); -// -// transformer.transform(currentUsers, usersToAdd); -// usersToAdd = transformer.getObjectsToAdd(); -// List usersToDelete = transformer.getObjectsToRemove(); try { conn = this.getConnection(); - if (usersToAdd.size() > 0) { + if (!usersToAdd.isEmpty()) { String query = "INSERT INTO DM_USER_POLICY (POLICY_ID, USERNAME) VALUES (?, ?)"; insertStmt = conn.prepareStatement(query); for (String username : usersToAdd) { @@ -206,22 +160,10 @@ public class PolicyDAOImpl implements PolicyDAO { } insertStmt.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(insertStmt, null); -// PolicyManagementDAOUtil.cleanupResources(deleteStmt, null); } return policy; } @@ -234,14 +176,14 @@ public class PolicyDAOImpl implements PolicyDAO { PreparedStatement deleteStmt = null; final List currentUsers = policy.getUsers(); - SetReferenceTransformer transformer = new SetReferenceTransformer(); + SetReferenceTransformer transformer = new SetReferenceTransformer<>(); transformer.transform(currentUsers, usersToAdd); usersToAdd = transformer.getObjectsToAdd(); List usersToDelete = transformer.getObjectsToRemove(); try { conn = this.getConnection(); - if (usersToAdd.size() > 0) { + if (!usersToAdd.isEmpty()) { String query = "INSERT INTO DM_USER_POLICY (POLICY_ID, USERNAME) VALUES (?, ?)"; insertStmt = conn.prepareStatement(query); for (String username : usersToAdd) { @@ -251,7 +193,7 @@ public class PolicyDAOImpl implements PolicyDAO { } insertStmt.executeBatch(); } - if (usersToDelete.size() > 0) { + if (!usersToDelete.isEmpty()) { String deleteQuery = "DELETE FROM DM_USER_POLICY WHERE USERNAME=? AND POLICY_ID=?"; deleteStmt = conn.prepareStatement(deleteQuery); for (String username : usersToDelete) { @@ -328,7 +270,6 @@ public class PolicyDAOImpl implements PolicyDAO { String query = "SELECT ACTION_TYPE, CORRECTIVE_POLICY_ID, FEATURE_ID, POLICY_ID, IS_REACTIVE " + "FROM DM_POLICY_CORRECTIVE_ACTION "; try (PreparedStatement stmt = conn.prepareStatement(query)) { - List correctiveActions = new ArrayList<>(); return extractCorrectivePolicies(stmt); } } catch (SQLException e) { @@ -826,7 +767,7 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; - List criteria = new ArrayList(); + List criteria = new ArrayList<>(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); @@ -912,8 +853,6 @@ public class PolicyDAOImpl implements PolicyDAO { } stmt.executeBatch(); } - // stmt.executeUpdate(); - } catch (SQLException | IOException e) { throw new PolicyManagerDAOException("Error occurred while inserting the criterion properties " + "to database", e); @@ -928,7 +867,7 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; - List criteria = new ArrayList(); + List criteria = new ArrayList<>(); try { conn = this.getConnection(); String query = "SELECT DPC.ID, DPC.CRITERIA_ID, DPCP.PROP_KEY, DPCP.PROP_VALUE, DPCP.CONTENT FROM " + @@ -1152,7 +1091,6 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; - List policies = new ArrayList(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { @@ -1180,7 +1118,7 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; - List deviceIdList = new ArrayList(); + List deviceIdList = new ArrayList<>(); try { conn = this.getConnection(); String query = "SELECT * FROM DM_DEVICE_POLICY WHERE POLICY_ID = ?"; @@ -1204,7 +1142,7 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; - List roleNames = new ArrayList(); + List roleNames = new ArrayList<>(); try { conn = this.getConnection(); String query = "SELECT * FROM DM_ROLE_POLICY WHERE POLICY_ID = ?"; @@ -1229,7 +1167,7 @@ public class PolicyDAOImpl implements PolicyDAO { PreparedStatement stmt = null; ResultSet resultSet = null; - List users = new ArrayList(); + List users = new ArrayList<>(); try { conn = this.getConnection(); String query = "SELECT * FROM DM_USER_POLICY WHERE POLICY_ID = ?"; @@ -1387,7 +1325,7 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; - List policyIds = new ArrayList(); + List policyIds = new ArrayList<>(); try { conn = this.getConnection(); String query = "SELECT * FROM DM_DEVICE_POLICY WHERE DEVICE_ID = ? "; @@ -1411,7 +1349,7 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; - List policyIds = new ArrayList(); + List policyIds = new ArrayList<>(); try { conn = this.getConnection(); String query = "SELECT * FROM DM_ROLE_POLICY WHERE ROLE_NAME = ? "; @@ -1435,7 +1373,7 @@ public class PolicyDAOImpl implements PolicyDAO { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; - List policyIds = new ArrayList(); + List policyIds = new ArrayList<>(); try { conn = this.getConnection(); String query = "SELECT * FROM DM_USER_POLICY WHERE USERNAME = ? "; @@ -1563,16 +1501,6 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); -// String userPolicy = "DELETE FROM DM_USER_POLICY WHERE POLICY_ID = ?"; -// stmt = conn.prepareStatement(userPolicy); -// stmt.setInt(1, policyId); -// stmt.executeUpdate(); -// -// String rolePolicy = "DELETE FROM DM_ROLE_POLICY WHERE POLICY_ID = ?"; -// stmt = conn.prepareStatement(rolePolicy); -// stmt.setInt(1, policyId); -// stmt.executeUpdate(); - String devicePolicy = "DELETE FROM DM_DEVICE_POLICY WHERE POLICY_ID = ?"; stmt = conn.prepareStatement(devicePolicy); stmt.setInt(1, policyId); @@ -1601,7 +1529,7 @@ public class PolicyDAOImpl implements PolicyDAO { } } - private Connection getConnection() throws PolicyManagerDAOException { + private Connection getConnection() { return PolicyManagementDAOFactory.getConnection(); } @@ -1821,15 +1749,6 @@ public class PolicyDAOImpl implements PolicyDAO { } finally { PolicyManagementDAOUtil.cleanupResources(stmt, resultSet); } -// -// if (policy != null && log.isDebugEnabled()) { -// log.debug("Applied policy logging details started ------------------"); -// log.debug("Applied policy name " + policy.getPolicyName() + "for the device id " + deviceId); -// log.debug(policy.getCompliance()); -// log.debug(policy.getId()); -// log.debug(policy.getPriorityId()); -// log.debug("Applied policy logging details finished...."); -// } return policy; } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java index 2cb4a295734..3c377b156c1 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/DelegationTask.java @@ -41,7 +41,8 @@ import java.util.Map; public class DelegationTask implements Task { private static final Log log = LogFactory.getLog(DelegationTask.class); - private PolicyConfiguration policyConfiguration = DeviceConfigurationManager.getInstance().getDeviceManagementConfig().getPolicyConfiguration(); + private PolicyConfiguration policyConfiguration = DeviceConfigurationManager.getInstance() + .getDeviceManagementConfig().getPolicyConfiguration(); @Override public void setProperties(Map map) { @@ -73,21 +74,17 @@ public class DelegationTask implements Task { List toBeNotified; for (String deviceType : deviceTypes) { try { - devices = new ArrayList<>(); toBeNotified = new ArrayList<>(); - devices.addAll(service.getAllDevices(deviceType, false)); - //HashMap deviceIdPolicy = policyManager.getAppliedPolicyIdsDeviceIds(); + devices = new ArrayList<>(service.getAllDevices(deviceType, false)); for (Device device : devices) { - // if (deviceIdPolicy.containsKey(device.getId())) { if (device != null && device.getEnrolmentInfo() != null - && device.getEnrolmentInfo().getStatus() != EnrolmentInfo.Status.REMOVED) { + && device.getEnrolmentInfo().getStatus() != EnrolmentInfo.Status.REMOVED) { toBeNotified.add(device); } - // } } if (!toBeNotified.isEmpty()) { - PolicyEnforcementDelegator enforcementDelegator = new PolicyEnforcementDelegatorImpl - (toBeNotified, updatedPolicyDeviceList.getUpdatedPolicyIds()); + PolicyEnforcementDelegator enforcementDelegator = new PolicyEnforcementDelegatorImpl( + toBeNotified, updatedPolicyDeviceList.getUpdatedPolicyIds()); enforcementDelegator.delegate(); } } catch (DeviceManagementException e) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegatorImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegatorImpl.java index 12ce24e824b..3667f258c61 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegatorImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/enforcement/PolicyEnforcementDelegatorImpl.java @@ -122,7 +122,6 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato return null; } return policy; - //return PolicyManagementDataHolder.getInstance().getPolicyEvaluationPoint().getEffectivePolicy(identifier); } catch (PolicyEvaluationException | PolicyManagementException e) { String msg = "Error occurred while retrieving the effective policy for devices."; log.error(msg, e); @@ -135,7 +134,7 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato PolicyDelegationException { try { String type = null; - if (deviceIdentifiers.size() > 0) { + if (!deviceIdentifiers.isEmpty()) { type = deviceIdentifiers.get(0).getType(); } PolicyManagementDataHolder.getInstance().getDeviceManagementService().addOperation(type, @@ -161,7 +160,7 @@ public class PolicyEnforcementDelegatorImpl implements PolicyEnforcementDelegato public void addPolicyRevokeOperation(List deviceIdentifiers) throws PolicyDelegationException { try { String type = null; - if (deviceIdentifiers.size() > 0) { + if (!deviceIdentifiers.isEmpty()) { type = deviceIdentifiers.get(0).getType(); } PolicyManagementDataHolder.getInstance().getDeviceManagementService().addOperation(type, diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java index c555b27b976..9c4be122af1 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyAdministratorPointImpl.java @@ -34,10 +34,8 @@ import org.wso2.carbon.policy.mgt.common.*; import org.wso2.carbon.policy.mgt.core.cache.PolicyCacheManager; import org.wso2.carbon.policy.mgt.core.cache.impl.PolicyCacheManagerImpl; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; -import org.wso2.carbon.policy.mgt.core.mgt.FeatureManager; import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; import org.wso2.carbon.policy.mgt.core.mgt.ProfileManager; -import org.wso2.carbon.policy.mgt.core.mgt.impl.FeatureManagerImpl; import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl; import org.wso2.carbon.policy.mgt.core.mgt.impl.ProfileManagerImpl; import org.wso2.carbon.policy.mgt.core.util.PolicyManagementConstants; @@ -53,28 +51,18 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { private PolicyManager policyManager; private ProfileManager profileManager; - private FeatureManager featureManager; - private PolicyCacheManager cacheManager; private PolicyConfiguration policyConfiguration; - // private PolicyEnforcementDelegator delegator; public PolicyAdministratorPointImpl() { this.policyManager = new PolicyManagerImpl(); this.profileManager = new ProfileManagerImpl(); - this.featureManager = new FeatureManagerImpl(); - this.cacheManager = PolicyCacheManagerImpl.getInstance(); - this.policyConfiguration = DeviceConfigurationManager.getInstance().getDeviceManagementConfig().getPolicyConfiguration(); - // this.delegator = new PolicyEnforcementDelegatorImpl(); + this.policyConfiguration = DeviceConfigurationManager.getInstance().getDeviceManagementConfig() + .getPolicyConfiguration(); } @Override public Policy addPolicy(Policy policy) throws PolicyManagementException { Policy resultantPolicy = policyManager.addPolicy(policy); -// try { -// delegator.delegate(resultantPolicy, resultantPolicy.getDevices()); -// } catch (PolicyDelegationException e) { -// throw new PolicyManagementException("Error occurred while delegating policy operation to the devices", e); -// } if (policyConfiguration.getCacheEnable()) { PolicyCacheManagerImpl.getInstance().rePopulateCache(); } @@ -84,11 +72,6 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { @Override public Policy updatePolicy(Policy policy) throws PolicyManagementException { Policy resultantPolicy = policyManager.updatePolicy(policy); -// try { -// delegator.delegate(resultantPolicy, resultantPolicy.getDevices()); -// } catch (PolicyDelegationException e) { -// throw new PolicyManagementException("Error occurred while delegating policy operation to the devices", e); -// } if (policyConfiguration.getCacheEnable()) { PolicyCacheManagerImpl.getInstance().rePopulateCache(); } @@ -190,7 +173,6 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { } } } - } catch (TaskException e) { String msg = "Error occurred while creating the policy delegation task for tenant " + PrivilegedCarbonContext. @@ -198,42 +180,6 @@ public class PolicyAdministratorPointImpl implements PolicyAdministratorPoint { log.error(msg, e); throw new PolicyManagementException(msg, e); } - -// List deviceTypes = policyManager.applyChangesMadeToPolicies(); -// -// if(log.isDebugEnabled()) { -// log.debug("Number of device types which policies are changed .......... : " + deviceTypes.size() ); -// } -// -// if (!deviceTypes.isEmpty()) { -// -// -// DeviceManagementProviderService service = PolicyManagementDataHolder.getInstance() -// .getDeviceManagementService(); -// List devices = new ArrayList<>(); -// for (DeviceType deviceType : deviceTypes) { -// try { -// devices.addAll(service.getAllDevices(deviceType.getName())); -// } catch (DeviceManagementException e) { -// throw new PolicyManagementException("Error occurred while taking the devices", e); -// } -// } -// HashMap deviceIdPolicy = policyManager.getAppliedPolicyIdsDeviceIds(); -// List toBeNotified = new ArrayList<>(); -// -// for (Device device : devices) { -// if (deviceIdPolicy.containsKey(device.getId())) { -// toBeNotified.add(device); -// } -// } -// if (!toBeNotified.isEmpty()) { -// -// // ExecutorService executorService = getExecutor(); -// // PolicyEnforcementDelegator enforcementDelegator = new PolicyEnforcementDelegatorImpl(toBeNotified); -//// Thread thread = new Thread(new PolicyEnforcementDelegatorImpl(toBeNotified)); -//// thread.start(); -// } -// } } @Override diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java index acb2c48794c..fbb61465d72 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/impl/PolicyFilterImpl.java @@ -63,7 +63,7 @@ public class PolicyFilterImpl implements PolicyFilter { } } - List temp = new ArrayList(); + List temp = new ArrayList<>(); for (Policy policy : policies) { if (policy.isActive()) { temp.add(policy); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/bean/UpdatedPolicyDeviceListBean.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/bean/UpdatedPolicyDeviceListBean.java index 2071d1ff5bd..bd0e925565c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/bean/UpdatedPolicyDeviceListBean.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/bean/UpdatedPolicyDeviceListBean.java @@ -18,8 +18,6 @@ package org.wso2.carbon.policy.mgt.core.mgt.bean; import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; -import org.wso2.carbon.device.mgt.core.dto.DeviceType; - import java.util.List; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java index 351da8495e0..65394c71383 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java @@ -32,7 +32,6 @@ import org.wso2.carbon.device.mgt.common.policy.mgt.Policy; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.ComplianceData; -import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.ComplianceFeature; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.NonComplianceData; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; 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 159372e73a5..9187676f008 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 @@ -241,9 +241,9 @@ public class PolicyManagerImpl implements PolicyManager { policy.getProfile().setUpdatedDate(currentTimestamp); policy.setPriorityId(previousPolicy.getPriorityId()); policy.setPolicyPayloadVersion(previousPolicy.getPolicyPayloadVersion()); - Policy updatedPolicy = policyDAO.updatePolicy(policy); - profileDAO.updateProfile(policy.getProfile()); + policyDAO.updatePolicy(policy); + profileDAO.updateProfile(policy.getProfile()); featureDAO.updateProfileFeatures(existingFeaturesList, profileId); if (!newFeaturesList.isEmpty()) { featureDAO.addProfileFeatures(newFeaturesList, profileId); @@ -646,8 +646,8 @@ public class PolicyManagerImpl implements PolicyManager { @Override public void inactivatePolicy(int policyId) throws PolicyManagementException { + Policy policy = this.getPolicy(policyId); try { - Policy policy = this.getPolicy(policyId); PolicyManagementDAOFactory.beginTransaction(); policyDAO.inactivatePolicy(policyId); policyDAO.recordUpdatedPolicy(policy); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java index 160d512ee52..d0a79cb53ea 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/mgt/impl/ProfileManagerImpl.java @@ -22,8 +22,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.policy.mgt.Profile; import org.wso2.carbon.device.mgt.common.policy.mgt.ProfileFeature; -import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; -import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; import org.wso2.carbon.policy.mgt.common.ProfileManagementException; import org.wso2.carbon.policy.mgt.core.dao.FeatureDAO; import org.wso2.carbon.policy.mgt.core.dao.FeatureManagerDAOException; @@ -41,17 +39,13 @@ import java.util.List; public class ProfileManagerImpl implements ProfileManager { - private static Log log = LogFactory.getLog(ProfileManagerImpl.class); + private static final Log log = LogFactory.getLog(ProfileManagerImpl.class); private ProfileDAO profileDAO; private FeatureDAO featureDAO; -// private DeviceDAO deviceDAO; - private DeviceTypeDAO deviceTypeDAO; public ProfileManagerImpl() { profileDAO = PolicyManagementDAOFactory.getProfileDAO(); featureDAO = PolicyManagementDAOFactory.getFeatureDAO(); -// deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); - deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); } @Override @@ -68,19 +62,21 @@ public class ProfileManagerImpl implements ProfileManager { PolicyManagementDAOFactory.commitTransaction(); } catch (ProfileManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); - throw new ProfileManagementException("Error occurred while adding the profile (" + - profile.getProfileName() + ")", e); + String msg = "Error occurred while adding the profile (" + profile.getProfileName() + ")"; + log.error(msg, e); + throw new ProfileManagementException(msg, e); } catch (FeatureManagerDAOException e) { PolicyManagementDAOFactory.rollbackTransaction(); - throw new ProfileManagementException("Error occurred while adding the profile features (" + - profile.getProfileName() + ")", e); + String msg = "Error occurred while adding the profile features (" + profile.getProfileName() + ")"; + log.error(msg, e); + throw new ProfileManagementException(msg, e); } catch (PolicyManagerDAOException e) { - throw new ProfileManagementException("Error occurred while adding the profile (" + - profile.getProfileName() + ") to the database", e); + String msg = "Error occurred while adding the profile (" + profile.getProfileName() + ") to the database"; + log.error(msg, e); + throw new ProfileManagementException(msg, e); } finally { PolicyManagementDAOFactory.closeConnection(); } - return profile; } @@ -115,7 +111,7 @@ public class ProfileManagerImpl implements ProfileManager { @Override public boolean deleteProfile(Profile profile) throws ProfileManagementException { - boolean bool = true; + boolean bool; try { PolicyManagementDAOFactory.beginTransaction(); featureDAO.deleteFeaturesOfProfile(profile); @@ -173,7 +169,7 @@ public class ProfileManagerImpl implements ProfileManager { for (Profile profile : profileList) { - List list = new ArrayList(); + List list = new ArrayList<>(); for (ProfileFeature profileFeature : featureList) { if (profile.getProfileId() == profileFeature.getProfileId()) { list.add(profileFeature); @@ -204,7 +200,7 @@ public class ProfileManagerImpl implements ProfileManager { featureList = featureDAO.getAllProfileFeatures(); for (Profile profile : profileList) { - List profileFeatureList = new ArrayList(); + List profileFeatureList = new ArrayList<>(); for (ProfileFeature profileFeature : featureList) { if (profile.getProfileId() == profileFeature.getProfileId()) { profileFeatureList.add(profileFeature); @@ -224,5 +220,4 @@ public class ProfileManagerImpl implements ProfileManager { } return profileList; } - } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java index 75a58da1e89..b4b358bc320 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/TaskScheduleServiceImpl.java @@ -38,7 +38,7 @@ import java.util.Map; public class TaskScheduleServiceImpl implements TaskScheduleService { - private static Log log = LogFactory.getLog(TaskScheduleServiceImpl.class); + private static final Log log = LogFactory.getLog(TaskScheduleServiceImpl.class); private PolicyConfiguration policyConfiguration;