diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/MobileDeviceManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/MobileDeviceManagementDAOFactory.java index ddee078a8..94d8b2a06 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/MobileDeviceManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/dao/MobileDeviceManagementDAOFactory.java @@ -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 currentConnection = new ThreadLocal(); - private static DataSource dataSource; + private static Map dataSourceMap = new HashMap(); + private static boolean isInitialized; - public static void init(MobileDataSourceConfig mobileDataSourceConfig) throws MobileDeviceMgtPluginException { - dataSource = resolveDataSource(mobileDataSourceConfig); + public static void init(Map 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 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 Map getDataSourceMap() { + return dataSourceMap; } - - 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(); - } - } - } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManager.java index 02039191e..8b321a882 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidDeviceManager.java @@ -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); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidFeatureManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidFeatureManager.java index ed6961864..999dfb655 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidFeatureManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/AndroidFeatureManager.java @@ -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); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/AndroidDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/AndroidDAOFactory.java index 7d84156a1..f1cb9f4bd 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/AndroidDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/AndroidDAOFactory.java @@ -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 currentConnection = new ThreadLocal(); + + 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(); + } + } } \ No newline at end of file diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/AndroidDeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/AndroidDeviceDAOImpl.java index 1c5b9133b..bec580a5b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/AndroidDeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/AndroidDeviceDAOImpl.java @@ -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 mobileDevices = new ArrayList(); 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(); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/AndroidFeatureDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/AndroidFeatureDAOImpl.java index fda907230..d597d3a5d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/AndroidFeatureDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/android/dao/impl/AndroidFeatureDAOImpl.java @@ -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(); } } @@ -230,14 +232,14 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { @Override public List getAllFeatures() throws MobileDeviceManagementDAOException { - + PreparedStatement stmt = null; ResultSet rs = null; Connection conn = null; List features = new ArrayList(); 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(); } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/WindowsDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/WindowsDAOFactory.java index cee6040e4..d1e0882a0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/WindowsDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/impl/windows/dao/WindowsDAOFactory.java @@ -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 currentConnection = new ThreadLocal(); + + 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(); + } + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementServiceComponent.java index 1a4647a3f..34bcdf307 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementServiceComponent.java @@ -78,9 +78,7 @@ public class MobileDeviceManagementServiceComponent { Map 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);