From 7f6b33a0c0697547c145e4e46b116a0a27a7fe7d Mon Sep 17 00:00:00 2001 From: Chatura Dilan Date: Tue, 25 Apr 2017 16:23:19 +0530 Subject: [PATCH] Fixes for Error handling --- .../ApplicationManagementServiceImpl.java | 15 +++++++++++- .../core/components/ApplicationManager.java | 5 ++-- .../impl/ApplicationManagerImpl.java | 23 ++++++------------- .../ApplicationConfigurationManager.java | 2 +- .../ApplicationManagementDAOException.java | 22 ++++-------------- .../dao/ApplicationManagementDAOImpl.java | 2 +- .../ApplicationManagerException.java | 10 +++----- .../core/util/ApplicationManagementUtil.java | 2 +- .../mgt/core/util/ConnectionManagerUtil.java | 12 +++++----- 9 files changed, 40 insertions(+), 53 deletions(-) diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementServiceImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementServiceImpl.java index a00a298eae..15ab68f938 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementServiceImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.api/src/main/java/org/wso2/carbon/device/application/mgt/api/services/impl/ApplicationManagementServiceImpl.java @@ -18,8 +18,12 @@ */ package org.wso2.carbon.device.application.mgt.api.services.impl; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.device.application.mgt.api.beans.ErrorResponse; import org.wso2.carbon.device.application.mgt.api.services.ApplicationManagementService; import org.wso2.carbon.device.application.mgt.core.components.ApplicationManager; +import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagerException; import org.wso2.carbon.device.application.mgt.core.util.ApplicationManagementUtil; import javax.ws.rs.POST; @@ -27,6 +31,8 @@ import javax.ws.rs.core.Response; public class ApplicationManagementServiceImpl implements ApplicationManagementService { + private static Log log = LogFactory.getLog(ApplicationManagementServiceImpl.class); + @POST @Override public Response createApplication(String ifModifiedSince, String name) { @@ -37,6 +43,13 @@ public class ApplicationManagementServiceImpl implements ApplicationManagementSe @Override public Response getApplications(String ifModifiedSince) { ApplicationManager applicationManager = ApplicationManagementUtil.getApplicationManager(); - return Response.ok().entity(applicationManager.getApplications().get(0).getName()).build(); + try { + return Response.ok().entity(applicationManager.getApplications()).build(); + } catch (ApplicationManagerException e) { + String msg = "Error occured while getting the application list"; + log.error(msg, e); + return Response.serverError().entity( + new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build(); + } } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/components/ApplicationManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/components/ApplicationManager.java index 7852e91d00..31d02b7521 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/components/ApplicationManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/components/ApplicationManager.java @@ -19,14 +19,15 @@ package org.wso2.carbon.device.application.mgt.core.components; import org.wso2.carbon.device.application.mgt.core.dto.Application; +import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagerException; import java.util.ArrayList; import java.util.List; public interface ApplicationManager { - public void createApplication(Application application); + public void createApplication(Application application) throws ApplicationManagerException; - public List getApplications(); + public List getApplications() throws ApplicationManagerException; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/components/impl/ApplicationManagerImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/components/impl/ApplicationManagerImpl.java index 9f2873f4f2..e48109eed0 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/components/impl/ApplicationManagerImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/components/impl/ApplicationManagerImpl.java @@ -25,6 +25,7 @@ import org.wso2.carbon.device.application.mgt.core.dao.ApplicationManagementDAO; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationManagementDAOException; import org.wso2.carbon.device.application.mgt.core.dao.ApplicationManagementDAOImpl; import org.wso2.carbon.device.application.mgt.core.dto.Application; +import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagerException; import org.wso2.carbon.device.application.mgt.core.internal.ApplicationManagementDataHolder; import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil; @@ -39,11 +40,11 @@ public class ApplicationManagerImpl implements ApplicationManager { private static ApplicationManagerImpl applicationManager = new ApplicationManagerImpl(); - private ApplicationManagerImpl(){ + private ApplicationManagerImpl() { } - public static ApplicationManagerImpl getInstance(){ + public static ApplicationManagerImpl getInstance() { return applicationManager; } @@ -54,21 +55,11 @@ public class ApplicationManagerImpl implements ApplicationManager { } @Override - public List getApplications() { - ApplicationManagementDataHolder dataHolder = ApplicationManagementDataHolder.getInstance(); - try { - ConnectionManagerUtil.openConnection(); - } catch (SQLException e) { - e.printStackTrace(); - } - ApplicationManagementDAO applicationManagementDAO = dataHolder.getApplicationManagementDAO(); + public List getApplications() throws ApplicationManagerException { + ConnectionManagerUtil.openConnection(); + ApplicationManagementDAO applicationManagementDAO = ApplicationManagementDataHolder.getInstance().getApplicationManagementDAO(); List applications = null; - try { - applications = applicationManagementDAO.getApplications(); - } catch (ApplicationManagementDAOException e) { - log.error(e); - } - + applications = applicationManagementDAO.getApplications(); ConnectionManagerUtil.closeConnection(); return applications; } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/ApplicationConfigurationManager.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/ApplicationConfigurationManager.java index d0d7af42bf..a0c6007dfd 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/ApplicationConfigurationManager.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/config/ApplicationConfigurationManager.java @@ -70,7 +70,7 @@ public class ApplicationConfigurationManager { Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); this.applicationManagerConfiguration = (ApplicationManagementConfigurations) unmarshaller.unmarshal(doc); } catch (Exception e) { - throw new ApplicationManagerException("Error occurred while initializing application config"); + throw new ApplicationManagerException("Error occurred while initializing application config", e); } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationManagementDAOException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationManagementDAOException.java index b800fb9d48..40874b026d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationManagementDAOException.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationManagementDAOException.java @@ -18,25 +18,11 @@ */ package org.wso2.carbon.device.application.mgt.core.dao; -public class ApplicationManagementDAOException extends Exception { +import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagerException; - String message; +public class ApplicationManagementDAOException extends ApplicationManagerException { - public ApplicationManagementDAOException(String message){ - this.message = message; - } - - @Override - public String getMessage() { - return message; - } - - public void setMessage(String message) { - this.message = message; - } - - @Override - public String toString() { - return super.toString(); + public ApplicationManagementDAOException(String message, Throwable throwable) { + super(message, throwable); } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationManagementDAOImpl.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationManagementDAOImpl.java index 431124565a..83c56da6e4 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationManagementDAOImpl.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/dao/ApplicationManagementDAOImpl.java @@ -81,7 +81,7 @@ public class ApplicationManagementDAOImpl implements ApplicationManagementDAO { } } catch (SQLException e) { - throw new ApplicationManagementDAOException("Error occurred while getting application List"); + throw new ApplicationManagementDAOException("Error occurred while getting application List", e); } finally { ApplicationManagementDAOUtil.cleanupResources(stmt, rs); } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ApplicationManagerException.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ApplicationManagerException.java index 63727ff05f..e937b86a5e 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ApplicationManagerException.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/exception/ApplicationManagerException.java @@ -22,8 +22,9 @@ public class ApplicationManagerException extends Exception { String message; - public ApplicationManagerException(String message){ - this.message = message; + public ApplicationManagerException(String message, Throwable throwable){ + super(message, throwable); + setMessage(message); } @Override @@ -34,9 +35,4 @@ public class ApplicationManagerException extends Exception { public void setMessage(String message) { this.message = message; } - - @Override - public String toString() { - return super.toString(); - } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationManagementUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationManagementUtil.java index 4a31cd9383..103cf97c7d 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationManagementUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ApplicationManagementUtil.java @@ -56,7 +56,7 @@ public class ApplicationManagementUtil { return docBuilder.parse(file); } catch (Exception e) { throw new ApplicationManagerException("Error occurred while parsing file, while converting " + - "to a org.w3c.dom.Document : " + e.getMessage()); + "to a org.w3c.dom.Document : ", e); } } } diff --git a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java index 6edc57547c..1bac612e71 100644 --- a/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java +++ b/components/application-mgt/org.wso2.carbon.device.application.mgt.core/src/main/java/org/wso2/carbon/device/application/mgt/core/util/ConnectionManagerUtil.java @@ -22,8 +22,8 @@ import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.application.mgt.core.config.datasource.DataSourceConfig; import org.wso2.carbon.device.application.mgt.core.config.datasource.JNDILookupDefinition; +import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagerException; import org.wso2.carbon.device.application.mgt.core.exception.IllegalTransactionStateException; -import org.wso2.carbon.device.application.mgt.core.exception.TransactionManagementException; import javax.naming.InitialContext; import javax.sql.DataSource; @@ -52,7 +52,7 @@ public class ConnectionManagerUtil { return currentConnection; } - public static void openConnection() throws SQLException { + public static void openConnection() throws ApplicationManagerException { Connection conn = currentConnection.get(); if (conn != null) { throw new IllegalTransactionStateException("A transaction is already active within the context of " + @@ -63,14 +63,14 @@ public class ConnectionManagerUtil { conn = dataSource.getConnection(); } catch (SQLException e) { currentTxState.set(TxState.CONNECTION_NOT_BORROWED); - throw e; + throw new ApplicationManagerException(e.getMessage(), e); } currentConnection.set(conn); currentTxState.set(TxState.CONNECTION_BORROWED); } - public static void beginTransaction() throws TransactionManagementException { + public static void beginTransaction() throws ApplicationManagerException { Connection conn = currentConnection.get(); if (conn != null) { throw new IllegalTransactionStateException("A transaction is already active within the context of " + @@ -80,7 +80,7 @@ public class ConnectionManagerUtil { try { conn = dataSource.getConnection(); } catch (SQLException e) { - throw new TransactionManagementException("Error occurred while retrieving a data source connection", e); + throw new ApplicationManagerException("Error occurred while retrieving a data source connection", e); } try { @@ -93,7 +93,7 @@ public class ConnectionManagerUtil { "Transaction has ended pre-maturely", e1); } currentTxState.set(TxState.CONNECTION_CLOSED); - throw new TransactionManagementException("Error occurred while setting auto-commit to false", e); + throw new ApplicationManagerException("Error occurred while setting auto-commit to false", e); } currentConnection.set(conn); currentTxState.set(TxState.CONNECTION_BORROWED);