mirror of
https://repository.entgra.net/community/device-mgt-plugins.git
synced 2025-09-16 23:42:15 +00:00
Refractor connection handling in mobile dao factory
This commit is contained in:
parent
10bf596281
commit
22a951c84d
@ -26,10 +26,10 @@ import org.wso2.carbon.device.mgt.mobile.config.datasource.MobileDataSourceConfi
|
||||
import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Hashtable;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Factory class used to create MobileDeviceManagement related DAO objects.
|
||||
@ -37,11 +37,26 @@ import java.util.List;
|
||||
public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceManagementDAOFactoryInterface {
|
||||
|
||||
private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOFactory.class);
|
||||
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
|
||||
private static DataSource dataSource;
|
||||
private static Map<String, DataSource> dataSourceMap = new HashMap<String, DataSource>();
|
||||
private static boolean isInitialized;
|
||||
|
||||
public static void init(MobileDataSourceConfig mobileDataSourceConfig) throws MobileDeviceMgtPluginException {
|
||||
dataSource = resolveDataSource(mobileDataSourceConfig);
|
||||
public static void init(Map<String, MobileDataSourceConfig> mobileDataSourceConfigMap)
|
||||
throws MobileDeviceMgtPluginException {
|
||||
DataSource dataSource;
|
||||
for (String pluginType : mobileDataSourceConfigMap.keySet()) {
|
||||
if (dataSourceMap.get(pluginType) == null) {
|
||||
dataSource = MobileDeviceManagementDAOFactory.resolveDataSource(mobileDataSourceConfigMap.get
|
||||
(pluginType));
|
||||
dataSourceMap.put(pluginType, dataSource);
|
||||
}
|
||||
}
|
||||
isInitialized = true;
|
||||
}
|
||||
|
||||
public static void init(String key, MobileDataSourceConfig mobileDataSourceConfig) throws
|
||||
MobileDeviceMgtPluginException {
|
||||
DataSource dataSource = MobileDeviceManagementDAOFactory.resolveDataSource(mobileDataSourceConfig);
|
||||
dataSourceMap.put(key, dataSource);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -80,73 +95,7 @@ public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceMa
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
public static void beginTransaction() throws MobileDeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.setAutoCommit(false);
|
||||
currentConnection.set(conn);
|
||||
} catch (SQLException e) {
|
||||
throw new MobileDeviceManagementDAOException("Error occurred while retrieving datasource connection", e);
|
||||
public static Map<String, DataSource> getDataSourceMap() {
|
||||
return dataSourceMap;
|
||||
}
|
||||
}
|
||||
|
||||
public static Connection getConnection() throws MobileDeviceManagementDAOException {
|
||||
if (currentConnection.get() == null) {
|
||||
try {
|
||||
currentConnection.set(dataSource.getConnection());
|
||||
} catch (SQLException e) {
|
||||
throw new MobileDeviceManagementDAOException("Error occurred while retrieving data source connection",
|
||||
e);
|
||||
}
|
||||
}
|
||||
return currentConnection.get();
|
||||
}
|
||||
|
||||
public static void commitTransaction() throws MobileDeviceManagementDAOException {
|
||||
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 MobileDeviceManagementDAOException("Error occurred while committing the transaction", e);
|
||||
} finally {
|
||||
closeConnection();
|
||||
}
|
||||
}
|
||||
|
||||
public static void closeConnection() throws MobileDeviceManagementDAOException {
|
||||
|
||||
Connection con = currentConnection.get();
|
||||
try {
|
||||
con.close();
|
||||
} catch (SQLException e) {
|
||||
log.error("Error occurred while close the connection");
|
||||
}
|
||||
currentConnection.remove();
|
||||
}
|
||||
|
||||
public static void rollbackTransaction() throws MobileDeviceManagementDAOException {
|
||||
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 MobileDeviceManagementDAOException("Error occurred while rollback the transaction", e);
|
||||
} finally {
|
||||
closeConnection();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -64,13 +64,13 @@ public class AndroidDeviceManager implements DeviceMgtService {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Enrolling a new Android device : " + device.getDeviceIdentifier());
|
||||
}
|
||||
MobileDeviceManagementDAOFactory.beginTransaction();
|
||||
AndroidDAOFactory.beginTransaction();
|
||||
status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO().addMobileDevice(
|
||||
mobileDevice);
|
||||
MobileDeviceManagementDAOFactory.commitTransaction();
|
||||
AndroidDAOFactory.commitTransaction();
|
||||
} catch (MobileDeviceManagementDAOException e) {
|
||||
try {
|
||||
MobileDeviceManagementDAOFactory.rollbackTransaction();
|
||||
AndroidDAOFactory.rollbackTransaction();
|
||||
} catch (MobileDeviceManagementDAOException mobileDAOEx) {
|
||||
String msg = "Error occurred while roll back the device enrol transaction :" + device.toString();
|
||||
log.warn(msg, mobileDAOEx);
|
||||
@ -90,13 +90,13 @@ public class AndroidDeviceManager implements DeviceMgtService {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Modifying the Android device enrollment data");
|
||||
}
|
||||
MobileDeviceManagementDAOFactory.beginTransaction();
|
||||
AndroidDAOFactory.beginTransaction();
|
||||
status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO()
|
||||
.updateMobileDevice(mobileDevice);
|
||||
MobileDeviceManagementDAOFactory.commitTransaction();
|
||||
AndroidDAOFactory.commitTransaction();
|
||||
} catch (MobileDeviceManagementDAOException e) {
|
||||
try {
|
||||
MobileDeviceManagementDAOFactory.rollbackTransaction();
|
||||
AndroidDAOFactory.rollbackTransaction();
|
||||
} catch (MobileDeviceManagementDAOException mobileDAOEx) {
|
||||
String msg = "Error occurred while roll back the update device transaction :" + device.toString();
|
||||
log.warn(msg, mobileDAOEx);
|
||||
@ -116,13 +116,13 @@ public class AndroidDeviceManager implements DeviceMgtService {
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("Dis-enrolling Android device : " + deviceId);
|
||||
}
|
||||
MobileDeviceManagementDAOFactory.beginTransaction();
|
||||
AndroidDAOFactory.beginTransaction();
|
||||
status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO()
|
||||
.deleteMobileDevice(deviceId.getId());
|
||||
MobileDeviceManagementDAOFactory.commitTransaction();
|
||||
AndroidDAOFactory.commitTransaction();
|
||||
} catch (MobileDeviceManagementDAOException e) {
|
||||
try {
|
||||
MobileDeviceManagementDAOFactory.rollbackTransaction();
|
||||
AndroidDAOFactory.rollbackTransaction();
|
||||
} catch (MobileDeviceManagementDAOException mobileDAOEx) {
|
||||
String msg = "Error occurred while roll back the device dis enrol transaction :" + deviceId.toString();
|
||||
log.warn(msg, mobileDAOEx);
|
||||
@ -200,13 +200,13 @@ public class AndroidDeviceManager implements DeviceMgtService {
|
||||
log.debug(
|
||||
"updating the details of Android device : " + device.getDeviceIdentifier());
|
||||
}
|
||||
MobileDeviceManagementDAOFactory.beginTransaction();
|
||||
AndroidDAOFactory.beginTransaction();
|
||||
status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO()
|
||||
.updateMobileDevice(mobileDevice);
|
||||
MobileDeviceManagementDAOFactory.commitTransaction();
|
||||
AndroidDAOFactory.commitTransaction();
|
||||
} catch (MobileDeviceManagementDAOException e) {
|
||||
try {
|
||||
MobileDeviceManagementDAOFactory.rollbackTransaction();
|
||||
AndroidDAOFactory.rollbackTransaction();
|
||||
} catch (MobileDeviceManagementDAOException mobileDAOEx) {
|
||||
String msg = "Error occurred while roll back the update device info transaction :" + device.toString();
|
||||
log.warn(msg, mobileDAOEx);
|
||||
|
||||
@ -48,14 +48,14 @@ public class AndroidFeatureManager implements FeatureManager {
|
||||
public boolean addFeature(Feature feature) throws DeviceManagementException {
|
||||
|
||||
try {
|
||||
mobileDeviceManagementDAOFactory.beginTransaction();
|
||||
AndroidDAOFactory.beginTransaction();
|
||||
MobileFeature mobileFeature = MobileDeviceManagementUtil.convertToMobileFeature(feature);
|
||||
featureDAO.addFeature(mobileFeature);
|
||||
mobileDeviceManagementDAOFactory.commitTransaction();
|
||||
AndroidDAOFactory.commitTransaction();
|
||||
return true;
|
||||
} catch (MobileDeviceManagementDAOException e) {
|
||||
try {
|
||||
mobileDeviceManagementDAOFactory.rollbackTransaction();
|
||||
AndroidDAOFactory.rollbackTransaction();
|
||||
} catch (MobileDeviceManagementDAOException e1) {
|
||||
log.warn("Error occurred while roll-backing the transaction", e);
|
||||
}
|
||||
@ -94,13 +94,13 @@ public class AndroidFeatureManager implements FeatureManager {
|
||||
public boolean removeFeature(String code) throws DeviceManagementException {
|
||||
boolean status = false;
|
||||
try {
|
||||
mobileDeviceManagementDAOFactory.beginTransaction();
|
||||
AndroidDAOFactory.beginTransaction();
|
||||
featureDAO.deleteFeatureByCode(code);
|
||||
mobileDeviceManagementDAOFactory.commitTransaction();
|
||||
AndroidDAOFactory.commitTransaction();
|
||||
status = true;
|
||||
} catch (MobileDeviceManagementDAOException e) {
|
||||
try {
|
||||
mobileDeviceManagementDAOFactory.rollbackTransaction();
|
||||
AndroidDAOFactory.rollbackTransaction();
|
||||
} catch (MobileDeviceManagementDAOException e1) {
|
||||
log.warn("Error occurred while roll-backing the transaction", e);
|
||||
}
|
||||
|
||||
@ -27,11 +27,19 @@ import org.wso2.carbon.device.mgt.mobile.impl.android.dao.impl.AndroidDeviceDAOI
|
||||
import org.wso2.carbon.device.mgt.mobile.impl.android.dao.impl.AndroidFeatureDAOImpl;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class AndroidDAOFactory extends MobileDeviceManagementDAOFactory
|
||||
implements MobileDeviceManagementDAOFactoryInterface {
|
||||
|
||||
private static final Log log = LogFactory.getLog(AndroidDAOFactory.class);
|
||||
protected static DataSource dataSource;
|
||||
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
|
||||
|
||||
public AndroidDAOFactory() {
|
||||
this.dataSource = getDataSourceMap().get(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MobileDeviceDAO getMobileDeviceDAO() {
|
||||
@ -61,4 +69,72 @@ public class AndroidDAOFactory extends MobileDeviceManagementDAOFactory
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void beginTransaction() throws MobileDeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.setAutoCommit(false);
|
||||
currentConnection.set(conn);
|
||||
} catch (SQLException e) {
|
||||
throw new MobileDeviceManagementDAOException("Error occurred while retrieving datasource connection", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Connection getConnection() throws MobileDeviceManagementDAOException {
|
||||
if (currentConnection.get() == null) {
|
||||
try {
|
||||
currentConnection.set(dataSource.getConnection());
|
||||
} catch (SQLException e) {
|
||||
throw new MobileDeviceManagementDAOException("Error occurred while retrieving data source connection",
|
||||
e);
|
||||
}
|
||||
}
|
||||
return currentConnection.get();
|
||||
}
|
||||
|
||||
public static void commitTransaction() throws MobileDeviceManagementDAOException {
|
||||
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 MobileDeviceManagementDAOException("Error occurred while committing the transaction", e);
|
||||
} finally {
|
||||
closeConnection();
|
||||
}
|
||||
}
|
||||
|
||||
public static void closeConnection() throws MobileDeviceManagementDAOException {
|
||||
|
||||
Connection con = currentConnection.get();
|
||||
try {
|
||||
con.close();
|
||||
} catch (SQLException e) {
|
||||
log.error("Error occurred while close the connection");
|
||||
}
|
||||
currentConnection.remove();
|
||||
}
|
||||
|
||||
public static void rollbackTransaction() throws MobileDeviceManagementDAOException {
|
||||
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 MobileDeviceManagementDAOException("Error occurred while rollback the transaction", e);
|
||||
} finally {
|
||||
closeConnection();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -25,6 +25,7 @@ import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOException;
|
||||
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil;
|
||||
import org.wso2.carbon.device.mgt.mobile.dto.MobileDevice;
|
||||
import org.wso2.carbon.device.mgt.mobile.impl.android.dao.AndroidDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.mobile.impl.android.util.AndroidPluginConstants;
|
||||
import org.wso2.carbon.device.mgt.mobile.impl.android.util.AndroidUtils;
|
||||
|
||||
@ -53,7 +54,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
|
||||
MobileDevice mobileDevice = null;
|
||||
ResultSet resultSet = null;
|
||||
try {
|
||||
conn = MobileDeviceManagementDAOFactory.getConnection();
|
||||
conn = AndroidDAOFactory.getConnection();
|
||||
String selectDBQuery =
|
||||
"SELECT ANDROID_DEVICE_ID, GCM_TOKEN, DEVICE_INFO, DEVICE_MODEL, SERIAL, " +
|
||||
"VENDOR, MAC_ADDRESS, DEVICE_NAME, LATITUDE, LONGITUDE, IMEI, IMSI, OS_VERSION" +
|
||||
@ -96,7 +97,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
|
||||
throw new MobileDeviceManagementDAOException(msg, e);
|
||||
} finally {
|
||||
MobileDeviceManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||
MobileDeviceManagementDAOFactory.closeConnection();
|
||||
AndroidDAOFactory.closeConnection();
|
||||
}
|
||||
|
||||
return mobileDevice;
|
||||
@ -109,7 +110,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
try {
|
||||
conn = MobileDeviceManagementDAOFactory.getConnection();
|
||||
conn = AndroidDAOFactory.getConnection();
|
||||
String createDBQuery =
|
||||
"INSERT INTO AD_DEVICE(ANDROID_DEVICE_ID, GCM_TOKEN, DEVICE_INFO, SERIAL, " +
|
||||
"VENDOR, MAC_ADDRESS, DEVICE_NAME, LATITUDE, LONGITUDE, IMEI, IMSI, " +
|
||||
@ -164,7 +165,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
try {
|
||||
conn = MobileDeviceManagementDAOFactory.getConnection();
|
||||
conn = AndroidDAOFactory.getConnection();
|
||||
String updateDBQuery =
|
||||
"UPDATE AD_DEVICE SET GCM_TOKEN = ?, DEVICE_INFO = ?, SERIAL = ?, VENDOR = ?, " +
|
||||
"MAC_ADDRESS = ?, DEVICE_NAME = ?, LATITUDE = ?, LONGITUDE = ?, IMEI = ?, " +
|
||||
@ -219,7 +220,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
try {
|
||||
conn = MobileDeviceManagementDAOFactory.getConnection();
|
||||
conn = AndroidDAOFactory.getConnection();
|
||||
String deleteDBQuery =
|
||||
"DELETE FROM AD_DEVICE WHERE ANDROID_DEVICE_ID = ?";
|
||||
stmt = conn.prepareStatement(deleteDBQuery);
|
||||
@ -253,7 +254,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
|
||||
List<MobileDevice> mobileDevices = new ArrayList<MobileDevice>();
|
||||
|
||||
try {
|
||||
conn = MobileDeviceManagementDAOFactory.getConnection();
|
||||
conn = AndroidDAOFactory.getConnection();
|
||||
String selectDBQuery =
|
||||
"SELECT ANDROID_DEVICE_ID, GCM_TOKEN, DEVICE_INFO, DEVICE_MODEL, SERIAL, " +
|
||||
"VENDOR, MAC_ADDRESS, DEVICE_NAME, LATITUDE, LONGITUDE, IMEI, IMSI, OS_VERSION " +
|
||||
@ -294,7 +295,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
|
||||
throw new MobileDeviceManagementDAOException(msg, e);
|
||||
} finally {
|
||||
MobileDeviceManagementDAOUtil.cleanupResources(stmt, resultSet);
|
||||
MobileDeviceManagementDAOFactory.closeConnection();
|
||||
AndroidDAOFactory.closeConnection();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -26,6 +26,7 @@ import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.mobile.dao.MobileFeatureDAO;
|
||||
import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil;
|
||||
import org.wso2.carbon.device.mgt.mobile.dto.MobileFeature;
|
||||
import org.wso2.carbon.device.mgt.mobile.impl.android.dao.AndroidDAOFactory;
|
||||
import org.wso2.carbon.device.mgt.mobile.impl.android.dao.AndroidFeatureManagementDAOException;
|
||||
import org.wso2.carbon.device.mgt.mobile.impl.android.util.AndroidPluginConstants;
|
||||
|
||||
@ -42,6 +43,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
|
||||
private static final Log log = LogFactory.getLog(AndroidFeatureDAOImpl.class);
|
||||
|
||||
public AndroidFeatureDAOImpl() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -51,7 +53,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
|
||||
boolean status = false;
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = MobileDeviceManagementDAOFactory.getConnection();
|
||||
conn = AndroidDAOFactory.getConnection();
|
||||
String sql = "INSERT INTO AD_FEATURE(CODE, NAME, DESCRIPTION) VALUES (?, ?, ?)";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, mobileFeature.getCode());
|
||||
@ -76,7 +78,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
|
||||
Connection conn = null;
|
||||
PreparedStatement stmt = null;
|
||||
try {
|
||||
conn = MobileDeviceManagementDAOFactory.getConnection();
|
||||
conn = AndroidDAOFactory.getConnection();
|
||||
String updateDBQuery =
|
||||
"UPDATE AD_FEATURE SET NAME = ?, DESCRIPTION = ?" +
|
||||
"WHERE CODE = ?";
|
||||
@ -112,7 +114,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
|
||||
boolean status = false;
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = MobileDeviceManagementDAOFactory.getConnection();
|
||||
conn = AndroidDAOFactory.getConnection();
|
||||
String sql = "DELETE FROM AD_FEATURE WHERE ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, mblFeatureId);
|
||||
@ -135,7 +137,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
|
||||
boolean status = false;
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = MobileDeviceManagementDAOFactory.getConnection();
|
||||
conn = AndroidDAOFactory.getConnection();
|
||||
String sql = "DELETE FROM AD_FEATURE WHERE CODE = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, mblFeatureCode);
|
||||
@ -158,7 +160,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
|
||||
ResultSet rs = null;
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = MobileDeviceManagementDAOFactory.getConnection();
|
||||
conn = AndroidDAOFactory.getConnection();
|
||||
String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM AD_FEATURE WHERE ID = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setInt(1, mblFeatureId);
|
||||
@ -182,7 +184,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
|
||||
mblFeatureId + "' from the Android database.", e);
|
||||
} finally {
|
||||
MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
MobileDeviceManagementDAOFactory.closeConnection();
|
||||
AndroidDAOFactory.closeConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@ -194,7 +196,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
|
||||
Connection conn = null;
|
||||
|
||||
try {
|
||||
conn = MobileDeviceManagementDAOFactory.getConnection();
|
||||
conn = AndroidDAOFactory.getConnection();
|
||||
String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM AD_FEATURE WHERE CODE = ?";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
stmt.setString(1, mblFeatureCode);
|
||||
@ -218,7 +220,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
|
||||
mblFeatureCode + "' from the Android database.", e);
|
||||
} finally {
|
||||
MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
MobileDeviceManagementDAOFactory.closeConnection();
|
||||
AndroidDAOFactory.closeConnection();
|
||||
}
|
||||
}
|
||||
|
||||
@ -237,7 +239,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
|
||||
List<MobileFeature> features = new ArrayList<MobileFeature>();
|
||||
|
||||
try {
|
||||
conn = MobileDeviceManagementDAOFactory.getConnection();
|
||||
conn = AndroidDAOFactory.getConnection();
|
||||
String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM AD_FEATURE";
|
||||
stmt = conn.prepareStatement(sql);
|
||||
rs = stmt.executeQuery();
|
||||
@ -260,7 +262,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
|
||||
"android features from the android database.", e);
|
||||
} finally {
|
||||
MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs);
|
||||
MobileDeviceManagementDAOFactory.closeConnection();
|
||||
AndroidDAOFactory.closeConnection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -18,12 +18,27 @@
|
||||
*/
|
||||
package org.wso2.carbon.device.mgt.mobile.impl.windows.dao;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants;
|
||||
import org.wso2.carbon.device.mgt.mobile.config.datasource.MobileDataSourceConfig;
|
||||
import org.wso2.carbon.device.mgt.mobile.dao.*;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory
|
||||
implements MobileDeviceManagementDAOFactoryInterface {
|
||||
|
||||
private static final Log log = LogFactory.getLog(WindowsDAOFactory.class);
|
||||
protected static DataSource dataSource;
|
||||
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
|
||||
|
||||
public WindowsDAOFactory() {
|
||||
this.dataSource = getDataSourceMap().get(DeviceManagementConstants.MobileDeviceTypes
|
||||
.MOBILE_DEVICE_TYPE_WINDOWS);
|
||||
}
|
||||
|
||||
@Override
|
||||
public MobileDeviceDAO getMobileDeviceDAO() {
|
||||
@ -54,4 +69,73 @@ public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory
|
||||
public MobileFeaturePropertyDAO getFeaturePropertyDAO() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void beginTransaction() throws MobileDeviceManagementDAOException {
|
||||
try {
|
||||
Connection conn = dataSource.getConnection();
|
||||
conn.setAutoCommit(false);
|
||||
currentConnection.set(conn);
|
||||
} catch (SQLException e) {
|
||||
throw new MobileDeviceManagementDAOException("Error occurred while retrieving datasource connection", e);
|
||||
}
|
||||
}
|
||||
|
||||
public static Connection getConnection() throws MobileDeviceManagementDAOException {
|
||||
if (currentConnection.get() == null) {
|
||||
try {
|
||||
currentConnection.set(dataSource.getConnection());
|
||||
} catch (SQLException e) {
|
||||
throw new MobileDeviceManagementDAOException("Error occurred while retrieving data source connection",
|
||||
e);
|
||||
}
|
||||
}
|
||||
return currentConnection.get();
|
||||
}
|
||||
|
||||
public static void commitTransaction() throws MobileDeviceManagementDAOException {
|
||||
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 MobileDeviceManagementDAOException("Error occurred while committing the transaction", e);
|
||||
} finally {
|
||||
closeConnection();
|
||||
}
|
||||
}
|
||||
|
||||
public static void closeConnection() throws MobileDeviceManagementDAOException {
|
||||
|
||||
Connection con = currentConnection.get();
|
||||
try {
|
||||
con.close();
|
||||
} catch (SQLException e) {
|
||||
log.error("Error occurred while close the connection");
|
||||
}
|
||||
currentConnection.remove();
|
||||
}
|
||||
|
||||
public static void rollbackTransaction() throws MobileDeviceManagementDAOException {
|
||||
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 MobileDeviceManagementDAOException("Error occurred while rollback the transaction", e);
|
||||
} finally {
|
||||
closeConnection();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -78,9 +78,7 @@ public class MobileDeviceManagementServiceComponent {
|
||||
Map<String, MobileDataSourceConfig> dsConfigMap =
|
||||
config.getMobileDeviceMgtRepository().getMobileDataSourceConfigMap();
|
||||
|
||||
AndroidDAOFactory.init(dsConfigMap.get(DeviceManagementConstants.MobileDeviceTypes
|
||||
.MOBILE_DEVICE_TYPE_ANDROID));
|
||||
WindowsDAOFactory.init(dsConfigMap.get(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS));
|
||||
MobileDeviceManagementDAOFactory.init(dsConfigMap);
|
||||
|
||||
String setupOption = System.getProperty("setup");
|
||||
if (setupOption != null) {
|
||||
@ -90,11 +88,10 @@ public class MobileDeviceManagementServiceComponent {
|
||||
"to begin");
|
||||
}
|
||||
try {
|
||||
DataSource dataSource;
|
||||
for (MobileDataSourceConfig dataSourceConfig : dsConfigMap.values()) {
|
||||
dataSource = MobileDeviceManagementDAOFactory.resolveDataSource(dataSourceConfig);
|
||||
for (String pluginType : dsConfigMap.keySet()){
|
||||
MobileDeviceManagementDAOUtil
|
||||
.setupMobileDeviceManagementSchema(dataSource);
|
||||
.setupMobileDeviceManagementSchema(MobileDeviceManagementDAOFactory.getDataSourceMap
|
||||
().get(pluginType));
|
||||
}
|
||||
} catch (MobileDeviceMgtPluginException e) {
|
||||
log.error("Exception occurred while initializing mobile device management database schema", e);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user