From 7b7a6b1ce197528644fb6d1330073304e4735dbd Mon Sep 17 00:00:00 2001 From: charitha Date: Fri, 28 Feb 2020 14:10:27 +0530 Subject: [PATCH] Fix issue in accessing device shared via group --- .../impl/DeviceManagementServiceImpl.java | 1 - .../mgt/dao/impl/DeviceDetailsDAOImpl.java | 6 +- .../impl/DeviceInformationManagerImpl.java | 8 +- .../DeviceManagementServiceComponent.java | 7 ++ .../DeviceTaskManagerServiceComponent.java | 79 ++++++++++++------- .../operation/mgt/OperationManagerImpl.java | 37 +++++++++ .../DeviceStatusTaskManagerServiceImpl.java | 2 +- .../impl/DeviceTaskManagerServiceImpl.java | 6 +- 8 files changed, 108 insertions(+), 38 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index cf697d57b2..520013f740 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -628,7 +628,6 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { deviceIdentifier.setType(type); informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService(); deviceLocation = informationManager.getDeviceLocation(deviceIdentifier); - } catch (DeviceDetailsMgtException e) { String msg = "Error occurred while getting the device location."; log.error(msg, e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java index 731c74b2e1..e9d592b47d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java @@ -273,7 +273,7 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { Connection conn; PreparedStatement stmt = null; ResultSet rs = null; - DeviceLocation location = new DeviceLocation(); + DeviceLocation location = null; try { conn = this.getConnection(); String sql = "SELECT * FROM DM_DEVICE_LOCATION WHERE DEVICE_ID = ? AND ENROLMENT_ID = ?"; @@ -282,7 +282,8 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { stmt.setInt(2, enrollmentId); rs = stmt.executeQuery(); - while (rs.next()) { + if (rs.next()) { + location = new DeviceLocation(); location.setDeviceId(deviceId); location.setLatitude(rs.getDouble("LATITUDE")); location.setLongitude(rs.getDouble("LONGITUDE")); @@ -294,7 +295,6 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { location.setCountry(rs.getString("COUNTRY")); location.setUpdatedTime(new java.util.Date(rs.getLong("UPDATE_TIMESTAMP"))); } - location.setDeviceId(deviceId); return location; } catch (SQLException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index 4d479b5207..cdb995a60f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -315,9 +315,13 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { getDeviceManagementProvider().getAllDevices(deviceIdentifiers.get(0).getType(), false); List deviceLocations = new ArrayList<>(); DeviceManagementDAOFactory.openConnection(); + DeviceLocation deviceLocation; for (Device device : devices) { - deviceLocations.add(deviceDetailsDAO.getDeviceLocation(device.getId(), - device.getEnrolmentInfo().getId())); + deviceLocation = deviceDetailsDAO.getDeviceLocation(device.getId(), + device.getEnrolmentInfo().getId()); + if (deviceLocation != null) { + deviceLocations.add(deviceLocation); + } } return deviceLocations; } catch (DeviceManagementException e) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java index 495d4e288d..508507e79b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -46,6 +46,8 @@ import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.device.mgt.core.config.tenant.PlatformConfigurationManagementServiceImpl; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager; +import org.wso2.carbon.device.mgt.core.device.details.mgt.impl.DeviceInformationManagerImpl; import org.wso2.carbon.device.mgt.core.geo.service.GeoLocationProviderServiceImpl; import org.wso2.carbon.device.mgt.core.notification.mgt.NotificationManagementServiceImpl; import org.wso2.carbon.device.mgt.core.notification.mgt.dao.NotificationManagementDAOFactory; @@ -57,6 +59,8 @@ import org.wso2.carbon.device.mgt.core.privacy.impl.PrivacyComplianceProviderImp import org.wso2.carbon.device.mgt.core.push.notification.mgt.PushNotificationProviderRepository; import org.wso2.carbon.device.mgt.core.push.notification.mgt.task.PushNotificationSchedulerTask; import org.wso2.carbon.device.mgt.core.report.mgt.ReportManagementServiceImpl; +import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService; +import org.wso2.carbon.device.mgt.core.search.mgt.impl.SearchManagerServiceImpl; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; @@ -325,6 +329,9 @@ public class DeviceManagementServiceComponent { /* Registering PermissionManager Service */ PermissionManagerService permissionManagerService = PermissionManagerServiceImpl.getInstance(); bundleContext.registerService(PermissionManagerService.class.getName(), permissionManagerService, null); + + bundleContext.registerService(DeviceInformationManager.class, new DeviceInformationManagerImpl(), null); + bundleContext.registerService(SearchManagerService.class, new SearchManagerServiceImpl(), null); } private void setupDeviceManagementSchema(DataSourceConfig config) throws DeviceManagementException { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceTaskManagerServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceTaskManagerServiceComponent.java index ee3072170c..489a29c72c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceTaskManagerServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceTaskManagerServiceComponent.java @@ -16,21 +16,17 @@ * under the License. */ - package org.wso2.carbon.device.mgt.core.internal; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.osgi.framework.BundleContext; import org.osgi.service.component.ComponentContext; import org.wso2.carbon.device.mgt.common.DeviceStatusTaskPluginConfig; import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; import org.wso2.carbon.device.mgt.core.config.DeviceConfigurationManager; import org.wso2.carbon.device.mgt.core.config.DeviceManagementConfig; -import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager; -import org.wso2.carbon.device.mgt.core.device.details.mgt.impl.DeviceInformationManagerImpl; import org.wso2.carbon.device.mgt.core.dto.DeviceType; -import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService; -import org.wso2.carbon.device.mgt.core.search.mgt.impl.SearchManagerServiceImpl; import org.wso2.carbon.device.mgt.core.status.task.DeviceStatusTaskException; import org.wso2.carbon.device.mgt.core.status.task.DeviceStatusTaskManagerService; import org.wso2.carbon.device.mgt.core.status.task.impl.DeviceStatusTaskManagerServiceImpl; @@ -39,7 +35,6 @@ import org.wso2.carbon.device.mgt.core.task.DeviceTaskManagerService; import org.wso2.carbon.device.mgt.core.task.impl.DeviceTaskManagerServiceImpl; import org.wso2.carbon.ntask.core.service.TaskService; -import java.util.ArrayList; import java.util.Map; /** @@ -52,9 +47,11 @@ import java.util.Map; * unbind="unsetTaskService" */ +@SuppressWarnings("unused") public class DeviceTaskManagerServiceComponent { private static Log log = LogFactory.getLog(DeviceTaskManagerServiceComponent.class); + private DeviceManagementConfig deviceManagementConfig; @SuppressWarnings("unused") protected void activate(ComponentContext componentContext) { @@ -62,45 +59,40 @@ public class DeviceTaskManagerServiceComponent { if (log.isDebugEnabled()) { log.debug("Initializing device task manager bundle."); } - getDeviceOperationMonitoringConfig(componentContext); + startOperationMonitoringTask(componentContext.getBundleContext()); //Start the DeviceStatusMonitoringTask for registered DeviceTypes - DeviceManagementConfig deviceManagementConfig = DeviceConfigurationManager.getInstance(). + deviceManagementConfig = DeviceConfigurationManager.getInstance(). getDeviceManagementConfig(); if (deviceManagementConfig != null && deviceManagementConfig.getDeviceStatusTaskConfig().isEnabled()) { - startDeviceStatusMonitoringTask(); + startDeviceStatusMonitoringTask(componentContext.getBundleContext()); } - - componentContext.getBundleContext().registerService(DeviceInformationManager.class, - new DeviceInformationManagerImpl(), null); - - componentContext.getBundleContext().registerService(SearchManagerService.class, - new SearchManagerServiceImpl(), null); } catch (Throwable e) { log.error("Error occurred while initializing device task manager service.", e); } } - private void getDeviceOperationMonitoringConfig(ComponentContext componentContext) + private void startOperationMonitoringTask(BundleContext bundleContext) throws DeviceMgtTaskException { DeviceTaskManagerService deviceTaskManagerService = new DeviceTaskManagerServiceImpl(); DeviceManagementDataHolder.getInstance().setDeviceTaskManagerService(deviceTaskManagerService); - componentContext.getBundleContext().registerService(DeviceTaskManagerService.class, - deviceTaskManagerService, null); + bundleContext.registerService(DeviceTaskManagerService.class, deviceTaskManagerService, null); Map deviceConfigMap = DeviceMonitoringOperationDataHolder .getInstance().getOperationMonitoringConfigFromMap(); - for (String platformType : new ArrayList<>(deviceConfigMap.keySet())) { - if (deviceConfigMap.get(platformType).isEnabled()){ - deviceTaskManagerService.startTask(platformType, deviceConfigMap.get(platformType)); + for (String platformType : deviceConfigMap.keySet()) { + OperationMonitoringTaskConfig taskConfig = deviceConfigMap.get(platformType); + if (taskConfig.isEnabled()) { + deviceTaskManagerService.startTask(platformType, taskConfig); } - deviceConfigMap.remove(platformType); } } - private void startDeviceStatusMonitoringTask() { + private void startDeviceStatusMonitoringTask(BundleContext bundleContext) { DeviceStatusTaskManagerService deviceStatusTaskManagerService = new DeviceStatusTaskManagerServiceImpl(); + DeviceManagementDataHolder.getInstance().setDeviceStatusTaskManagerService(deviceStatusTaskManagerService); + bundleContext.registerService(DeviceStatusTaskManagerService.class, deviceStatusTaskManagerService, null); Map deviceStatusTaskPluginConfigs = DeviceManagementDataHolder. getInstance().getDeviceStatusTaskPluginConfigs(); - for (DeviceType deviceType : new ArrayList<>(deviceStatusTaskPluginConfigs.keySet())) { + for (DeviceType deviceType : deviceStatusTaskPluginConfigs.keySet()) { try { deviceStatusTaskManagerService.startTask(deviceType, deviceStatusTaskPluginConfigs.get(deviceType)); } catch (DeviceStatusTaskException e) { @@ -113,10 +105,41 @@ public class DeviceTaskManagerServiceComponent { @SuppressWarnings("unused") protected void deactivate(ComponentContext componentContext) { try { -// DeviceTaskManagerService taskManagerService = new DeviceTaskManagerServiceImpl(); -// taskManagerService.stopTask(); + stopOperationMonitoringTask(); + if (deviceManagementConfig != null && deviceManagementConfig.getDeviceStatusTaskConfig().isEnabled()) { + stopDeviceStatusMonitoringTask(); + } } catch (Throwable e) { - log.error("Error occurred while destroying the device details retrieving task manager service.", e); + log.error("Error occurred while shutting down device task manager service.", e); + } + } + + private void stopOperationMonitoringTask() + throws DeviceMgtTaskException { + DeviceTaskManagerService deviceTaskManagerService = DeviceManagementDataHolder.getInstance() + .getDeviceTaskManagerService(); + Map deviceConfigMap = DeviceMonitoringOperationDataHolder + .getInstance().getOperationMonitoringConfigFromMap(); + for (String platformType : deviceConfigMap.keySet()) { + OperationMonitoringTaskConfig taskConfig = deviceConfigMap.get(platformType); + if (taskConfig.isEnabled()) { + deviceTaskManagerService.stopTask(platformType, taskConfig); + } + } + } + + private void stopDeviceStatusMonitoringTask() { + DeviceStatusTaskManagerService deviceStatusTaskManagerService = DeviceManagementDataHolder.getInstance() + .getDeviceStatusTaskManagerService(); + Map deviceStatusTaskPluginConfigs = DeviceManagementDataHolder. + getInstance().getDeviceStatusTaskPluginConfigs(); + for (DeviceType deviceType : deviceStatusTaskPluginConfigs.keySet()) { + try { + deviceStatusTaskManagerService.stopTask(deviceType, deviceStatusTaskPluginConfigs.get(deviceType)); + } catch (DeviceStatusTaskException e) { + log.error("Exception occurred while stopping the DeviceStatusMonitoring Task for deviceType '" + + deviceType + "'", e); + } } } @@ -133,4 +156,4 @@ public class DeviceTaskManagerServiceComponent { } DeviceManagementDataHolder.getInstance().setTaskService(null); } -} \ No newline at end of file +} 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 699aee61f3..28526736be 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 @@ -1165,4 +1165,41 @@ public class OperationManagerImpl implements OperationManager { private boolean isSameUser(String user, String owner) { return user.equalsIgnoreCase(owner); } + + private List getOperations(DeviceIdentifier deviceId, Operation.Status status, int enrolmentId) + throws OperationManagementException { + List operations = new ArrayList<>(); + List dtoOperationList = new ArrayList<>(); + + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status internalStatus = + org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation.Status.valueOf(status.name()); + + try { + OperationManagementDAOFactory.openConnection(); + dtoOperationList.addAll(commandOperationDAO.getOperationsByDeviceAndStatus( + enrolmentId, internalStatus)); + dtoOperationList.addAll(configOperationDAO.getOperationsByDeviceAndStatus( + enrolmentId, internalStatus)); + dtoOperationList.addAll(profileOperationDAO.getOperationsByDeviceAndStatus( + enrolmentId, internalStatus)); + dtoOperationList.addAll(policyOperationDAO.getOperationsByDeviceAndStatus( + enrolmentId, internalStatus)); + Operation operation; + for (org.wso2.carbon.device.mgt.core.dto.operation.mgt.Operation dtoOperation : dtoOperationList) { + operation = OperationDAOUtil.convertOperation(dtoOperation); + operations.add(operation); + } + operations.sort(new OperationIdComparator()); + } catch (OperationManagementDAOException e) { + throw new OperationManagementException("Error occurred while retrieving the list of " + + "pending operations assigned for '" + deviceId.getType() + + "' device '" + deviceId.getId() + "'", e); + } catch (SQLException e) { + throw new OperationManagementException( + "Error occurred while opening a connection to the data source", e); + } finally { + OperationManagementDAOFactory.closeConnection(); + } + return operations; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/status/task/impl/DeviceStatusTaskManagerServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/status/task/impl/DeviceStatusTaskManagerServiceImpl.java index 988be3f4d0..058085e5ea 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/status/task/impl/DeviceStatusTaskManagerServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/status/task/impl/DeviceStatusTaskManagerServiceImpl.java @@ -100,7 +100,7 @@ public class DeviceStatusTaskManagerServiceImpl implements DeviceStatusTaskManag try { TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService(); String taskName = DEVICE_STATUS_MONITORING_TASK_TYPE + "_" + deviceType.getName() + "_" + deviceType.getId(); - if (taskService.isServerInit()) { + if (taskService != null && taskService.isServerInit()) { TaskManager taskManager = taskService.getTaskManager(DEVICE_STATUS_MONITORING_TASK_TYPE); taskManager.deleteTask(taskName); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerServiceImpl.java index d564fd7542..30b70760d1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/task/impl/DeviceTaskManagerServiceImpl.java @@ -109,9 +109,9 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { try { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); TaskService taskService = DeviceManagementDataHolder.getInstance().getTaskService(); - if (taskService.isServerInit()) { + if (taskService != null && taskService.isServerInit()) { TaskManager taskManager = taskService.getTaskManager(TASK_TYPE); - String taskName = deviceType + String.valueOf(tenantId); + String taskName = deviceType + tenantId; taskManager.deleteTask(taskName); } } catch (TaskException e) { @@ -133,7 +133,7 @@ public class DeviceTaskManagerServiceImpl implements DeviceTaskManagerService { TaskManager taskManager = taskService.getTaskManager(TASK_TYPE); if (taskManager.isTaskScheduled(deviceType)) { - String taskName = deviceType + String.valueOf(tenantId); + String taskName = deviceType + tenantId; taskManager.deleteTask(taskName); TaskInfo.TriggerInfo triggerInfo = new TaskInfo.TriggerInfo(); triggerInfo.setIntervalMillis(operationMonitoringTaskConfig.getFrequency());