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;
|
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.api.services.ApplicationManagementService;
|
||||||
import org.wso2.carbon.device.application.mgt.core.components.ApplicationManager;
|
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 org.wso2.carbon.device.application.mgt.core.util.ApplicationManagementUtil;
|
||||||
|
|
||||||
import javax.ws.rs.POST;
|
import javax.ws.rs.POST;
|
||||||
@ -27,6 +31,8 @@ import javax.ws.rs.core.Response;
|
|||||||
|
|
||||||
public class ApplicationManagementServiceImpl implements ApplicationManagementService {
|
public class ApplicationManagementServiceImpl implements ApplicationManagementService {
|
||||||
|
|
||||||
|
private static Log log = LogFactory.getLog(ApplicationManagementServiceImpl.class);
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
@Override
|
@Override
|
||||||
public Response createApplication(String ifModifiedSince, String name) {
|
public Response createApplication(String ifModifiedSince, String name) {
|
||||||
@ -37,6 +43,13 @@ public class ApplicationManagementServiceImpl implements ApplicationManagementSe
|
|||||||
@Override
|
@Override
|
||||||
public Response getApplications(String ifModifiedSince) {
|
public Response getApplications(String ifModifiedSince) {
|
||||||
ApplicationManager applicationManager = ApplicationManagementUtil.getApplicationManager();
|
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;
|
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.dto.Application;
|
||||||
|
import org.wso2.carbon.device.application.mgt.core.exception.ApplicationManagerException;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public interface ApplicationManager {
|
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.ApplicationManagementDAOException;
|
||||||
import org.wso2.carbon.device.application.mgt.core.dao.ApplicationManagementDAOImpl;
|
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.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.internal.ApplicationManagementDataHolder;
|
||||||
import org.wso2.carbon.device.application.mgt.core.util.ConnectionManagerUtil;
|
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 static ApplicationManagerImpl applicationManager = new ApplicationManagerImpl();
|
||||||
|
|
||||||
private ApplicationManagerImpl(){
|
private ApplicationManagerImpl() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static ApplicationManagerImpl getInstance(){
|
public static ApplicationManagerImpl getInstance() {
|
||||||
return applicationManager;
|
return applicationManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -54,21 +55,11 @@ public class ApplicationManagerImpl implements ApplicationManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Application> getApplications() {
|
public List<Application> getApplications() throws ApplicationManagerException {
|
||||||
ApplicationManagementDataHolder dataHolder = ApplicationManagementDataHolder.getInstance();
|
|
||||||
try {
|
|
||||||
ConnectionManagerUtil.openConnection();
|
ConnectionManagerUtil.openConnection();
|
||||||
} catch (SQLException e) {
|
ApplicationManagementDAO applicationManagementDAO = ApplicationManagementDataHolder.getInstance().getApplicationManagementDAO();
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
ApplicationManagementDAO applicationManagementDAO = dataHolder.getApplicationManagementDAO();
|
|
||||||
List<Application> applications = null;
|
List<Application> applications = null;
|
||||||
try {
|
|
||||||
applications = applicationManagementDAO.getApplications();
|
applications = applicationManagementDAO.getApplications();
|
||||||
} catch (ApplicationManagementDAOException e) {
|
|
||||||
log.error(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
ConnectionManagerUtil.closeConnection();
|
ConnectionManagerUtil.closeConnection();
|
||||||
return applications;
|
return applications;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -70,7 +70,7 @@ public class ApplicationConfigurationManager {
|
|||||||
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
|
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
|
||||||
this.applicationManagerConfiguration = (ApplicationManagementConfigurations) unmarshaller.unmarshal(doc);
|
this.applicationManagerConfiguration = (ApplicationManagementConfigurations) unmarshaller.unmarshal(doc);
|
||||||
} catch (Exception e) {
|
} 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;
|
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){
|
public ApplicationManagementDAOException(String message, Throwable throwable) {
|
||||||
this.message = message;
|
super(message, throwable);
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String getMessage() {
|
|
||||||
return message;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setMessage(String message) {
|
|
||||||
this.message = message;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return super.toString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -81,7 +81,7 @@ public class ApplicationManagementDAOImpl implements ApplicationManagementDAO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new ApplicationManagementDAOException("Error occurred while getting application List");
|
throw new ApplicationManagementDAOException("Error occurred while getting application List", e);
|
||||||
} finally {
|
} finally {
|
||||||
ApplicationManagementDAOUtil.cleanupResources(stmt, rs);
|
ApplicationManagementDAOUtil.cleanupResources(stmt, rs);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -22,8 +22,9 @@ public class ApplicationManagerException extends Exception {
|
|||||||
|
|
||||||
String message;
|
String message;
|
||||||
|
|
||||||
public ApplicationManagerException(String message){
|
public ApplicationManagerException(String message, Throwable throwable){
|
||||||
this.message = message;
|
super(message, throwable);
|
||||||
|
setMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -34,9 +35,4 @@ public class ApplicationManagerException extends Exception {
|
|||||||
public void setMessage(String message) {
|
public void setMessage(String message) {
|
||||||
this.message = message;
|
this.message = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString() {
|
|
||||||
return super.toString();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -56,7 +56,7 @@ public class ApplicationManagementUtil {
|
|||||||
return docBuilder.parse(file);
|
return docBuilder.parse(file);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new ApplicationManagerException("Error occurred while parsing file, while converting " +
|
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.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.DataSourceConfig;
|
||||||
import org.wso2.carbon.device.application.mgt.core.config.datasource.JNDILookupDefinition;
|
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.IllegalTransactionStateException;
|
||||||
import org.wso2.carbon.device.application.mgt.core.exception.TransactionManagementException;
|
|
||||||
|
|
||||||
import javax.naming.InitialContext;
|
import javax.naming.InitialContext;
|
||||||
import javax.sql.DataSource;
|
import javax.sql.DataSource;
|
||||||
@ -52,7 +52,7 @@ public class ConnectionManagerUtil {
|
|||||||
return currentConnection;
|
return currentConnection;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void openConnection() throws SQLException {
|
public static void openConnection() throws ApplicationManagerException {
|
||||||
Connection conn = currentConnection.get();
|
Connection conn = currentConnection.get();
|
||||||
if (conn != null) {
|
if (conn != null) {
|
||||||
throw new IllegalTransactionStateException("A transaction is already active within the context of " +
|
throw new IllegalTransactionStateException("A transaction is already active within the context of " +
|
||||||
@ -63,14 +63,14 @@ public class ConnectionManagerUtil {
|
|||||||
conn = dataSource.getConnection();
|
conn = dataSource.getConnection();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
currentTxState.set(TxState.CONNECTION_NOT_BORROWED);
|
currentTxState.set(TxState.CONNECTION_NOT_BORROWED);
|
||||||
throw e;
|
throw new ApplicationManagerException(e.getMessage(), e);
|
||||||
}
|
}
|
||||||
currentConnection.set(conn);
|
currentConnection.set(conn);
|
||||||
currentTxState.set(TxState.CONNECTION_BORROWED);
|
currentTxState.set(TxState.CONNECTION_BORROWED);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public static void beginTransaction() throws TransactionManagementException {
|
public static void beginTransaction() throws ApplicationManagerException {
|
||||||
Connection conn = currentConnection.get();
|
Connection conn = currentConnection.get();
|
||||||
if (conn != null) {
|
if (conn != null) {
|
||||||
throw new IllegalTransactionStateException("A transaction is already active within the context of " +
|
throw new IllegalTransactionStateException("A transaction is already active within the context of " +
|
||||||
@ -80,7 +80,7 @@ public class ConnectionManagerUtil {
|
|||||||
try {
|
try {
|
||||||
conn = dataSource.getConnection();
|
conn = dataSource.getConnection();
|
||||||
} catch (SQLException e) {
|
} 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 {
|
try {
|
||||||
@ -93,7 +93,7 @@ public class ConnectionManagerUtil {
|
|||||||
"Transaction has ended pre-maturely", e1);
|
"Transaction has ended pre-maturely", e1);
|
||||||
}
|
}
|
||||||
currentTxState.set(TxState.CONNECTION_CLOSED);
|
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);
|
currentConnection.set(conn);
|
||||||
currentTxState.set(TxState.CONNECTION_BORROWED);
|
currentTxState.set(TxState.CONNECTION_BORROWED);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user