diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TransactionManagementException.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TransactionManagementException.java new file mode 100644 index 00000000000..72cfb87f74a --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TransactionManagementException.java @@ -0,0 +1,58 @@ +/* + * 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.common; + +public class TransactionManagementException extends Exception { + + private static final long serialVersionUID = -3151279321929070297L; + + private String errorMessage; + + public String getErrorMessage() { + return errorMessage; + } + + public void setErrorMessage(String errorMessage) { + this.errorMessage = errorMessage; + } + + public TransactionManagementException(String msg, Exception nestedEx) { + super(msg, nestedEx); + setErrorMessage(msg); + } + + public TransactionManagementException(String message, Throwable cause) { + super(message, cause); + setErrorMessage(message); + } + + public TransactionManagementException(String msg) { + super(msg); + setErrorMessage(msg); + } + + public TransactionManagementException() { + super(); + } + + public TransactionManagementException(Throwable cause) { + super(cause); + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java index e83c0c9a970..78e81dbe1ac 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java @@ -27,6 +27,7 @@ import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceIdentifier; import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManagementException; import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; @@ -60,10 +61,8 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem private DeviceDAO deviceDAO; private ApplicationDAO applicationDAO; private ApplicationMappingDAO applicationMappingDAO; - private boolean isTest; private static final String GET_APP_LIST_URL = "store/apis/assets/mobileapp?domain=carbon.super&page=1"; - private static final Log log = LogFactory.getLog(ApplicationManagerProviderServiceImpl.class); public ApplicationManagerProviderServiceImpl(AppManagementConfig appManagementConfig, @@ -87,12 +86,11 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem this.applicationMappingDAO = DeviceManagementDAOFactory.getApplicationMappingDAO(); } - ApplicationManagerProviderServiceImpl(DeviceManagementPluginRepository pluginRepository, boolean testMode) { + ApplicationManagerProviderServiceImpl(DeviceManagementPluginRepository pluginRepository) { this.pluginRepository = pluginRepository; this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); this.applicationDAO = DeviceManagementDAOFactory.getApplicationDAO(); this.applicationMappingDAO = DeviceManagementDAOFactory.getApplicationMappingDAO(); - isTest = testMode; } @Override @@ -124,10 +122,8 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem } } - public void updateInstalledApplicationListOfDevice( DeviceIdentifier deviceIdentifier, List applications) throws ApplicationManagementException { - } private OAuthConsumerAppDTO getAppInfo() throws ApplicationManagementException { @@ -191,8 +187,8 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem if (log.isDebugEnabled()) { log.debug("num of apps installed:" + installedAppList.size()); } - List appsToAdd = new ArrayList(); - List appIdsToRemove = new ArrayList(); + List appsToAdd = new ArrayList<>(); + List appIdsToRemove = new ArrayList<>(); for (Application installedApp : installedAppList) { if (!applications.contains(installedApp)) { @@ -231,24 +227,16 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem } applicationMappingDAO.removeApplicationMapping(device.getId(), appIdsToRemove, tenantId); DeviceManagementDAOFactory.commitTransaction(); - } catch (DeviceManagementDAOException deviceDaoEx) { - String errorMsg = "Error occurred saving application list to the device"; - log.error(errorMsg + ":" + deviceIdentifier.toString()); - try { - DeviceManagementDAOFactory.rollbackTransaction(); - } catch (DeviceManagementDAOException e) { - log.error("Error occurred while roll back transaction", e); - } - throw new ApplicationManagementException(errorMsg, deviceDaoEx); + } catch (DeviceManagementDAOException | TransactionManagementException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + throw new ApplicationManagementException("Error occurred saving application list to the device", e); } } - - @Override public List getApplicationListForDevice( DeviceIdentifier deviceId) throws ApplicationManagementException { - Device device = null; + Device device; try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); device = deviceDAO.getDevice(deviceId, tenantId); @@ -278,4 +266,5 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem deviceManagementService.getType() + "'", e); } } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java index fab9f4bf6c9..be15bd9551b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/DeviceManagementDAOFactory.java @@ -20,6 +20,7 @@ package org.wso2.carbon.device.mgt.core.dao; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition; import org.wso2.carbon.device.mgt.core.dao.impl.*; @@ -33,17 +34,17 @@ import java.util.List; public class DeviceManagementDAOFactory { - private static DataSource dataSource; - private static final Log log = LogFactory.getLog(DeviceManagementDAOFactory.class); + private static DataSource dataSource; + private static final Log log = LogFactory.getLog(DeviceManagementDAOFactory.class); private static ThreadLocal currentConnection = new ThreadLocal(); - public static DeviceDAO getDeviceDAO() { - return new DeviceDAOImpl(); - } + public static DeviceDAO getDeviceDAO() { + return new DeviceDAOImpl(); + } - public static DeviceTypeDAO getDeviceTypeDAO() { - return new DeviceTypeDAOImpl(); - } + public static DeviceTypeDAO getDeviceTypeDAO() { + return new DeviceTypeDAOImpl(); + } public static EnrolmentDAO getEnrollmentDAO() { return new EnrolmentDAOImpl(); @@ -57,56 +58,36 @@ public class DeviceManagementDAOFactory { return new ApplicationMappingDAOImpl(); } - public static void init(DataSourceConfig config) { - dataSource = resolveDataSource(config); - } + public static void init(DataSourceConfig config) { + dataSource = resolveDataSource(config); + } - public static void init(DataSource dtSource) { - dataSource = dtSource; - } + public static void init(DataSource dtSource) { + dataSource = dtSource; + } - public static void beginTransaction() throws DeviceManagementDAOException { + public static void beginTransaction() throws TransactionManagementException { try { Connection conn = dataSource.getConnection(); conn.setAutoCommit(false); currentConnection.set(conn); } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving config.datasource connection", e); + throw new TransactionManagementException("Error occurred while retrieving config.datasource connection", e); } } - public static void openConnection() throws DeviceManagementDAOException { - try { - currentConnection.set(dataSource.getConnection()); - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while acquiring config.datasource connection", e); - } + public static void openConnection() throws SQLException { + currentConnection.set(dataSource.getConnection()); } - public static Connection getConnection() throws DeviceManagementDAOException { + public static Connection getConnection() throws SQLException { if (currentConnection.get() == null) { - try { - currentConnection.set(dataSource.getConnection()); - } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while retrieving data source connection", e); - } + currentConnection.set(dataSource.getConnection()); } return currentConnection.get(); } - public static void closeConnection() throws DeviceManagementDAOException { - Connection con = currentConnection.get(); - if (con != null) { - try { - con.close(); - } catch (SQLException e) { - log.warn("Error occurred while close the connection"); - } - currentConnection.remove(); - } - } - - public static void commitTransaction() throws DeviceManagementDAOException { + public static void commitTransaction() throws TransactionManagementException { try { Connection conn = currentConnection.get(); if (conn != null) { @@ -118,11 +99,11 @@ public class DeviceManagementDAOFactory { } } } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while committing the transaction", e); + throw new TransactionManagementException("Error occurred while committing the transaction", e); } } - public static void rollbackTransaction() throws DeviceManagementDAOException { + public static void rollbackTransaction() { try { Connection conn = currentConnection.get(); if (conn != null) { @@ -134,43 +115,55 @@ public class DeviceManagementDAOFactory { } } } catch (SQLException e) { - throw new DeviceManagementDAOException("Error occurred while rollbacking the transaction", e); + log.warn("Error occurred while rollbacking the transaction", e); + } + } + + public static void closeConnection() { + Connection con = currentConnection.get(); + if (con != null) { + try { + con.close(); + } catch (SQLException e) { + log.warn("Error occurred while close the connection"); + } + currentConnection.remove(); } } /** - * Resolve data source from the data source definition - * - * @param config data source configuration - * @return data source resolved from the data source definition - */ - private static DataSource resolveDataSource(DataSourceConfig config) { - DataSource dataSource = null; - if (config == null) { - throw new RuntimeException( - "Device Management Repository data source configuration " + "is null and " + - "thus, is not initialized"); - } - JNDILookupDefinition jndiConfig = config.getJndiLookupDefinition(); - if (jndiConfig != null) { - if (log.isDebugEnabled()) { - log.debug("Initializing Device Management Repository data source using the JNDI " + - "Lookup Definition"); - } - List jndiPropertyList = - jndiConfig.getJndiProperties(); - if (jndiPropertyList != null) { - Hashtable jndiProperties = new Hashtable(); - for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) { - jndiProperties.put(prop.getName(), prop.getValue()); - } - dataSource = DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), jndiProperties); - } else { - dataSource = DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), null); - } - } - return dataSource; - } + * Resolve data source from the data source definition + * + * @param config data source configuration + * @return data source resolved from the data source definition + */ + private static DataSource resolveDataSource(DataSourceConfig config) { + DataSource dataSource = null; + if (config == null) { + throw new RuntimeException( + "Device Management Repository data source configuration " + "is null and " + + "thus, is not initialized"); + } + JNDILookupDefinition jndiConfig = config.getJndiLookupDefinition(); + if (jndiConfig != null) { + if (log.isDebugEnabled()) { + log.debug("Initializing Device Management Repository data source using the JNDI " + + "Lookup Definition"); + } + List jndiPropertyList = + jndiConfig.getJndiProperties(); + if (jndiPropertyList != null) { + Hashtable jndiProperties = new Hashtable(); + for (JNDILookupDefinition.JNDIProperty prop : jndiPropertyList) { + jndiProperties.put(prop.getName(), prop.getValue()); + } + dataSource = DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), jndiProperties); + } else { + dataSource = DeviceManagementDAOUtil.lookupDataSource(jndiConfig.getJndiName(), null); + } + } + return dataSource; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java index 6f44df6a809..9e2251e982f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationDAOImpl.java @@ -181,7 +181,7 @@ public class ApplicationDAOImpl implements ApplicationDAO { } } - private Connection getConnection() throws DeviceManagementDAOException { + private Connection getConnection() throws SQLException { return DeviceManagementDAOFactory.getConnection(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java index f0c769a56b6..a7d1605de79 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/ApplicationMappingDAOImpl.java @@ -129,9 +129,8 @@ public class ApplicationMappingDAOImpl implements ApplicationMappingDAO { } } - private Connection getConnection() throws DeviceManagementDAOException { + private Connection getConnection() throws SQLException { return DeviceManagementDAOFactory.getConnection(); } - } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java index f82038ca3ef..977ae3ad85b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceDAOImpl.java @@ -237,7 +237,7 @@ public class DeviceDAOImpl implements DeviceDAO { return devices; } - private Connection getConnection() throws DeviceManagementDAOException { + private Connection getConnection() throws SQLException { return DeviceManagementDAOFactory.getConnection(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java index cc69c6d2c12..148634eaac7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/DeviceTypeDAOImpl.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.core.dao.impl; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.mgt.common.TransactionManagementException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.DeviceTypeDAO; @@ -62,7 +63,7 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO { Connection conn; PreparedStatement stmt = null; ResultSet rs = null; - List deviceTypes = new ArrayList();; + List deviceTypes = new ArrayList<>(); try { conn = this.getConnection(); String sql = "SELECT ID AS DEVICE_TYPE_ID, NAME AS DEVICE_TYPE FROM DM_DEVICE_TYPE"; @@ -141,7 +142,7 @@ public class DeviceTypeDAOImpl implements DeviceTypeDAO { } - private Connection getConnection() throws DeviceManagementDAOException { + private Connection getConnection() throws SQLException { return DeviceManagementDAOFactory.getConnection(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrolmentDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrolmentDAOImpl.java index b616202f828..e4e4f79eec3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrolmentDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/EnrolmentDAOImpl.java @@ -199,7 +199,7 @@ public class EnrolmentDAOImpl implements EnrolmentDAO { } } - private Connection getConnection() throws DeviceManagementDAOException { + private Connection getConnection() throws SQLException { return DeviceManagementDAOFactory.getConnection(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index b6c43ec0888..f50c146a841 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -37,18 +37,18 @@ import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent; import org.wso2.carbon.device.mgt.core.internal.EmailServiceDataHolder; import org.wso2.carbon.device.mgt.core.internal.PluginInitializationListener; -import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.user.api.UserStoreException; import java.io.IOException; import java.net.URLDecoder; import java.net.URLEncoder; +import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.List; public class DeviceManagementProviderServiceImpl implements DeviceManagementProviderService, - PluginInitializationListener { + PluginInitializationListener { private DeviceDAO deviceDAO; private DeviceTypeDAO deviceTypeDAO; @@ -56,10 +56,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv private DeviceManagementPluginRepository pluginRepository; private static Log log = LogFactory.getLog(DeviceManagementProviderServiceImpl.class); - private int tenantId; public DeviceManagementProviderServiceImpl() { - this.pluginRepository = new DeviceManagementPluginRepository(); initDataAccessObjects(); /* Registering a listener to retrieve events when some device management service plugin is installed after @@ -67,16 +65,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementServiceComponent.registerPluginInitializationListener(this); } - /** - * This constructor calls from unit tests - * - * @param pluginRepo - */ - DeviceManagementProviderServiceImpl(DeviceManagementPluginRepository pluginRepo, boolean test) { - this.pluginRepository = pluginRepo; - initDataAccessObjects(); - } - private void initDataAccessObjects() { this.deviceDAO = DeviceManagementDAOFactory.getDeviceDAO(); this.deviceTypeDAO = DeviceManagementDAOFactory.getDeviceTypeDAO(); @@ -116,70 +104,72 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManager dms = this.getPluginRepository().getDeviceManagementService(device.getType()).getDeviceManager(); dms.enrollDevice(device); - try { - if (dms.isClaimable(deviceIdentifier)) { - device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.INACTIVE); - } else { - device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.ACTIVE); - } - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - - Device existingDevice = this.getDevice(deviceIdentifier); - - if (existingDevice != null) { - EnrolmentInfo existingEnrolmentInfo = existingDevice.getEnrolmentInfo(); - EnrolmentInfo newEnrolmentInfo = device.getEnrolmentInfo(); - if (existingEnrolmentInfo != null && newEnrolmentInfo != null) { - if (existingEnrolmentInfo.equals(newEnrolmentInfo)) { - device.getEnrolmentInfo().setDateOfEnrolment(existingEnrolmentInfo.getDateOfEnrolment()); - this.modifyEnrollment(device); - status = true; - } else { - this.setStatus(deviceIdentifier, existingEnrolmentInfo.getOwner(), EnrolmentInfo.Status.INACTIVE); + + if (dms.isClaimable(deviceIdentifier)) { + device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.INACTIVE); + } else { + device.getEnrolmentInfo().setStatus(EnrolmentInfo.Status.ACTIVE); + } + int tenantId = this.getTenantId(); + + Device existingDevice = this.getDevice(deviceIdentifier); + + if (existingDevice != null) { + EnrolmentInfo existingEnrolmentInfo = existingDevice.getEnrolmentInfo(); + EnrolmentInfo newEnrolmentInfo = device.getEnrolmentInfo(); + if (existingEnrolmentInfo != null && newEnrolmentInfo != null) { + if (existingEnrolmentInfo.equals(newEnrolmentInfo)) { + device.getEnrolmentInfo().setDateOfEnrolment(existingEnrolmentInfo.getDateOfEnrolment()); + this.modifyEnrollment(device); + status = true; + } else { + this.setStatus(deviceIdentifier, existingEnrolmentInfo.getOwner(), EnrolmentInfo.Status.INACTIVE); + int enrolmentId; + try { DeviceManagementDAOFactory.beginTransaction(); - int enrolmentId = enrolmentDAO.addEnrollment(existingDevice.getId(), newEnrolmentInfo, tenantId); + enrolmentId = enrolmentDAO.addEnrollment(existingDevice.getId(), newEnrolmentInfo, tenantId); DeviceManagementDAOFactory.commitTransaction(); if (log.isDebugEnabled()) { log.debug("An enrolment is successfully updated with the id '" + enrolmentId + - "' associated with " + "the device identified by key '" + device.getDeviceIdentifier() + - "', which belongs to " + "platform '" + device.getType() + " upon the user '" + - device.getEnrolmentInfo().getOwner() + "'"); + "' associated with " + "the device identified by key '" + + device.getDeviceIdentifier() + "', which belongs to " + "platform '" + + device.getType() + " upon the user '" + device.getEnrolmentInfo().getOwner() + "'"); } - status = true; + } catch (TransactionManagementException | DeviceManagementDAOException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + log.error("Error occurred while adding enrolment related metadata", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); } + status = true; } - } else { + } + } else { + int enrolmentId = 0; + try { DeviceManagementDAOFactory.beginTransaction(); DeviceType type = deviceTypeDAO.getDeviceType(device.getType()); int deviceId = deviceDAO.addDevice(type.getId(), device, tenantId); - int enrolmentId = enrolmentDAO.addEnrollment(deviceId, device.getEnrolmentInfo(), tenantId); + enrolmentId = enrolmentDAO.addEnrollment(deviceId, device.getEnrolmentInfo(), tenantId); DeviceManagementDAOFactory.commitTransaction(); - - if (log.isDebugEnabled()) { - log.debug("An enrolment is successfully created with the id '" + enrolmentId + "' associated with " + - "the device identified by key '" + device.getDeviceIdentifier() + "', which belongs to " + - "platform '" + device.getType() + " upon the user '" + - device.getEnrolmentInfo().getOwner() + "'"); - } - status = true; - } - - } catch (DeviceManagementDAOException e) { - try { + } catch (DeviceManagementDAOException | TransactionManagementException e) { DeviceManagementDAOFactory.rollbackTransaction(); - } catch (DeviceManagementDAOException e1) { - log.warn("Error occurred while roll-backing the current transaction", e); - } - throw new DeviceManagementException("Error occurred while enrolling the device " + - "'" + device.getId() + "'", e); - } finally { - try { + log.error("Error occurred while adding metadata of '" + device.getType() + "' device carrying " + + "the identifier '" + device.getDeviceIdentifier() + "'", e); + } finally { DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing the connection", e); } + + if (log.isDebugEnabled()) { + log.debug("An enrolment is successfully created with the id '" + enrolmentId + "' associated with " + + "the device identified by key '" + device.getDeviceIdentifier() + "', which belongs to " + + "platform '" + device.getType() + " upon the user '" + + device.getEnrolmentInfo().getOwner() + "'"); + } + status = true; } + return status; } @@ -190,27 +180,20 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv this.getPluginRepository().getDeviceManagementService(device.getType()).getDeviceManager(); boolean status = dms.modifyEnrollment(device); try { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = this.getTenantId(); DeviceManagementDAOFactory.beginTransaction(); + DeviceType type = deviceTypeDAO.getDeviceType(device.getType()); int deviceId = deviceDAO.updateDevice(type.getId(), device, tenantId); enrolmentDAO.updateEnrollment(deviceId, device.getEnrolmentInfo(), tenantId); DeviceManagementDAOFactory.commitTransaction(); - } catch (DeviceManagementDAOException e) { - try { - DeviceManagementDAOFactory.rollbackTransaction(); - } catch (DeviceManagementDAOException e1) { - log.warn("Error occurred while roll-backing the current transaction", e); - } + } catch (DeviceManagementDAOException | TransactionManagementException e) { + DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException("Error occurred while modifying the device " + - "'" + device.getId() + "'", e); + "'" + device.getId() + "'", e); } finally { - try { - DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing the connection", e); - } + DeviceManagementDAOFactory.closeConnection(); } return status; } @@ -218,10 +201,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = this.getTenantId(); DeviceManager dms = this.getPluginRepository().getDeviceManagementService(deviceId.getType()).getDeviceManager(); try { + DeviceManagementDAOFactory.beginTransaction(); + Device device = deviceDAO.getDevice(deviceId, tenantId); DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType()); @@ -230,11 +215,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv enrolmentDAO.updateEnrollment(device.getId(), device.getEnrolmentInfo(), tenantId); deviceDAO.updateDevice(deviceType.getId(), device, tenantId); - } catch (DeviceManagementDAOException e) { - String errorMsg = "Error occurred while fetch device for device Identifier:"; - log.error(errorMsg + deviceId.toString(), e); - throw new DeviceManagementException(errorMsg, e); - + DeviceManagementDAOFactory.commitTransaction(); + } catch (DeviceManagementDAOException | TransactionManagementException e) { + DeviceManagementDAOFactory.rollbackTransaction(); + throw new DeviceManagementException("Error occurred while disenrolling '" + deviceId.getType() + + "' device with the identifier '" + deviceId.getId() + "'", e); + } finally { + DeviceManagementDAOFactory.closeConnection(); } return dms.disenrollDevice(deviceId); } @@ -262,26 +249,22 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getAllDevices() throws DeviceManagementException { - List devices = new ArrayList(); + List devices = new ArrayList<>(); List allDevices; try { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - DeviceManagementDAOFactory.getConnection(); - allDevices = deviceDAO.getDevices(tenantId); - } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.openConnection(); + + allDevices = deviceDAO.getDevices(this.getTenantId()); + } catch (DeviceManagementDAOException | SQLException e) { throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " + - "the current tenant", e); + "the current tenant", e); } finally { - try { - DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing the connection", e); - } + DeviceManagementDAOFactory.closeConnection(); } for (Device device : allDevices) { - DeviceManagementService managementService = this.getPluginRepository(). - getDeviceManagementService(device.getType()); - if(managementService != null) { + DeviceManagementService managementService = this.getPluginRepository(). + getDeviceManagementService(device.getType()); + if (managementService != null) { Device dmsDevice = managementService.getDeviceManager().getDevice( new DeviceIdentifier(device.getDeviceIdentifier(), device.getType())); if (dmsDevice != null) { @@ -296,21 +279,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getAllDevices(String type) throws DeviceManagementException { - List devices = new ArrayList(); + List devices = new ArrayList<>(); List allDevices; try { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - DeviceManagementDAOFactory.getConnection(); - allDevices = deviceDAO.getDevices(type, tenantId); - } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.openConnection(); + + allDevices = deviceDAO.getDevices(type, this.getTenantId()); + } catch (DeviceManagementDAOException | SQLException e) { throw new DeviceManagementException("Error occurred while retrieving all devices of type '" + - type + "' that are being managed within the scope of current tenant", e); + type + "' that are being managed within the scope of current tenant", e); } finally { - try { - DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing the connection", e); - } + DeviceManagementDAOFactory.closeConnection(); } for (Device device : allDevices) { @@ -332,7 +311,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public void sendEnrolmentInvitation(EmailMessageProperties emailMessageProperties) throws DeviceManagementException { - List notificationMessages = DeviceConfigurationManager.getInstance().getNotificationMessagesConfig().getNotificationMessagesList(); @@ -347,7 +325,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv for (NotificationMessages notificationMessage : notificationMessages) { if (org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailNotifications.ENROL_NOTIFICATION_TYPE .equals( - notificationMessage.getType())) { + notificationMessage.getType())) { messageHeader = notificationMessage.getHeader(); messageBody = notificationMessage.getBody(); messageFooter1 = notificationMessage.getFooterLine1(); @@ -363,13 +341,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { messageHeader = messageHeader.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.FIRST_NAME + "\\}", - URLEncoder.encode(emailMessageProperties.getFirstName(), - EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); + URLEncoder.encode(emailMessageProperties.getFirstName(), + EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); messageBody = messageBody.trim() + System.getProperty("line.separator") + - System.getProperty("line.separator") + url.replaceAll("\\{" - + EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}", - URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), - EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); + System.getProperty("line.separator") + url.replaceAll("\\{" + + EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}", + URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), + EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); messageBuilder.append(messageHeader).append(System.getProperty("line.separator")) .append(System.getProperty("line.separator")); @@ -377,12 +355,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv messageBuilder.append(System.getProperty("line.separator")).append(System.getProperty("line.separator")); messageBuilder.append(messageFooter1.trim()) .append(System.getProperty("line.separator")).append(messageFooter2.trim()).append(System - .getProperty("line.separator")).append(messageFooter3.trim()); + .getProperty("line.separator")).append(messageFooter3.trim()); } catch (IOException e) { log.error("IO error in processing enrol email message " + emailMessageProperties); throw new DeviceManagementException("Error replacing tags in email template '" + - emailMessageProperties.getSubject() + "'", e); + emailMessageProperties.getSubject() + "'", e); } emailMessageProperties.setMessageBody(messageBuilder.toString()); emailMessageProperties.setSubject(subject); @@ -420,23 +398,23 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { messageHeader = messageHeader.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.FIRST_NAME + "\\}", - URLEncoder.encode(emailMessageProperties.getFirstName(), - EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); + URLEncoder.encode(emailMessageProperties.getFirstName(), + EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); messageBody = messageBody.trim().replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants - .USERNAME - + "\\}", - URLEncoder.encode(emailMessageProperties.getUserName(), EmailConstants.EnrolmentEmailConstants - .ENCODED_SCHEME)); + .USERNAME + + "\\}", + URLEncoder.encode(emailMessageProperties.getUserName(), EmailConstants.EnrolmentEmailConstants + .ENCODED_SCHEME)); messageBody = messageBody.replaceAll("\\{" + EmailConstants.EnrolmentEmailConstants.PASSWORD + "\\}", - URLEncoder.encode(emailMessageProperties.getPassword(), EmailConstants.EnrolmentEmailConstants - .ENCODED_SCHEME)); + URLEncoder.encode(emailMessageProperties.getPassword(), EmailConstants.EnrolmentEmailConstants + .ENCODED_SCHEME)); messageBody = messageBody + System.getProperty("line.separator") + url.replaceAll("\\{" - + EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}", - URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), - EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); + + EmailConstants.EnrolmentEmailConstants.DOWNLOAD_URL + "\\}", + URLDecoder.decode(emailMessageProperties.getEnrolmentUrl(), + EmailConstants.EnrolmentEmailConstants.ENCODED_SCHEME)); messageBuilder.append(messageHeader).append(System.getProperty("line.separator")); messageBuilder.append(messageBody).append(System.getProperty("line.separator")).append( @@ -447,7 +425,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } catch (IOException e) { log.error("IO error in processing enrol email message " + emailMessageProperties); throw new DeviceManagementException("Error replacing tags in email template '" + - emailMessageProperties.getSubject() + "'", e); + emailMessageProperties.getSubject() + "'", e); } emailMessageProperties.setMessageBody(messageBuilder.toString()); emailMessageProperties.setSubject(subject); @@ -458,23 +436,18 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv public Device getDevice(DeviceIdentifier deviceId) throws DeviceManagementException { Device device; try { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - device = deviceDAO.getDevice(deviceId, tenantId); + device = deviceDAO.getDevice(deviceId, this.getTenantId()); } catch (DeviceManagementDAOException e) { throw new DeviceManagementException("Error occurred while obtaining the device for id " + - "'" + deviceId.getId() + "'", e); + "'" + deviceId.getId() + "'", e); } finally { - try { - DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing the connection", e); - } + DeviceManagementDAOFactory.closeConnection(); } if (device != null) { // The changes made here to prevent unit tests getting failed. They failed because when running the unit // tests there is no osgi services. So getDeviceManager() returns a null. - DeviceManagementService service = this.getPluginRepository().getDeviceManagementService(deviceId.getType()); - if(service != null) { + DeviceManagementService service = this.getPluginRepository().getDeviceManagementService(deviceId.getType()); + if (service != null) { DeviceManager dms = service.getDeviceManager(); Device pluginSpecificInfo = dms.getDevice(deviceId); if (pluginSpecificInfo != null) { @@ -513,25 +486,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { DeviceManagementDAOFactory.beginTransaction(); - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = this.getTenantId(); Device device = deviceDAO.getDevice(deviceId, tenantId); boolean success = enrolmentDAO.setStatus(device.getId(), currentOwner, status, tenantId); DeviceManagementDAOFactory.commitTransaction(); return success; - } catch (DeviceManagementDAOException e) { - try { - DeviceManagementDAOFactory.rollbackTransaction(); - } catch (DeviceManagementDAOException e1) { - log.warn("Error occurred while rollbacking the current transaction", e); - } + } catch (DeviceManagementDAOException | TransactionManagementException e) { + DeviceManagementDAOFactory.rollbackTransaction(); throw new DeviceManagementException("Error occurred while setting enrollment status", e); } finally { - try { - DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing the connection", e); - } + DeviceManagementDAOFactory.closeConnection(); } } @@ -565,7 +530,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public int addOperation(Operation operation, List devices) throws - OperationManagementException { + OperationManagementException { return DeviceManagementDataHolder.getInstance().getOperationManager().addOperation(operation, devices); } @@ -617,21 +582,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getDevicesOfUser(String username) throws DeviceManagementException { - List devices = new ArrayList(); + List devices = new ArrayList<>(); List userDevices; try { - DeviceManagementDAOFactory.getConnection(); - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - userDevices = deviceDAO.getDevicesOfUser(username, tenantId); - } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.openConnection(); + userDevices = deviceDAO.getDevicesOfUser(username, this.getTenantId()); + } catch (DeviceManagementDAOException | SQLException e) { throw new DeviceManagementException("Error occurred while retrieving the list of devices that " + - "belong to the user '" + username + "'", e); + "belong to the user '" + username + "'", e); } finally { - try { - DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing the connection", e); - } + DeviceManagementDAOFactory.closeConnection(); } for (Device device : userDevices) { @@ -651,32 +611,28 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getAllDevicesOfRole(String role) throws DeviceManagementException { - List devices = new ArrayList(); + List devices = new ArrayList<>(); String[] users; - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + int tenantId = this.getTenantId(); try { users = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId) .getUserStoreManager().getUserListOfRole(role); } catch (UserStoreException e) { throw new DeviceManagementException("Error occurred while obtaining the users, who are assigned " + - "with the role '" + role + "'", e); + "with the role '" + role + "'", e); } List userDevices; for (String user : users) { - userDevices = new ArrayList(); + userDevices = new ArrayList<>(); try { - DeviceManagementDAOFactory.getConnection(); + DeviceManagementDAOFactory.openConnection(); userDevices = deviceDAO.getDevicesOfUser(user, tenantId); - } catch (DeviceManagementDAOException e) { + } catch (DeviceManagementDAOException | SQLException e) { log.error("Error occurred while obtaining the devices of user '" + user + "'", e); } finally { - try { - DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing the connection", e); - } + DeviceManagementDAOFactory.closeConnection(); } for (Device device : userDevices) { Device dmsDevice = @@ -696,37 +652,27 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public int getDeviceCount() throws DeviceManagementException { try { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - DeviceManagementDAOFactory.getConnection(); - return deviceDAO.getDeviceCount(tenantId); - } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.openConnection(); + return deviceDAO.getDeviceCount(this.getTenantId()); + } catch (DeviceManagementDAOException | SQLException e) { throw new DeviceManagementException("Error occurred while retrieving the device count", e); } finally { - try { - DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing the connection", e); - } + DeviceManagementDAOFactory.closeConnection(); } } @Override public List getDevicesByName(String deviceName) throws DeviceManagementException { - List devices = new ArrayList(); + List devices = new ArrayList<>(); List allDevices; try { - DeviceManagementDAOFactory.getConnection(); - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - allDevices = deviceDAO.getDevicesByName(deviceName, tenantId); - } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.openConnection(); + allDevices = deviceDAO.getDevicesByName(deviceName, this.getTenantId()); + } catch (DeviceManagementDAOException | SQLException e) { throw new DeviceManagementException("Error occurred while fetching the list of devices that matches to '" - + deviceName + "'", e); + + deviceName + "'", e); } finally { - try { - DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing the connection", e); - } + DeviceManagementDAOFactory.closeConnection(); } for (Device device : allDevices) { Device dmsDevice = @@ -745,13 +691,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status status) throws DeviceManagementException { - - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); try { DeviceType deviceType = deviceTypeDAO.getDeviceType(device.getType()); device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); device.getEnrolmentInfo().setStatus(status); - deviceDAO.updateDevice(deviceType.getId(), device, tenantId); + deviceDAO.updateDevice(deviceType.getId(), device, this.getTenantId()); } catch (DeviceManagementDAOException deviceDaoEx) { String errorMsg = "Error occured update device enrolment status : " + device.getId(); log.error(errorMsg, deviceDaoEx); @@ -765,7 +709,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv pluginRepository.addDeviceManagementProvider(deviceManagementService); } catch (DeviceManagementException e) { log.error("Error occurred while registering device management plugin '" + - deviceManagementService.getType() + "'", e); + deviceManagementService.getType() + "'", e); } } @@ -775,27 +719,23 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv pluginRepository.removeDeviceManagementProvider(deviceManagementService); } catch (DeviceManagementException e) { log.error("Error occurred while un-registering device management plugin '" + - deviceManagementService.getType() + "'", e); + deviceManagementService.getType() + "'", e); } } public List getDevicesByStatus(EnrolmentInfo.Status status) throws DeviceManagementException { - List devices = new ArrayList(); + List devices = new ArrayList<>(); List allDevices; try { - DeviceManagementDAOFactory.getConnection(); - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - allDevices = deviceDAO.getDevicesByStatus(status, tenantId); - } catch (DeviceManagementDAOException e) { + DeviceManagementDAOFactory.openConnection(); + allDevices = deviceDAO.getDevicesByStatus(status, this.getTenantId()); + } catch (DeviceManagementDAOException | SQLException e) { throw new DeviceManagementException( "Error occurred while fetching the list of devices that matches to status: '" + status + "'", e); } finally { - try { - DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing the connection", e); - } + DeviceManagementDAOFactory.closeConnection(); + } for (Device device : allDevices) { @@ -811,4 +751,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } return devices; } + + private int getTenantId() { + return CarbonContext.getThreadLocalCarbonContext().getTenantId(); + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderServiceTest.java index ad607d74cf8..edab3737a2c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagementProviderServiceTest.java @@ -73,7 +73,7 @@ public class ApplicationManagementProviderServiceTest { deviceIdentifier.setType(device.getType()); AppManagementConfig appManagementConfig = new AppManagementConfig(); - appMgtProvider = new ApplicationManagerProviderServiceImpl(deviceManagementPluginRepository, true); + appMgtProvider = new ApplicationManagerProviderServiceImpl(deviceManagementPluginRepository); try { appMgtProvider.updateApplicationListInstalledInDevice(deviceIdentifier, applications); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceTests.java index 02c649187fc..19726da1e1b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/ApplicationPersistenceTests.java @@ -27,6 +27,8 @@ import org.wso2.carbon.device.mgt.common.app.mgt.Application; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; import org.wso2.carbon.device.mgt.core.common.TestDataHolder; +import java.sql.SQLException; + public class ApplicationPersistenceTests extends BaseDeviceManagementTest { private static final Log log = LogFactory.getLog(ApplicationPersistenceTests.class); @@ -34,21 +36,15 @@ public class ApplicationPersistenceTests extends BaseDeviceManagementTest { @Test public void testAddApplication() { - /* Initializing source application bean to be tested */ - /* Adding dummy application to the application store */ String testAppIdentifier = "test sample1"; try { DeviceManagementDAOFactory.openConnection(); applicationDAO.addApplication(TestDataHolder.generateApplicationDummyData(testAppIdentifier), -1234); - } catch (DeviceManagementDAOException e) { + } catch (DeviceManagementDAOException | SQLException e) { log.error("Error occurred while adding application test sample1", e); } finally { - try { - DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing the connection", e); - } + DeviceManagementDAOFactory.closeConnection(); } /* Retrieving the application by its name */ Application target = null; @@ -66,16 +62,16 @@ public class ApplicationPersistenceTests extends BaseDeviceManagementTest { } private Application getApplication(String appIdentifier, int tenantId) throws DeviceManagementDAOException { + Application application = null; try { DeviceManagementDAOFactory.openConnection(); - return applicationDAO.getApplication(appIdentifier, tenantId); + application = applicationDAO.getApplication(appIdentifier, tenantId); + } catch (SQLException e) { + log.error("Error occurred while metadata corresponding to the application '" + appIdentifier + "'", e); } finally { - try { - DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing connection", e); - } + DeviceManagementDAOFactory.closeConnection(); } + return application; } @BeforeClass @@ -83,4 +79,5 @@ public class ApplicationPersistenceTests extends BaseDeviceManagementTest { public void init() throws Exception { this.initDatSource(); } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DevicePersistTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DevicePersistTests.java index f3f1f7ebc91..603954ea761 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DevicePersistTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/DevicePersistTests.java @@ -52,16 +52,12 @@ public class DevicePersistTests extends BaseDeviceManagementTest { try { DeviceManagementDAOFactory.openConnection(); deviceTypeDAO.addDeviceType(deviceType); - } catch (DeviceManagementDAOException e) { + } catch (DeviceManagementDAOException | SQLException e) { String msg = "Error occurred while adding device type '" + deviceType.getName() + "'"; log.error(msg, e); Assert.fail(msg, e); } finally { - try { - DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing the connection", e); - } + DeviceManagementDAOFactory.closeConnection(); } Integer targetTypeId = null; @@ -89,17 +85,13 @@ public class DevicePersistTests extends BaseDeviceManagementTest { device.setId(deviceId); deviceDAO.addEnrollment(device, tenantId); TestDataHolder.initialTestDevice = device; - } catch (DeviceManagementDAOException e) { + } catch (DeviceManagementDAOException | SQLException e) { String msg = "Error occurred while adding '" + device.getType() + "' device with the identifier '" + device.getDeviceIdentifier() + "'"; log.error(msg, e); Assert.fail(msg, e); } finally { - try { - DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing the connection", e); - } + DeviceManagementDAOFactory.closeConnection(); } int targetId = -1; @@ -176,17 +168,12 @@ public class DevicePersistTests extends BaseDeviceManagementTest { DeviceIdentifier deviceId = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); deviceDAO.setEnrolmentStatus(deviceId, device.getEnrolmentInfo().getOwner(), Status.ACTIVE, TestDataHolder.SUPER_TENANT_ID); - - } catch (DeviceManagementDAOException e) { + } catch (DeviceManagementDAOException | SQLException e) { String msg = "Error occurred while setting enrolment status"; log.error(msg, e); Assert.fail(msg, e); } finally { - try { - DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing the connection", e); - } + DeviceManagementDAOFactory.closeConnection(); } Status target = null; try { @@ -210,15 +197,11 @@ public class DevicePersistTests extends BaseDeviceManagementTest { DeviceManagementDAOFactory.openConnection(); DeviceIdentifier deviceId = new DeviceIdentifier(identifier, deviceType); return deviceDAO.getEnrolmentStatus(deviceId, device.getEnrolmentInfo().getOwner(), tenantId); - } catch (DeviceManagementDAOException e) { + } catch (DeviceManagementDAOException | SQLException e) { throw new DeviceManagementDAOException("Error occurred while retrieving the current status of the " + "enrolment", e); } finally { - try { - DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing the connection", e); - } + DeviceManagementDAOFactory.closeConnection(); } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentPersistenceTests.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentPersistenceTests.java index c1a90ce2034..35ca0e4d313 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentPersistenceTests.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/dao/EnrolmentPersistenceTests.java @@ -27,6 +27,8 @@ import org.wso2.carbon.device.mgt.common.EnrolmentInfo; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; import org.wso2.carbon.device.mgt.core.common.TestDataHolder; +import java.sql.SQLException; + public class EnrolmentPersistenceTests extends BaseDeviceManagementTest { private static final Log log = LogFactory.getLog(EnrolmentPersistenceTests.class); @@ -46,14 +48,10 @@ public class EnrolmentPersistenceTests extends BaseDeviceManagementTest { try { DeviceManagementDAOFactory.openConnection(); enrolmentDAO.addEnrollment(deviceId, source, TestDataHolder.SUPER_TENANT_ID); - } catch (DeviceManagementDAOException e) { + } catch (DeviceManagementDAOException | SQLException e) { log.error("Error occurred while adding enrollment", e); } finally { - try { - DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing the connection", e); - } + DeviceManagementDAOFactory.closeConnection(); } /* Retrieving the enrolment associated with the given deviceId and owner */ EnrolmentInfo target = null; @@ -70,16 +68,16 @@ public class EnrolmentPersistenceTests extends BaseDeviceManagementTest { private EnrolmentInfo getEnrolmentConfig(int deviceId, String currentOwner, int tenantId) throws DeviceManagementDAOException { + EnrolmentInfo enrolmentInfo = null; try { DeviceManagementDAOFactory.openConnection(); - return enrolmentDAO.getEnrolment(deviceId, currentOwner, tenantId); + enrolmentInfo = enrolmentDAO.getEnrolment(deviceId, currentOwner, tenantId); + } catch (SQLException e) { + log.error("Error occurred while retrieving enrolment corresponding to device id '" + deviceId + "'", e); } finally { - try { - DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing connection", e); - } + DeviceManagementDAOFactory.closeConnection(); } + return enrolmentInfo; } @BeforeClass @@ -87,4 +85,5 @@ public class EnrolmentPersistenceTests extends BaseDeviceManagementTest { public void init() throws Exception { this.initDatSource(); } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java index e79930ee04f..8b6c4cd5e47 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceTest.java @@ -21,19 +21,14 @@ import org.testng.Assert; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; -import org.wso2.carbon.context.CarbonContext; -import org.wso2.carbon.context.PrivilegedCarbonContext; -import org.wso2.carbon.context.internal.CarbonContextDataHolder; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.DeviceManagementException; import org.wso2.carbon.device.mgt.core.DeviceManagementPluginRepository; import org.wso2.carbon.device.mgt.core.TestDeviceManagementService; import org.wso2.carbon.device.mgt.core.common.BaseDeviceManagementTest; import org.wso2.carbon.device.mgt.core.common.TestDataHolder; -import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; -import org.wso2.carbon.utils.multitenancy.MultitenantConstants; public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTest { @@ -47,23 +42,22 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes initDatSource(); } - @Test + @Test public void testEnrollment() { - try { DeviceManagementPluginRepository deviceManagementPluginRepository = new DeviceManagementPluginRepository(); - TestDeviceManagementService testDeviceManagementService = new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE); + TestDeviceManagementService testDeviceManagementService = + new TestDeviceManagementService(TestDataHolder.TEST_DEVICE_TYPE); deviceManagementPluginRepository.addDeviceManagementProvider(testDeviceManagementService); - deviceManagementProviderService = new DeviceManagementProviderServiceImpl(deviceManagementPluginRepository, - true); + deviceManagementProviderService = new DeviceManagementProviderServiceImpl(); DeviceManagerUtil.registerDeviceType(TestDataHolder.TEST_DEVICE_TYPE); Device device = TestDataHolder.generateDummyDeviceData(TestDataHolder.TEST_DEVICE_TYPE); boolean isEnrolled = deviceManagementProviderService.enrollDevice(device); - Assert.assertEquals(isEnrolled,true,"Enrolment fail"); - if (isEnrolled){ + Assert.assertEquals(isEnrolled, true, "Enrolment fail"); + if (isEnrolled) { TestDataHolder.initialTestDevice = device; } @@ -72,15 +66,11 @@ public class DeviceManagementProviderServiceTest extends BaseDeviceManagementTes log.error(msg, e); Assert.fail(msg, e); } finally { - try { - DeviceManagementDAOFactory.closeConnection(); - } catch (DeviceManagementDAOException e) { - log.warn("Error occurred while closing the connection", e); - } + DeviceManagementDAOFactory.closeConnection(); } } @AfterClass - public void cleanResources(){ + public void cleanResources() { } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml index cf107283b8f..78c1493fe70 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/testng.xml @@ -33,7 +33,7 @@ - +