From 0c7843f2fe8512e6c5c287d2e4c11d4df18b6dc2 Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Tue, 13 Aug 2019 18:40:10 +0530 Subject: [PATCH] Modify logic of saving DeviceTypePluginDAOManager to support multi tenancy --- .../type/template/DeviceTypeManager.java | 18 +++++++++--- .../DeviceTypePluginExtensionServiceImpl.java | 29 +++++++++++++++++-- .../DeviceTypePluginExtensionException.java | 12 ++++++++ .../spi/DeviceTypePluginExtensionService.java | 4 +-- 4 files changed, 54 insertions(+), 9 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/exception/DeviceTypePluginExtensionException.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManager.java index 1551d302b7..1169d7eebf 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManager.java @@ -59,6 +59,7 @@ import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceDAOD import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceTypePluginDAOManager; import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypeDeployerPayloadException; import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypeMgtPluginException; +import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypePluginExtensionException; import org.wso2.carbon.device.mgt.extensions.device.type.template.feature.ConfigurationBasedFeatureManager; import org.wso2.carbon.device.mgt.extensions.device.type.template.util.DeviceTypePluginConstants; import org.wso2.carbon.device.mgt.extensions.device.type.template.util.DeviceTypeUtils; @@ -223,10 +224,19 @@ public class DeviceTypeManager implements DeviceManager { * device type plugin in working with its DAO components */ private void setDeviceTypePluginManager() { - if (StringUtils.isNotEmpty(deviceType) && deviceTypePluginDAOManager != null) { - DeviceTypePluginExtensionService deviceTypeManagerExtensionService = - new DeviceTypePluginExtensionServiceImpl(); - deviceTypeManagerExtensionService.addPluginDAOManager(deviceType, deviceTypePluginDAOManager); + if (StringUtils.isNotEmpty(deviceType)) { + if (deviceTypePluginDAOManager != null) { + DeviceTypePluginExtensionService deviceTypeManagerExtensionService = + new DeviceTypePluginExtensionServiceImpl(); + deviceTypeManagerExtensionService.addPluginDAOManager(deviceType, deviceTypePluginDAOManager); + } else { + log.warn("Could not save DeviceTypePluginDAOManager for device type: " + deviceType + + " since DeviceTypePluginDAOManager is null."); + } + } else { + String msg = "Could not save DeviceTypePluginDAOManager since device type is null or empty."; + log.error(msg); + throw new DeviceTypePluginExtensionException(msg); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypePluginExtensionServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypePluginExtensionServiceImpl.java index 4ccaeadb60..bfe01098b6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypePluginExtensionServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypePluginExtensionServiceImpl.java @@ -17,7 +17,11 @@ */ package org.wso2.carbon.device.mgt.extensions.device.type.template; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceTypePluginDAOManager; +import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypePluginExtensionException; import org.wso2.carbon.device.mgt.extensions.spi.DeviceTypePluginExtensionService; import java.util.HashMap; @@ -25,19 +29,38 @@ import java.util.Map; public class DeviceTypePluginExtensionServiceImpl implements DeviceTypePluginExtensionService { + private static final Log log = LogFactory.getLog(DeviceTypePluginExtensionServiceImpl.class); + private static volatile Map pluginDAOManagers = new HashMap<>(); @Override public void addPluginDAOManager(String deviceType, DeviceTypePluginDAOManager pluginDAOManager) { + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); if (pluginDAOManager != null) { - if (!pluginDAOManagers.containsKey(deviceType)) { - pluginDAOManagers.put(deviceType, pluginDAOManager); + if (!pluginDAOManagers.containsKey(tenantId + deviceType)) { + if (log.isDebugEnabled()) { + log.debug("Saving DeviceTypePluginDAOManager against tenant id " + tenantId + + " and device type: " + deviceType); + } + pluginDAOManagers.put(tenantId + deviceType, pluginDAOManager); } } } @Override public DeviceTypePluginDAOManager getPluginDAOManager(String deviceType) { - return pluginDAOManagers.get(deviceType); + int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); + if (pluginDAOManagers.containsKey(tenantId + deviceType)) { + if (log.isDebugEnabled()) { + log.debug("Retrieving DeviceTypePluginDAOManager against tenant id " + tenantId + + " and device type: " + deviceType); + } + return pluginDAOManagers.get(tenantId + deviceType); + } else { + String msg = "DeviceTypePluginDAOManager could not be found against tenant id " + tenantId + + " and device type: " + deviceType; + log.error(msg); + throw new DeviceTypePluginExtensionException(msg); + } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/exception/DeviceTypePluginExtensionException.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/exception/DeviceTypePluginExtensionException.java new file mode 100644 index 0000000000..7afbd12cd2 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/exception/DeviceTypePluginExtensionException.java @@ -0,0 +1,12 @@ +package org.wso2.carbon.device.mgt.extensions.device.type.template.exception; + +public class DeviceTypePluginExtensionException extends RuntimeException { + + public DeviceTypePluginExtensionException(String msg) { + super(msg); + } + + public DeviceTypePluginExtensionException(String msg, Throwable cause) { + super(msg, cause); + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/spi/DeviceTypePluginExtensionService.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/spi/DeviceTypePluginExtensionService.java index 3c57300cec..26aaf481e2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/spi/DeviceTypePluginExtensionService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/spi/DeviceTypePluginExtensionService.java @@ -26,14 +26,14 @@ import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceType public interface DeviceTypePluginExtensionService { /** - * Save device type specific pluginDAOManager in a HashMap + * Save device type specific DeviceTypePluginDAOManager in a HashMap againast tenant ID and device type * @param deviceType - Type of the device (i.e; android, ios, windows) * @param pluginDAOManager - Device type plugin DAO manager instance to be saved against device type */ void addPluginDAOManager(String deviceType, DeviceTypePluginDAOManager pluginDAOManager); /** - * Retrieve the DeviceTypePluginDAOManager instance given the device type + * Retrieve the DeviceTypePluginDAOManager instance against tenant ID and given device type * @param deviceType - Type of the device (i.e; android, ios, windows) * @return an Instance of {@link DeviceTypePluginDAOManager} */