From 4fdeec89cc6489652ada29b876bb704a7c4fdc0d Mon Sep 17 00:00:00 2001 From: Megala Date: Thu, 2 Mar 2017 15:11:59 +0530 Subject: [PATCH 1/2] Revert f3f7635d3b568d7d1c45996e0eb03f31d6ca5c15 --- .../carbon/device/mgt/core/dao/DeviceDAO.java | 7 ------ .../core/dao/impl/AbstractDeviceDAOImpl.java | 22 ------------------- .../DeviceManagementProviderService.java | 5 ----- .../DeviceManagementProviderServiceImpl.java | 15 ------------- .../task/impl/DeviceDetailsRetrieverTask.java | 22 ++++++------------- .../policy/mgt/core/task/MonitoringTask.java | 19 +++++----------- 6 files changed, 13 insertions(+), 77 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java index a634fe93b6..1eb39fc502 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceDAO.java @@ -420,12 +420,5 @@ public interface DeviceDAO { */ List getEnrolmentsByStatus(List deviceIds, Status status, int tenantId) throws DeviceManagementDAOException; - - /** - * Retrieving the tenants which are have device enrolled. - * @return - * @throws DeviceManagementDAOException - */ - List getDeviceEnrolledTenants() throws DeviceManagementDAOException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java index 7131d97323..ffb9aacc1f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/AbstractDeviceDAOImpl.java @@ -1062,26 +1062,4 @@ public abstract class AbstractDeviceDAOImpl implements DeviceDAO { return devices; } - public List getDeviceEnrolledTenants() throws DeviceManagementDAOException { - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - List tenants = new ArrayList<>(); - try { - conn = this.getConnection(); - String sql = "SELECT distinct(TENANT_ID) FROM DM_DEVICE"; - stmt = conn.prepareStatement(sql); - rs = stmt.executeQuery(); - while (rs.next()) { - tenants.add(rs.getInt("TENANT_ID")); - } - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving tenants which have " + - "device registered.", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); - } - return tenants; - } - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index c973cd0afc..a48cb423c8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -307,10 +307,6 @@ public interface DeviceManagementProviderService { PolicyMonitoringManager getPolicyMonitoringManager(String deviceType); - - List getDeviceEnrolledTenants() throws DeviceManagementException; - - /** * Change device status. * @@ -321,5 +317,4 @@ public interface DeviceManagementProviderService { */ boolean changeDeviceStatus(DeviceIdentifier deviceIdentifier, EnrolmentInfo.Status newStatus) throws DeviceManagementException; - } 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 89846e27e5..b61bafe1de 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 @@ -1279,21 +1279,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return dms.getPolicyMonitoringManager(); } - @Override - public List getDeviceEnrolledTenants() throws DeviceManagementException { - try { - DeviceManagementDAOFactory.openConnection(); - return deviceDAO.getDeviceEnrolledTenants(); - } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while retrieving the tenants " + - "which have device enrolled.", e); - } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); - } finally { - DeviceManagementDAOFactory.closeConnection(); - } - } - @Override public List getDevicesOfUser(String username) throws DeviceManagementException { List devices = new ArrayList<>(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java index 9040af6654..3337c65c4b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java @@ -23,7 +23,6 @@ import com.google.gson.Gson; 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.DeviceManagementException; import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.task.DeviceMgtTaskException; @@ -34,7 +33,6 @@ import org.wso2.carbon.user.api.UserStoreException; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import java.util.List; -import java.util.List; import java.util.Map; public class DeviceDetailsRetrieverTask implements Task { @@ -66,18 +64,15 @@ public class DeviceDetailsRetrieverTask implements Task { log.debug("Device details retrieving task started to run."); } try { -// Tenant tenants[] = DeviceManagementDataHolder.getInstance(). -// getRealmService().getTenantManager().getAllTenants(); - - List tenants = DeviceManagementDataHolder.getInstance(). - getDeviceManagementProvider().getDeviceEnrolledTenants(); - for (Integer tenant : tenants) { - String tenantDomain = DeviceManagementDataHolder.getInstance(). - getRealmService().getTenantManager().getDomain(tenant); + Tenant tenants[] = DeviceManagementDataHolder.getInstance(). + getRealmService().getTenantManager().getAllTenants(); + + for (Tenant tenant : tenants) { + try { PrivilegedCarbonContext.startTenantFlow(); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenant.getDomain()); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant.getId()); DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(deviceType, operationMonitoringTaskConfig); //pass the configurations also from here, monitoring tasks @@ -93,9 +88,6 @@ public class DeviceDetailsRetrieverTask implements Task { } } catch (UserStoreException e) { log.error("Error occurred while trying to get the available tenants", e); - } catch (DeviceManagementException e) { - log.error("Error occurred while trying to get the available tenants " + - "from device manager provider service.", e); } 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 eb195cd8bc..dce753ba51 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 @@ -30,9 +30,8 @@ import org.wso2.carbon.ntask.core.Task; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager; +import org.wso2.carbon.user.api.Tenant; import org.wso2.carbon.user.api.UserStoreException; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import java.util.ArrayList; import java.util.List; @@ -62,19 +61,15 @@ public class MonitoringTask implements Task { } try { - - PolicyManagementDataHolder.getInstance(). + Tenant tenants[] = PolicyManagementDataHolder.getInstance(). getRealmService().getTenantManager().getAllTenants(); - DeviceManagementProviderService deviceManagementService = new DeviceManagementProviderServiceImpl(); - List tenants = deviceManagementService.getDeviceEnrolledTenants(); - for (Integer tenant : tenants) { - String tenantDomain = PolicyManagementDataHolder.getInstance(). - getRealmService().getTenantManager().getDomain(tenant); + for (Tenant tenant : tenants) { + try { PrivilegedCarbonContext.startTenantFlow(); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenantDomain); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenant.getDomain()); + PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant.getId()); MonitoringManager monitoringManager = PolicyManagementDataHolder.getInstance().getMonitoringManager(); List deviceTypes = new ArrayList<>(); @@ -147,8 +142,6 @@ public class MonitoringTask implements Task { } catch (UserStoreException e) { log.error("Error occurred while trying to get the available tenants", e); - } catch (DeviceManagementException e) { - log.error("Error occurred while trying to get the available tenants from device manager service ", e); } } From a2d0c59e17b0dc0c21b25af3b43090329e4dc1f9 Mon Sep 17 00:00:00 2001 From: Megala Date: Thu, 2 Mar 2017 15:18:34 +0530 Subject: [PATCH 2/2] Revert 2988357eb91199d9dc444ea098c54f3e3fe56c68 --- .../task/impl/DeviceDetailsRetrieverTask.java | 43 ++---- .../policy/mgt/core/task/MonitoringTask.java | 134 ++++++++---------- 2 files changed, 67 insertions(+), 110 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java index 3337c65c4b..6d1228563c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceDetailsRetrieverTask.java @@ -22,23 +22,17 @@ package org.wso2.carbon.device.mgt.core.task.impl; import com.google.gson.Gson; 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.OperationMonitoringTaskConfig; -import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.task.DeviceMgtTaskException; import org.wso2.carbon.device.mgt.core.task.DeviceTaskManager; import org.wso2.carbon.ntask.core.Task; -import org.wso2.carbon.user.api.Tenant; -import org.wso2.carbon.user.api.UserStoreException; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import java.util.List; import java.util.Map; public class DeviceDetailsRetrieverTask implements Task { private static Log log = LogFactory.getLog(DeviceDetailsRetrieverTask.class); - // private DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(); +// private DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(); private String deviceType; private String oppConfig; private OperationMonitoringTaskConfig operationMonitoringTaskConfig; @@ -63,35 +57,18 @@ public class DeviceDetailsRetrieverTask implements Task { if (log.isDebugEnabled()) { log.debug("Device details retrieving task started to run."); } - try { - Tenant tenants[] = DeviceManagementDataHolder.getInstance(). - getRealmService().getTenantManager().getAllTenants(); - - for (Tenant tenant : tenants) { - try { - PrivilegedCarbonContext.startTenantFlow(); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenant.getDomain()); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant.getId()); - DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(deviceType, - operationMonitoringTaskConfig); - //pass the configurations also from here, monitoring tasks - try { - deviceTaskManager.addOperations(); - } catch (DeviceMgtTaskException e) { - log.error("Error occurred while trying to add the operations to " + - "device to retrieve device details.", e); - } - } finally { - PrivilegedCarbonContext.endTenantFlow(); - } - } - } catch (UserStoreException e) { - log.error("Error occurred while trying to get the available tenants", e); + DeviceTaskManager deviceTaskManager = new DeviceTaskManagerImpl(deviceType, + operationMonitoringTaskConfig); + //pass the configurations also from here, monitoring tasks + try { + deviceTaskManager.addOperations(); + } catch (DeviceMgtTaskException e) { + log.error( + "Error occurred while trying to add the operations to device to retrieve device details.", + e); } - } } - 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 dce753ba51..eb28b9e7d8 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 @@ -21,17 +21,16 @@ package org.wso2.carbon.policy.mgt.core.task; 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.EnrolmentInfo; import org.wso2.carbon.device.mgt.common.policy.mgt.PolicyMonitoringManager; +import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; +import org.wso2.carbon.device.mgt.core.config.policy.PolicyConfiguration; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.ntask.core.Task; import org.wso2.carbon.device.mgt.common.policy.mgt.monitor.PolicyComplianceException; import org.wso2.carbon.policy.mgt.core.internal.PolicyManagementDataHolder; import org.wso2.carbon.policy.mgt.core.mgt.MonitoringManager; -import org.wso2.carbon.user.api.Tenant; -import org.wso2.carbon.user.api.UserStoreException; import java.util.ArrayList; import java.util.List; @@ -60,88 +59,69 @@ public class MonitoringTask implements Task { log.debug("Monitoring task started to run."); } + MonitoringManager monitoringManager = PolicyManagementDataHolder.getInstance().getMonitoringManager(); + List deviceTypes = new ArrayList<>(); + List configDeviceTypes = new ArrayList<>(); try { - Tenant tenants[] = PolicyManagementDataHolder.getInstance(). - getRealmService().getTenantManager().getAllTenants(); - - for (Tenant tenant : tenants) { - - try { - PrivilegedCarbonContext.startTenantFlow(); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain(tenant.getDomain()); - PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenant.getId()); - - MonitoringManager monitoringManager = PolicyManagementDataHolder.getInstance().getMonitoringManager(); - List deviceTypes = new ArrayList<>(); - List configDeviceTypes = new ArrayList<>(); - try { - deviceTypes = monitoringManager.getDeviceTypes(); - for (String deviceType : deviceTypes) { - if (isPlatformExist(deviceType)) { - configDeviceTypes.add(deviceType); - } - } - } catch (PolicyComplianceException e) { - log.error("Error occurred while getting the device types."); + deviceTypes = monitoringManager.getDeviceTypes(); + for (String deviceType : deviceTypes) { + if (isPlatformExist(deviceType)) { + configDeviceTypes.add(deviceType); + } + } + } catch (PolicyComplianceException e) { + log.error("Error occurred while getting the device types."); + } + if (!deviceTypes.isEmpty()) { + try { + DeviceManagementProviderService deviceManagementProviderService = + PolicyManagementDataHolder.getInstance().getDeviceManagementService(); + for (String deviceType : configDeviceTypes) { + if (log.isDebugEnabled()) { + log.debug("Running task for device type : " + deviceType); } - if (!deviceTypes.isEmpty()) { - try { - DeviceManagementProviderService deviceManagementProviderService = - PolicyManagementDataHolder.getInstance().getDeviceManagementService(); - for (String deviceType : configDeviceTypes) { - if (log.isDebugEnabled()) { - log.debug("Running task for device type : " + deviceType); - } - PolicyMonitoringManager monitoringService = - PolicyManagementDataHolder.getInstance().getDeviceManagementService() - .getPolicyMonitoringManager(deviceType); - List devices = deviceManagementProviderService.getAllDevices(deviceType); - if (monitoringService != null && !devices.isEmpty()) { - List notifiableDevices = new ArrayList<>(); - if (log.isDebugEnabled()) { - log.debug("Removing inactive and blocked devices from the list for the device type : " + - deviceType); - } - for (Device device : devices) { - - EnrolmentInfo.Status status = device.getEnrolmentInfo().getStatus(); - if (status.equals(EnrolmentInfo.Status.BLOCKED) || - status.equals(EnrolmentInfo.Status.REMOVED) || - status.equals(EnrolmentInfo.Status.UNCLAIMED) || - status.equals(EnrolmentInfo.Status.DISENROLLMENT_REQUESTED) || - status.equals(EnrolmentInfo.Status.SUSPENDED)) { - continue; - } else { - notifiableDevices.add(device); - } - } - if (log.isDebugEnabled()) { - log.debug("Following devices selected to send the notification for " + deviceType); - for (Device device : notifiableDevices) { - log.debug(device.getDeviceIdentifier()); - } - } - if (!notifiableDevices.isEmpty()) { - monitoringManager.addMonitoringOperation(notifiableDevices); - } - } + PolicyMonitoringManager monitoringService = + PolicyManagementDataHolder.getInstance().getDeviceManagementService() + .getPolicyMonitoringManager(deviceType); + List devices = deviceManagementProviderService.getAllDevices(deviceType); + if (monitoringService != null && !devices.isEmpty()) { + List notifiableDevices = new ArrayList<>(); + if (log.isDebugEnabled()) { + log.debug("Removing inactive and blocked devices from the list for the device type : " + + deviceType); + } + for (Device device : devices) { + + EnrolmentInfo.Status status = device.getEnrolmentInfo().getStatus(); + if (status.equals(EnrolmentInfo.Status.BLOCKED) || + status.equals(EnrolmentInfo.Status.REMOVED) || + status.equals(EnrolmentInfo.Status.UNCLAIMED) || + status.equals(EnrolmentInfo.Status.DISENROLLMENT_REQUESTED) || + status.equals(EnrolmentInfo.Status.SUSPENDED)) { + continue; + } else { + notifiableDevices.add(device); } - if (log.isDebugEnabled()) { - log.debug("Monitoring task running completed."); + } + if (log.isDebugEnabled()) { + log.debug("Following devices selected to send the notification for " + deviceType); + for (Device device : notifiableDevices) { + log.debug(device.getDeviceIdentifier()); } - } catch (Exception e) { - log.error("Error occurred while trying to run a task.", e); } - } else { - log.info("No device types registered currently. So did not run the monitoring task."); + if (!notifiableDevices.isEmpty()) { + monitoringManager.addMonitoringOperation(notifiableDevices); + } } - } finally { - PrivilegedCarbonContext.endTenantFlow(); } + if (log.isDebugEnabled()) { + log.debug("Monitoring task running completed."); + } + } catch (Exception e) { + log.error("Error occurred while trying to run a task.", e); } - - } catch (UserStoreException e) { - log.error("Error occurred while trying to get the available tenants", e); + } else { + log.info("No device types registered currently. So did not run the monitoring task."); } }