mirror of
https://repository.entgra.net/community/device-mgt-core.git
synced 2025-10-06 02:01:45 +00:00
Fixes for Error handling
This commit is contained in:
parent
29bd1283d0
commit
ec78984cca
@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<Application> getApplications();
|
||||
public List<Application> getApplications() throws ApplicationManagerException;
|
||||
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -54,21 +55,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Application> getApplications() {
|
||||
ApplicationManagementDataHolder dataHolder = ApplicationManagementDataHolder.getInstance();
|
||||
try {
|
||||
public List<Application> getApplications() throws ApplicationManagerException {
|
||||
ConnectionManagerUtil.openConnection();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
ApplicationManagementDAO applicationManagementDAO = dataHolder.getApplicationManagementDAO();
|
||||
ApplicationManagementDAO applicationManagementDAO = ApplicationManagementDataHolder.getInstance().getApplicationManagementDAO();
|
||||
List<Application> applications = null;
|
||||
try {
|
||||
applications = applicationManagementDAO.getApplications();
|
||||
} catch (ApplicationManagementDAOException e) {
|
||||
log.error(e);
|
||||
}
|
||||
|
||||
ConnectionManagerUtil.closeConnection();
|
||||
return applications;
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user