From fbfb86c53e3082ff51731607a3e412caf1a12089 Mon Sep 17 00:00:00 2001 From: ayyoob Date: Sun, 23 Oct 2016 15:45:29 +0530 Subject: [PATCH] refactored DAO handler --- .../template/dao/DeviceTypeDAOHandler.java | 107 ++++++++++++++++++ .../template/dao/DeviceTypePluginDAO.java | 20 ++-- .../dao/DeviceTypePluginDAOManager.java | 97 +--------------- 3 files changed, 120 insertions(+), 104 deletions(-) create mode 100644 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/dao/DeviceTypeDAOHandler.java 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/dao/DeviceTypeDAOHandler.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/dao/DeviceTypeDAOHandler.java new file mode 100644 index 0000000000..ce3f90c7ea --- /dev/null +++ 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/dao/DeviceTypeDAOHandler.java @@ -0,0 +1,107 @@ +package org.wso2.carbon.device.mgt.extensions.device.type.deployer.template.dao; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.exception.DeviceTypeDeployerFileException; +import org.wso2.carbon.device.mgt.extensions.device.type.deployer.exception.DeviceTypeMgtPluginException; + +import javax.naming.Context; +import javax.naming.InitialContext; +import javax.naming.NamingException; +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; + +/** + * This component handles the connections + */ +public class DeviceTypeDAOHandler { + + private static final Log log = LogFactory.getLog(DeviceTypeDAOHandler.class); + + private DataSource dataSource; + private ThreadLocal currentConnection = new ThreadLocal(); + + public DeviceTypeDAOHandler(String datasourceName) { + initDAO(datasourceName); + } + + public void initDAO(String datasourceName) { + try { + Context ctx = new InitialContext(); + dataSource = (DataSource) ctx.lookup(datasourceName); + } catch (NamingException e) { + throw new DeviceTypeDeployerFileException("Error while looking up the data source: " + datasourceName, e); + } + } + + public void beginTransaction() throws DeviceTypeMgtPluginException { + try { + Connection conn = dataSource.getConnection(); + conn.setAutoCommit(false); + currentConnection.set(conn); + } catch (SQLException e) { + throw new DeviceTypeMgtPluginException("Error occurred while retrieving datasource connection", e); + } + } + + public Connection getConnection() throws DeviceTypeMgtPluginException { + if (currentConnection.get() == null) { + try { + currentConnection.set(dataSource.getConnection()); + } catch (SQLException e) { + throw new DeviceTypeMgtPluginException("Error occurred while retrieving data source connection", e); + } + } + return currentConnection.get(); + } + + public void commitTransaction() throws DeviceTypeMgtPluginException { + try { + Connection conn = currentConnection.get(); + if (conn != null) { + conn.commit(); + } else { + if (log.isDebugEnabled()) { + log.debug("Datasource connection associated with the current thread is null, hence commit " + + "has not been attempted"); + } + } + } catch (SQLException e) { + throw new DeviceTypeMgtPluginException("Error occurred while committing the transaction", e); + } finally { + closeConnection(); + } + } + + public void closeConnection() throws DeviceTypeMgtPluginException { + + Connection con = currentConnection.get(); + if (con != null) { + try { + con.close(); + } catch (SQLException e) { + log.error("Error occurred while close the connection"); + } + } + currentConnection.remove(); + } + + public void rollbackTransaction() throws DeviceTypeMgtPluginException { + try { + Connection conn = currentConnection.get(); + if (conn != null) { + conn.rollback(); + } else { + if (log.isDebugEnabled()) { + log.debug("Datasource connection associated with the current thread is null, hence rollback " + + "has not been attempted"); + } + } + } catch (SQLException e) { + throw new DeviceTypeMgtPluginException("Error occurred while rollback the transaction", e); + } finally { + closeConnection(); + } + } +} 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/dao/DeviceTypePluginDAO.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/dao/DeviceTypePluginDAO.java index 903ad6ef6c..4840d73fd4 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/dao/DeviceTypePluginDAO.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/dao/DeviceTypePluginDAO.java @@ -39,12 +39,12 @@ import java.util.List; public class DeviceTypePluginDAO { private static final Log log = LogFactory.getLog(DeviceTypePluginDAO.class); - private DeviceTypePluginDAOManager deviceTypePluginDAOManager; + private DeviceTypeDAOHandler deviceTypeDAOHandler; private DeviceDAODefinition deviceDAODefinition; public DeviceTypePluginDAO(DeviceDAODefinition deviceDAODefinition, - DeviceTypePluginDAOManager deviceTypePluginDAOManager) { - this.deviceTypePluginDAOManager = deviceTypePluginDAOManager; + DeviceTypeDAOHandler deviceTypeDAOHandler) { + this.deviceTypeDAOHandler = deviceTypeDAOHandler; this.deviceDAODefinition = deviceDAODefinition; } @@ -54,7 +54,7 @@ public class DeviceTypePluginDAO { Device device = null; ResultSet resultSet = null; try { - conn = deviceTypePluginDAOManager.getConnection(); + conn = deviceTypeDAOHandler.getConnection(); String selectDBQuery = "SELECT " + getDeviceTableColumnNames() + " FROM " + deviceDAODefinition.getDeviceTableName() + " WHERE " + deviceDAODefinition.getPrimarkey() + " = ?"; stmt = conn.prepareStatement(selectDBQuery); @@ -84,7 +84,7 @@ public class DeviceTypePluginDAO { throw new DeviceTypeMgtPluginException(msg, e); } finally { DeviceTypeUtils.cleanupResources(stmt, resultSet); - deviceTypePluginDAOManager.closeConnection(); + deviceTypeDAOHandler.closeConnection(); } return device; @@ -95,7 +95,7 @@ public class DeviceTypePluginDAO { Connection conn = null; PreparedStatement stmt = null; try { - conn = deviceTypePluginDAOManager.getConnection(); + conn = deviceTypeDAOHandler.getConnection(); String createDBQuery = "INSERT INTO " + deviceDAODefinition.getDeviceTableName() + "(" + deviceDAODefinition.getPrimarkey() + " , " + getDeviceTableColumnNames() + ") VALUES (" + getPreparedInputString(deviceDAODefinition.getColumnNames().size() + 1) + ")"; @@ -130,7 +130,7 @@ public class DeviceTypePluginDAO { Connection conn = null; PreparedStatement stmt = null; try { - conn = deviceTypePluginDAOManager.getConnection(); + conn = deviceTypeDAOHandler.getConnection(); String updateDBQuery = "UPDATE " + deviceDAODefinition.getDeviceTableName() + " SET " + getDeviceTableColumnNamesForUpdateQuery()+ " WHERE " + deviceDAODefinition.getPrimarkey() + " = ?"; @@ -165,7 +165,7 @@ public class DeviceTypePluginDAO { Connection conn = null; PreparedStatement stmt = null; try { - conn = deviceTypePluginDAOManager.getConnection(); + conn = deviceTypeDAOHandler.getConnection(); String deleteDBQuery = "DELETE FROM " + deviceDAODefinition.getDeviceTableName() + " WHERE " + deviceDAODefinition.getPrimarkey() + " = ?"; stmt = conn.prepareStatement(deleteDBQuery); @@ -196,7 +196,7 @@ public class DeviceTypePluginDAO { Device device; List devices = new ArrayList<>(); try { - conn = deviceTypePluginDAOManager.getConnection(); + conn = deviceTypeDAOHandler.getConnection(); String selectDBQuery = "SELECT " + getDeviceTableColumnNames() + " FROM " + deviceDAODefinition.getDeviceTableName(); stmt = conn.prepareStatement(selectDBQuery); @@ -226,7 +226,7 @@ public class DeviceTypePluginDAO { throw new DeviceTypeMgtPluginException(msg, e); } finally { DeviceTypeUtils.cleanupResources(stmt, resultSet); - deviceTypePluginDAOManager.closeConnection(); + deviceTypeDAOHandler.closeConnection(); } } 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/dao/DeviceTypePluginDAOManager.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/dao/DeviceTypePluginDAOManager.java index e34c15e3a0..d7c7b249c8 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/dao/DeviceTypePluginDAOManager.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/dao/DeviceTypePluginDAOManager.java @@ -19,111 +19,20 @@ package org.wso2.carbon.device.mgt.extensions.device.type.deployer.template.dao; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.extensions.device.type.deployer.config.DeviceManagementConfiguration; -import org.wso2.carbon.device.mgt.extensions.device.type.deployer.exception.DeviceTypeDeployerFileException; -import org.wso2.carbon.device.mgt.extensions.device.type.deployer.exception.DeviceTypeMgtPluginException; - -import javax.naming.Context; -import javax.naming.InitialContext; -import javax.naming.NamingException; -import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.SQLException; - public class DeviceTypePluginDAOManager { - private static final Log log = LogFactory.getLog(DeviceTypePluginDAOManager.class); - private DataSource dataSource; - private ThreadLocal currentConnection = new ThreadLocal(); private DeviceTypePluginDAO deviceTypePluginDAO; public DeviceTypePluginDAOManager(String datasourceName, DeviceDAODefinition deviceDAODefinition) { - initDAO(datasourceName); - deviceTypePluginDAO = new DeviceTypePluginDAO(deviceDAODefinition, this); + DeviceTypeDAOHandler deviceTypeDAOHandler = new DeviceTypeDAOHandler(datasourceName); + deviceTypePluginDAO = new DeviceTypePluginDAO(deviceDAODefinition, deviceTypeDAOHandler); } - public void initDAO(String datasourceName) { - try { - Context ctx = new InitialContext(); - dataSource = (DataSource) ctx.lookup(datasourceName); - } catch (NamingException e) { - throw new DeviceTypeDeployerFileException("Error while looking up the data source: " + datasourceName, e); - } - } + public DeviceTypePluginDAO getDeviceDAO() { return deviceTypePluginDAO; } - public void beginTransaction() throws DeviceTypeMgtPluginException { - try { - Connection conn = dataSource.getConnection(); - conn.setAutoCommit(false); - currentConnection.set(conn); - } catch (SQLException e) { - throw new DeviceTypeMgtPluginException("Error occurred while retrieving datasource connection", e); - } - } - - public Connection getConnection() throws DeviceTypeMgtPluginException { - if (currentConnection.get() == null) { - try { - currentConnection.set(dataSource.getConnection()); - } catch (SQLException e) { - throw new DeviceTypeMgtPluginException("Error occurred while retrieving data source connection", e); - } - } - return currentConnection.get(); - } - public void commitTransaction() throws DeviceTypeMgtPluginException { - try { - Connection conn = currentConnection.get(); - if (conn != null) { - conn.commit(); - } else { - if (log.isDebugEnabled()) { - log.debug("Datasource connection associated with the current thread is null, hence commit " - + "has not been attempted"); - } - } - } catch (SQLException e) { - throw new DeviceTypeMgtPluginException("Error occurred while committing the transaction", e); - } finally { - closeConnection(); - } - } - - public void closeConnection() throws DeviceTypeMgtPluginException { - - Connection con = currentConnection.get(); - if (con != null) { - try { - con.close(); - } catch (SQLException e) { - log.error("Error occurred while close the connection"); - } - } - currentConnection.remove(); - } - - public void rollbackTransaction() throws DeviceTypeMgtPluginException { - try { - Connection conn = currentConnection.get(); - if (conn != null) { - conn.rollback(); - } else { - if (log.isDebugEnabled()) { - log.debug("Datasource connection associated with the current thread is null, hence rollback " - + "has not been attempted"); - } - } - } catch (SQLException e) { - throw new DeviceTypeMgtPluginException("Error occurred while rollback the transaction", e); - } finally { - closeConnection(); - } - } } \ No newline at end of file