From ebc896ccd43b86c3dda0fce3efca159ec057451e Mon Sep 17 00:00:00 2001 From: Vishanth Date: Thu, 15 Dec 2016 10:30:56 +0530 Subject: [PATCH 1/2] removing the device type from constraint to support multi tenancy since it is not unique --- .../src/main/resources/dbscripts/cdm/h2.sql | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql index 8a5195b4cd..eb30a07b12 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -147,8 +147,8 @@ CREATE TABLE IF NOT EXISTS DM_PROFILE ( UPDATED_TIME DATETIME NOT NULL , PRIMARY KEY (ID) , CONSTRAINT DM_PROFILE_DEVICE_TYPE - FOREIGN KEY (DEVICE_TYPE ) - REFERENCES DM_DEVICE_TYPE (NAME) + FOREIGN KEY (DEVICE_TYPE, TENANT_ID) + REFERENCES DM_DEVICE_TYPE (NAME, PROVIDER_TENANT_ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); @@ -200,11 +200,6 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE_POLICY ( FOREIGN KEY (POLICY_ID ) REFERENCES DM_POLICY (ID ) ON DELETE NO ACTION - ON UPDATE NO ACTION, - CONSTRAINT FK_DEVICE_TYPE_POLICY_DEVICE_TYPE - FOREIGN KEY (DEVICE_TYPE ) - REFERENCES DM_DEVICE_TYPE (NAME) - ON DELETE NO ACTION ON UPDATE NO ACTION ); From 1714328db04a15522c4cd5b0a8fdcb01bb527bff Mon Sep 17 00:00:00 2001 From: Vishanth Date: Thu, 15 Dec 2016 10:32:26 +0530 Subject: [PATCH 2/2] datasource access for multi tenancy --- .../deployer/template/DeviceTypeManager.java | 45 ++++++++++++------- 1 file changed, 28 insertions(+), 17 deletions(-) diff --git a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManager.java b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManager.java index 8a9bcd7ed5..eba2f18721 100644 --- a/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManager.java +++ b/components/device-mgt-extensions/org.wso2.carbon.device.mgt.extensions.device.type.deployer/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/deployer/template/DeviceTypeManager.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.extensions.device.type.deployer.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.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; @@ -41,6 +42,7 @@ import org.wso2.carbon.device.mgt.extensions.device.type.deployer.template.util. import org.wso2.carbon.device.mgt.extensions.license.mgt.registry.RegistryBasedLicenseManager; import org.wso2.carbon.registry.api.RegistryException; import org.wso2.carbon.registry.api.Resource; +import org.wso2.carbon.utils.multitenancy.MultitenantConstants; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; @@ -118,25 +120,34 @@ public class DeviceTypeManager implements DeviceManager { throw new DeviceTypeDeployerFileException("Could not find definition for table: " + tableName); } propertiesExist = true; - DeviceDAODefinition deviceDAODefinition = new DeviceDAODefinition(deviceDefinitionTable); - String datasourceName = deviceTypeConfiguration.getDataSource().getJndiConfig().getName(); - if (datasourceName != null && !datasourceName.isEmpty()) { - String setupOption = System.getProperty("setup"); - if (setupOption != null) { - if (log.isDebugEnabled()) { - log.debug("-Dsetup is enabled. Device management repository schema initialization is about " + - "to begin"); - } - try { - DeviceTypeUtils.setupDeviceManagementSchema(datasourceName, deviceType, - deviceDAODefinition.getDeviceTableName()); - } catch (DeviceTypeMgtPluginException e) { - log.error("Exception occurred while initializing device management database schema", e); + + try { + PrivilegedCarbonContext.startTenantFlow(); + PrivilegedCarbonContext.getThreadLocalCarbonContext() + .setTenantDomain(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true); + + DeviceDAODefinition deviceDAODefinition = new DeviceDAODefinition(deviceDefinitionTable); + String datasourceName = deviceTypeConfiguration.getDataSource().getJndiConfig().getName(); + if (datasourceName != null && !datasourceName.isEmpty()) { + String setupOption = System.getProperty("setup"); + if (setupOption != null) { + if (log.isDebugEnabled()) { + log.debug("-Dsetup is enabled. Device management repository schema initialization is about " + + "to begin"); + } + try { + DeviceTypeUtils.setupDeviceManagementSchema(datasourceName, deviceType, + deviceDAODefinition.getDeviceTableName()); + } catch (DeviceTypeMgtPluginException e) { + log.error("Exception occurred while initializing device management database schema", e); + } } + deviceTypePluginDAOManager = new DeviceTypePluginDAOManager(datasourceName, deviceDAODefinition); + } else { + throw new DeviceTypeDeployerFileException("Invalid datasource name."); } - deviceTypePluginDAOManager = new DeviceTypePluginDAOManager(datasourceName, deviceDAODefinition); - } else { - throw new DeviceTypeDeployerFileException("Invalid datasource name."); + } finally { + PrivilegedCarbonContext.endTenantFlow(); } } }