From 9d81ee6cb40531583caeabf3aba8409436b3888e Mon Sep 17 00:00:00 2001 From: Saad Sahibjan Date: Thu, 1 Aug 2019 17:59:08 +0530 Subject: [PATCH] Expose device type extension dao plugin via device type deployer --- .../type/template/DeviceTypeManager.java | 32 +++++++++++++++++++ .../config/DeviceTypeConfiguration.java | 23 +++++++++++++ .../template/dao/DeviceTypeDAOHandler.java | 14 ++++++++ 3 files changed, 69 insertions(+) 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 948e5a5ff63..fc331d82202 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 @@ -34,6 +34,7 @@ */ package org.wso2.carbon.device.mgt.extensions.device.type.template; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.w3c.dom.Document; @@ -48,9 +49,11 @@ import org.wso2.carbon.device.mgt.common.configuration.mgt.PlatformConfiguration import org.wso2.carbon.device.mgt.common.license.mgt.License; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManagementException; import org.wso2.carbon.device.mgt.common.license.mgt.LicenseManager; +import org.wso2.carbon.device.mgt.extensions.spi.DeviceTypeManagerExtensionService; import org.wso2.carbon.device.mgt.extensions.device.type.template.config.DataSource; import org.wso2.carbon.device.mgt.extensions.device.type.template.config.DeviceDetails; import org.wso2.carbon.device.mgt.extensions.device.type.template.config.DeviceTypeConfiguration; +import org.wso2.carbon.device.mgt.extensions.device.type.template.config.DeviceTypeManagerExtensionConfig; import org.wso2.carbon.device.mgt.extensions.device.type.template.config.Feature; import org.wso2.carbon.device.mgt.extensions.device.type.template.config.Table; import org.wso2.carbon.device.mgt.extensions.device.type.template.config.TableConfig; @@ -213,6 +216,35 @@ public class DeviceTypeManager implements DeviceManager { } } } + setDeviceTypeManagerExtensionServices(deviceTypeConfiguration); + } + + private void setDeviceTypeManagerExtensionServices(DeviceTypeConfiguration deviceTypeConfiguration) { + DeviceTypeManagerExtensionConfig deviceTypeExtensionConfig = deviceTypeConfiguration.getDeviceTypeExtensionConfig(); + if (deviceTypeExtensionConfig != null) { + String extensionClass = deviceTypeExtensionConfig.getExtensionClass(); + if (StringUtils.isNotEmpty(extensionClass)) { + try { + Class clz = Class.forName(extensionClass); + DeviceTypeManagerExtensionService deviceTypeManagerExtensionService = (DeviceTypeManagerExtensionService) clz.newInstance(); + if (deviceTypePluginDAOManager != null) { + deviceTypeManagerExtensionService.setDeviceTypePluginDAOManager(deviceTypePluginDAOManager); + } + } catch (ClassNotFoundException e) { + String msg = "Extension class cannot be located"; + log.error(msg, e); + throw new DeviceTypeDeployerPayloadException(msg, e); + } catch (IllegalAccessException e) { + String msg = "Cannot access the class or its constructor is not accessible."; + log.error(msg, e); + throw new DeviceTypeDeployerPayloadException(msg, e); + } catch (InstantiationException e) { + String msg = "Extension class instantiation is failed"; + log.error(msg, e); + throw new DeviceTypeDeployerPayloadException(msg, e); + } + } + } } @Override diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/DeviceTypeConfiguration.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/DeviceTypeConfiguration.java index b1ba2d8fe8b..568aa5ae73a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/DeviceTypeConfiguration.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/config/DeviceTypeConfiguration.java @@ -105,6 +105,8 @@ public class DeviceTypeConfiguration { @XmlElementWrapper(name = "StartupOperationConfig") @XmlElement(name = "Operation", required = true) protected List startupOperations; + @XmlElement(name = "DeviceTypeManagerExtensionConfig") + private DeviceTypeManagerExtensionConfig deviceTypeExtensionConfig; public List getOperations() { return operations; @@ -402,4 +404,25 @@ public class DeviceTypeConfiguration { public void setStartupOperations(List startupOperations) { this.startupOperations = startupOperations; } + + /** + * Gets the value of DeviceTypeManagerExtensionConfig + * + * @return possible object is + * {@link DeviceTypeManagerExtensionConfig} + */ + public DeviceTypeManagerExtensionConfig getDeviceTypeExtensionConfig() { + return deviceTypeExtensionConfig; + } + + /** + * Sets the value for DeviceTypeManagerExtensionConfig + * + * @param deviceTypeExtensionConfig possible object is + * {@link DeviceTypeManagerExtensionConfig} + */ + public void setDeviceTypeExtensionConfig( + DeviceTypeManagerExtensionConfig deviceTypeExtensionConfig) { + this.deviceTypeExtensionConfig = deviceTypeExtensionConfig; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypeDAOHandler.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypeDAOHandler.java index e31149fafe9..b37eca5f853 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypeDAOHandler.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypeDAOHandler.java @@ -2,6 +2,7 @@ package org.wso2.carbon.device.mgt.extensions.device.type.template.dao; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.IllegalTransactionStateException; import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypeDeployerPayloadException; import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypeMgtPluginException; @@ -35,6 +36,19 @@ public class DeviceTypeDAOHandler { } } + public void openConnection() throws DeviceTypeMgtPluginException { + try { + Connection conn = currentConnection.get(); + if (conn != null) { + throw new IllegalTransactionStateException("Database connection has already been obtained."); + } + conn = dataSource.getConnection(); + currentConnection.set(conn); + } catch (SQLException e) { + throw new DeviceTypeMgtPluginException("Failed to get a database connection.", e); + } + } + public void beginTransaction() throws DeviceTypeMgtPluginException { try { Connection conn = dataSource.getConnection();