From 31bb87db41b5c8a9b5405c5bc1be5623c338292e Mon Sep 17 00:00:00 2001 From: shamalka Date: Mon, 29 Aug 2022 17:07:49 +0530 Subject: [PATCH] Add missing error logs and fix formatting issues --- .../impl/DeviceManagementServiceImpl.java | 44 ++++++++----- .../core/dao/impl/tracker/TrackerDAOImpl.java | 62 +++++++++++++------ .../dao/util/TrackerManagementDAOUtil.java | 6 +- .../impl/DeviceInformationManagerImpl.java | 2 +- .../DeviceManagementProviderServiceImpl.java | 9 ++- .../impl/DeviceAPIClientServiceImpl.java | 1 + 6 files changed, 81 insertions(+), 43 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 9fea7b108c..962373be34 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -132,7 +132,15 @@ import org.wso2.carbon.utils.multitenancy.MultitenantUtils; import javax.validation.Valid; import javax.validation.constraints.Size; -import javax.ws.rs.*; +import javax.ws.rs.DELETE; +import javax.ws.rs.DefaultValue; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.Response; import java.sql.Timestamp; import java.text.ParseException; @@ -485,9 +493,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { String currentUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); JSONObject obj = new JSONObject(DeviceAPIClientServiceImpl.returnUser(currentUser)); - if(obj.has("error")){ + if (obj.has("error")) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(obj.getString("error")).build(); - }else{ + } else { int userId = obj.getInt("id"); List traccarValidIdList = new ArrayList<>(); /*Get Device Id List*/ @@ -504,8 +512,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { status.add("CREATED"); status.add("UNREACHABLE"); boolean isStatusEmpty = true; - for (String statusString : status){ - if (StringUtils.isNotBlank(statusString)){ + for (String statusString : status) { + if (StringUtils.isNotBlank(statusString)) { isStatusEmpty = false; break; } @@ -525,16 +533,15 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { if (result == null || result.getData() == null || result.getData().isEmpty()) { devices.setList(new ArrayList()); devices.setCount(0); - }else{ + } else { devices.setList((List) result.getData()); devices.setCount(result.getRecordsTotal()); } int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - for(int i=0; i getAllUserDevices = DeviceAPIClientServiceImpl.getUserIdofPermissionByUserIdNIdList(userId, traccarValidIdList); @@ -557,29 +563,35 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { getAllUserDevice.getTraccarDeviceId(), TraccarHandlerConstants.Types.REMOVE_TYPE_SINGLE); } - - } catch (JSONException e){ + } catch (JSONException e) { String msg = "not a JSONObject, "; log.error(msg); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } catch (DeviceManagementException e) { String msg = "Error occurred while fetching all enrolled devices"; log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } catch (DeviceAccessAuthorizationException e) { String msg = "Error occurred while checking device access authorization"; log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } catch (TrackerManagementDAOException e) { String msg = "Error occurred while mapping with deviceId ."; log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } catch (ExecutionException e) { - log.error("ExecutionException : " + e); + String msg = "ExecutionException occured "; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } catch (InterruptedException e) { - log.error("InterruptedException : " + e); + String msg = "InterruptedException occured "; + log.error(msg, e); + return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } /*Get Device Id List*/ - return Response.status(Response.Status.OK).entity(obj.getString("token")).build(); } - }else{ + } else { return Response.status(Response.Status.BAD_REQUEST).entity("Traccar is not enabled").build(); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/tracker/TrackerDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/tracker/TrackerDAOImpl.java index 0be2d2a798..d98b630de3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/tracker/TrackerDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/tracker/TrackerDAOImpl.java @@ -18,6 +18,8 @@ package org.wso2.carbon.device.mgt.core.dao.impl.tracker; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.TrackerDeviceInfo; import org.wso2.carbon.device.mgt.common.TrackerGroupInfo; import org.wso2.carbon.device.mgt.common.TrackerPermissionInfo; @@ -36,6 +38,8 @@ import java.util.List; public class TrackerDAOImpl implements TrackerDAO { + private static final Log log = LogFactory.getLog(TrackerDAOImpl.class); + @Override public Boolean addTrackerDevice(int traccarDeviceId, int deviceId, int tenantId) throws TrackerManagementDAOException { @@ -51,7 +55,9 @@ public class TrackerDAOImpl implements TrackerDAO { return true; } catch (SQLException e) { - throw new TrackerManagementDAOException("Error occurred while adding on trackerDevice mapping table", e); + String msg = "Error occurred while adding on trackerDevice mapping table"; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); } finally { TrackerManagementDAOUtil.cleanupResources(stmt, null); } @@ -74,6 +80,7 @@ public class TrackerDAOImpl implements TrackerDAO { return true; } catch (SQLException e) { String msg = "Error occurred while updating status on trackerDevice mapping table"; + log.error(msg, e); throw new TrackerManagementDAOException(msg, e); } finally { TrackerManagementDAOUtil.cleanupResources(stmt, null); @@ -88,7 +95,7 @@ public class TrackerDAOImpl implements TrackerDAO { try { Connection conn = TrackerManagementDAOFactory.getConnection(); String sql = "DELETE FROM DM_EXT_DEVICE_MAPPING WHERE DEVICE_ID = ? AND TENANT_ID = ? "; - stmt = conn.prepareStatement(sql, new String[] {"id"}); + stmt = conn.prepareStatement(sql, new String[]{"id"}); stmt.setInt(1, deviceId); stmt.setInt(2, tenantId); stmt.executeUpdate(); @@ -98,7 +105,9 @@ public class TrackerDAOImpl implements TrackerDAO { } return status; } catch (SQLException e) { - throw new TrackerManagementDAOException("Error occurred while removing on trackerDevice table", e); + String msg = "Error occurred while removing on trackerDevice table"; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); } finally { TrackerManagementDAOUtil.cleanupResources(stmt, null); } @@ -122,7 +131,9 @@ public class TrackerDAOImpl implements TrackerDAO { } return trackerDeviceInfo; } catch (SQLException e) { - throw new TrackerManagementDAOException("Error occurred while retrieving data from the trackerDevice table ", e); + String msg = "Error occurred while retrieving data from the trackerDevice table "; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); } finally { TrackerManagementDAOUtil.cleanupResources(stmt, rs); } @@ -132,7 +143,7 @@ public class TrackerDAOImpl implements TrackerDAO { public Boolean addTrackerGroup(int traccarGroupId, int groupId, int tenantId) throws TrackerManagementDAOException { PreparedStatement stmt = null; - int status = 1 ; + int status = 1; try { Connection conn = TrackerManagementDAOFactory.getConnection(); String sql = "INSERT INTO DM_EXT_GROUP_MAPPING(TRACCAR_GROUP_ID, GROUP_ID, TENANT_ID, STATUS) VALUES(?, ?, ?, ?)"; @@ -146,6 +157,7 @@ public class TrackerDAOImpl implements TrackerDAO { return true; } catch (SQLException e) { String msg = "Error occurred while adding on traccarGroup mapping table"; + log.error(msg, e); throw new TrackerManagementDAOException(msg, e); } finally { TrackerManagementDAOUtil.cleanupResources(stmt, null); @@ -169,6 +181,7 @@ public class TrackerDAOImpl implements TrackerDAO { return true; } catch (SQLException e) { String msg = "Error occurred while updating status on traccarGroup mapping table"; + log.error(msg, e); throw new TrackerManagementDAOException(msg, e); } finally { TrackerManagementDAOUtil.cleanupResources(stmt, null); @@ -183,7 +196,7 @@ public class TrackerDAOImpl implements TrackerDAO { try { Connection conn = TrackerManagementDAOFactory.getConnection(); String sql = "DELETE FROM DM_EXT_GROUP_MAPPING WHERE ID = ? "; - stmt = conn.prepareStatement(sql, new String[] {"id"}); + stmt = conn.prepareStatement(sql, new String[]{"id"}); stmt.setInt(1, id); stmt.executeUpdate(); rs = stmt.getGeneratedKeys(); @@ -192,7 +205,9 @@ public class TrackerDAOImpl implements TrackerDAO { } return status; } catch (SQLException e) { - throw new TrackerManagementDAOException("Error occurred while removing from traccarGroup mapping table", e); + String msg = "Error occurred while removing from traccarGroup mapping table"; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); } finally { TrackerManagementDAOUtil.cleanupResources(stmt, null); } @@ -216,7 +231,9 @@ public class TrackerDAOImpl implements TrackerDAO { } return trackerGroupInfo; } catch (SQLException e) { - throw new TrackerManagementDAOException("Error occurred while retrieving data from the traccarGroup mapping table ", e); + String msg = "Error occurred while retrieving data from the traccarGroup mapping table "; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); } finally { TrackerManagementDAOUtil.cleanupResources(stmt, rs); } @@ -236,7 +253,9 @@ public class TrackerDAOImpl implements TrackerDAO { return true; } catch (SQLException e) { - throw new TrackerManagementDAOException("Error occurred while adding permission on permissions mapping table", e); + String msg = "Error occurred while adding permission on permissions mapping table"; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); } finally { TrackerManagementDAOUtil.cleanupResources(stmt, null); } @@ -249,19 +268,20 @@ public class TrackerDAOImpl implements TrackerDAO { try { Connection conn = TrackerManagementDAOFactory.getConnection(); String sql = "DELETE FROM DM_EXT_PERMISSION_MAPPING WHERE TRACCAR_DEVICE_ID = ?"; - if(removeType != TraccarHandlerConstants.Types.REMOVE_TYPE_MULTIPLE){ + if (removeType != TraccarHandlerConstants.Types.REMOVE_TYPE_MULTIPLE) { sql = sql + " AND TRACCAR_USER_ID = ? "; } stmt = conn.prepareStatement(sql); stmt.setInt(1, deviceId); - if(removeType != TraccarHandlerConstants.Types.REMOVE_TYPE_MULTIPLE){ + if (removeType != TraccarHandlerConstants.Types.REMOVE_TYPE_MULTIPLE) { stmt.setInt(2, userId); } stmt.execute(); - return true; } catch (SQLException e) { - throw new TrackerManagementDAOException("Error occurred while removing permission from permissions mapping table", e); + String msg = "Error occurred while removing permission from permissions mapping table"; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); } finally { TrackerManagementDAOUtil.cleanupResources(stmt, null); } @@ -287,7 +307,9 @@ public class TrackerDAOImpl implements TrackerDAO { } return trackerPermissionInfo; } catch (SQLException e) { - throw new TrackerManagementDAOException("Error occurred while retrieving permissions data from permissions mapping table ", e); + String msg = "Error occurred while retrieving permissions data from permissions mapping table "; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); } finally { TrackerManagementDAOUtil.cleanupResources(stmt, rs); } @@ -303,7 +325,7 @@ public class TrackerDAOImpl implements TrackerDAO { Connection conn = TrackerManagementDAOFactory.getConnection(); String sql = "SELECT TRACCAR_DEVICE_ID, TRACCAR_USER_ID FROM DM_EXT_PERMISSION_MAPPING WHERE " + "TRACCAR_USER_ID = ? "; - if(NotInDeviceIdList!=null && (!NotInDeviceIdList.isEmpty())){ + if (NotInDeviceIdList != null && (!NotInDeviceIdList.isEmpty())) { sql += TrackerManagementDAOUtil.buildDeviceIdNotInQuery(NotInDeviceIdList); } sql += " ORDER BY TRACCAR_USER_ID ASC"; @@ -311,8 +333,8 @@ public class TrackerDAOImpl implements TrackerDAO { stmt = conn.prepareStatement(sql); int paramIdx = 1; stmt.setInt(paramIdx++, userId); - if(NotInDeviceIdList!=null && (!NotInDeviceIdList.isEmpty())){ - for (int id : NotInDeviceIdList) { + if (NotInDeviceIdList != null && (!NotInDeviceIdList.isEmpty())) { + for (int id : NotInDeviceIdList) { stmt.setInt(paramIdx++, id); } } @@ -325,6 +347,7 @@ public class TrackerDAOImpl implements TrackerDAO { return trackerPermissionInfo; } catch (SQLException e) { String msg = "Error occurred while retrieving data from the permissions mapping table "; + log.error(msg, e); throw new TrackerManagementDAOException(msg, e); } finally { TrackerManagementDAOUtil.cleanupResources(stmt, rs); @@ -335,7 +358,6 @@ public class TrackerDAOImpl implements TrackerDAO { public Boolean getUserIdofPermissionByDeviceIdNUserId(int deviceId, int userId) throws TrackerManagementDAOException { PreparedStatement stmt = null; ResultSet rs = null; - TrackerPermissionInfo trackerPermissionInfo = null; try { Connection conn = TrackerManagementDAOFactory.getConnection(); String sql = "SELECT TRACCAR_DEVICE_ID, TRACCAR_USER_ID FROM DM_EXT_PERMISSION_MAPPING WHERE " + @@ -347,7 +369,9 @@ public class TrackerDAOImpl implements TrackerDAO { rs = stmt.executeQuery(); return rs.next(); } catch (SQLException e) { - throw new TrackerManagementDAOException("Error occurred while retrieving permissions data from permissions mapping table ", e); + String msg = "Error occurred while retrieving permissions data from permissions mapping table "; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); } finally { TrackerManagementDAOUtil.cleanupResources(stmt, rs); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/TrackerManagementDAOUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/TrackerManagementDAOUtil.java index 117b994762..fb06d34d01 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/TrackerManagementDAOUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/util/TrackerManagementDAOUtil.java @@ -95,7 +95,9 @@ public final class TrackerManagementDAOUtil { final InitialContext context = new InitialContext(jndiProperties); return (DataSource) context.lookup(dataSourceName); } catch (Exception e) { - throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e); + String msg = "Error in looking up data source: " + e.getMessage(); + log.error(msg, e); + throw new RuntimeException(msg, e); } } @@ -129,11 +131,11 @@ public final class TrackerManagementDAOUtil { public static String buildDeviceIdNotInQuery(List DeviceIdList) throws TrackerManagementDAOException { if (DeviceIdList == null || DeviceIdList.isEmpty()) { String msg = "SQL query build for Device Id list failed. Device Id list cannot be empty or null"; + log.error(msg); throw new TrackerManagementDAOException(msg); } StringJoiner joiner = new StringJoiner(",", " AND TRACCAR_DEVICE_ID NOT IN(", ")"); DeviceIdList.stream().map(status -> "?").forEach(joiner::add); - return joiner.toString(); } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index a5bc8b4096..553d1c3fa5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -399,7 +399,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { log.error("InterruptedException : " + e); //throw new RuntimeException(e); } - }else{ + } else { log.info("location publishing is disabled and traccan disabled"); } //Tracker update GPS Location diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 53cab62fc0..f4b991c711 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -428,9 +428,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } catch (ExecutionException e) { log.error("ExecutionException : " + e); //throw new RuntimeException(e); + //Exception was not thrown due to being conflicted with non-traccar features } catch (InterruptedException e) { log.error("InterruptedException : " + e); //throw new RuntimeException(e); + //Exception was not thrown due to being conflicted with non-traccar features } } //enroll Traccar device @@ -614,10 +616,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv String msg = "Error occurred while initiating transaction"; log.error(msg, e); throw new DeviceManagementException(msg, e); - } catch (Exception e) { - String msg = "Error occurred while dis-enrolling device: " + deviceId.getId(); - log.error(msg, e); - throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -3969,9 +3967,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv deviceLocation.setSpeed(Float.parseFloat(speed)); deviceLocation.setBearing(Float.parseFloat(bearing)); deviceInformationManager.addDeviceLocation(device, deviceLocation); - } catch (Exception e) { + } catch (DeviceDetailsMgtException e) { //We are not failing the execution since this is not critical for the functionality. But logging as // a warning for reference. + //Exception was not thrown due to being conflicted with non-traccar features log.warn("Error occurred while trying to add '" + device.getType() + "' device '" + device.getDeviceIdentifier() + "' (id:'" + device.getId() + "') location (lat:" + latitude + ", lon:" + longitude + ", altitude: " + altitude + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/DeviceAPIClientServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/DeviceAPIClientServiceImpl.java index a8d4ee85ce..9559ac262c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/DeviceAPIClientServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/DeviceAPIClientServiceImpl.java @@ -26,6 +26,7 @@ import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.TrackerDeviceInfo; import org.wso2.carbon.device.mgt.common.TrackerPermissionInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; +import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.exceptions.TrackerAlreadyExistException; import org.wso2.carbon.device.mgt.core.dao.TrackerManagementDAOException;