From fca2e515b0423f746293b5c3951cf05c016607b6 Mon Sep 17 00:00:00 2001 From: Dileesha Rajapakse Date: Tue, 3 Nov 2015 16:00:30 +0530 Subject: [PATCH 01/17] Refactored oracle DB script --- .../main/resources/dbscripts/cdm/oracle.sql | 32 +++++++++---------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql index 2af12dc572..22e263508a 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -26,7 +26,7 @@ CREATE TABLE DM_DEVICE ( DEVICE_IDENTIFICATION VARCHAR2(300) DEFAULT NULL, TENANT_ID NUMBER(10) DEFAULT 0, CONSTRAINT PK_DM_DEVICE PRIMARY KEY (ID), - CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_ID ) + CONSTRAINT FK_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_ID ) REFERENCES DM_DEVICE_TYPE (ID) ) / @@ -71,7 +71,7 @@ CREATE TABLE DM_CONFIG_OPERATION ( OPERATION_ID NUMBER(10) NOT NULL, OPERATION_CONFIG BLOB DEFAULT NULL, CONSTRAINT PK_DM_CONFIG_OPERATION PRIMARY KEY (OPERATION_ID), - CONSTRAINT fk_dm_operation_config FOREIGN KEY (OPERATION_ID) REFERENCES + CONSTRAINT FK_DM_OPERATION_CONFIG FOREIGN KEY (OPERATION_ID) REFERENCES DM_OPERATION (ID) ) / @@ -79,7 +79,7 @@ CREATE TABLE DM_COMMAND_OPERATION ( OPERATION_ID NUMBER(10) NOT NULL, ENABLED NUMBER(10) DEFAULT 0 NOT NULL, CONSTRAINT PK_DM_COMMAND_OPERATION PRIMARY KEY (OPERATION_ID), - CONSTRAINT fk_dm_operation_command FOREIGN KEY (OPERATION_ID) REFERENCES + CONSTRAINT FK_DM_OPERATION_COMMAND FOREIGN KEY (OPERATION_ID) REFERENCES DM_OPERATION (ID) ) / @@ -88,7 +88,7 @@ CREATE TABLE DM_POLICY_OPERATION ( ENABLED NUMBER(10) DEFAULT 0 NOT NULL, OPERATION_DETAILS BLOB DEFAULT NULL, CONSTRAINT PK_DM_POLICY_OPERATION PRIMARY KEY (OPERATION_ID), - CONSTRAINT fk_dm_operation_policy FOREIGN KEY (OPERATION_ID) REFERENCES + CONSTRAINT FK_DM_OPERATION_POLICY FOREIGN KEY (OPERATION_ID) REFERENCES DM_OPERATION (ID) ) / @@ -97,7 +97,7 @@ CREATE TABLE DM_PROFILE_OPERATION ( ENABLED NUMBER(10) DEFAULT 0 NOT NULL, OPERATION_DETAILS BLOB DEFAULT NULL, CONSTRAINT PK_DM_PROFILE_OPERATION PRIMARY KEY (OPERATION_ID), - CONSTRAINT fk_dm_operation_profile FOREIGN KEY (OPERATION_ID) REFERENCES + CONSTRAINT FK_DM_OPERATION_PROFILE FOREIGN KEY (OPERATION_ID) REFERENCES DM_OPERATION (ID) ) / @@ -111,7 +111,7 @@ CREATE TABLE DM_ENROLMENT ( DATE_OF_LAST_UPDATE TIMESTAMP(0) DEFAULT NULL, TENANT_ID NUMBER(10) NOT NULL, CONSTRAINT PK_DM_ENROLMENT PRIMARY KEY (ID), - CONSTRAINT fk_dm_device_enrolment FOREIGN KEY (DEVICE_ID) REFERENCES + CONSTRAINT FK_DM_DEVICE_ENROLMENT FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ) / @@ -129,29 +129,29 @@ WHEN (NEW.ID IS NULL) END; / -CREATE TABLE DM_ENROLMENT_op_map ( +CREATE TABLE DM_ENROLMENT_OP_MAP ( ID NUMBER(10) NOT NULL, ENROLMENT_ID NUMBER(10) NOT NULL, OPERATION_ID NUMBER(10) NOT NULL, STATUS VARCHAR2(50) NULL, PRIMARY KEY (ID), - CONSTRAINT fk_dm_device_op_map_device FOREIGN KEY (ENROLMENT_ID) REFERENCES + CONSTRAINT FK_DM_DEVICE_OP_MAP_DEVICE FOREIGN KEY (ENROLMENT_ID) REFERENCES DM_ENROLMENT (ID), - CONSTRAINT fk_dm_device_op_map_operation FOREIGN KEY (OPERATION_ID) REFERENCES + CONSTRAINT FK_DM_DEVICE_OP_MAP_OPERATION FOREIGN KEY (OPERATION_ID) REFERENCES DM_OPERATION (ID) ) / -- Generate ID using sequence and trigger -CREATE SEQUENCE DM_ENROLMENT_op_map_seq START WITH 1 INCREMENT BY 1 NOCACHE +CREATE SEQUENCE DM_ENROLMENT_OP_MAP_seq START WITH 1 INCREMENT BY 1 NOCACHE / -CREATE OR REPLACE TRIGGER DM_ENROLMENT_op_map_seq_tr +CREATE OR REPLACE TRIGGER DM_ENROLMENT_OP_MAP_seq_tr BEFORE INSERT ON DM_ENROLMENT_op_map REFERENCING NEW AS NEW FOR EACH ROW WHEN (NEW.ID IS NULL) BEGIN - SELECT DM_ENROLMENT_op_map_seq.NEXTVAL INTO :NEW.ID FROM DUAL; + SELECT DM_ENROLMENT_OP_MAP_seq.NEXTVAL INTO :NEW.ID FROM DUAL; END; / @@ -161,9 +161,9 @@ CREATE TABLE DM_DEVICE_OPERATION_RESPONSE ( OPERATION_ID NUMBER(10) NOT NULL, OPERATION_RESPONSE BLOB DEFAULT NULL, CONSTRAINT PK_DM_DEVICE_OP_RESPONSE PRIMARY KEY (ID), - CONSTRAINT fk_dm_device_op_res_device FOREIGN KEY (DEVICE_ID) REFERENCES + CONSTRAINT FK_DM_DEVICE_OP_RES_DEVICE FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID), - CONSTRAINT fk_dm_device_op_res_operation FOREIGN KEY (OPERATION_ID) REFERENCES + CONSTRAINT FK_DM_DEVICE_OP_RES_OPERATION FOREIGN KEY (OPERATION_ID) REFERENCES DM_OPERATION (ID) ) / @@ -621,9 +621,9 @@ CREATE TABLE DM_NOTIFICATION ( STATUS VARCHAR2(10) NULL, DESCRIPTION VARCHAR2(100) NULL, CONSTRAINT PK_DM_NOTIFICATION PRIMARY KEY (NOTIFICATION_ID), - CONSTRAINT fk_dm_device_notification FOREIGN KEY (DEVICE_ID) REFERENCES + CONSTRAINT FK_DM_DEVICE_NOTIFICATION FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID), - CONSTRAINT fk_dm_operation_notification FOREIGN KEY (OPERATION_ID) REFERENCES + CONSTRAINT FK_DM_OPERATION_NOTIFICATION FOREIGN KEY (OPERATION_ID) REFERENCES DM_OPERATION (ID) ) / From b7e4e90358c94b3e24502fd2455e0570057d405f Mon Sep 17 00:00:00 2001 From: Dileesha Rajapakse Date: Thu, 5 Nov 2015 10:42:11 +0530 Subject: [PATCH 02/17] Refactored mssql DB script --- .../src/main/resources/dbscripts/cdm/mssql.sql | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql index e78fb33827..fa32bcea2a 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -35,7 +35,7 @@ CREATE TABLE DM_CONFIG_OPERATION ( CREATE TABLE DM_COMMAND_OPERATION ( OPERATION_ID INTEGER NOT NULL, - ENABLED BIT NOT NULL DEFAULT FALSE, + ENABLED BIT NOT NULL DEFAULT 'FALSE', PRIMARY KEY (OPERATION_ID), CONSTRAINT fk_dm_operation_command FOREIGN KEY (OPERATION_ID) REFERENCES DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION @@ -337,20 +337,6 @@ CREATE TABLE DM_POLICY_COMPLIANCE_FEATURES ( ON UPDATE NO ACTION ); -CREATE TABLE DM_ENROLMENT ( - ID INTEGER IDENTITY NOT NULL, - DEVICE_ID INTEGER NOT NULL, - OWNER VARCHAR(50) NOT NULL, - OWNERSHIP VARCHAR(45) DEFAULT NULL, - STATUS VARCHAR(50) NULL, - DATE_OF_ENROLMENT DATETIME2(0) DEFAULT NULL, - DATE_OF_LAST_UPDATE DATETIME2(0) DEFAULT NULL, - TENANT_ID INT NOT NULL, - PRIMARY KEY (ID), - CONSTRAINT fk_dm_device_enrolment FOREIGN KEY (DEVICE_ID) REFERENCES - DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION -); - CREATE TABLE DM_APPLICATION ( ID INTEGER IDENTITY NOT NULL, NAME VARCHAR(150) NOT NULL, From 123ea228ad7867dbf3b7f5720e9d129b0d1f1db0 Mon Sep 17 00:00:00 2001 From: Dileesha Rajapakse Date: Thu, 12 Nov 2015 10:30:45 +0530 Subject: [PATCH 03/17] Refactored Enrolment DAO class --- .../device/mgt/core/dao/impl/EnrollmentDAOImpl.java | 8 ++++---- .../src/main/resources/dbscripts/cdm/oracle.sql | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrollmentDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrollmentDAOImpl.java index 0e4334b206..f3a5a58511 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrollmentDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrollmentDAOImpl.java @@ -42,7 +42,7 @@ public class EnrollmentDAOImpl implements EnrollmentDAO { conn = this.getConnection(); String sql = "INSERT INTO DM_ENROLMENT(DEVICE_ID, OWNER, OWNERSHIP, STATUS, " + "DATE_OF_ENROLMENT, DATE_OF_LAST_UPDATE, TENANT_ID) VALUES(?, ?, ?, ?, ?, ?, ?)"; - stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); + stmt = conn.prepareStatement(sql, new String[] {"id"}); stmt.setInt(1, deviceId); stmt.setString(2, enrolmentInfo.getOwner()); stmt.setString(3, enrolmentInfo.getOwnership().toString()); @@ -76,7 +76,7 @@ public class EnrollmentDAOImpl implements EnrollmentDAO { String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, " + "DATE_OF_ENROLMENT = ?, DATE_OF_LAST_UPDATE = ? WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?" + " AND ID = ?"; - stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); + stmt = conn.prepareStatement(sql, new String[] {"id"}); stmt.setString(1, enrolmentInfo.getOwnership().toString()); stmt.setString(2, enrolmentInfo.getStatus().toString()); stmt.setTimestamp(3, new Timestamp(enrolmentInfo.getDateOfEnrolment())); @@ -109,7 +109,7 @@ public class EnrollmentDAOImpl implements EnrollmentDAO { conn = this.getConnection(); String sql = "UPDATE DM_ENROLMENT SET OWNERSHIP = ?, STATUS = ?, " + "DATE_OF_ENROLMENT = ?, DATE_OF_LAST_UPDATE = ? WHERE ID = ?"; - stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); + stmt = conn.prepareStatement(sql, new String[] {"id"}); stmt.setString(1, enrolmentInfo.getOwnership().toString()); stmt.setString(2, enrolmentInfo.getStatus().toString()); stmt.setTimestamp(3, new Timestamp(enrolmentInfo.getDateOfEnrolment())); @@ -140,7 +140,7 @@ public class EnrollmentDAOImpl implements EnrollmentDAO { try { conn = this.getConnection(); String sql = "DELETE DM_ENROLMENT WHERE DEVICE_ID = ? AND OWNER = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); + stmt = conn.prepareStatement(sql, new String[] {"id"}); stmt.setInt(1, deviceId); stmt.setString(2, currentOwner); stmt.setInt(3, tenantId); diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql index a73f5d5648..605317c619 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -161,7 +161,7 @@ CREATE TABLE DM_DEVICE_OPERATION_RESPONSE ( OPERATION_ID NUMBER(10) NOT NULL, OPERATION_RESPONSE BLOB DEFAULT NULL, CONSTRAINT PK_DM_DEVICE_OP_RESPONSE PRIMARY KEY (ID), - CONSTRAINT FK_DM_DEVICE_OP_RES_DEVICE FOREIGN KEY (DEVICE_ID) REFERENCES + CONSTRAINT FK_DM_DEVICE_OP_RES_DEVICE FOREIGN KEY (ENROLMENT_ID) REFERENCES DM_DEVICE (ID), CONSTRAINT FK_DM_DEVICE_OP_RES_OPERATION FOREIGN KEY (OPERATION_ID) REFERENCES DM_OPERATION (ID) From 8c1d418e446c4a100e55730da736d17cbf7c4465 Mon Sep 17 00:00:00 2001 From: Dileesha Rajapakse Date: Thu, 12 Nov 2015 19:16:21 +0530 Subject: [PATCH 04/17] Refactored oracle DB script --- .../src/main/resources/dbscripts/cdm/oracle.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql index 605317c619..98735101ca 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -129,7 +129,7 @@ WHEN (NEW.ID IS NULL) END; / -CREATE TABLE DM_ENROLMENT_OP_MAP ( +CREATE TABLE DM_ENROLMENT_OP_MAPPING ( ID NUMBER(10) NOT NULL, ENROLMENT_ID NUMBER(10) NOT NULL, OPERATION_ID NUMBER(10) NOT NULL, @@ -146,7 +146,7 @@ CREATE SEQUENCE DM_ENROLMENT_OP_MAP_seq START WITH 1 INCREMENT BY 1 NOCACHE / CREATE OR REPLACE TRIGGER DM_ENROLMENT_OP_MAP_seq_tr BEFORE INSERT -ON DM_ENROLMENT_op_map +ON DM_ENROLMENT_OP_MAPPING REFERENCING NEW AS NEW FOR EACH ROW WHEN (NEW.ID IS NULL) From 786eb93bcae7f86d9281b62e94f2201c2660475e Mon Sep 17 00:00:00 2001 From: Dileesha Rajapakse Date: Fri, 13 Nov 2015 14:15:05 +0530 Subject: [PATCH 05/17] Refactored Operation DAO classes --- .../device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java | 6 +++--- .../mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java index 246819b9b4..8e56611751 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java @@ -49,7 +49,7 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { conn = this.getConnection(); String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, APPLICATION_ID, " + "TENANT_ID) VALUES (?, ?, ?)"; - stmt = conn.prepareStatement(sql, new String[] {"id"}); + stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); stmt.setInt(1, deviceId); stmt.setInt(2, applicationId); stmt.setInt(3, tenantId); @@ -78,7 +78,7 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { conn = this.getConnection(); String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, APPLICATION_ID, " + "TENANT_ID) VALUES (?, ?, ?)"; - stmt = conn.prepareStatement(sql, new String[] {"id"}); + stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); for (int applicationId : applicationIds) { stmt.setInt(1, deviceId); @@ -109,7 +109,7 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { conn = this.getConnection(); String sql = "DELETE DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ? AND " + "APPLICATION_ID = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql, new String[] {"id"}); + stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); for (Integer appId : appIdList) { stmt.setInt(1, deviceId); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java index 8a282c83f2..97e140fd90 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/OperationDAOImpl.java @@ -45,7 +45,7 @@ public class OperationDAOImpl implements OperationDAO { Connection connection = OperationManagementDAOFactory.getConnection(); String sql = "INSERT INTO DM_OPERATION(TYPE, CREATED_TIMESTAMP, RECEIVED_TIMESTAMP, OPERATION_CODE) " + "VALUES (?, ?, ?, ?)"; - stmt = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); + stmt = connection.prepareStatement(sql, new String[] {"id"}); stmt.setString(1, operation.getType().toString()); stmt.setTimestamp(2, new Timestamp(new Date().getTime())); stmt.setTimestamp(3, null); From e14f09b8a87b522c9a7a26994cff69fe87a3adf1 Mon Sep 17 00:00:00 2001 From: harshanl Date: Fri, 13 Nov 2015 21:33:33 +0530 Subject: [PATCH 06/17] Added requireDeviceAuthorization support --- .../device/mgt/common/DeviceManager.java | 7 +++++++ .../core/DeviceManagementPluginRepository.java | 3 +++ .../DeviceAccessAuthorizationServiceImpl.java | 18 +++++++++--------- .../internal/DeviceManagementDataHolder.java | 10 +++++----- .../device/mgt/core/TestDeviceManager.java | 5 +++++ 5 files changed, 29 insertions(+), 14 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManager.java index 3481dc8e17..c019276768 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/DeviceManager.java @@ -150,4 +150,11 @@ public interface DeviceManager { void addLicense(License license) throws LicenseManagementException; + /** + * Method to enable to device-authentication for the device-platform. + * + * @return Returns boolean status to enable or disable device-authentication. + */ + boolean requireDeviceAuthorization(); + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java index 971212b923..24b88903d8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/DeviceManagementPluginRepository.java @@ -21,6 +21,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.common.spi.DeviceManagementService; +import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent; import org.wso2.carbon.device.mgt.core.internal.DeviceManagerStartupListener; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; @@ -48,6 +49,8 @@ public class DeviceManagementPluginRepository implements DeviceManagerStartupLis /* Initializing Device Management Service Provider */ provider.init(); DeviceManagerUtil.registerDeviceType(deviceType); + DeviceManagementDataHolder.getInstance().setRequireDeviceAuthorization(deviceType, + provider.getDeviceManager().requireDeviceAuthorization()); } } catch (DeviceManagementException e) { throw new DeviceManagementException("Error occurred while adding device management provider '" + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/authorization/DeviceAccessAuthorizationServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/authorization/DeviceAccessAuthorizationServiceImpl.java index 1595873737..bed880e72c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/authorization/DeviceAccessAuthorizationServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/authorization/DeviceAccessAuthorizationServiceImpl.java @@ -21,10 +21,7 @@ package org.wso2.carbon.device.mgt.core.authorization; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.common.EnrolmentInfo; +import org.wso2.carbon.device.mgt.common.*; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; import org.wso2.carbon.device.mgt.common.authorization.DeviceAuthorizationResult; @@ -74,7 +71,7 @@ public class DeviceAccessAuthorizationServiceImpl implements DeviceAccessAuthori String username = this.getUserName(); int tenantId = this.getTenantId(); if (username == null || username.isEmpty()) { - return false; + return !DeviceManagementDataHolder.getInstance().requireDeviceAuthorization(deviceIdentifier.getType()); } try { //Check for admin users. If the user is an admin user we authorize the access to that device. @@ -227,11 +224,14 @@ public class DeviceAccessAuthorizationServiceImpl implements DeviceAccessAuthori private String getUserName() { String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - String tenantDomain = MultitenantUtils.getTenantDomain(username); - if (username.endsWith(tenantDomain)) { - return username.substring(0, username.lastIndexOf("@")); + if (username != null && username.isEmpty()) { + String tenantDomain = MultitenantUtils.getTenantDomain(username); + if (username.endsWith(tenantDomain)) { + return username.substring(0, username.lastIndexOf("@")); + } + return username; } - return username; + return null; } private int getTenantId() { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java index b618288b9c..0921ef5081 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java @@ -44,7 +44,7 @@ public class DeviceManagementDataHolder { private AppManagementConfig appManagerConfig; private OperationManager operationManager; private ConfigurationContextService configurationContextService; - private HashMap isUserLevelAutherizationAllowMap; + private HashMap requireDeviceAuthorization = new HashMap<>(); private DeviceAccessAuthorizationService deviceAccessAuthorizationService; private static DeviceManagementDataHolder thisInstance = new DeviceManagementDataHolder(); @@ -148,12 +148,12 @@ public class DeviceManagementDataHolder { this.configurationContextService = configurationContextService; } - public void setIsUserLevelAutherizationAllow(String pluginType, boolean isAllowUserAuthentication) { - isUserLevelAutherizationAllowMap.put(pluginType,isAllowUserAuthentication); + public void setRequireDeviceAuthorization(String pluginType, boolean requireAuthentication) { + requireDeviceAuthorization.put(pluginType,requireAuthentication); } - public boolean getIsUserLevelAutherizationAllow(String pluginType) { - return isUserLevelAutherizationAllowMap.get(pluginType); + public boolean requireDeviceAuthorization(String pluginType) { + return requireDeviceAuthorization.get(pluginType); } public DeviceAccessAuthorizationService getDeviceAccessAuthorizationService() { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java index a9ff90f96c..d8ed7a6dc6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/TestDeviceManager.java @@ -116,4 +116,9 @@ public class TestDeviceManager implements DeviceManager { } + @Override + public boolean requireDeviceAuthorization() { + return false; + } + } From 6f0276ab2f5c8f6c8c65c919470dc62623503274 Mon Sep 17 00:00:00 2001 From: Dileesha Rajapakse Date: Mon, 16 Nov 2015 11:39:52 +0530 Subject: [PATCH 07/17] Modified Application Mapping DAO classes --- .../mgt/core/dao/ApplicationMappingDAO.java | 2 +- .../dao/impl/ApplicationMappingDAOImpl.java | 18 +++++++++--------- .../main/resources/dbscripts/cdm/oracle.sql | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationMappingDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationMappingDAO.java index 372ea626ec..e246c1c92b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationMappingDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/ApplicationMappingDAO.java @@ -26,7 +26,7 @@ public interface ApplicationMappingDAO { int addApplicationMapping(int deviceId, int applicationId, int tenantId) throws DeviceManagementDAOException; - List addApplicationMappings(int deviceId, List applicationIds, int tenantId) + void addApplicationMappings(int deviceId, List applicationIds, int tenantId) throws DeviceManagementDAOException; void removeApplicationMapping(int deviceId, List appIdList, int tenantId) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java index 8e56611751..b9dff843a3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java @@ -49,7 +49,7 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { conn = this.getConnection(); String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, APPLICATION_ID, " + "TENANT_ID) VALUES (?, ?, ?)"; - stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); + stmt = conn.prepareStatement(sql, new String[] {"id"}); stmt.setInt(1, deviceId); stmt.setInt(2, applicationId); stmt.setInt(3, tenantId); @@ -68,7 +68,7 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { } @Override - public List addApplicationMappings(int deviceId, List applicationIds, + public void addApplicationMappings(int deviceId, List applicationIds, int tenantId) throws DeviceManagementDAOException { Connection conn; PreparedStatement stmt = null; @@ -78,7 +78,7 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { conn = this.getConnection(); String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, APPLICATION_ID, " + "TENANT_ID) VALUES (?, ?, ?)"; - stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); + stmt = conn.prepareStatement(sql); for (int applicationId : applicationIds) { stmt.setInt(1, deviceId); @@ -88,11 +88,11 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { } stmt.executeBatch(); - rs = stmt.getGeneratedKeys(); - while (rs.next()) { - mappingIds.add(rs.getInt(1)); - } - return mappingIds; +// rs = stmt.getGeneratedKeys(); +// while (rs.next()) { +// mappingIds.add(rs.getInt(1)); +// } +// return mappingIds; } catch (SQLException e) { throw new DeviceManagementDAOException("Error occurred while adding device application mappings", e); } finally { @@ -109,7 +109,7 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { conn = this.getConnection(); String sql = "DELETE DM_DEVICE_APPLICATION_MAPPING WHERE DEVICE_ID = ? AND " + "APPLICATION_ID = ? AND TENANT_ID = ?"; - stmt = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS); + stmt = conn.prepareStatement(sql); for (Integer appId : appIdList) { stmt.setInt(1, deviceId); diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql index 98735101ca..2b33f64118 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -162,7 +162,7 @@ CREATE TABLE DM_DEVICE_OPERATION_RESPONSE ( OPERATION_RESPONSE BLOB DEFAULT NULL, CONSTRAINT PK_DM_DEVICE_OP_RESPONSE PRIMARY KEY (ID), CONSTRAINT FK_DM_DEVICE_OP_RES_DEVICE FOREIGN KEY (ENROLMENT_ID) REFERENCES - DM_DEVICE (ID), + DM_ENROLMENT (ID), CONSTRAINT FK_DM_DEVICE_OP_RES_OPERATION FOREIGN KEY (OPERATION_ID) REFERENCES DM_OPERATION (ID) ) From cb02c9da80b977c528f54666974f43c67bea3f82 Mon Sep 17 00:00:00 2001 From: harshanl Date: Mon, 16 Nov 2015 11:43:52 +0530 Subject: [PATCH 08/17] Fixed issues in DeviceAuthorization --- .../DeviceAccessAuthorizationServiceImpl.java | 11 +++++------ .../framework/WebappAuthenticationValve.java | 2 -- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/authorization/DeviceAccessAuthorizationServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/authorization/DeviceAccessAuthorizationServiceImpl.java index bed880e72c..47f1a8024e 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/authorization/DeviceAccessAuthorizationServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/authorization/DeviceAccessAuthorizationServiceImpl.java @@ -20,7 +20,7 @@ package org.wso2.carbon.device.mgt.core.authorization; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.*; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException; import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationService; @@ -31,7 +31,6 @@ import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.permission.mgt.PermissionUtils; import org.wso2.carbon.user.api.UserRealm; import org.wso2.carbon.user.api.UserStoreException; -import org.wso2.carbon.utils.multitenancy.MultitenantUtils; import java.util.HashMap; import java.util.List; @@ -223,9 +222,9 @@ public class DeviceAccessAuthorizationServiceImpl implements DeviceAccessAuthori } private String getUserName() { - String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); - if (username != null && username.isEmpty()) { - String tenantDomain = MultitenantUtils.getTenantDomain(username); + String username = CarbonContext.getThreadLocalCarbonContext().getUsername(); + if (username != null && !username.isEmpty()) { + String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain(); if (username.endsWith(tenantDomain)) { return username.substring(0, username.lastIndexOf("@")); } @@ -235,7 +234,7 @@ public class DeviceAccessAuthorizationServiceImpl implements DeviceAccessAuthori } private int getTenantId() { - return PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + return CarbonContext.getThreadLocalCarbonContext().getTenantId(); } private boolean addAdminPermissionToRegistry() throws PermissionManagementException { diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValve.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValve.java index d2be75ca18..25ef416506 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValve.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/WebappAuthenticationValve.java @@ -28,9 +28,7 @@ import org.wso2.carbon.tomcat.ext.valves.CompositeValve; import org.wso2.carbon.webapp.authenticator.framework.authenticator.WebappAuthenticator; import javax.servlet.http.HttpServletResponse; -import java.util.Arrays; import java.util.HashMap; -import java.util.List; import java.util.StringTokenizer; public class WebappAuthenticationValve extends CarbonTomcatValve { From 7023537fcdd75ea0aaef088b8880946942fa113e Mon Sep 17 00:00:00 2001 From: Dilshan Edirisuriya Date: Mon, 16 Nov 2015 13:49:02 +0530 Subject: [PATCH 09/17] Setting owner in authentication info --- .../authenticator/CertificateAuthenticator.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/CertificateAuthenticator.java b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/CertificateAuthenticator.java index 88d695cf16..2dd530c16f 100644 --- a/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/CertificateAuthenticator.java +++ b/components/webapp-authenticator-framework/org.wso2.carbon.webapp.authenticator.framework/src/main/java/org/wso2/carbon/webapp/authenticator/framework/authenticator/CertificateAuthenticator.java @@ -7,6 +7,7 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.certificate.mgt.core.exception.KeystoreException; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; +import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.core.scep.SCEPException; import org.wso2.carbon.device.mgt.core.scep.SCEPManager; import org.wso2.carbon.device.mgt.core.scep.TenantedDeviceWrapper; @@ -68,6 +69,14 @@ public class CertificateAuthenticator implements WebappAuthenticator { TenantedDeviceWrapper tenantedDeviceWrapper = scepManager.getValidatedDevice(deviceIdentifier); authenticationInfo.setTenantDomain(tenantedDeviceWrapper.getTenantDomain()); authenticationInfo.setTenantId(tenantedDeviceWrapper.getTenantId()); + + if(tenantedDeviceWrapper.getDevice() != null && + tenantedDeviceWrapper.getDevice().getEnrolmentInfo() != null) { + + EnrolmentInfo enrolmentInfo = tenantedDeviceWrapper.getDevice().getEnrolmentInfo(); + authenticationInfo.setUsername(enrolmentInfo.getOwner()); + } + authenticationInfo.setStatus(Status.CONTINUE); } } From 91d9db1ffaac83552c1bcd8bbc9ec596deed0c2d Mon Sep 17 00:00:00 2001 From: Dileesha Rajapakse Date: Mon, 16 Nov 2015 14:15:54 +0530 Subject: [PATCH 10/17] Refactored ApplicationMapping and ProfileOperation DAO classes --- .../dao/impl/ApplicationMappingDAOImpl.java | 2 +- .../mgt/dao/impl/ProfileOperationDAOImpl.java | 28 +++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java index b9dff843a3..45154a54a2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java @@ -73,7 +73,7 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { Connection conn; PreparedStatement stmt = null; ResultSet rs = null; - List mappingIds = new ArrayList<>(); + try { conn = this.getConnection(); String sql = "INSERT INTO DM_DEVICE_APPLICATION_MAPPING (DEVICE_ID, APPLICATION_ID, " + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java index 664ac370da..04e39eef98 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/ProfileOperationDAOImpl.java @@ -37,22 +37,46 @@ public class ProfileOperationDAOImpl extends OperationDAOImpl { public int addOperation(Operation operation) throws OperationManagementDAOException { PreparedStatement stmt = null; + ByteArrayOutputStream bao = null; + ObjectOutputStream oos = null; + int operationId; try { operationId = super.addOperation(operation); operation.setCreatedTimeStamp(new Timestamp(new java.util.Date().getTime()).toString()); operation.setId(operationId); operation.setEnabled(true); - ProfileOperation profileOp = (ProfileOperation) operation; + //ProfileOperation profileOp = (ProfileOperation) operation; Connection conn = OperationManagementDAOFactory.getConnection(); stmt = conn.prepareStatement("INSERT INTO DM_PROFILE_OPERATION(OPERATION_ID, OPERATION_DETAILS) " + "VALUES(?, ?)"); + + bao = new ByteArrayOutputStream(); + oos = new ObjectOutputStream(bao); + oos.writeObject(operation); + stmt.setInt(1, operationId); - stmt.setObject(2, profileOp); + stmt.setBytes(2, bao.toByteArray()); stmt.executeUpdate(); } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while adding profile operation", e); + } catch (IOException e) { + throw new OperationManagementDAOException("Error occurred while serializing profile operation object", e); } finally { + if (bao != null) { + try { + bao.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ByteArrayOutputStream", e); + } + } + if (oos != null) { + try { + oos.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ObjectOutputStream", e); + } + } OperationManagementDAOUtil.cleanupResources(stmt); } return operationId; From 6446be100d7810e524709acf7ae8c1366ee39a92 Mon Sep 17 00:00:00 2001 From: Dileesha Rajapakse Date: Mon, 16 Nov 2015 16:49:35 +0530 Subject: [PATCH 11/17] Refactored Policy Management DAO classes --- .../mgt/dao/impl/PolicyOperationDAOImpl.java | 25 ++++++++++++++++++- .../policy/mgt/core/dao/MonitoringDAO.java | 2 +- .../mgt/core/dao/impl/FeatureDAOImpl.java | 10 +++----- .../mgt/core/dao/impl/MonitoringDAOImpl.java | 4 +-- .../mgt/core/dao/impl/PolicyDAOImpl.java | 6 ++--- .../mgt/core/dao/impl/ProfileDAOImpl.java | 2 +- .../core/mgt/impl/MonitoringManagerImpl.java | 2 +- 7 files changed, 35 insertions(+), 16 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java index 07c6112bd6..1bff85ae61 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/dao/impl/PolicyOperationDAOImpl.java @@ -39,6 +39,8 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl { public int addOperation(Operation operation) throws OperationManagementDAOException { int operationId; PreparedStatement stmt = null; + ByteArrayOutputStream bao = null; + ObjectOutputStream oos = null; try { operationId = super.addOperation(operation); operation.setCreatedTimeStamp(new Timestamp(new java.util.Date().getTime()).toString()); @@ -48,12 +50,33 @@ public class PolicyOperationDAOImpl extends OperationDAOImpl { Connection conn = OperationManagementDAOFactory.getConnection(); stmt = conn.prepareStatement("INSERT INTO DM_POLICY_OPERATION(OPERATION_ID, OPERATION_DETAILS) " + "VALUES(?, ?)"); + + bao = new ByteArrayOutputStream(); + oos = new ObjectOutputStream(bao); + oos.writeObject(operation); + stmt.setInt(1, operationId); - stmt.setObject(2, policyOperation); + stmt.setBytes(2, bao.toByteArray()); stmt.executeUpdate(); } catch (SQLException e) { throw new OperationManagementDAOException("Error occurred while adding policy operation", e); + } catch (IOException e) { + throw new OperationManagementDAOException("Error occurred while serializing policy operation object", e); } finally { + if (bao != null) { + try { + bao.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ByteArrayOutputStream", e); + } + } + if (oos != null) { + try { + oos.close(); + } catch (IOException e) { + log.warn("Error occurred while closing ObjectOutputStream", e); + } + } OperationManagementDAOUtil.cleanupResources(stmt); } return operationId; diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java index 522b20f370..a7f42df06c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/MonitoringDAO.java @@ -46,7 +46,7 @@ public interface MonitoringDAO { void setDeviceAsCompliance(int deviceId, int enrolmentId, int policyId) throws MonitoringDAOException; - void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId, List + void addNonComplianceFeatures(int policyComplianceStatusId, int deviceId, List complianceFeatures) throws MonitoringDAOException; 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 9688d1c53d..0af516ac70 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 @@ -67,7 +67,7 @@ public class FeatureDAOImpl implements FeatureDAO { conn = this.getConnection(); String query = "INSERT INTO DM_PROFILE_FEATURES (PROFILE_ID, FEATURE_CODE, DEVICE_TYPE_ID, CONTENT, " + "TENANT_ID) VALUES (?, ?, ?, ?, ?)"; - stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); + stmt = conn.prepareStatement(query, new String[] {"id"}); for (ProfileFeature feature : features) { stmt.setInt(1, profileId); @@ -113,13 +113,9 @@ public class FeatureDAOImpl implements FeatureDAO { String query = "UPDATE DM_PROFILE_FEATURES SET CONTENT = ? WHERE PROFILE_ID = ? AND FEATURE_CODE = ? AND" + " TENANT_ID = ?"; - stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); + stmt = conn.prepareStatement(query); for (ProfileFeature feature : features) { - if (conn.getMetaData().getDriverName().contains("H2")) { - stmt.setBytes(1, PolicyManagerUtil.getBytes(feature.getContent())); - } else { - stmt.setBytes(1, PolicyManagerUtil.getBytes(feature.getContent())); - } + stmt.setBytes(1, PolicyManagerUtil.getBytes(feature.getContent())); stmt.setInt(2, profileId); stmt.setString(3, feature.getFeatureCode()); stmt.setInt(4, tenantId); 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 54d0ff0198..20dac46f78 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 @@ -206,7 +206,7 @@ public class MonitoringDAOImpl implements MonitoringDAO { } @Override - public void addNoneComplianceFeatures(int policyComplianceStatusId, int deviceId, List + public void addNonComplianceFeatures(int policyComplianceStatusId, int deviceId, List complianceFeatures) throws MonitoringDAOException { Connection conn; PreparedStatement stmt = null; @@ -215,7 +215,7 @@ public class MonitoringDAOImpl implements MonitoringDAO { conn = this.getConnection(); String query = "INSERT INTO DM_POLICY_COMPLIANCE_FEATURES (COMPLIANCE_STATUS_ID, FEATURE_CODE, STATUS, " + "TENANT_ID) VALUES (?, ?, ?, ?) "; - stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); + stmt = conn.prepareStatement(query); for (ComplianceFeature feature : complianceFeatures) { stmt.setInt(1, policyComplianceStatusId); stmt.setString(2, feature.getFeatureCode()); 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 33a56b6623..c29c17f832 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 @@ -442,7 +442,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "INSERT INTO DM_CRITERIA (TENANT_ID, NAME) VALUES (?, ?)"; - stmt = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); + stmt = conn.prepareStatement(query, new String[] {"id"}); stmt.setInt(1, tenantId); stmt.setString(2, criteria.getName()); stmt.executeUpdate(); @@ -622,7 +622,7 @@ public class PolicyDAOImpl implements PolicyDAO { try { conn = this.getConnection(); String query = "INSERT INTO DM_POLICY_CRITERIA (CRITERIA_ID, POLICY_ID) VALUES (?, ?)"; - stmt = conn.prepareStatement(query, Statement.RETURN_GENERATED_KEYS); + stmt = conn.prepareStatement(query, new String[] {"id"}); List criteria = policy.getPolicyCriterias(); for (PolicyCriterion criterion : criteria) { @@ -1348,7 +1348,7 @@ public class PolicyDAOImpl implements PolicyDAO { conn = this.getConnection(); String query = "INSERT INTO DM_POLICY (NAME, PROFILE_ID, TENANT_ID, PRIORITY, COMPLIANCE, OWNERSHIP_TYPE," + "UPDATED, ACTIVE, DESCRIPTION) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"; - stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); + stmt = conn.prepareStatement(query, new String[] {"id"}); stmt.setString(1, policy.getPolicyName()); stmt.setInt(2, policy.getProfile().getProfileId()); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java index fc10f61ee1..371c545b35 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/ProfileDAOImpl.java @@ -51,7 +51,7 @@ public class ProfileDAOImpl implements ProfileDAO { conn = this.getConnection(); String query = "INSERT INTO DM_PROFILE " + "(PROFILE_NAME,TENANT_ID, DEVICE_TYPE_ID, CREATED_TIME, UPDATED_TIME) VALUES (?, ?, ?, ?, ?)"; - stmt = conn.prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS); + stmt = conn.prepareStatement(query, new String[] {"id"}); stmt.setString(1, profile.getProfileName()); stmt.setInt(2, tenantId); 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 f6d14166a4..1d1844aed2 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 @@ -120,7 +120,7 @@ public class MonitoringManagerImpl implements MonitoringManager { if (log.isDebugEnabled()) { log.debug("Compliance status primary key " + complianceData.getId()); } - monitoringDAO.addNoneComplianceFeatures(complianceData.getId(), device.getId(), + monitoringDAO.addNonComplianceFeatures(complianceData.getId(), device.getId(), complianceFeatures); PolicyManagementDAOFactory.commitTransaction(); From 813f9072d8a07ef70eb25a824368e72d7cc5faf7 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 16 Nov 2015 17:15:16 +0530 Subject: [PATCH 12/17] Fixing the database issue which causes the deviceids slelect did not work --- .../carbon/policy/mgt/core/dao/PolicyDAO.java | 2 +- .../mgt/core/dao/impl/PolicyDAOImpl.java | 7 ++-- .../core/mgt/impl/MonitoringManagerImpl.java | 34 +++++++++++-------- .../policy/mgt/core/MonitoringTestCase.java | 6 +--- .../src/test/resources/testng.xml | 2 +- 5 files changed, 26 insertions(+), 25 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 d1065e17a5..facb7528c9 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 @@ -140,7 +140,7 @@ public interface PolicyDAO { Policy getAppliedPolicy(int deviceId, int enrollmentId) throws PolicyManagerDAOException; - HashMap getAppliedPolicyIds(List deviceIds) throws PolicyManagerDAOException; + HashMap getAppliedPolicyIds() throws PolicyManagerDAOException; HashMap getAppliedPolicyIdsDeviceIds() 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 33a56b6623..318505991b 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 @@ -1563,7 +1563,7 @@ public class PolicyDAOImpl implements PolicyDAO { } @Override - public HashMap getAppliedPolicyIds(List deviceIds) throws PolicyManagerDAOException { + public HashMap getAppliedPolicyIds() throws PolicyManagerDAOException { Connection conn; PreparedStatement stmt = null; ResultSet resultSet = null; @@ -1571,10 +1571,9 @@ public class PolicyDAOImpl implements PolicyDAO { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { conn = this.getConnection(); - String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE DEVICE_ID = ? AND TENANT_ID = ?"; + String query = "SELECT * FROM DM_DEVICE_POLICY_APPLIED WHERE TENANT_ID = ?"; stmt = conn.prepareStatement(query); - stmt.setString(1, PolicyManagerUtil.makeString(deviceIds)); - stmt.setInt(2, tenantId); + stmt.setInt(1, tenantId); resultSet = stmt.executeQuery(); while (resultSet.next()) { 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 f6d14166a4..344de73897 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 @@ -62,8 +62,8 @@ public class MonitoringManagerImpl implements MonitoringManager { private static final Log log = LogFactory.getLog(MonitoringManagerImpl.class); private static final String OPERATION_MONITOR = "MONITOR"; - private static final String OPERATION_INFO = "DEVICE_INFO"; - private static final String OPERATION_APP_LIST = "APPLICATION_LIST"; + private static final String OPERATION_INFO = "DEVICE_INFO"; + private static final String OPERATION_APP_LIST = "APPLICATION_LIST"; public MonitoringManagerImpl() { this.policyDAO = PolicyManagementDAOFactory.getPolicyDAO(); @@ -229,7 +229,7 @@ public class MonitoringManagerImpl implements MonitoringManager { //int tenantId = PolicyManagerUtil.getTenantId(); Map deviceIds = new HashMap<>(); List complianceDatas = new ArrayList<>(); - HashMap devicePolicyIdMap; + HashMap devicePolicyIdMap = null; try { PolicyManagementDAOFactory.openConnection(); @@ -247,7 +247,13 @@ public class MonitoringManagerImpl implements MonitoringManager { } List deviceIDs = new ArrayList<>(deviceIds.keySet()); - devicePolicyIdMap = policyDAO.getAppliedPolicyIds(deviceIDs); + HashMap temp = policyDAO.getAppliedPolicyIds(); + for (Integer id : deviceIDs) { + if (temp != null && temp.containsKey(id)) { + devicePolicyIdMap.put(id, temp.get(id)); + } + } + } catch (SQLException e) { throw new PolicyComplianceException("SQL error occurred while getting monitoring details.", e); } catch (MonitoringDAOException e) { @@ -380,19 +386,19 @@ public class MonitoringManagerImpl implements MonitoringManager { monitoringOperation.setEnabled(true); monitoringOperation.setType(Operation.Type.COMMAND); monitoringOperation.setCode(OPERATION_MONITOR); - CommandOperation infoOperation = new CommandOperation(); - infoOperation.setEnabled(true); - infoOperation.setType(Operation.Type.COMMAND); - infoOperation.setCode(OPERATION_INFO); - CommandOperation appListOperation = new CommandOperation(); - appListOperation.setEnabled(true); - appListOperation.setType(Operation.Type.COMMAND); - appListOperation.setCode(OPERATION_APP_LIST); +// CommandOperation infoOperation = new CommandOperation(); +// infoOperation.setEnabled(true); +// infoOperation.setType(Operation.Type.COMMAND); +// infoOperation.setCode(OPERATION_INFO); +// CommandOperation appListOperation = new CommandOperation(); +// appListOperation.setEnabled(true); +// appListOperation.setType(Operation.Type.COMMAND); +// appListOperation.setCode(OPERATION_APP_LIST); DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); service.addOperation(monitoringOperation, deviceIdentifiers); - service.addOperation(infoOperation, deviceIdentifiers); - service.addOperation(appListOperation, deviceIdentifiers); +// service.addOperation(infoOperation, deviceIdentifiers); +// service.addOperation(appListOperation, deviceIdentifiers); } private List getDeviceIdentifiersFromDevices(List devices) { diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java index f1fdfed8e1..f0943c6f8c 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/java/org/wso2/carbon/policy/mgt/core/MonitoringTestCase.java @@ -31,12 +31,8 @@ import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationManagerImpl; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; -import org.wso2.carbon.ntask.common.TaskException; -import org.wso2.carbon.ntask.core.service.TaskService; -import org.wso2.carbon.ntask.core.service.impl.TaskServiceImpl; import org.wso2.carbon.policy.mgt.common.Policy; import org.wso2.carbon.policy.mgt.common.PolicyManagementException; -import org.wso2.carbon.policy.mgt.common.PolicyMonitoringTaskException; import org.wso2.carbon.policy.mgt.common.monitor.PolicyComplianceException; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager; @@ -127,7 +123,7 @@ public class MonitoringTestCase extends BasePolicyManagementDAOTest { DeviceManagementProviderService service = new DeviceManagementProviderServiceImpl(); List devices = service.getAllDevices(ANDROID); - monitoringManager.addMonitoringOperation(devices); + // monitoringManager.addMonitoringOperation(devices); log.debug("Compliance operations adding done."); diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml index 24a1f0d80e..8d74befa5d 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/test/resources/testng.xml @@ -22,7 +22,7 @@ - + From 0f146e83faf3a06413f20c567c54465baf743903 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 16 Nov 2015 17:17:15 +0530 Subject: [PATCH 13/17] Fixing the issue which cause policy not bieng applied to device which were enrolled before the adding the policy to the system --- .../policy/mgt/core/enforcement/DelegationTask.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) 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 ad9f58654c..2de040e451 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 @@ -32,7 +32,6 @@ import org.wso2.carbon.policy.mgt.core.mgt.PolicyManager; import org.wso2.carbon.policy.mgt.core.mgt.impl.PolicyManagerImpl; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; import java.util.Map; @@ -73,13 +72,13 @@ public class DelegationTask implements Task { throw new PolicyManagementException("Error occurred while taking the devices", e); } } - HashMap deviceIdPolicy = policyManager.getAppliedPolicyIdsDeviceIds(); +// HashMap deviceIdPolicy = policyManager.getAppliedPolicyIdsDeviceIds(); List toBeNotified = new ArrayList<>(); for (Device device : devices) { - if (deviceIdPolicy.containsKey(device.getId())) { - toBeNotified.add(device); - } +// if (deviceIdPolicy.containsKey(device.getId())) { + toBeNotified.add(device); +// } } if (!toBeNotified.isEmpty()) { PolicyEnforcementDelegator enforcementDelegator = new PolicyEnforcementDelegatorImpl(toBeNotified); From fed4f72d4bec72c151f031a366b9e3d56fd30493 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 16 Nov 2015 17:41:07 +0530 Subject: [PATCH 14/17] Fixing the null pointer --- .../carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 344de73897..598fd80b7e 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 @@ -249,7 +249,7 @@ public class MonitoringManagerImpl implements MonitoringManager { HashMap temp = policyDAO.getAppliedPolicyIds(); for (Integer id : deviceIDs) { - if (temp != null && temp.containsKey(id)) { + if (temp != null && !temp.isEmpty() && temp.containsKey(id)) { devicePolicyIdMap.put(id, temp.get(id)); } } From 700b308b452b99242de43da19c0496182364fd07 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 16 Nov 2015 17:44:30 +0530 Subject: [PATCH 15/17] Fixing the null pointer and removnig innactive devices being monitored --- .../org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java index 2fd0a5b07e..cf803dec28 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/task/MonitoringTask.java @@ -23,7 +23,6 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.EnrolmentInfo; -import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; 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.DeviceType; @@ -90,7 +89,7 @@ public class MonitoringTask implements Task { PolicyManagementDataHolder.getInstance().getPolicyMonitoringService(deviceType.getName()); List devices = deviceManagementProviderService.getAllDevices(deviceType.getName()); if (monitoringService != null && !devices.isEmpty()) { - monitoringManager.addMonitoringOperation(devices); + List notifiableDevices = new ArrayList<>(); @@ -118,6 +117,7 @@ public class MonitoringTask implements Task { log.debug(device.getDeviceIdentifier()); } } + monitoringManager.addMonitoringOperation(notifiableDevices); monitoringService.notifyDevices(notifiableDevices); } } From 271c4f76cb969bed95e6ce85ab2ab42a02a730c0 Mon Sep 17 00:00:00 2001 From: geethkokila Date: Mon, 16 Nov 2015 18:08:21 +0530 Subject: [PATCH 16/17] Fixnig the null pointer isssue --- .../carbon/policy/mgt/core/mgt/impl/MonitoringManagerImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 a1d945c356..1eb91150b8 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 @@ -229,7 +229,7 @@ public class MonitoringManagerImpl implements MonitoringManager { //int tenantId = PolicyManagerUtil.getTenantId(); Map deviceIds = new HashMap<>(); List complianceDatas = new ArrayList<>(); - HashMap devicePolicyIdMap = null; + HashMap devicePolicyIdMap = new HashMap<>(); try { PolicyManagementDAOFactory.openConnection(); From a3e20367f269376d5867612a00d30d5ccdcd1965 Mon Sep 17 00:00:00 2001 From: harshanl Date: Mon, 16 Nov 2015 19:51:59 +0530 Subject: [PATCH 17/17] Fixed issue in Policy Task when DeviceAuthorization enabled. --- .../mgt/core/operation/mgt/OperationManagerImpl.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java index 7e444c706b..0337a0a079 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/operation/mgt/OperationManagerImpl.java @@ -82,8 +82,13 @@ public class OperationManagerImpl implements OperationManager { } } try { - List authorizedDeviceList = DeviceManagementDataHolder.getInstance(). - getDeviceAccessAuthorizationService().isUserAuthorized(deviceIds).getAuthorizedDevices(); + List authorizedDeviceList; + if (operation != null && PolicyOperation.POLICY_OPERATION_CODE.equals(operation.getCode())) { + authorizedDeviceList = deviceIds; + } else { + authorizedDeviceList = DeviceManagementDataHolder.getInstance(). + getDeviceAccessAuthorizationService().isUserAuthorized(deviceIds).getAuthorizedDevices(); + } if (authorizedDeviceList.size() > 0) { try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId();