From 4516aeecec54749b0c26980fe72650c09c7e97b2 Mon Sep 17 00:00:00 2001 From: manoj Date: Thu, 14 May 2015 19:11:53 +0530 Subject: [PATCH 1/3] 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 b86ec09a41..3ad5cbfcc2 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 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..9b3c6a3ff1 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 96991d0b79..a223b6f001 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 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/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); } From d416a38eaf8dcf84bc9cc6f504897fdf4336dec5 Mon Sep 17 00:00:00 2001 From: manoj Date: Thu, 14 May 2015 19:40:56 +0530 Subject: [PATCH 2/3] Remove bundle activator --- ...MobileDeviceManagementBundleActivator.java | 120 ------------------ 1 file changed, 120 deletions(-) delete mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.mobile.impl/src/main/java/org/wso2/carbon/device/mgt/mobile/internal/MobileDeviceManagementBundleActivator.java 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; - } - - -} From 10bf596281d4c4b73bb5acf2cfc2f26d957ebede Mon Sep 17 00:00:00 2001 From: manoj Date: Thu, 14 May 2015 20:44:40 +0530 Subject: [PATCH 3/3] Refractor DB connections and resultsets --- .../dao/MobileDeviceManagementDAOFactory.java | 11 +++--- .../impl/android/AndroidDeviceManager.java | 35 +++++++++++++++++-- .../impl/android/AndroidFeatureManager.java | 14 ++------ .../dao/impl/AndroidDeviceDAOImpl.java | 20 +++++++---- .../dao/impl/AndroidFeatureDAOImpl.java | 21 ++++++----- 5 files changed, 65 insertions(+), 36 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 3ad5cbfcc2..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,19 +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.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; 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. @@ -43,7 +40,7 @@ public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceMa private static ThreadLocal currentConnection = new ThreadLocal(); private static DataSource dataSource; - public static void init(MobileDataSourceConfig mobileDataSourceConfig ) throws MobileDeviceMgtPluginException { + public static void init(MobileDataSourceConfig mobileDataSourceConfig) throws MobileDeviceMgtPluginException { dataSource = resolveDataSource(mobileDataSourceConfig); } @@ -83,7 +80,6 @@ public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceMa return dataSource; } - public static void beginTransaction() throws MobileDeviceManagementDAOException { try { Connection conn = dataSource.getConnection(); @@ -105,6 +101,7 @@ public abstract class MobileDeviceManagementDAOFactory implements MobileDeviceMa } return currentConnection.get(); } + public static void commitTransaction() throws MobileDeviceManagementDAOException { try { Connection conn = currentConnection.get(); @@ -119,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/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 9b3c6a3ff1..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 @@ -51,6 +51,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ Connection conn = null; PreparedStatement stmt = null; MobileDevice mobileDevice = null; + ResultSet resultSet = null; try { conn = MobileDeviceManagementDAOFactory.getConnection(); String selectDBQuery = @@ -59,7 +60,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ " 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(); @@ -94,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; @@ -150,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; } @@ -205,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; } @@ -235,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; } @@ -243,10 +245,13 @@ 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 = MobileDeviceManagementDAOFactory.getConnection(); String selectDBQuery = @@ -254,7 +259,7 @@ public class AndroidDeviceDAOImpl implements MobileDeviceDAO{ "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. @@ -288,7 +293,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(); } } 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 a223b6f001..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 @@ -46,10 +46,10 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { @Override public boolean addFeature(MobileFeature mobileFeature) throws MobileDeviceManagementDAOException { + PreparedStatement stmt = null; boolean status = false; Connection conn = null; - try { conn = MobileDeviceManagementDAOFactory.getConnection(); String sql = "INSERT INTO AD_FEATURE(CODE, NAME, DESCRIPTION) VALUES (?, ?, ?)"; @@ -65,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; } @@ -100,13 +100,14 @@ 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; @@ -122,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; } @@ -145,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; } @@ -180,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(); } } @@ -215,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(); } } @@ -227,6 +230,7 @@ public class AndroidFeatureDAOImpl implements MobileFeatureDAO { @Override public List getAllFeatures() throws MobileDeviceManagementDAOException { + PreparedStatement stmt = null; ResultSet rs = null; Connection conn = null; @@ -255,7 +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); + MobileDeviceManagementDAOUtil.cleanupResources(stmt, rs); + MobileDeviceManagementDAOFactory.closeConnection(); } } }