From 4516aeecec54749b0c26980fe72650c09c7e97b2 Mon Sep 17 00:00:00 2001 From: manoj Date: Thu, 14 May 2015 19:11:53 +0530 Subject: [PATCH] Refractor Mobile DAO Factory. Remove device source map and config map --- .../dao/MobileDeviceManagementDAOFactory.java | 40 ++----------------- .../impl/android/dao/AndroidDAOFactory.java | 10 +---- .../dao/impl/AndroidDeviceDAOImpl.java | 26 +++--------- .../dao/impl/AndroidFeatureDAOImpl.java | 30 +++++--------- .../impl/windows/dao/WindowsDAOFactory.java | 3 -- ...obileDeviceManagementServiceComponent.java | 14 ++++--- 6 files changed, 30 insertions(+), 93 deletions(-) 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 b86ec09a4..3ad5cbfcc 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 @@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.mobile.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.DataSourceNotAvailableException; import org.wso2.carbon.device.mgt.mobile.common.MobileDeviceMgtPluginException; import org.wso2.carbon.device.mgt.mobile.config.datasource.JNDILookupDefinition; @@ -39,22 +40,11 @@ import java.util.Map; public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceManagementDAOFactoryInterface { private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOFactory.class); - private static Map mobileDataSourceConfigMap; - private static Map dataSourceMap; - private static boolean isInitialized; private static ThreadLocal currentConnection = new ThreadLocal(); - protected static DataSource dataSource; + private static DataSource dataSource; - public static void init() throws MobileDeviceMgtPluginException { - - dataSourceMap = new HashMap(); - DataSource dataSource; - for (String pluginType : mobileDataSourceConfigMap.keySet()) { - dataSource = MobileDeviceManagementDAOFactory.resolveDataSource(mobileDataSourceConfigMap.get - (pluginType)); - dataSourceMap.put(pluginType, dataSource); - } - isInitialized = true; + public static void init(MobileDataSourceConfig mobileDataSourceConfig ) throws MobileDeviceMgtPluginException { + dataSource = resolveDataSource(mobileDataSourceConfig); } /** @@ -93,28 +83,6 @@ public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceMa return dataSource; } - public static Map getMobileDataSourceConfigMap() { - return mobileDataSourceConfigMap; - } - - public static void setMobileDataSourceConfigMap(Map mobileDataSourceConfigMap) { - MobileDeviceManagementDAOFactory.mobileDataSourceConfigMap = mobileDataSourceConfigMap; - } - - public static DataSource getDataSource(String type) { - return dataSourceMap.get(type); - } - - public static Map getDataSourceMap() { - return dataSourceMap; - } - - private static void assertDataSourceInitialization() { - if (!isInitialized) { - throw new DataSourceNotAvailableException("Mobile device management metadata repository data source " + - "is not initialized"); - } - } public static void beginTransaction() throws MobileDeviceManagementDAOException { try { 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 5ad45354a..7d84156a1 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 @@ -32,16 +32,10 @@ public class AndroidDAOFactory extends MobileDeviceManagementDAOFactory implements MobileDeviceManagementDAOFactoryInterface { private static final Log log = LogFactory.getLog(AndroidDAOFactory.class); - private static DataSource dataSource; - - public static void init() { - dataSource = MobileDeviceManagementDAOFactory.getDataSource(DeviceManagementConstants.MobileDeviceTypes. - MOBILE_DEVICE_TYPE_ANDROID); - } @Override public MobileDeviceDAO getMobileDeviceDAO() { - return new AndroidDeviceDAOImpl(dataSource); + return new AndroidDeviceDAOImpl(); } @Override @@ -60,7 +54,7 @@ public class AndroidDAOFactory extends MobileDeviceManagementDAOFactory } @Override public MobileFeatureDAO getMobileFeatureDao() { - return new AndroidFeatureDAOImpl(dataSource); + return new AndroidFeatureDAOImpl(); } public MobileFeaturePropertyDAO getFeaturePropertyDAO() { 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 2927e523f..9b3c6a3ff 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 @@ -22,6 +22,7 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceDAO; 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.util.AndroidPluginConstants; @@ -42,13 +43,8 @@ import java.util.Map; */ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ - private DataSource dataSource; private static final Log log = LogFactory.getLog(AndroidDeviceDAOImpl.class); - public AndroidDeviceDAOImpl(DataSource dataSource) { - this.dataSource = dataSource; - } - @Override public MobileDevice getMobileDevice(String mblDeviceId) throws MobileDeviceManagementDAOException { @@ -56,7 +52,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ PreparedStatement stmt = null; MobileDevice mobileDevice = null; try { - conn = this.getConnection(); + conn = MobileDeviceManagementDAOFactory.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" + @@ -111,7 +107,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ Connection conn = null; PreparedStatement stmt = null; try { - conn = this.getConnection(); + conn = MobileDeviceManagementDAOFactory.getConnection(); String createDBQuery = "INSERT INTO AD_DEVICE(ANDROID_DEVICE_ID, GCM_TOKEN, DEVICE_INFO, SERIAL, " + "VENDOR, MAC_ADDRESS, DEVICE_NAME, LATITUDE, LONGITUDE, IMEI, IMSI, " + @@ -166,7 +162,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ Connection conn = null; PreparedStatement stmt = null; try { - conn = this.getConnection(); + conn = MobileDeviceManagementDAOFactory.getConnection(); String updateDBQuery = "UPDATE AD_DEVICE SET GCM_TOKEN = ?, DEVICE_INFO = ?, SERIAL = ?, VENDOR = ?, " + "MAC_ADDRESS = ?, DEVICE_NAME = ?, LATITUDE = ?, LONGITUDE = ?, IMEI = ?, " + @@ -221,7 +217,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ Connection conn = null; PreparedStatement stmt = null; try { - conn = this.getConnection(); + conn = MobileDeviceManagementDAOFactory.getConnection(); String deleteDBQuery = "DELETE FROM AD_DEVICE WHERE ANDROID_DEVICE_ID = ?"; stmt = conn.prepareStatement(deleteDBQuery); @@ -252,7 +248,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ MobileDevice mobileDevice; List mobileDevices = new ArrayList(); try { - conn = this.getConnection(); + conn = MobileDeviceManagementDAOFactory.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 " + @@ -296,14 +292,4 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ } } - private Connection getConnection() throws MobileDeviceManagementDAOException { - try { - return dataSource.getConnection(); - } catch (SQLException e) { - String msg = "Error occurred while obtaining a connection from the mobile device " + - "management metadata repository datasource"; - log.error(msg, e); - throw new MobileDeviceManagementDAOException(msg, 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/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 96991d0b7..a223b6f00 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 @@ -22,6 +22,7 @@ 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.dao.MobileDeviceManagementDAOException; +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; @@ -38,11 +39,9 @@ import java.util.List; public class AndroidFeatureDAOImpl implements MobileFeatureDAO { - private DataSource dataSource; private static final Log log = LogFactory.getLog(AndroidFeatureDAOImpl.class); - public AndroidFeatureDAOImpl(DataSource dataSource) { - this.dataSource = dataSource; + public AndroidFeatureDAOImpl() { } @Override @@ -52,7 +51,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { Connection conn = null; try { - conn = this.getConnection(); + conn = MobileDeviceManagementDAOFactory.getConnection(); String sql = "INSERT INTO AD_FEATURE(CODE, NAME, DESCRIPTION) VALUES (?, ?, ?)"; stmt = conn.prepareStatement(sql); stmt.setString(1, mobileFeature.getCode()); @@ -77,7 +76,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { Connection conn = null; PreparedStatement stmt = null; try { - conn = this.getConnection(); + conn = MobileDeviceManagementDAOFactory.getConnection(); String updateDBQuery = "UPDATE AD_FEATURE SET NAME = ?, DESCRIPTION = ?" + "WHERE CODE = ?"; @@ -112,7 +111,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { boolean status = false; Connection conn = null; try { - conn = this.getConnection(); + conn = MobileDeviceManagementDAOFactory.getConnection(); String sql = "DELETE FROM AD_FEATURE WHERE ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, mblFeatureId); @@ -135,7 +134,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { boolean status = false; Connection conn = null; try { - conn = this.getConnection(); + conn = MobileDeviceManagementDAOFactory.getConnection(); String sql = "DELETE FROM AD_FEATURE WHERE CODE = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, mblFeatureCode); @@ -158,7 +157,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { ResultSet rs = null; Connection conn = null; try { - conn = this.getConnection(); + conn = MobileDeviceManagementDAOFactory.getConnection(); String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM AD_FEATURE WHERE ID = ?"; stmt = conn.prepareStatement(sql); stmt.setInt(1, mblFeatureId); @@ -193,7 +192,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { Connection conn = null; try { - conn = this.getConnection(); + conn = MobileDeviceManagementDAOFactory.getConnection(); String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM AD_FEATURE WHERE CODE = ?"; stmt = conn.prepareStatement(sql); stmt.setString(1, mblFeatureCode); @@ -234,7 +233,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { List features = new ArrayList(); try { - conn = this.getConnection(); + conn = MobileDeviceManagementDAOFactory.getConnection(); String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM AD_FEATURE"; stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); @@ -259,15 +258,4 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, rs); } } - - private Connection getConnection() throws MobileDeviceManagementDAOException { - try { - return dataSource.getConnection(); - } catch (SQLException e) { - String msg = "Error occurred while obtaining a connection from the Android mobile device " + - "management metadata repository datasource"; - log.error(msg, e); - throw new MobileDeviceManagementDAOException(msg, e); - } - } } 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 27417a6d7..cee6040e4 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 @@ -24,9 +24,6 @@ import org.wso2.carbon.device.mgt.mobile.dao.*; public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory implements MobileDeviceManagementDAOFactoryInterface { - public static void init(MobileDataSourceConfig config) { - dataSource = resolveDataSource(config); - } @Override public MobileDeviceDAO getMobileDeviceDAO() { 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 612a93b7a..1a4647a3f 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 @@ -26,6 +26,7 @@ import org.osgi.service.component.ComponentContext; import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; import org.wso2.carbon.device.mgt.common.spi.DeviceMgtService; import org.wso2.carbon.device.mgt.mobile.common.MobileDeviceMgtPluginException; +import org.wso2.carbon.device.mgt.mobile.common.MobilePluginConstants; import org.wso2.carbon.device.mgt.mobile.config.MobileDeviceConfigurationManager; import org.wso2.carbon.device.mgt.mobile.config.MobileDeviceManagementConfig; import org.wso2.carbon.device.mgt.mobile.config.datasource.MobileDataSourceConfig; @@ -34,6 +35,7 @@ import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.mobile.impl.android.AndroidDeviceManager; import org.wso2.carbon.device.mgt.mobile.impl.android.dao.AndroidDAOFactory; import org.wso2.carbon.device.mgt.mobile.impl.windows.WindowsDeviceManager; +import org.wso2.carbon.device.mgt.mobile.impl.windows.dao.WindowsDAOFactory; import org.wso2.carbon.ndatasource.core.DataSourceService; import javax.sql.DataSource; @@ -62,6 +64,7 @@ public class MobileDeviceManagementServiceComponent { private static final Log log = LogFactory.getLog(MobileDeviceManagementServiceComponent.class); protected void activate(ComponentContext ctx) { + if (log.isDebugEnabled()) { log.debug("Activating Mobile Device Management Service Component"); } @@ -74,10 +77,10 @@ public class MobileDeviceManagementServiceComponent { .getMobileDeviceManagementConfig(); Map dsConfigMap = config.getMobileDeviceMgtRepository().getMobileDataSourceConfigMap(); - MobileDeviceManagementDAOFactory.setMobileDataSourceConfigMap(dsConfigMap); - MobileDeviceManagementDAOFactory.init(); - AndroidDAOFactory.init(); + AndroidDAOFactory.init(dsConfigMap.get(DeviceManagementConstants.MobileDeviceTypes + .MOBILE_DEVICE_TYPE_ANDROID)); + WindowsDAOFactory.init(dsConfigMap.get(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_WINDOWS)); String setupOption = System.getProperty("setup"); if (setupOption != null) { @@ -87,8 +90,9 @@ public class MobileDeviceManagementServiceComponent { "to begin"); } try { - Map dataSourceMap = MobileDeviceManagementDAOFactory.getDataSourceMap(); - for (DataSource dataSource : dataSourceMap.values()) { + DataSource dataSource; + for (MobileDataSourceConfig dataSourceConfig : dsConfigMap.values()) { + dataSource = MobileDeviceManagementDAOFactory.resolveDataSource(dataSourceConfig); MobileDeviceManagementDAOUtil .setupMobileDeviceManagementSchema(dataSource); }