From e4d6070bee013cd171013f372e54cf27e792b65b Mon Sep 17 00:00:00 2001 From: Geeth Munasinghe Date: Wed, 17 Dec 2014 10:32:46 +0530 Subject: [PATCH] Adding the datasources changes --- .../mgt/core/dao/impl/PolicyDAOImpl.java | 11 ++++++ .../dao/util/PolicyManagementDAOUtil.java | 34 +++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java index 0a5c6bc74..41c37e7ce 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/impl/PolicyDAOImpl.java @@ -25,6 +25,8 @@ import org.wso2.carbon.policy.mgt.core.dao.PolicyDAO; import org.wso2.carbon.policy.mgt.core.dao.PolicyManagerDAOException; import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; public class PolicyDAOImpl implements PolicyDAO { @@ -70,4 +72,13 @@ public class PolicyDAOImpl implements PolicyDAO { public Policy getPolicy(String deviceID, String deviceType) throws PolicyManagerDAOException { return null; } + + private Connection getConnection() throws PolicyManagerDAOException { + try { + return dataSource.getConnection(); + } catch (SQLException e) { + throw new PolicyManagerDAOException("Error occurred while obtaining a connection from the policy " + + "management metadata repository datasource", e); + } + } } diff --git a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/util/PolicyManagementDAOUtil.java b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/util/PolicyManagementDAOUtil.java index 55d4be5ec..75363231e 100644 --- a/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/util/PolicyManagementDAOUtil.java +++ b/components/policy-mgt/org.wso2.carbon.policy.mgt.core/src/main/java/org/wso2/carbon/policy/mgt/core/dao/util/PolicyManagementDAOUtil.java @@ -18,11 +18,21 @@ package org.wso2.carbon.policy.mgt.core.dao.util; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import javax.naming.InitialContext; import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.Hashtable; public class PolicyManagementDAOUtil { + + private static final Log log = LogFactory.getLog(PolicyManagementDAOUtil.class); + public static DataSource lookupDataSource(String dataSourceName, final Hashtable jndiProperties) { try { if (jndiProperties == null || jndiProperties.isEmpty()) { @@ -35,4 +45,28 @@ public class PolicyManagementDAOUtil { } } + public static void cleanupResources(Connection conn, PreparedStatement stmt, ResultSet rs) { + if (rs != null) { + try { + rs.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing result set", e); + } + } + if (stmt != null) { + try { + stmt.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing prepared statement", e); + } + } + if (conn != null) { + try { + conn.close(); + } catch (SQLException e) { + log.warn("Error occurred while closing database connection", e); + } + } + } + }