diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManager.java index 1169d7eebfd..4e60210424f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManager.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypeManager.java @@ -228,7 +228,14 @@ public class DeviceTypeManager implements DeviceManager { if (deviceTypePluginDAOManager != null) { DeviceTypePluginExtensionService deviceTypeManagerExtensionService = new DeviceTypePluginExtensionServiceImpl(); - deviceTypeManagerExtensionService.addPluginDAOManager(deviceType, deviceTypePluginDAOManager); + try { + deviceTypeManagerExtensionService.addPluginDAOManager(deviceType, deviceTypePluginDAOManager); + } catch (DeviceTypePluginExtensionException e) { + String msg = "Error occurred while saving DeviceTypePluginDAOManager for device type: " + + deviceType; + log.error(msg); + throw new DeviceTypeDeployerPayloadException(msg); + } } else { log.warn("Could not save DeviceTypePluginDAOManager for device type: " + deviceType + " since DeviceTypePluginDAOManager is null."); @@ -236,7 +243,7 @@ public class DeviceTypeManager implements DeviceManager { } else { String msg = "Could not save DeviceTypePluginDAOManager since device type is null or empty."; log.error(msg); - throw new DeviceTypePluginExtensionException(msg); + throw new DeviceTypeDeployerPayloadException(msg); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypePluginExtensionServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypePluginExtensionServiceImpl.java index bfe01098b61..e32c9e2d573 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypePluginExtensionServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/DeviceTypePluginExtensionServiceImpl.java @@ -34,21 +34,26 @@ public class DeviceTypePluginExtensionServiceImpl implements DeviceTypePluginExt private static volatile Map pluginDAOManagers = new HashMap<>(); @Override - public void addPluginDAOManager(String deviceType, DeviceTypePluginDAOManager pluginDAOManager) { + public void addPluginDAOManager(String deviceType, DeviceTypePluginDAOManager pluginDAOManager) + throws DeviceTypePluginExtensionException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); - if (pluginDAOManager != null) { - if (!pluginDAOManagers.containsKey(tenantId + deviceType)) { - if (log.isDebugEnabled()) { - log.debug("Saving DeviceTypePluginDAOManager against tenant id " + tenantId + - " and device type: " + deviceType); - } - pluginDAOManagers.put(tenantId + deviceType, pluginDAOManager); + if (pluginDAOManager == null) { + String msg = "Cannot save DeviceTypePluginDAOManager against tenant id " + tenantId + + " and device type: " + deviceType + " since DeviceTypePluginDAOManager is null"; + log.error(msg); + throw new DeviceTypePluginExtensionException(msg); + } + if (!pluginDAOManagers.containsKey(tenantId + deviceType)) { + if (log.isDebugEnabled()) { + log.debug("Saving DeviceTypePluginDAOManager against tenant id " + tenantId + + " and device type: " + deviceType); } + pluginDAOManagers.put(tenantId + deviceType, pluginDAOManager); } } @Override - public DeviceTypePluginDAOManager getPluginDAOManager(String deviceType) { + public DeviceTypePluginDAOManager getPluginDAOManager(String deviceType) throws DeviceTypePluginExtensionException { int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); if (pluginDAOManagers.containsKey(tenantId + deviceType)) { if (log.isDebugEnabled()) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypeDAOHandler.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypeDAOHandler.java index edbbba52c67..417d0788688 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypeDAOHandler.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/dao/DeviceTypeDAOHandler.java @@ -49,7 +49,9 @@ public class DeviceTypeDAOHandler { Context ctx = new InitialContext(); dataSource = (DataSource) ctx.lookup(datasourceName); } catch (NamingException e) { - throw new DeviceTypeDeployerPayloadException("Error while looking up the data source: " + datasourceName, e); + String msg = "Error while looking up the data source: " + datasourceName; + log.error(msg, e); + throw new DeviceTypeDeployerPayloadException(msg, e); } } @@ -57,12 +59,16 @@ public class DeviceTypeDAOHandler { try { Connection conn = currentConnection.get(); if (conn != null) { - throw new IllegalTransactionStateException("Database connection has already been obtained."); + String msg = "Database connection has already been obtained."; + log.error(msg); + throw new IllegalTransactionStateException(msg); } conn = dataSource.getConnection(); currentConnection.set(conn); } catch (SQLException e) { - throw new DeviceTypeMgtPluginException("Failed to get a database connection.", e); + String msg = "Failed to get a database connection."; + log.error(msg, e); + throw new DeviceTypeMgtPluginException(msg, e); } } @@ -72,7 +78,9 @@ public class DeviceTypeDAOHandler { conn.setAutoCommit(false); currentConnection.set(conn); } catch (SQLException e) { - throw new DeviceTypeMgtPluginException("Error occurred while retrieving datasource connection", e); + String msg = "Error occurred while retrieving datasource connection"; + log.error(msg, e); + throw new DeviceTypeMgtPluginException(msg, e); } } @@ -81,7 +89,9 @@ public class DeviceTypeDAOHandler { try { currentConnection.set(dataSource.getConnection()); } catch (SQLException e) { - throw new DeviceTypeMgtPluginException("Error occurred while retrieving data source connection", e); + String msg = "Error occurred while retrieving data source connection"; + log.error(msg, e); + throw new DeviceTypeMgtPluginException(msg, e); } } return currentConnection.get(); @@ -90,25 +100,28 @@ public class DeviceTypeDAOHandler { public void commitTransaction() { Connection conn = currentConnection.get(); if (conn == null) { - throw new IllegalStateException("No connection is associated with the current transaction. " + - "This might have ideally been caused by not properly initiating the " + - "transaction via 'beginTransaction'/'openConnection' methods"); + String msg = "No connection is associated with the current transaction. This might have ideally been " + + "caused by not properly initiating the transaction via " + + "'beginTransaction'/'openConnection' methods"; + log.error(msg); + throw new IllegalStateException(msg); } try { conn.commit(); } catch (SQLException e) { - log.error("Error occurred while committing the transaction.", e); + String msg = "Error occurred while committing the transaction."; + log.error(msg, e); } } public void closeConnection() { - Connection con = currentConnection.get(); if (con != null) { try { con.close(); } catch (SQLException e) { - log.error("Error occurred while close the connection"); + String msg = "Error occurred while close the connection"; + log.error(msg, e); } } currentConnection.remove(); @@ -117,14 +130,17 @@ public class DeviceTypeDAOHandler { public void rollbackTransaction() { Connection conn = currentConnection.get(); if (conn == null) { - throw new IllegalStateException("No connection is associated with the current transaction. " + - "This might have ideally been caused by not properly initiating the " + - "transaction via 'beginTransaction'/'openConnection' methods"); + String msg = "No connection is associated with the current transaction. This might have ideally been " + + "caused by not properly initiating the transaction via " + + "'beginTransaction'/'openConnection' methods"; + log.error(msg); + throw new IllegalStateException(msg); } try { conn.rollback(); } catch (SQLException e) { - log.error("Error occurred while roll-backing the transaction.", e); + String msg = "Error occurred while roll-backing the transaction."; + log.error(msg, e); } } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/exception/DeviceTypePluginExtensionException.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/exception/DeviceTypePluginExtensionException.java index 7afbd12cd2b..b0603f43bfb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/exception/DeviceTypePluginExtensionException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/device/type/template/exception/DeviceTypePluginExtensionException.java @@ -1,6 +1,6 @@ package org.wso2.carbon.device.mgt.extensions.device.type.template.exception; -public class DeviceTypePluginExtensionException extends RuntimeException { +public class DeviceTypePluginExtensionException extends Exception { public DeviceTypePluginExtensionException(String msg) { super(msg); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/spi/DeviceTypePluginExtensionService.java b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/spi/DeviceTypePluginExtensionService.java index 26aaf481e2d..f91ed2e985c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/spi/DeviceTypePluginExtensionService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.extensions/src/main/java/org/wso2/carbon/device/mgt/extensions/spi/DeviceTypePluginExtensionService.java @@ -18,6 +18,7 @@ package org.wso2.carbon.device.mgt.extensions.spi; import org.wso2.carbon.device.mgt.extensions.device.type.template.dao.DeviceTypePluginDAOManager; +import org.wso2.carbon.device.mgt.extensions.device.type.template.exception.DeviceTypePluginExtensionException; /** * This represents the device type plugin extension service which can be used by any device type plugin implementation @@ -29,13 +30,16 @@ public interface DeviceTypePluginExtensionService { * Save device type specific DeviceTypePluginDAOManager in a HashMap againast tenant ID and device type * @param deviceType - Type of the device (i.e; android, ios, windows) * @param pluginDAOManager - Device type plugin DAO manager instance to be saved against device type + * @throws DeviceTypePluginExtensionException when pluginDAOManager is null */ - void addPluginDAOManager(String deviceType, DeviceTypePluginDAOManager pluginDAOManager); + void addPluginDAOManager(String deviceType, DeviceTypePluginDAOManager pluginDAOManager) + throws DeviceTypePluginExtensionException; /** * Retrieve the DeviceTypePluginDAOManager instance against tenant ID and given device type * @param deviceType - Type of the device (i.e; android, ios, windows) * @return an Instance of {@link DeviceTypePluginDAOManager} + * @throws DeviceTypePluginExtensionException when pluginDAOManager cannot be found */ - DeviceTypePluginDAOManager getPluginDAOManager(String deviceType); + DeviceTypePluginDAOManager getPluginDAOManager(String deviceType) throws DeviceTypePluginExtensionException; }