Refractor Mobile DAO Factory. Remove device source map and config map

revert-dabc3590
manoj 10 years ago
parent e8c3c71e2e
commit 4516aeecec

@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.mobile.dao;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; 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.DataSourceNotAvailableException;
import org.wso2.carbon.device.mgt.mobile.common.MobileDeviceMgtPluginException; import org.wso2.carbon.device.mgt.mobile.common.MobileDeviceMgtPluginException;
import org.wso2.carbon.device.mgt.mobile.config.datasource.JNDILookupDefinition; import org.wso2.carbon.device.mgt.mobile.config.datasource.JNDILookupDefinition;
@ -39,22 +40,11 @@ import java.util.Map;
public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceManagementDAOFactoryInterface { public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceManagementDAOFactoryInterface {
private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOFactory.class); private static final Log log = LogFactory.getLog(MobileDeviceManagementDAOFactory.class);
private static Map<String, MobileDataSourceConfig> mobileDataSourceConfigMap;
private static Map<String, DataSource> dataSourceMap;
private static boolean isInitialized;
private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>(); private static ThreadLocal<Connection> currentConnection = new ThreadLocal<Connection>();
protected static DataSource dataSource; private static DataSource dataSource;
public static void init() throws MobileDeviceMgtPluginException { public static void init(MobileDataSourceConfig mobileDataSourceConfig ) throws MobileDeviceMgtPluginException {
dataSource = resolveDataSource(mobileDataSourceConfig);
dataSourceMap = new HashMap<String, DataSource>();
DataSource dataSource;
for (String pluginType : mobileDataSourceConfigMap.keySet()) {
dataSource = MobileDeviceManagementDAOFactory.resolveDataSource(mobileDataSourceConfigMap.get
(pluginType));
dataSourceMap.put(pluginType, dataSource);
}
isInitialized = true;
} }
/** /**
@ -93,28 +83,6 @@ public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceMa
return dataSource; return dataSource;
} }
public static Map<String, MobileDataSourceConfig> getMobileDataSourceConfigMap() {
return mobileDataSourceConfigMap;
}
public static void setMobileDataSourceConfigMap(Map<String, MobileDataSourceConfig> mobileDataSourceConfigMap) {
MobileDeviceManagementDAOFactory.mobileDataSourceConfigMap = mobileDataSourceConfigMap;
}
public static DataSource getDataSource(String type) {
return dataSourceMap.get(type);
}
public static Map<String, DataSource> 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 { public static void beginTransaction() throws MobileDeviceManagementDAOException {
try { try {

@ -32,16 +32,10 @@ public class AndroidDAOFactory extends MobileDeviceManagementDAOFactory
implements MobileDeviceManagementDAOFactoryInterface { implements MobileDeviceManagementDAOFactoryInterface {
private static final Log log = LogFactory.getLog(AndroidDAOFactory.class); 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 @Override
public MobileDeviceDAO getMobileDeviceDAO() { public MobileDeviceDAO getMobileDeviceDAO() {
return new AndroidDeviceDAOImpl(dataSource); return new AndroidDeviceDAOImpl();
} }
@Override @Override
@ -60,7 +54,7 @@ public class AndroidDAOFactory extends MobileDeviceManagementDAOFactory
} }
@Override public MobileFeatureDAO getMobileFeatureDao() { @Override public MobileFeatureDAO getMobileFeatureDao() {
return new AndroidFeatureDAOImpl(dataSource); return new AndroidFeatureDAOImpl();
} }
public MobileFeaturePropertyDAO getFeaturePropertyDAO() { public MobileFeaturePropertyDAO getFeaturePropertyDAO() {

@ -22,6 +22,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceDAO; 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.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.dao.util.MobileDeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.mobile.dto.MobileDevice; import org.wso2.carbon.device.mgt.mobile.dto.MobileDevice;
import org.wso2.carbon.device.mgt.mobile.impl.android.util.AndroidPluginConstants; import org.wso2.carbon.device.mgt.mobile.impl.android.util.AndroidPluginConstants;
@ -42,13 +43,8 @@ import java.util.Map;
*/ */
public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
private DataSource dataSource;
private static final Log log = LogFactory.getLog(AndroidDeviceDAOImpl.class); private static final Log log = LogFactory.getLog(AndroidDeviceDAOImpl.class);
public AndroidDeviceDAOImpl(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override @Override
public MobileDevice getMobileDevice(String mblDeviceId) public MobileDevice getMobileDevice(String mblDeviceId)
throws MobileDeviceManagementDAOException { throws MobileDeviceManagementDAOException {
@ -56,7 +52,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
PreparedStatement stmt = null; PreparedStatement stmt = null;
MobileDevice mobileDevice = null; MobileDevice mobileDevice = null;
try { try {
conn = this.getConnection(); conn = MobileDeviceManagementDAOFactory.getConnection();
String selectDBQuery = String selectDBQuery =
"SELECT ANDROID_DEVICE_ID, GCM_TOKEN, DEVICE_INFO, DEVICE_MODEL, SERIAL, " + "SELECT ANDROID_DEVICE_ID, GCM_TOKEN, DEVICE_INFO, DEVICE_MODEL, SERIAL, " +
"VENDOR, MAC_ADDRESS, DEVICE_NAME, LATITUDE, LONGITUDE, IMEI, IMSI, OS_VERSION" + "VENDOR, MAC_ADDRESS, DEVICE_NAME, LATITUDE, LONGITUDE, IMEI, IMSI, OS_VERSION" +
@ -111,7 +107,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = this.getConnection(); conn = MobileDeviceManagementDAOFactory.getConnection();
String createDBQuery = String createDBQuery =
"INSERT INTO AD_DEVICE(ANDROID_DEVICE_ID, GCM_TOKEN, DEVICE_INFO, SERIAL, " + "INSERT INTO AD_DEVICE(ANDROID_DEVICE_ID, GCM_TOKEN, DEVICE_INFO, SERIAL, " +
"VENDOR, MAC_ADDRESS, DEVICE_NAME, LATITUDE, LONGITUDE, IMEI, IMSI, " + "VENDOR, MAC_ADDRESS, DEVICE_NAME, LATITUDE, LONGITUDE, IMEI, IMSI, " +
@ -166,7 +162,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = this.getConnection(); conn = MobileDeviceManagementDAOFactory.getConnection();
String updateDBQuery = String updateDBQuery =
"UPDATE AD_DEVICE SET GCM_TOKEN = ?, DEVICE_INFO = ?, SERIAL = ?, VENDOR = ?, " + "UPDATE AD_DEVICE SET GCM_TOKEN = ?, DEVICE_INFO = ?, SERIAL = ?, VENDOR = ?, " +
"MAC_ADDRESS = ?, DEVICE_NAME = ?, LATITUDE = ?, LONGITUDE = ?, IMEI = ?, " + "MAC_ADDRESS = ?, DEVICE_NAME = ?, LATITUDE = ?, LONGITUDE = ?, IMEI = ?, " +
@ -221,7 +217,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = this.getConnection(); conn = MobileDeviceManagementDAOFactory.getConnection();
String deleteDBQuery = String deleteDBQuery =
"DELETE FROM AD_DEVICE WHERE ANDROID_DEVICE_ID = ?"; "DELETE FROM AD_DEVICE WHERE ANDROID_DEVICE_ID = ?";
stmt = conn.prepareStatement(deleteDBQuery); stmt = conn.prepareStatement(deleteDBQuery);
@ -252,7 +248,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{
MobileDevice mobileDevice; MobileDevice mobileDevice;
List<MobileDevice> mobileDevices = new ArrayList<MobileDevice>(); List<MobileDevice> mobileDevices = new ArrayList<MobileDevice>();
try { try {
conn = this.getConnection(); conn = MobileDeviceManagementDAOFactory.getConnection();
String selectDBQuery = String selectDBQuery =
"SELECT ANDROID_DEVICE_ID, GCM_TOKEN, DEVICE_INFO, DEVICE_MODEL, SERIAL, " + "SELECT ANDROID_DEVICE_ID, GCM_TOKEN, DEVICE_INFO, DEVICE_MODEL, SERIAL, " +
"VENDOR, MAC_ADDRESS, DEVICE_NAME, LATITUDE, LONGITUDE, IMEI, IMSI, OS_VERSION " + "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);
}
}
} }

@ -22,6 +22,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.DeviceManagementConstants; 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.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.MobileFeatureDAO;
import org.wso2.carbon.device.mgt.mobile.dao.util.MobileDeviceManagementDAOUtil; 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.dto.MobileFeature;
@ -38,11 +39,9 @@ import java.util.List;
public class AndroidFeatureDAOImpl implements MobileFeatureDAO { public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
private DataSource dataSource;
private static final Log log = LogFactory.getLog(AndroidFeatureDAOImpl.class); private static final Log log = LogFactory.getLog(AndroidFeatureDAOImpl.class);
public AndroidFeatureDAOImpl(DataSource dataSource) { public AndroidFeatureDAOImpl() {
this.dataSource = dataSource;
} }
@Override @Override
@ -52,7 +51,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
Connection conn = null; Connection conn = null;
try { try {
conn = this.getConnection(); conn = MobileDeviceManagementDAOFactory.getConnection();
String sql = "INSERT INTO AD_FEATURE(CODE, NAME, DESCRIPTION) VALUES (?, ?, ?)"; String sql = "INSERT INTO AD_FEATURE(CODE, NAME, DESCRIPTION) VALUES (?, ?, ?)";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, mobileFeature.getCode()); stmt.setString(1, mobileFeature.getCode());
@ -77,7 +76,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
Connection conn = null; Connection conn = null;
PreparedStatement stmt = null; PreparedStatement stmt = null;
try { try {
conn = this.getConnection(); conn = MobileDeviceManagementDAOFactory.getConnection();
String updateDBQuery = String updateDBQuery =
"UPDATE AD_FEATURE SET NAME = ?, DESCRIPTION = ?" + "UPDATE AD_FEATURE SET NAME = ?, DESCRIPTION = ?" +
"WHERE CODE = ?"; "WHERE CODE = ?";
@ -112,7 +111,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
boolean status = false; boolean status = false;
Connection conn = null; Connection conn = null;
try { try {
conn = this.getConnection(); conn = MobileDeviceManagementDAOFactory.getConnection();
String sql = "DELETE FROM AD_FEATURE WHERE ID = ?"; String sql = "DELETE FROM AD_FEATURE WHERE ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, mblFeatureId); stmt.setInt(1, mblFeatureId);
@ -135,7 +134,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
boolean status = false; boolean status = false;
Connection conn = null; Connection conn = null;
try { try {
conn = this.getConnection(); conn = MobileDeviceManagementDAOFactory.getConnection();
String sql = "DELETE FROM AD_FEATURE WHERE CODE = ?"; String sql = "DELETE FROM AD_FEATURE WHERE CODE = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, mblFeatureCode); stmt.setString(1, mblFeatureCode);
@ -158,7 +157,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
ResultSet rs = null; ResultSet rs = null;
Connection conn = null; Connection conn = null;
try { try {
conn = this.getConnection(); conn = MobileDeviceManagementDAOFactory.getConnection();
String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM AD_FEATURE WHERE ID = ?"; String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM AD_FEATURE WHERE ID = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setInt(1, mblFeatureId); stmt.setInt(1, mblFeatureId);
@ -193,7 +192,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
Connection conn = null; Connection conn = null;
try { try {
conn = this.getConnection(); conn = MobileDeviceManagementDAOFactory.getConnection();
String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM AD_FEATURE WHERE CODE = ?"; String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM AD_FEATURE WHERE CODE = ?";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
stmt.setString(1, mblFeatureCode); stmt.setString(1, mblFeatureCode);
@ -234,7 +233,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
List<MobileFeature> features = new ArrayList<MobileFeature>(); List<MobileFeature> features = new ArrayList<MobileFeature>();
try { try {
conn = this.getConnection(); conn = MobileDeviceManagementDAOFactory.getConnection();
String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM AD_FEATURE"; String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM AD_FEATURE";
stmt = conn.prepareStatement(sql); stmt = conn.prepareStatement(sql);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
@ -259,15 +258,4 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO {
MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, rs); 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);
}
}
} }

@ -24,9 +24,6 @@ import org.wso2.carbon.device.mgt.mobile.dao.*;
public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory public class WindowsDAOFactory extends MobileDeviceManagementDAOFactory
implements MobileDeviceManagementDAOFactoryInterface { implements MobileDeviceManagementDAOFactoryInterface {
public static void init(MobileDataSourceConfig config) {
dataSource = resolveDataSource(config);
}
@Override @Override
public MobileDeviceDAO getMobileDeviceDAO() { public MobileDeviceDAO getMobileDeviceDAO() {

@ -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.DeviceManagementConstants;
import org.wso2.carbon.device.mgt.common.spi.DeviceMgtService; 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.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.MobileDeviceConfigurationManager;
import org.wso2.carbon.device.mgt.mobile.config.MobileDeviceManagementConfig; import org.wso2.carbon.device.mgt.mobile.config.MobileDeviceManagementConfig;
import org.wso2.carbon.device.mgt.mobile.config.datasource.MobileDataSourceConfig; 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.AndroidDeviceManager;
import org.wso2.carbon.device.mgt.mobile.impl.android.dao.AndroidDAOFactory; 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.WindowsDeviceManager;
import org.wso2.carbon.device.mgt.mobile.impl.windows.dao.WindowsDAOFactory;
import org.wso2.carbon.ndatasource.core.DataSourceService; import org.wso2.carbon.ndatasource.core.DataSourceService;
import javax.sql.DataSource; import javax.sql.DataSource;
@ -62,6 +64,7 @@ public class MobileDeviceManagementServiceComponent {
private static final Log log = LogFactory.getLog(MobileDeviceManagementServiceComponent.class); private static final Log log = LogFactory.getLog(MobileDeviceManagementServiceComponent.class);
protected void activate(ComponentContext ctx) { protected void activate(ComponentContext ctx) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Activating Mobile Device Management Service Component"); log.debug("Activating Mobile Device Management Service Component");
} }
@ -74,10 +77,10 @@ public class MobileDeviceManagementServiceComponent {
.getMobileDeviceManagementConfig(); .getMobileDeviceManagementConfig();
Map<String, MobileDataSourceConfig> dsConfigMap = Map<String, MobileDataSourceConfig> dsConfigMap =
config.getMobileDeviceMgtRepository().getMobileDataSourceConfigMap(); 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"); String setupOption = System.getProperty("setup");
if (setupOption != null) { if (setupOption != null) {
@ -87,8 +90,9 @@ public class MobileDeviceManagementServiceComponent {
"to begin"); "to begin");
} }
try { try {
Map<String, DataSource> dataSourceMap = MobileDeviceManagementDAOFactory.getDataSourceMap(); DataSource dataSource;
for (DataSource dataSource : dataSourceMap.values()) { for (MobileDataSourceConfig dataSourceConfig : dsConfigMap.values()) {
dataSource = MobileDeviceManagementDAOFactory.resolveDataSource(dataSourceConfig);
MobileDeviceManagementDAOUtil MobileDeviceManagementDAOUtil
.setupMobileDeviceManagementSchema(dataSource); .setupMobileDeviceManagementSchema(dataSource);
} }

Loading…
Cancel
Save