Handle exception and log error messages related to DeviceTypeDAOHandler

revert-70aa11f8
Saad Sahibjan 5 years ago
parent 0c7843f2fe
commit 4a58aea2d0

@ -228,7 +228,14 @@ public class DeviceTypeManager implements DeviceManager {
if (deviceTypePluginDAOManager != null) { if (deviceTypePluginDAOManager != null) {
DeviceTypePluginExtensionService deviceTypeManagerExtensionService = DeviceTypePluginExtensionService deviceTypeManagerExtensionService =
new DeviceTypePluginExtensionServiceImpl(); 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 { } else {
log.warn("Could not save DeviceTypePluginDAOManager for device type: " + deviceType + log.warn("Could not save DeviceTypePluginDAOManager for device type: " + deviceType +
" since DeviceTypePluginDAOManager is null."); " since DeviceTypePluginDAOManager is null.");
@ -236,7 +243,7 @@ public class DeviceTypeManager implements DeviceManager {
} else { } else {
String msg = "Could not save DeviceTypePluginDAOManager since device type is null or empty."; String msg = "Could not save DeviceTypePluginDAOManager since device type is null or empty.";
log.error(msg); log.error(msg);
throw new DeviceTypePluginExtensionException(msg); throw new DeviceTypeDeployerPayloadException(msg);
} }
} }

@ -34,21 +34,26 @@ public class DeviceTypePluginExtensionServiceImpl implements DeviceTypePluginExt
private static volatile Map<String, DeviceTypePluginDAOManager> pluginDAOManagers = new HashMap<>(); private static volatile Map<String, DeviceTypePluginDAOManager> pluginDAOManagers = new HashMap<>();
@Override @Override
public void addPluginDAOManager(String deviceType, DeviceTypePluginDAOManager pluginDAOManager) { public void addPluginDAOManager(String deviceType, DeviceTypePluginDAOManager pluginDAOManager)
throws DeviceTypePluginExtensionException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
if (pluginDAOManager != null) { if (pluginDAOManager == null) {
if (!pluginDAOManagers.containsKey(tenantId + deviceType)) { String msg = "Cannot save DeviceTypePluginDAOManager against tenant id " + tenantId
if (log.isDebugEnabled()) { + " and device type: " + deviceType + " since DeviceTypePluginDAOManager is null";
log.debug("Saving DeviceTypePluginDAOManager against tenant id " + tenantId + log.error(msg);
" and device type: " + deviceType); throw new DeviceTypePluginExtensionException(msg);
} }
pluginDAOManagers.put(tenantId + deviceType, pluginDAOManager); 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 @Override
public DeviceTypePluginDAOManager getPluginDAOManager(String deviceType) { public DeviceTypePluginDAOManager getPluginDAOManager(String deviceType) throws DeviceTypePluginExtensionException {
int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
if (pluginDAOManagers.containsKey(tenantId + deviceType)) { if (pluginDAOManagers.containsKey(tenantId + deviceType)) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {

@ -49,7 +49,9 @@ public class DeviceTypeDAOHandler {
Context ctx = new InitialContext(); Context ctx = new InitialContext();
dataSource = (DataSource) ctx.lookup(datasourceName); dataSource = (DataSource) ctx.lookup(datasourceName);
} catch (NamingException e) { } 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 { try {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
if (conn != null) { 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(); conn = dataSource.getConnection();
currentConnection.set(conn); currentConnection.set(conn);
} catch (SQLException e) { } 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); conn.setAutoCommit(false);
currentConnection.set(conn); currentConnection.set(conn);
} catch (SQLException e) { } 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 { try {
currentConnection.set(dataSource.getConnection()); currentConnection.set(dataSource.getConnection());
} catch (SQLException e) { } 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(); return currentConnection.get();
@ -90,25 +100,28 @@ public class DeviceTypeDAOHandler {
public void commitTransaction() { public void commitTransaction() {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
if (conn == null) { if (conn == null) {
throw new IllegalStateException("No connection is associated with the current transaction. " + String msg = "No connection is associated with the current transaction. This might have ideally been " +
"This might have ideally been caused by not properly initiating the " + "caused by not properly initiating the transaction via " +
"transaction via 'beginTransaction'/'openConnection' methods"); "'beginTransaction'/'openConnection' methods";
log.error(msg);
throw new IllegalStateException(msg);
} }
try { try {
conn.commit(); conn.commit();
} catch (SQLException e) { } 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() { public void closeConnection() {
Connection con = currentConnection.get(); Connection con = currentConnection.get();
if (con != null) { if (con != null) {
try { try {
con.close(); con.close();
} catch (SQLException e) { } 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(); currentConnection.remove();
@ -117,14 +130,17 @@ public class DeviceTypeDAOHandler {
public void rollbackTransaction() { public void rollbackTransaction() {
Connection conn = currentConnection.get(); Connection conn = currentConnection.get();
if (conn == null) { if (conn == null) {
throw new IllegalStateException("No connection is associated with the current transaction. " + String msg = "No connection is associated with the current transaction. This might have ideally been " +
"This might have ideally been caused by not properly initiating the " + "caused by not properly initiating the transaction via " +
"transaction via 'beginTransaction'/'openConnection' methods"); "'beginTransaction'/'openConnection' methods";
log.error(msg);
throw new IllegalStateException(msg);
} }
try { try {
conn.rollback(); conn.rollback();
} catch (SQLException e) { } 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);
} }
} }
} }

@ -1,6 +1,6 @@
package org.wso2.carbon.device.mgt.extensions.device.type.template.exception; 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) { public DeviceTypePluginExtensionException(String msg) {
super(msg); super(msg);

@ -18,6 +18,7 @@
package org.wso2.carbon.device.mgt.extensions.spi; 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.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 * 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 * 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 deviceType - Type of the device (i.e; android, ios, windows)
* @param pluginDAOManager - Device type plugin DAO manager instance to be saved against device type * @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 * Retrieve the DeviceTypePluginDAOManager instance against tenant ID and given device type
* @param deviceType - Type of the device (i.e; android, ios, windows) * @param deviceType - Type of the device (i.e; android, ios, windows)
* @return an Instance of {@link DeviceTypePluginDAOManager} * @return an Instance of {@link DeviceTypePluginDAOManager}
* @throws DeviceTypePluginExtensionException when pluginDAOManager cannot be found
*/ */
DeviceTypePluginDAOManager getPluginDAOManager(String deviceType); DeviceTypePluginDAOManager getPluginDAOManager(String deviceType) throws DeviceTypePluginExtensionException;
} }

Loading…
Cancel
Save