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 b86ec09a41..ddee078a8f 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,18 +20,16 @@ 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.mobile.DataSourceNotAvailableException; 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.MobileDataSourceConfig; 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. @@ -39,22 +37,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; - - public static void init() throws MobileDeviceMgtPluginException { + private static DataSource dataSource; - 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,29 +80,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 { Connection conn = dataSource.getConnection(); @@ -137,6 +101,7 @@ public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceMa } return currentConnection.get(); } + public static void commitTransaction() throws MobileDeviceManagementDAOException { try { Connection conn = currentConnection.get(); @@ -151,7 +116,7 @@ public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceMa } catch (SQLException e) { throw new MobileDeviceManagementDAOException("Error occurred while committing the transaction", e); } finally { - closeConnection(); + 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/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 925d6b1e1e..02039191e8 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,11 +64,18 @@ public class AndroidDeviceManager implements DeviceMgtService { if (log.isDebugEnabled()) { log.debug("Enrolling a new Android device : " + device.getDeviceIdentifier()); } + MobileDeviceManagementDAOFactory.beginTransaction(); status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO().addMobileDevice( mobileDevice); + MobileDeviceManagementDAOFactory.commitTransaction(); } catch (MobileDeviceManagementDAOException e) { - String msg = "Error while enrolling the Android device : " + - device.getDeviceIdentifier(); + try { + MobileDeviceManagementDAOFactory.rollbackTransaction(); + } catch (MobileDeviceManagementDAOException mobileDAOEx) { + String msg = "Error occurred while roll back the device enrol transaction :" + device.toString(); + log.warn(msg, mobileDAOEx); + } + String msg = "Error while enrolling the Android device : " + device.getDeviceIdentifier(); log.error(msg, e); throw new DeviceManagementException(msg, e); } @@ -83,9 +90,17 @@ public class AndroidDeviceManager implements DeviceMgtService { if (log.isDebugEnabled()) { log.debug("Modifying the Android device enrollment data"); } + MobileDeviceManagementDAOFactory.beginTransaction(); status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO() .updateMobileDevice(mobileDevice); + MobileDeviceManagementDAOFactory.commitTransaction(); } catch (MobileDeviceManagementDAOException e) { + try { + MobileDeviceManagementDAOFactory.rollbackTransaction(); + } catch (MobileDeviceManagementDAOException mobileDAOEx) { + String msg = "Error occurred while roll back the update device transaction :" + device.toString(); + log.warn(msg, mobileDAOEx); + } String msg = "Error while updating the enrollment of the Android device : " + device.getDeviceIdentifier(); log.error(msg, e); @@ -101,9 +116,17 @@ public class AndroidDeviceManager implements DeviceMgtService { if (log.isDebugEnabled()) { log.debug("Dis-enrolling Android device : " + deviceId); } + MobileDeviceManagementDAOFactory.beginTransaction(); status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO() .deleteMobileDevice(deviceId.getId()); + MobileDeviceManagementDAOFactory.commitTransaction(); } catch (MobileDeviceManagementDAOException e) { + try { + MobileDeviceManagementDAOFactory.rollbackTransaction(); + } catch (MobileDeviceManagementDAOException mobileDAOEx) { + String msg = "Error occurred while roll back the device dis enrol transaction :" + deviceId.toString(); + log.warn(msg, mobileDAOEx); + } String msg = "Error while removing the Android device : " + deviceId.getId(); log.error(msg, e); throw new DeviceManagementException(msg, e); @@ -177,9 +200,17 @@ public class AndroidDeviceManager implements DeviceMgtService { log.debug( "updating the details of Android device : " + device.getDeviceIdentifier()); } + MobileDeviceManagementDAOFactory.beginTransaction(); status = mobileDeviceManagementDAOFactory.getMobileDeviceDAO() .updateMobileDevice(mobileDevice); + MobileDeviceManagementDAOFactory.commitTransaction(); } catch (MobileDeviceManagementDAOException e) { + try { + MobileDeviceManagementDAOFactory.rollbackTransaction(); + } catch (MobileDeviceManagementDAOException mobileDAOEx) { + String msg = "Error occurred while roll back the update device info transaction :" + device.toString(); + log.warn(msg, mobileDAOEx); + } String msg = "Error while updating the Android device : " + device.getDeviceIdentifier(); log.error(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/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 cb73732794..ed69618642 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 @@ -70,11 +70,6 @@ public class AndroidFeatureManager implements FeatureManager { Feature feature = MobileDeviceManagementUtil.convertToFeature(mobileFeature); return feature; } catch (MobileDeviceManagementDAOException e) { - try { - mobileDeviceManagementDAOFactory.rollbackTransaction(); - } catch (MobileDeviceManagementDAOException e1) { - log.warn("Error occurred while roll-backing the transaction", e); - } throw new DeviceManagementException("Error occurred while retrieving the feature", e); } } @@ -90,13 +85,8 @@ public class AndroidFeatureManager implements FeatureManager { } return featureList; } catch (MobileDeviceManagementDAOException e) { - try { - mobileDeviceManagementDAOFactory.rollbackTransaction(); - } catch (MobileDeviceManagementDAOException e1) { - log.warn("Error occurred while roll-backing the transaction", e); - } - throw new DeviceManagementException("Error occurred while retrieving the list of " + - "features registered for Android platform", e); + throw new DeviceManagementException("Error occurred while retrieving the list of features registered for " + + "Android platform", 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 5ad45354ad..7d84156a12 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 2927e523f4..1c5b9133b2 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,28 +43,24 @@ 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 { Connection conn = null; PreparedStatement stmt = null; MobileDevice mobileDevice = null; + ResultSet resultSet = 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" + " FROM AD_DEVICE WHERE ANDROID_DEVICE_ID = ?"; stmt = conn.prepareStatement(selectDBQuery); stmt.setString(1, mblDeviceId); - ResultSet resultSet = stmt.executeQuery(); + resultSet = stmt.executeQuery(); if (resultSet.next()) { mobileDevice = new MobileDevice(); @@ -98,7 +95,8 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ log.error(msg, e); throw new MobileDeviceManagementDAOException(msg, e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, resultSet); + MobileDeviceManagementDAOFactory.closeConnection(); } return mobileDevice; @@ -111,7 +109,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, " + @@ -154,7 +152,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ log.error(msg, e); throw new MobileDeviceManagementDAOException(msg, e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); } return status; } @@ -166,7 +164,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 = ?, " + @@ -209,7 +207,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ log.error(msg, e); throw new MobileDeviceManagementDAOException(msg, e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); } return status; } @@ -221,7 +219,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); @@ -239,7 +237,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ log.error(msg, e); throw new MobileDeviceManagementDAOException(msg, e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); } return status; } @@ -247,18 +245,21 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ @Override public List getAllMobileDevices() throws MobileDeviceManagementDAOException { + Connection conn = null; PreparedStatement stmt = null; + ResultSet resultSet = null; 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 " + "FROM AD_DEVICE"; stmt = conn.prepareStatement(selectDBQuery); - ResultSet resultSet = stmt.executeQuery(); + resultSet = stmt.executeQuery(); while (resultSet.next()) { mobileDevice = new MobileDevice(); mobileDevice.setMobileDeviceId(resultSet.getString(AndroidPluginConstants. @@ -292,18 +293,9 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ log.error(msg, e); throw new MobileDeviceManagementDAOException(msg, e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, resultSet); + MobileDeviceManagementDAOFactory.closeConnection(); } } - 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 96991d0b79..fda907230b 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,21 +39,19 @@ 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 public boolean addFeature(MobileFeature mobileFeature) throws MobileDeviceManagementDAOException { + PreparedStatement stmt = null; boolean status = false; 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()); @@ -66,7 +65,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { "Error occurred while adding android feature '" + mobileFeature.getName() + "' into the metadata repository", e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); } return status; } @@ -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 = ?"; @@ -101,18 +100,19 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { log.error(msg, e); throw new AndroidFeatureManagementDAOException(msg, e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); } return status; } @Override public boolean deleteFeatureById(int mblFeatureId) throws MobileDeviceManagementDAOException { + PreparedStatement stmt = null; 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); @@ -123,7 +123,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { "Error occurred while deleting android feature '" + mblFeatureId + "' from Android database.", e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); } return status; } @@ -135,7 +135,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); @@ -146,7 +146,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { "Error occurred while deleting android feature '" + mblFeatureCode + "' from Android database.", e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, null); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, null); } return status; } @@ -158,7 +158,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); @@ -181,7 +181,8 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { "Error occurred while retrieving android feature '" + mblFeatureId + "' from the Android database.", e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, rs); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs); + MobileDeviceManagementDAOFactory.closeConnection(); } } @@ -193,7 +194,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); @@ -216,7 +217,8 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { "Error occurred while retrieving android feature '" + mblFeatureCode + "' from the Android database.", e); } finally { - MobileDeviceManagementDAOUtil.cleanupResources(conn, stmt, rs); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs); + MobileDeviceManagementDAOFactory.closeConnection(); } } @@ -228,13 +230,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 = this.getConnection(); + conn = MobileDeviceManagementDAOFactory.getConnection(); String sql = "SELECT ID, CODE, NAME, DESCRIPTION FROM AD_FEATURE"; stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); @@ -256,18 +259,8 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { throw new AndroidFeatureManagementDAOException("Error occurred while retrieving all " + "android features from the android database.", e); } finally { - 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); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs); + MobileDeviceManagementDAOFactory.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 27417a6d79..cee6040e48 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/MobileDeviceManagementBundleActivator.java b/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementBundleActivator.java deleted file mode 100644 index ba1149454d..0000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementBundleActivator.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * WSO2 Inc. licenses this file to you under the Apache License, - * Version 2.0 (the "License"); you may not use this file except - * in compliance with the License. - * you may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.wso2.carbon.device.mgt.mobile.internal; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.osgi.framework.*; -import org.wso2.carbon.device.mgt.common.spi.DeviceManager; -import org.wso2.carbon.device.mgt.mobile.DataSourceListener; -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; -import org.wso2.carbon.device.mgt.mobile.dao.MobileDeviceManagementDAOFactory; -import org.wso2.carbon.device.mgt.mobile.impl.android.AndroidDeviceManager; -import org.wso2.carbon.device.mgt.mobile.impl.windows.WindowsDeviceManager; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -/** - * BundleActivator of MobileDeviceManagement component. - */ -public class MobileDeviceManagementBundleActivator implements BundleActivator, BundleListener { - - private ServiceRegistration androidServiceRegRef; - private ServiceRegistration windowsServiceRegRef; - - private static List dataSourceListeners = - new ArrayList(); - - private static final String SYMBOLIC_NAME_DATA_SOURCE_COMPONENT = - "org.wso2.carbon.ndatasource.core"; - private static final Log log = LogFactory.getLog(MobileDeviceManagementBundleActivator.class); - - @Override - public void start(BundleContext bundleContext) throws Exception { - try { - if (log.isDebugEnabled()) { - log.debug("Activating Mobile Device Management Service bundle"); - } - bundleContext.addBundleListener(this); - - /* Initialize the data source configuration */ - MobileDeviceConfigurationManager.getInstance().initConfig(); - MobileDeviceManagementConfig config = MobileDeviceConfigurationManager.getInstance() - .getMobileDeviceManagementConfig(); - Map mobileDataSourceConfigMap = - config.getMobileDeviceMgtRepository().getMobileDataSourceConfigMap(); - MobileDeviceManagementDAOFactory.setMobileDataSourceConfigMap(mobileDataSourceConfigMap); - - androidServiceRegRef = - bundleContext.registerService(DeviceManager.class.getName(), - new AndroidDeviceManager(), null); - windowsServiceRegRef = - bundleContext.registerService(DeviceManager.class.getName(), - new WindowsDeviceManager(), null); - - if (log.isDebugEnabled()) { - log.debug("Mobile Device Management Service bundle is activated"); - } - } catch (Throwable e) { - log.error("Error occurred while activating Mobile Device Management bundle", e); - } - } - - @Override - public void stop(BundleContext bundleContext) throws Exception { - if (log.isDebugEnabled()) { - log.debug("Deactivating Mobile Device Management Service"); - } - try { - androidServiceRegRef.unregister(); - windowsServiceRegRef.unregister(); - - bundleContext.removeBundleListener(this); - } catch (Throwable e) { - log.error("Error occurred while de-activating Mobile Device Management bundle", e); - } - } - - @Override - public void bundleChanged(BundleEvent bundleEvent) { - int eventType = bundleEvent.getType(); - String bundleSymbolicName = bundleEvent.getBundle().getSymbolicName(); - - if (SYMBOLIC_NAME_DATA_SOURCE_COMPONENT.equals(bundleSymbolicName) && - eventType == BundleEvent.STARTED) { - for (DataSourceListener listener : this.getDataSourceListeners()) { - listener.notifyObserver(); - } - } - } - - public static void registerDataSourceListener(DataSourceListener listener) { - dataSourceListeners.add(listener); - } - - private List getDataSourceListeners() { - return dataSourceListeners; - } - - -} 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 612a93b7ae..1a4647a3fc 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); }