From 6628d97745973282b98d9b40f09ba9c4193f4d9c Mon Sep 17 00:00:00 2001 From: GDLMadushanka Date: Fri, 4 Aug 2017 18:19:51 +0530 Subject: [PATCH 1/5] Fix for 1261 --- .pom.xml.swp | Bin 0 -> 16384 bytes .../jaggeryapps/devicemgt/api/device-api.jag | 1 + .../oauth/token-protected-service-invokers.js | 8 +++++--- 3 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 .pom.xml.swp diff --git a/.pom.xml.swp b/.pom.xml.swp new file mode 100644 index 0000000000000000000000000000000000000000..f89e01a40fab8e125782d12895c2d487b7de7fcc GIT binary patch literal 16384 zcmeHNON=8&8E%#&FhEE|Bm@!?wGJpVV!GO%nPjs|uY;N0C01s}!+3T#D|Qsx?XstL z?Cz$k+a3d16cOg1 zx4WGYFZDwypcHsz6u4EJpDV>%{m#Nsa>tv_zA|A|6s3StKq;UUPzopolmbctrGQf4 zP$&@kZ`D4Ao*t3CJtx0kmHWOfKTGoZ`5gaW$v+^ke!$2L z+K+$b_)q8fe@Z^BXr9UOFXq~RmmF{!{~wZnzifYyS5-5%T z*IfJE9RGsk)Bb-y$Nxq0ss6X$G#dZUl27O5VvhflTVe zR`Pd9{;?b{FRL%5fKosypcGIFC`xfA7)XI+mj{;TT z2yg}U`453l0Pg|b4qV5Xz>k5;zy@#s{E*K<6gnM#xR+7#))B>9N7xhHuIIkI;m{-=!o7_BG2A{4BSa6-EK$M$&Wf3YyNg zL*@#dvA(6*u-UxdrGBW}9S5DAz?gL{r=xSBKrn&1XGP)WjdjOnBKteG6~?dAP0&c6 zH7wuPMN=4r@bm(`6R@}X&niwW&w7s>eXi>_o`j?>9%tcPjD=80#T!{=#{ z-c!`;E0xvy{k8R+J{Xm~Wwjl*UB-K}LfG_Y5+(B|Z{$tec&}#2#QH{)x|mniIVbN) z3+QPvp7`N4O*8$#y98kg-syGu$$}KCYxS5*E%bMjQHW-&)|QPpLLsp{vCW+w>wdpa z`}48gT(MX(9$a3k!?A1Wj>|*KwHds)bArbgmptS!Oze*P-i?vHx)ZzTy@iL04;AS) zZx-UUD{er9Tqcpo-vs%REaA0fmopk)=03AU?j;?|ZAZ9;k+z3$Bv1b^ZR@*>!ThvbI)PS+Ab1liC_ttgW1?u2*X-_;rRd%a1f2 z9tKV$5?f%`_=v+t3_KTui2cvHxTkUwun$E}0N2KV;BJB&g4O6S2+cJwLi$iRYn9BLT*CxykZCcm1v#;WnRkk)Gnn(;21G zY%@bDoeo(OKAfyE)OS6s7ESz}(#-p?>4?lECkrK7ED-$j@|uwUWFZP$`n`qw%9^PE zb=2fv07U(tKL7j<>it!q29$uSsPTUT{1o^MFb}*L_%-VLuL2Bs8}KaRJOO+i_!=PM zejZRCrGQdEDWDWk3Md7X0!jg;fKosyFjfJaWh4g&!$YIMLlGVBoc0oY%%rrxWc943 zXH&lA_~kH&@MvWWq2ZWJSfUE&A(drOd7hT$XoX?Z-QI!9h_IJ4&ZIz;%o@P>-#EisdX^}npghD(VktZ5`PC2t99DNLu2eRq%!|c&gDZbV#VIn8-!Ldg2 dF*DYD;95RTqSC-(VI0%NF+uOxNZ7og_CI7u%0&PG literal 0 HcmV?d00001 diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag index e49a8afba2..84a9b98bdc 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/api/device-api.jag @@ -45,6 +45,7 @@ if (!user) { } else { queryString = "?" + queryString; } + var deviceType = request.getParameter("deviceType"); // need a better solution here deviceTypeConfig = utility.getDeviceTypeConfig(deviceType); if (deviceTypeConfig && deviceTypeConfig.deviceType.downloadAgentUri) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-protected-service-invokers.js b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-protected-service-invokers.js index efcb3c6cae..cfde7134b6 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-protected-service-invokers.js +++ b/components/device-mgt/org.wso2.carbon.device.mgt.ui/src/main/resources/jaggeryapps/devicemgt/app/modules/oauth/token-protected-service-invokers.js @@ -39,7 +39,7 @@ var invokers = function () { var constants = require("/app/modules/constants.js"); var userModule = require("/app/modules/business-controllers/user.js")["userModule"]; var tokenUtil = require("/app/modules/oauth/token-handlers.js")["handlers"]; - + var tokenHandler = require("/app/modules/oauth/token-handler-utils.js")["utils"]; /** * This method reads the token pair from the session and return the access token. * If the token pair is not set in the session, this will return null. @@ -259,10 +259,12 @@ var invokers = function () { var wsRequest = new ws.WSRequest(); var options = []; if (devicemgtProps["isOAuthEnabled"]) { - var accessToken = privateMethods.getAccessToken(); + var adminUsername = devicemgtProps["adminUser"]; + var accessToken = tokenHandler.getJwtToken(adminUsername); + var decoded = tokenHandler.encode(accessToken); if (accessToken) { var authenticationHeaderName = String(constants["AUTHORIZATION_HEADER"]); - var authenticationHeaderValue = String(constants["BEARER_PREFIX"] + accessToken); + var authenticationHeaderValue = String(constants["BEARER_PREFIX"] + decoded); var headers = []; var oAuthAuthenticationData = {}; oAuthAuthenticationData.name = authenticationHeaderName; From d7220d39afe1697f532596f9084604fd2f79f2fd Mon Sep 17 00:00:00 2001 From: GDLMadushanka Date: Tue, 29 Aug 2017 14:12:17 +0530 Subject: [PATCH 2/5] logging errors in devmgt service and groupmgt service --- .pom.xml.swp | Bin 16384 -> 0 bytes .../DeviceManagementProviderServiceImpl.java | 716 ++++++++++++++---- .../GroupManagementProviderServiceImpl.java | 412 ++++++++-- 3 files changed, 911 insertions(+), 217 deletions(-) delete mode 100644 .pom.xml.swp diff --git a/.pom.xml.swp b/.pom.xml.swp deleted file mode 100644 index f89e01a40fab8e125782d12895c2d487b7de7fcc..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 16384 zcmeHNON=8&8E%#&FhEE|Bm@!?wGJpVV!GO%nPjs|uY;N0C01s}!+3T#D|Qsx?XstL z?Cz$k+a3d16cOg1 zx4WGYFZDwypcHsz6u4EJpDV>%{m#Nsa>tv_zA|A|6s3StKq;UUPzopolmbctrGQf4 zP$&@kZ`D4Ao*t3CJtx0kmHWOfKTGoZ`5gaW$v+^ke!$2L z+K+$b_)q8fe@Z^BXr9UOFXq~RmmF{!{~wZnzifYyS5-5%T z*IfJE9RGsk)Bb-y$Nxq0ss6X$G#dZUl27O5VvhflTVe zR`Pd9{;?b{FRL%5fKosypcGIFC`xfA7)XI+mj{;TT z2yg}U`453l0Pg|b4qV5Xz>k5;zy@#s{E*K<6gnM#xR+7#))B>9N7xhHuIIkI;m{-=!o7_BG2A{4BSa6-EK$M$&Wf3YyNg zL*@#dvA(6*u-UxdrGBW}9S5DAz?gL{r=xSBKrn&1XGP)WjdjOnBKteG6~?dAP0&c6 zH7wuPMN=4r@bm(`6R@}X&niwW&w7s>eXi>_o`j?>9%tcPjD=80#T!{=#{ z-c!`;E0xvy{k8R+J{Xm~Wwjl*UB-K}LfG_Y5+(B|Z{$tec&}#2#QH{)x|mniIVbN) z3+QPvp7`N4O*8$#y98kg-syGu$$}KCYxS5*E%bMjQHW-&)|QPpLLsp{vCW+w>wdpa z`}48gT(MX(9$a3k!?A1Wj>|*KwHds)bArbgmptS!Oze*P-i?vHx)ZzTy@iL04;AS) zZx-UUD{er9Tqcpo-vs%REaA0fmopk)=03AU?j;?|ZAZ9;k+z3$Bv1b^ZR@*>!ThvbI)PS+Ab1liC_ttgW1?u2*X-_;rRd%a1f2 z9tKV$5?f%`_=v+t3_KTui2cvHxTkUwun$E}0N2KV;BJB&g4O6S2+cJwLi$iRYn9BLT*CxykZCcm1v#;WnRkk)Gnn(;21G zY%@bDoeo(OKAfyE)OS6s7ESz}(#-p?>4?lECkrK7ED-$j@|uwUWFZP$`n`qw%9^PE zb=2fv07U(tKL7j<>it!q29$uSsPTUT{1o^MFb}*L_%-VLuL2Bs8}KaRJOO+i_!=PM zejZRCrGQdEDWDWk3Md7X0!jg;fKosyFjfJaWh4g&!$YIMLlGVBoc0oY%%rrxWc943 zXH&lA_~kH&@MvWWq2ZWJSfUE&A(drOd7hT$XoX?Z-QI!9h_IJ4&ZIz;%o@P>-#EisdX^}npghD(VktZ5`PC2t99DNLu2eRq%!|c&gDZbV#VIn8-!Ldg2 dF*DYD;95RTqSC-(VI0%NF+uOxNZ7og_CI7u%0&PG 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 1e09960283..488818d55f 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 @@ -160,6 +160,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean enrollDevice(Device device) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Enrolling the device " + device.getId() + "of type " + device.getType()); + } boolean status = false; DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); @@ -228,9 +231,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - throw new DeviceManagementException("Error occurred while adding enrolment related metadata", e); + String msg = "Error occurred while adding enrolment related metadata"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (TransactionManagementException e) { - throw new DeviceManagementException("Error occurred while initiating transaction", e); + String msg = "Error occurred while initiating transaction"; + log.error(msg); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -246,10 +257,18 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - throw new DeviceManagementException("Error occurred while adding metadata of '" + device.getType() + - "' device carrying the identifier '" + device.getDeviceIdentifier() + "'", e); + String msg = "Error occurred while adding metadata of '" + device.getType() + + "' device carrying the identifier '" + device.getDeviceIdentifier() + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (TransactionManagementException e) { - throw new DeviceManagementException("Error occurred while initiating transaction", e); + String msg = "Error occurred while initiating transaction"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -266,7 +285,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv if (status) { addDeviceToGroups(deviceIdentifier, device.getEnrolmentInfo().getOwnership()); addInitialOperations(deviceIdentifier, device.getType()); - } return status; } @@ -274,6 +292,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean modifyEnrollment(Device device) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Modifying enrollment for device " + device.getDeviceIdentifier() + " of type " + device.getType()); + } DeviceManager deviceManager = this.getDeviceManager(device.getType()); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); if (deviceManager == null) { @@ -301,10 +322,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv this.removeDeviceFromCache(deviceIdentifier); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - throw new DeviceManagementException("Error occurred while modifying the device " + - "'" + device.getId() + "'", e); + String msg = "Error occurred while modifying the device '" + device.getId() + "'"; + log.error(msg); + throw new DeviceManagementException(msg, e); } catch (TransactionManagementException e) { - throw new DeviceManagementException("Error occurred while initiating transaction", e); + String msg = "Error occurred while initiating transaction"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -318,10 +346,18 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.openConnection(); enrolmentInfos = enrollmentDAO.getEnrollmentsOfUser(deviceId, user, this.getTenantId()); } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while obtaining the enrollment information device for" + - "id '" + deviceId + "' and user : " + user, e); + String msg = "Error occurred while obtaining the enrollment information device for id '" + deviceId + + "' and user : " + user; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -330,6 +366,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Dis-enrolling device " + deviceId.getId() + " of type " + deviceId.getType()); + } DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); if (deviceManager == null) { if (log.isDebugEnabled()) { @@ -362,15 +401,22 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.beginTransaction(); enrollmentDAO.updateEnrollment(device.getId(), device.getEnrolmentInfo(), tenantId); deviceDAO.updateDevice(device, tenantId); - DeviceManagementDAOFactory.commitTransaction(); this.removeDeviceFromCache(deviceId); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - throw new DeviceManagementException("Error occurred while dis-enrolling '" + deviceId.getType() + - "' device with the identifier '" + deviceId.getId() + "'", e); + String msg = "Error occurred while dis-enrolling '" + deviceId.getType() + + "' device with the identifier '" + deviceId.getId() + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (TransactionManagementException e) { - throw new DeviceManagementException("Error occurred while initiating transaction", e); + String msg = "Error occurred while initiating transaction"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -419,6 +465,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getAllDevices(String deviceType, boolean requireDeviceInfo) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Getting all devices of type " + deviceType); + } List allDevices; try { DeviceManagementDAOFactory.openConnection(); @@ -430,14 +479,21 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return null; } } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while retrieving all devices of type '" + - deviceType + "' that are being managed within the scope of current tenant", e); + String msg = "Error occurred while retrieving all devices of type '" + + deviceType + "' that are being managed within the scope of current tenant"; + log.error(msg); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } - if (requireDeviceInfo) { return this.getAllDeviceInfo(allDevices); } @@ -451,15 +507,25 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getAllDevices(boolean requireDeviceInfo) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Getting all devices"); + } List allDevices; try { DeviceManagementDAOFactory.openConnection(); allDevices = deviceDAO.getDevices(this.getTenantId()); } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " + - "the current tenant", e); + String msg = "Error occurred while retrieving device list pertaining to the current tenant"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -477,15 +543,25 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getDevices(Date since, boolean requireDeviceInfo) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Getting all devices since date " + since.toString()); + } List allDevices; try { DeviceManagementDAOFactory.openConnection(); allDevices = deviceDAO.getDevices(since.getTime(), this.getTenantId()); } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " + - "the current tenant", e); + String msg = "Error occurred while retrieving device list pertaining to the current tenant"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -503,6 +579,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public PaginationResult getDevicesByType(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get devices with pagination"); + } PaginationResult paginationResult = new PaginationResult(); List allDevices = new ArrayList<>(); int count = 0; @@ -514,10 +593,18 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv allDevices = deviceDAO.getDevices(request, tenantId); count = deviceDAO.getDeviceCountByType(deviceType, tenantId); } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " + - "the current tenant of type " + deviceType, e); + String msg = "Error occurred while retrieving device list pertaining to the current tenant of type " + + deviceType; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -540,6 +627,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public PaginationResult getAllDevices(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get devices with pagination"); + } List devicesForRoles = null; PaginationResult paginationResult = new PaginationResult(); List allDevices = new ArrayList<>(); @@ -560,10 +650,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv allDevices = deviceDAO.getDevices(request, tenantId); count = deviceDAO.getDeviceCount(request, tenantId); } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while retrieving device list pertaining to " + - "the current tenant", e); + String msg = "Error occurred while retrieving device list pertaining to the current tenant"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -580,6 +677,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public Device getDevice(DeviceIdentifier deviceId, boolean requireDeviceInfo) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get device by device id " + deviceId.getId() + " of type " + deviceId.getType()); + } int tenantId = this.getTenantId(); Device device = this.getDeviceFromCache(deviceId); if (device == null) { @@ -596,10 +696,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } this.addDeviceToCache(deviceId, device); } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while obtaining the device for id " + - "'" + deviceId.getId() + "'", e); + String msg = "Error occurred while obtaining the device for '" + deviceId.getId() + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -612,6 +719,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public void sendEnrolmentInvitation(String templateName, EmailMetaInfo metaInfo) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Send enrollment invitation"); + } Map, Object>> params = new HashMap<>(); Properties props = metaInfo.getProperties(); Enumeration e = props.propertyNames(); @@ -629,12 +739,21 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv metaInfo.getRecipients()).build(); DeviceManagementDataHolder.getInstance().getEmailSenderService().sendEmail(ctx); } catch (EmailSendingFailedException ex) { - throw new DeviceManagementException("Error occurred while sending enrollment invitation", ex); + String msg = "Error occurred while sending enrollment invitation"; + log.error(msg, ex); + throw new DeviceManagementException(msg, ex); + } catch (Exception ex) { + String msg = "Error occurred"; + log.error(msg, ex); + throw new DeviceManagementException(msg, ex); } } @Override public void sendRegistrationEmail(EmailMetaInfo metaInfo) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Send registration email"); + } Map, Object>> params = new HashMap<>(); params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.FIRST_NAME, new TypedValue, Object>(String.class, metaInfo.getProperty("first-name"))); @@ -657,12 +776,21 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv metaInfo.getRecipients()).build(); DeviceManagementDataHolder.getInstance().getEmailSenderService().sendEmail(ctx); } catch (EmailSendingFailedException e) { - throw new DeviceManagementException("Error occurred while sending user registration notification", e); + String msg = "Error occurred while sending user registration notification"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } } @Override public HashMap getTenantedDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get tenanted device with id " + deviceIdentifier.getId() + " of type " + deviceIdentifier.getType()); + } HashMap deviceHashMap; try { DeviceManagementDAOFactory.openConnection(); @@ -675,10 +803,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return null; } } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while obtaining the device for id " + - "'" + deviceIdentifier.getId() + "'", e); + String msg = "Error occurred while obtaining the device for id '" + deviceIdentifier.getId() + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -692,6 +827,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public Device getDeviceWithTypeProperties(DeviceIdentifier deviceId) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get tenanted device with type properties"); + } Device device = this.getDevice(deviceId, false); DeviceManager deviceManager = this.getDeviceManager(device.getType()); @@ -718,6 +856,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public Device getDevice(DeviceIdentifier deviceId, Date since, boolean requireDeviceInfo) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get device since " + since.toString() + " with identifier " + deviceId.getId() + " and type " + + deviceId.getType()); + } Device device; try { DeviceManagementDAOFactory.openConnection(); @@ -730,10 +872,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return null; } } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while obtaining the device for id " + - "'" + deviceId.getId() + "'", e); + String msg = "Error occurred while obtaining the device for id '" + deviceId.getId() + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -751,6 +900,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public Device getDevice(DeviceIdentifier deviceId, EnrolmentInfo.Status status, boolean requireDeviceInfo) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get device with identifier " + deviceId.getId() + " and type " + deviceId.getType()); + } Device device; try { DeviceManagementDAOFactory.openConnection(); @@ -763,22 +915,31 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return null; } } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while obtaining the device for id " + - "'" + deviceId.getId() + "'", e); + String msg = "Error occurred while obtaining the device for id '" + deviceId.getId() + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } if (requireDeviceInfo) { device = this.getAllDeviceInfo(device); } - return device; } @Override public List getAvailableDeviceTypes() throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get available device types"); + } List deviceTypesProvidedByTenant; List publicSharedDeviceTypesInDB; List deviceTypesResponse = new ArrayList<>(); @@ -814,9 +975,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while obtaining the device types.", e); + String msg = "Error occurred while obtaining the device types."; + log.info(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.info(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -825,6 +994,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean updateDeviceInfo(DeviceIdentifier deviceId, Device device) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Update device info of device " + deviceId.getId()); + } DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); if (deviceManager == null) { if (log.isDebugEnabled()) { @@ -838,6 +1010,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Set ownership of device " + deviceId.getId()); + } DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); if (deviceManager == null) { if (log.isDebugEnabled()) { @@ -865,6 +1040,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean setStatus(DeviceIdentifier deviceId, String currentOwner, EnrolmentInfo.Status status) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Set status of device " + deviceId.getId()); + } try { boolean success = false; int tenantId = this.getTenantId(); @@ -879,9 +1057,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return success; } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - throw new DeviceManagementException("Error occurred while setting enrollment status", e); + String msg = "Error occurred while setting enrollment status"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (TransactionManagementException e) { - throw new DeviceManagementException("Error occurred while initiating transaction", e); + String msg = "Error occurred while initiating transaction"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -890,6 +1076,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean setStatus(String currentOwner, EnrolmentInfo.Status status) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Update enrollment with status"); + } try { boolean success = false; int tenantId = this.getTenantId(); @@ -899,9 +1088,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv return success; } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - throw new DeviceManagementException("Error occurred while setting enrollment status", e); + String msg = "Error occurred while setting enrollment status"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (TransactionManagementException e) { - throw new DeviceManagementException("Error occurred while initiating transaction", e); + String msg = "Error occurred while initiating transaction"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -923,6 +1120,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public License getLicense(String deviceType, String languageCode) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get the licence for device type " + deviceType); + } DeviceManager deviceManager = this.getDeviceManager(deviceType); License license; if (deviceManager == null) { @@ -941,13 +1141,22 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } return license; } catch (LicenseManagementException e) { - throw new DeviceManagementException("Error occurred while retrieving license configured for " + - "device type '" + deviceType + "' and language code '" + languageCode + "'", e); + String msg = "Error occurred while retrieving license configured for " + + "device type '" + deviceType + "' and language code '" + languageCode + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } } @Override public void addLicense(String deviceType, License license) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Add the licence for device type " + deviceType); + } DeviceManager deviceManager = this.getDeviceManager(deviceType); if (deviceManager == null) { if (log.isDebugEnabled()) { @@ -959,8 +1168,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { deviceManager.addLicense(license); } catch (LicenseManagementException e) { - throw new DeviceManagementException("Error occurred while adding license for " + - "device type '" + deviceType + "'", e); + String msg = "Error occurred while adding license for device type '" + deviceType + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } } @@ -1092,15 +1306,26 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getDevicesOfUser(String username, boolean requireDeviceInfo) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get devices of user with username " + username); + } List userDevices; try { DeviceManagementDAOFactory.openConnection(); userDevices = deviceDAO.getDevicesOfUser(username, this.getTenantId()); } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while retrieving the list of devices that " + - "belong to the user '" + username + "'", e); + String msg = "Error occurred while retrieving the list of devices that " + + "belong to the user '" + username + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -1113,21 +1338,32 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getDevicesOfUser(String username, String deviceType) throws DeviceManagementException { - return this.getDevicesOfUser(username, deviceType, true); + return this.getDevicesOfUser(username, deviceType, true); } @Override public List getDevicesOfUser(String username, String deviceType, boolean requireDeviceInfo) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get devices of user with username " + username); + } List userDevices; try { DeviceManagementDAOFactory.openConnection(); userDevices = deviceDAO.getDevicesOfUser(username, deviceType, this.getTenantId()); } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while retrieving the list of devices that " + - "belong to the user '" + username + "'", e); + String msg = "Error occurred while retrieving the list of devices that " + + "belong to the user '" + username + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -1146,6 +1382,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public PaginationResult getDevicesOfUser(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get paginated results of devices of user " + request.getOwner()); + } PaginationResult result = new PaginationResult(); int deviceCount = 0; int tenantId = this.getTenantId(); @@ -1157,10 +1396,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv userDevices = deviceDAO.getDevicesOfUser(request, tenantId); deviceCount = deviceDAO.getDeviceCountByUser(username, tenantId); } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while retrieving the list of devices that " + - "belong to the user '" + username + "'", e); + String msg = "Error occurred while retrieving the list of devices that belong to the user '" + username + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -1185,6 +1431,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public PaginationResult getDevicesByOwnership(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get devices by ownership " + request.getOwnership()); + } PaginationResult result = new PaginationResult(); List allDevices; int deviceCount = 0; @@ -1196,10 +1445,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv allDevices = deviceDAO.getDevicesByOwnership(request, tenantId); deviceCount = deviceDAO.getDeviceCountByOwnership(ownerShip, tenantId); } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException( - "Error occurred while fetching the list of devices that matches to ownership : '" + ownerShip + "'", e); + String msg = "Error occurred while fetching the list of devices that matches to ownership : '" + ownerShip + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -1221,6 +1477,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getAllDevicesOfRole(String role, boolean requireDeviceInfo) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get devices of role " + role); + } List devices = new ArrayList<>(); String[] users; int tenantId = this.getTenantId(); @@ -1228,8 +1487,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv users = DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm(tenantId) .getUserStoreManager().getUserListOfRole(role); } catch (UserStoreException e) { - throw new DeviceManagementException("Error occurred while obtaining the users, who are assigned " + - "with the role '" + role + "'", e); + String msg = "Error occurred while obtaining the users, who are assigned with the role '" + role + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } List userDevices; @@ -1239,7 +1503,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.openConnection(); userDevices = deviceDAO.getDevicesOfUser(user, tenantId); } catch (DeviceManagementDAOException | SQLException e) { - log.error("Error occurred while obtaining the devices of user '" + user + "'", e); + String msg = "Error occurred while obtaining the devices of user '" + user + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -1252,14 +1522,24 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public int getDeviceCount(String username) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Getting device count of the user " + username); + } try { DeviceManagementDAOFactory.openConnection(); return deviceDAO.getDeviceCount(username, this.getTenantId()); } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while retrieving the device count of user '" - + username + "'", e); + String msg = "Error occurred while retrieving the device count of user '" + username + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -1267,13 +1547,24 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public int getDeviceCount() throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get devices count"); + } try { DeviceManagementDAOFactory.openConnection(); return deviceDAO.getDeviceCount(this.getTenantId()); } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while retrieving the device count", e); + String msg = "Error occurred while retrieving the device count"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -1282,6 +1573,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getDevicesByNameAndType(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get devices by name " + request.getDeviceName() + " and type " + request.getDeviceType()); + } List devices = new ArrayList<>(); List allDevices; int limit = DeviceManagerUtil.validateDeviceListPageSize(request.getRowCount()); @@ -1289,11 +1583,21 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.openConnection(); allDevices = deviceDAO.getDevicesByNameAndType(request.getDeviceName(), request.getDeviceType(), this.getTenantId(), request.getStartIndex(), limit); + List filterd = null; + filterd.get(0); } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while fetching the list of devices that matches to '" - + request.getDeviceName() + "'", e); + String msg = "Error occurred while fetching the list of devices that matches to '" + + request.getDeviceName() + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -1312,6 +1616,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public PaginationResult getDevicesByName(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get devices by name " + request.getDeviceName()); + } PaginationResult result = new PaginationResult(); int tenantId = this.getTenantId(); List allDevices = new ArrayList<>(); @@ -1324,10 +1631,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv result.setRecordsTotal(deviceCount); result.setRecordsFiltered(deviceCount); } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while fetching the list of devices that matches to '" - + deviceName + "'", e); + String msg = "Error occurred while fetching the list of devices that matches to '" + deviceName + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -1342,6 +1656,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status status) throws DeviceManagementException { try { + if (log.isDebugEnabled()) { + log.debug("Updating enrolment for device " + device.getDeviceIdentifier() + " of type " + device.getType()); + } DeviceManagementDAOFactory.beginTransaction(); device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); device.getEnrolmentInfo().setStatus(status); @@ -1349,10 +1666,18 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.commitTransaction(); } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - throw new DeviceManagementException("Error occurred update device enrolment status : '" + - device.getId() + "'", e); + String msg = "Error occurred while updating device enrolment status for " + device.getDeviceIdentifier() + + " of type " + device.getType(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (TransactionManagementException e) { - throw new DeviceManagementException("Error occurred while initiating transaction", e); + String msg = "Error occurred while initiating transaction"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -1360,21 +1685,34 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public void registerDeviceManagementService(DeviceManagementService deviceManagementService) { + if (log.isDebugEnabled()) { + log.debug("Registering device management service"); + } try { pluginRepository.addDeviceManagementProvider(deviceManagementService); } catch (DeviceManagementException e) { - log.error("Error occurred while registering device management plugin '" + - deviceManagementService.getType() + "'", e); + String msg = "Error occurred while registering device management plugin '" + + deviceManagementService.getType() + "'"; + log.error(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); } } @Override public void unregisterDeviceManagementService(DeviceManagementService deviceManagementService) { + if (log.isDebugEnabled()) { + log.debug("Unregister a device management service"); + } try { pluginRepository.removeDeviceManagementProvider(deviceManagementService); } catch (DeviceManagementException e) { log.error("Error occurred while un-registering device management plugin '" + deviceManagementService.getType() + "'", e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); } } @@ -1386,6 +1724,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getDevicesByStatus(EnrolmentInfo.Status status, boolean requireDeviceInfo) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("get devices by status"); + } List allDevices; try { DeviceManagementDAOFactory.openConnection(); @@ -1394,7 +1735,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv throw new DeviceManagementException( "Error occurred while fetching the list of devices that matches to status: '" + status + "'", e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -1412,6 +1759,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public PaginationResult getDevicesByStatus(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("get devices by status with pagination"); + } PaginationResult result = new PaginationResult(); List allDevices = new ArrayList<>(); int tenantId = this.getTenantId(); @@ -1424,10 +1774,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv result.setRecordsTotal(deviceCount); result.setRecordsFiltered(deviceCount); } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException( - "Error occurred while fetching the list of devices that matches to status: '" + status + "'", e); + String msg = "Error occurred while fetching the list of devices that matches to status: '" + status + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -1470,6 +1827,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean changeDeviceStatus(DeviceIdentifier deviceIdentifier, EnrolmentInfo.Status newStatus) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Change device status of " + deviceIdentifier.getId()); + } boolean isDeviceUpdated = false; Device device = getDevice(deviceIdentifier, false); int deviceId = device.getId(); @@ -1494,14 +1854,24 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getDeviceEnrolledTenants() throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("get device enrolled tenants"); + } try { DeviceManagementDAOFactory.openConnection(); return deviceDAO.getDeviceEnrolledTenants(); } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while retrieving the tenants " + - "which have device enrolled.", e); + String msg = "Error occurred while retrieving the tenants which have device enrolled."; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -1509,6 +1879,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv private boolean updateEnrollment(int deviceId, EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Update enrollment of device " + deviceId); + } boolean isUpdatedEnrollment = false; boolean isAutoCommit = true; try { @@ -1520,10 +1893,18 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv isUpdatedEnrollment = true; } } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while updating the enrollment information device for" + - "id '" + deviceId + "' .", e); + String msg = "Error occurred while updating the enrollment information device for" + + "id '" + deviceId + "' ."; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { try { DeviceManagementDAOFactory.getConnection().setAutoCommit(isAutoCommit); @@ -1562,6 +1943,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv */ private void addDeviceToGroups(DeviceIdentifier deviceIdentifier, EnrolmentInfo.OwnerShip ownership) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Add device " + deviceIdentifier.getId() + " to default group"); + } GroupManagementProviderService groupManagementProviderService = new GroupManagementProviderServiceImpl(); try { DeviceGroup defaultGroup = createDefaultGroup(groupManagementProviderService, ownership.toString()); @@ -1571,14 +1955,24 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv groupManagementProviderService.addDevices(defaultGroup.getGroupId(), deviceIdentifiers); } } catch (DeviceNotFoundException e) { - throw new DeviceManagementException("Unable to find the device with the id: '" + deviceIdentifier.getId(), - e); + String msg = "Unable to find the device with the id: '" + deviceIdentifier.getId(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (GroupManagementException e) { - throw new DeviceManagementException("An error occurred when adding the device to the group.", e); + String msg = "An error occurred when adding the device to the group."; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } } private void addInitialOperations(DeviceIdentifier deviceIdentifier, String deviceType) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Add initial operations to the device " + deviceIdentifier.getId()); + } DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder.getInstance(). getDeviceManagementProvider(); DeviceManagementService deviceManagementService = @@ -1597,11 +1991,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv try { deviceManagementProviderService.addOperation(deviceType, operation, deviceIdentifiers); } catch (OperationManagementException e) { - throw new DeviceManagementException("Unable to add the operation for the device with the id: '" - + deviceIdentifier.getId(), e); + String msg = "Unable to add the operation for the device with the id: '" + deviceIdentifier.getId(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (InvalidDeviceException e) { - throw new DeviceManagementException("Unable to find the device with the id: '" - + deviceIdentifier.getId(), e); + String msg = "Unable to find the device with the id: '" + deviceIdentifier.getId(); + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } } } @@ -1618,6 +2018,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv */ private DeviceGroup createDefaultGroup(GroupManagementProviderService service, String groupName) throws GroupManagementException { + if (log.isDebugEnabled()) { + log.debug("Create default group with name " + groupName); + } DeviceGroup defaultGroup = service.getGroup(groupName); if (defaultGroup == null) { defaultGroup = new DeviceGroup(groupName); @@ -1628,10 +2031,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv service.createGroup(defaultGroup, DeviceGroupConstants.Roles.DEFAULT_ADMIN_ROLE, DeviceGroupConstants.Permissions.DEFAULT_ADMIN_PERMISSIONS); } catch (GroupAlreadyExistException e) { - if (log.isDebugEnabled()) { - log.debug("Default group: " + defaultGroup.getName() + " already exists. Skipping group creation.", - e); - } + String msg = "Default group: " + defaultGroup.getName() + " already exists. Skipping group creation."; + log.error(msg); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } return service.getGroup(groupName); } else { @@ -1648,14 +2054,25 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public DeviceType getDeviceType(String deviceType) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("get device type " + deviceType); + } try { DeviceManagementDAOFactory.openConnection(); int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); return deviceTypeDAO.getDeviceType(deviceType, tenantId); } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while obtaining the device type " + deviceType, e); + String msg = "Error occurred while obtaining the device type " + deviceType; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -1663,15 +2080,25 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getDeviceTypes() throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get device types"); + } int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId(); try { DeviceManagementDAOFactory.openConnection(); return deviceTypeDAO.getDeviceTypes(tenantId); } catch (DeviceManagementDAOException e) { - throw new DeviceManagementException("Error occurred while obtaining the device types for tenant " - + tenantId, e); + String msg = "Error occurred while obtaining the device types for tenant " + tenantId; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - throw new DeviceManagementException("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -1680,28 +2107,34 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public void notifyPullNotificationSubscriber(DeviceIdentifier deviceIdentifier, Operation operation) throws PullNotificationExecutionFailedException { + if (log.isDebugEnabled()) { + log.debug("Notify pull notification subscriber"); + } DeviceManagementService dms = pluginRepository.getDeviceManagementService(deviceIdentifier.getType(), this.getTenantId()); if (dms == null) { String message = "Device type '" + deviceIdentifier.getType() + "' does not have an associated device management " + "plugin registered within the framework"; - if (log.isDebugEnabled()) { - log.debug(message); - } + log.error(message); throw new PullNotificationExecutionFailedException(message); } PullNotificationSubscriber pullNotificationSubscriber = dms.getPullNotificationSubscriber(); if (pullNotificationSubscriber == null) { - throw new PullNotificationExecutionFailedException("Pull Notification Subscriber is not configured " + - "for device type" + deviceIdentifier.getType()); + String message = "Pull Notification Subscriber is not configured " + + "for device type" + deviceIdentifier.getType(); + log.error(message); + throw new PullNotificationExecutionFailedException(message); } pullNotificationSubscriber.execute(deviceIdentifier, operation); } /** - * Returns all the device-info including location of the given device. + * Returns all the device-info including location of the given device. */ - private DeviceInfo getDeviceInfo(Device device) { + private DeviceInfo getDeviceInfo(Device device) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get device info of device " + device.getId() + " of type " + device.getType()); + } DeviceInfo info = null; try { DeviceManagementDAOFactory.openConnection(); @@ -1711,10 +2144,17 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv info.setLocation(location); } } catch (DeviceDetailsMgtDAOException e) { - log.error("Error occurred while retrieving advance info of '" + device.getType() + - "' that carries the id '" + device.getDeviceIdentifier() + "'"); + String msg = "Error occurred while retrieving advance info of '" + device.getType() + + "' that carries the id '" + device.getDeviceIdentifier() + "'"; + log.error(msg); + throw new DeviceManagementException(msg, e); } catch (SQLException e) { - log.error("Error occurred while opening a connection to the data source", e); + String msg = "Error occurred while opening a connection to the data source"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -1722,9 +2162,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } /** - * Returns all the installed apps of the given device. + * Returns all the installed apps of the given device. */ private List getInstalledApplications(Device device) { + if (log.isDebugEnabled()) { + log.debug("Get installed applications of device " + device.getId() + " of type " + device.getType()); + } List applications = new ArrayList<>(); try { DeviceManagementDAOFactory.openConnection(); @@ -1735,6 +2178,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv "which carries the id '" + device.getId() + "'", e); } catch (SQLException e) { log.error("Error occurred while opening a connection to the data source", e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); } @@ -1742,17 +2188,18 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } /** - * Returns all the available information (device-info, location, applications and plugin-db data) - * of the given device list. + * Returns all the available information (device-info, location, applications and plugin-db data) + * of the given device list. */ - private List getAllDeviceInfo(List allDevices) - throws DeviceManagementException { + private List getAllDeviceInfo(List allDevices) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get all device info of devices"); + } List devices = new ArrayList<>(); if (allDevices != null) { for (Device device : allDevices) { device.setDeviceInfo(this.getDeviceInfo(device)); device.setApplications(this.getInstalledApplications(device)); - DeviceManager deviceManager = this.getDeviceManager(device.getType()); if (deviceManager == null) { if (log.isDebugEnabled()) { @@ -1775,10 +2222,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } /** - * Returns all the available information (device-info, location, applications and plugin-db data) - * of a given device. + * Returns all the available information (device-info, location, applications and plugin-db data) + * of a given device. */ private Device getAllDeviceInfo(Device device) throws DeviceManagementException { + if (log.isDebugEnabled()) { + log.debug("Get all device info of device " + device.getId() + " of type " + device.getType()); + } device.setDeviceInfo(this.getDeviceInfo(device)); device.setApplications(this.getInstalledApplications(device)); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index ad8ecf77d1..55ec895316 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -65,6 +65,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public void createGroup(DeviceGroup deviceGroup, String defaultRole, String[] defaultPermissions) throws GroupManagementException, GroupAlreadyExistException { + if (log.isDebugEnabled()) { + log.debug("Creating group" + deviceGroup.getName()); + } if (deviceGroup == null) { throw new GroupManagementException("DeviceGroup cannot be null.", new NullPointerException()); } @@ -80,10 +83,17 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); - throw new GroupManagementException("Error occurred while adding deviceGroup " + - "'" + deviceGroup.getName() + "' to database.", e); + String msg = "Error occurred while adding deviceGroup '" + deviceGroup.getName() + "' to database."; + log.error(msg, e); + throw new GroupManagementException(msg, e); } catch (TransactionManagementException e) { - throw new GroupManagementException("Error occurred while initiating transaction.", e); + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); } @@ -99,6 +109,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public void updateGroup(DeviceGroup deviceGroup, int groupId) throws GroupManagementException, GroupAlreadyExistException { + if (log.isDebugEnabled()) { + log.debug("update group" + deviceGroup.getName()); + } if (deviceGroup == null) { throw new GroupManagementException("DeviceGroup cannot be null.", new NullPointerException()); } @@ -114,10 +127,17 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); - throw new GroupManagementException("Error occurred while modifying deviceGroup " + - "'" + deviceGroup.getName() + "'.", e); + String msg = "Error occurred while modifying deviceGroup '" + deviceGroup.getName() + "'."; + log.error(msg, e); + throw new GroupManagementException(msg, e); } catch (TransactionManagementException e) { - throw new GroupManagementException("Error occurred while initiating transaction.", e); + String msg = "Error occurred while initiating transaction."; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); } @@ -128,6 +148,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid */ @Override public boolean deleteGroup(int groupId) throws GroupManagementException { + if (log.isDebugEnabled()) { + log.debug("Delete group " +groupId ); + } DeviceGroup deviceGroup = getGroup(groupId); if (deviceGroup == null) { return false; @@ -142,9 +165,17 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid return true; } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); - throw new GroupManagementException("Error occurred while removing group data.", e); + String msg = "Error occurred while removing group data."; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (TransactionManagementException e) { - throw new GroupManagementException("Error occurred while initiating transaction.", e); + String msg = "Error occurred while initiating transaction."; + log.error(msg,e); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); } @@ -155,14 +186,25 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid */ @Override public DeviceGroup getGroup(int groupId) throws GroupManagementException { + if (log.isDebugEnabled()) { + log.debug("Get group by id " + groupId); + } DeviceGroup deviceGroup; try { GroupManagementDAOFactory.openConnection(); deviceGroup = this.groupDAO.getGroup(groupId, CarbonContext.getThreadLocalCarbonContext().getTenantId()); } catch (GroupManagementDAOException e) { - throw new GroupManagementException("Error occurred while obtaining group '" + groupId + "'", e); + String msg = "Error occurred while obtaining group '" + groupId + "'"; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (SQLException e) { - throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); + String msg = "Error occurred while opening a connection to the data source."; + log.error(msg,e); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); } @@ -174,14 +216,25 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid */ @Override public DeviceGroup getGroup(String groupName) throws GroupManagementException { + if (log.isDebugEnabled()) { + log.debug("Get group by name " + groupName); + } DeviceGroup deviceGroup; try { GroupManagementDAOFactory.openConnection(); deviceGroup = this.groupDAO.getGroup(groupName, CarbonContext.getThreadLocalCarbonContext().getTenantId()); } catch (GroupManagementDAOException e) { - throw new GroupManagementException("Error occurred while obtaining group with name: '" + groupName + "'", e); + String msg = "Error occurred while obtaining group with name: '" + groupName + "'"; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (SQLException e) { - throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); + String msg= "Error occurred while opening a connection to the data source."; + log.error(msg,e); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); } @@ -190,15 +243,26 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public List getGroups() throws GroupManagementException { + if (log.isDebugEnabled()) { + log.debug("Get groups"); + } List deviceGroups = new ArrayList<>(); try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.openConnection(); deviceGroups = this.groupDAO.getGroups(tenantId); } catch (GroupManagementDAOException e) { - throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e); + String msg = "Error occurred while retrieving all groups in tenant"; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (SQLException e) { - throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); + String msg = "Error occurred while opening a connection to the data source."; + log.error(msg); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); } @@ -207,6 +271,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public PaginationResult getGroups(GroupPaginationRequest request) throws GroupManagementException { + if (log.isDebugEnabled()) { + log.debug("Get groups with pagination"); + } request = DeviceManagerUtil.validateGroupListPageSize(request); List deviceGroups = new ArrayList<>(); try { @@ -214,9 +281,17 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid GroupManagementDAOFactory.openConnection(); deviceGroups = this.groupDAO.getGroups(request, tenantId); } catch (GroupManagementDAOException e) { - throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e); + String msg = "Error occurred while retrieving all groups in tenant"; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (SQLException e) { - throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); + String msg = "Error occurred while opening a connection to the data source."; + log.error(msg,e); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); } @@ -228,6 +303,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public List getGroups(String username) throws GroupManagementException { + if (log.isDebugEnabled()) { + log.debug("Get groups of owner "+username); + } Map groups = new HashMap<>(); UserStoreManager userStoreManager; try { @@ -245,11 +323,20 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid groups.put(deviceGroup.getGroupId(), deviceGroup); } } catch (UserStoreException e) { - throw new GroupManagementException("Error occurred while getting user store manager.", e); + String msg = "Error occurred while getting user store manager."; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (SQLException e) { - throw new GroupManagementException("Error occurred while retrieving all groups accessible to user.", e); + String msg = "Error occurred while retrieving all groups accessible to user."; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (GroupManagementDAOException e) { + log.error(e); throw new GroupManagementException(e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); } @@ -257,6 +344,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } private List getGroupIds(String username) throws GroupManagementException { + if (log.isDebugEnabled()) { + log.debug("Get groups Ids of owner "+username); + } UserStoreManager userStoreManager; List deviceGroupIds = new ArrayList<>(); try { @@ -268,11 +358,20 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid deviceGroupIds = this.groupDAO.getOwnGroupIds(username, tenantId); deviceGroupIds.addAll(this.groupDAO.getGroupIds(roleList, tenantId)); } catch (UserStoreException e) { - throw new GroupManagementException("Error occurred while getting user store manager.", e); + String msg = "Error occurred while getting user store manager."; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (SQLException e) { - throw new GroupManagementException("Error occurred while retrieving all groups accessible to user.", e); + String msg = "Error occurred while retrieving all groups accessible to user."; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (GroupManagementDAOException e) { + log.error(e); throw new GroupManagementException(e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); } @@ -282,6 +381,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public PaginationResult getGroups(String currentUser, GroupPaginationRequest request) throws GroupManagementException { + if (log.isDebugEnabled()) { + log.debug("Get all groups of user "+currentUser); + } request = DeviceManagerUtil.validateGroupListPageSize(request); List allDeviceGroupIdsOfUser = getGroupIds(currentUser); List allMatchingGroups = new ArrayList<>(); @@ -290,9 +392,17 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid GroupManagementDAOFactory.openConnection(); allMatchingGroups = this.groupDAO.getGroups(request, allDeviceGroupIdsOfUser, tenantId); } catch (GroupManagementDAOException e) { - throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e); + String msg = "Error occurred while retrieving all groups in tenant"; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (SQLException e) { - throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); + String msg = "Error occurred while opening a connection to the data source."; + log.error(msg,e); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); } @@ -304,28 +414,50 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public int getGroupCount() throws GroupManagementException { + if (log.isDebugEnabled()) { + log.debug("Get groups count"); + } try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.openConnection(); return groupDAO.getGroupCount(tenantId); } catch (GroupManagementDAOException e) { - throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e); + String msg = "Error occurred while retrieving all groups in tenant"; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (SQLException e) { - throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); + String msg= "Error occurred while opening a connection to the data source."; + log.error(msg,e); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); } } private int getGroupCount(GroupPaginationRequest request) throws GroupManagementException { + if (log.isDebugEnabled()) { + log.debug("Get groups count"); + } try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.openConnection(); return groupDAO.getGroupCount(request, tenantId); } catch (GroupManagementDAOException e) { - throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e); + String msg = "Error occurred while retrieving all groups in tenant"; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (SQLException e) { - throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); + String msg = "Error occurred while opening a connection to the data source."; + log.error(msg,e); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); } @@ -336,6 +468,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid */ @Override public int getGroupCount(String username) throws GroupManagementException { + if (log.isDebugEnabled()) { + log.debug("Get groups count of "+username); + } UserStoreManager userStoreManager; int count; try { @@ -348,11 +483,21 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid count += groupDAO.getGroupsCount(roleList, tenantId); return count; } catch (UserStoreException e) { - throw new GroupManagementException("Error occurred while getting user store manager.", e); + String msg = "Error occurred while getting user store manager."; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (GroupManagementDAOException e) { - throw new GroupManagementException("Error occurred while retrieving group count of user '" + username + "'", e); + String msg = "Error occurred while retrieving group count of user '" + username + "'"; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (SQLException e) { - throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); + String msg = "Error occurred while opening a connection to the data source."; + log.error(msg,e); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); } @@ -364,41 +509,52 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public void manageGroupSharing(int groupId, List newRoles) throws GroupManagementException, RoleDoesNotExistException { - int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); - UserStoreManager userStoreManager; - try { - userStoreManager = - DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm( - tenantId).getUserStoreManager(); - List currentUserRoles = getRoles(groupId); - GroupManagementDAOFactory.beginTransaction(); - if (newRoles != null) { - for (String role : newRoles) { - if (!userStoreManager.isExistingRole(role)) { - throw new RoleDoesNotExistException("Role '" + role + "' does not exists in the user store."); - } - // Removing role from current user roles of the group will return true if role exist. - // So we don't need to add it to the db again. - if (!currentUserRoles.remove(role)) { - // If group doesn't have the role, it is adding to the db. - groupDAO.addRole(groupId, role, tenantId); - } + if (log.isDebugEnabled()) { + log.debug("Group sharing for group "+groupId); + } + int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + UserStoreManager userStoreManager; + try { + userStoreManager = + DeviceManagementDataHolder.getInstance().getRealmService().getTenantUserRealm( + tenantId).getUserStoreManager(); + List currentUserRoles = getRoles(groupId); + GroupManagementDAOFactory.beginTransaction(); + if (newRoles != null) { + for (String role : newRoles) { + if (!userStoreManager.isExistingRole(role)) { + throw new RoleDoesNotExistException("Role '" + role + "' does not exists in the user store."); + } + // Removing role from current user roles of the group will return true if role exist. + // So we don't need to add it to the db again. + if (!currentUserRoles.remove(role)) { + // If group doesn't have the role, it is adding to the db. + groupDAO.addRole(groupId, role, tenantId); } } - for (String role : currentUserRoles) { - // Removing old roles from db which are not available in the new roles list. - groupDAO.removeRole(groupId, role, tenantId); - } - GroupManagementDAOFactory.commitTransaction(); - } catch (GroupManagementDAOException e) { - GroupManagementDAOFactory.rollbackTransaction(); - throw new GroupManagementException(e); - } catch (UserStoreException e) { - throw new GroupManagementException("User store error in updating sharing roles.", e); - } catch (TransactionManagementException e) { - throw new GroupManagementException(e); - } finally { - GroupManagementDAOFactory.closeConnection(); + } + for (String role : currentUserRoles) { + // Removing old roles from db which are not available in the new roles list. + groupDAO.removeRole(groupId, role, tenantId); + } + GroupManagementDAOFactory.commitTransaction(); + } catch (GroupManagementDAOException e) { + GroupManagementDAOFactory.rollbackTransaction(); + log.error(e); + throw new GroupManagementException(e); + } catch (UserStoreException e) { + String msg = "User store error in updating sharing roles."; + log.error(msg,e); + throw new GroupManagementException(msg, e); + } catch (TransactionManagementException e) { + log.error(e); + throw new GroupManagementException(e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); + } finally { + GroupManagementDAOFactory.closeConnection(); } } @@ -407,14 +563,25 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid */ @Override public List getRoles(int groupId) throws GroupManagementException { + if (log.isDebugEnabled()) { + log.debug("Group roles for group "+groupId); + } try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); GroupManagementDAOFactory.openConnection(); return groupDAO.getRoles(groupId, tenantId); } catch (GroupManagementDAOException e) { - throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e); + String msg = "Error occurred while retrieving all groups in tenant"; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (SQLException e) { - throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); + String msg = "Error occurred while opening a connection to the data source."; + log.error(msg,e); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); } @@ -426,6 +593,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public List getDevices(int groupId, int startIndex, int rowCount) throws GroupManagementException { + if (log.isDebugEnabled()) { + log.debug("Group devices of group "+groupId); + } int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); List devices; try { @@ -433,11 +603,21 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid GroupManagementDAOFactory.openConnection(); devices = this.groupDAO.getDevices(groupId, startIndex, rowCount, tenantId); } catch (GroupManagementDAOException e) { - throw new GroupManagementException("Error occurred while getting devices in group.", e); + String msg = "Error occurred while getting devices in group."; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (SQLException e) { - throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); + String msg = "Error occurred while opening a connection to the data source."; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (DeviceManagementException e) { - throw new GroupManagementException("Error occurred while validating the limit of the devices to be returned", e); + String msg = "Error occurred while validating the limit of the devices to be returned"; + log.error(msg,e); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); } @@ -449,13 +629,24 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid */ @Override public int getDeviceCount(int groupId) throws GroupManagementException { + if (log.isDebugEnabled()) { + log.debug("Group devices count of group "+groupId); + } try { GroupManagementDAOFactory.openConnection(); return groupDAO.getDeviceCount(groupId, CarbonContext.getThreadLocalCarbonContext().getTenantId()); } catch (GroupManagementDAOException e) { - throw new GroupManagementException("Error occurred while retrieving all groups in tenant", e); + String msg = "Error occurred while retrieving all groups in tenant"; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (SQLException e) { - throw new GroupManagementException("Error occurred while opening a connection to the data source.", e); + String msg = "Error occurred while opening a connection to the data source."; + log.error(msg,e); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); } @@ -467,6 +658,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public void addDevices(int groupId, List deviceIdentifiers) throws GroupManagementException, DeviceNotFoundException { + if (log.isDebugEnabled()) { + log.debug("Group devices to the group "+groupId); + } Device device; try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -483,12 +677,22 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } GroupManagementDAOFactory.commitTransaction(); } catch (DeviceManagementException e) { - throw new GroupManagementException("Error occurred while retrieving device.", e); + String msg = "Error occurred while retrieving device."; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); - throw new GroupManagementException("Error occurred while adding device to group.", e); + String msg = "Error occurred while adding device to group."; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (TransactionManagementException e) { - throw new GroupManagementException("Error occurred while initiating transaction.", e); + String msg = "Error occurred while initiating transaction."; + log.error(msg,e); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); } @@ -500,6 +704,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public void removeDevice(int groupId, List deviceIdentifiers) throws GroupManagementException, DeviceNotFoundException { + if (log.isDebugEnabled()) { + log.debug("Remove devices from the group "+groupId); + } Device device; try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -514,12 +721,22 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } GroupManagementDAOFactory.commitTransaction(); } catch (DeviceManagementException e) { - throw new GroupManagementException("Error occurred while retrieving device.", e); + String msg = "Error occurred while retrieving device."; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (TransactionManagementException e) { - throw new GroupManagementException("Error occurred while initiating transaction.", e); + String msg = "Error occurred while initiating transaction."; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); - throw new GroupManagementException("Error occurred while adding device to group.", e); + String msg = "Error occurred while adding device to group."; + log.error(msg,e); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); } @@ -530,6 +747,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid */ @Override public List getGroups(String username, String permission) throws GroupManagementException { + if (log.isDebugEnabled()) { + log.debug("Get groups of user "+username); + } List deviceGroups = getGroups(username); Map permittedDeviceGroups = new HashMap<>(); UserRealm userRealm; @@ -546,13 +766,22 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } } } catch (UserStoreException e) { - throw new GroupManagementException("Error occurred while getting user realm.", e); + String msg = "Error occurred while getting user realm."; + log.error(msg,e); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } return new ArrayList<>(permittedDeviceGroups.values()); } @Override public List getGroups(DeviceIdentifier deviceIdentifier) throws GroupManagementException { + if (log.isDebugEnabled()) { + log.debug("Get groups of device "+deviceIdentifier.getId()); + } DeviceManagementProviderService managementProviderService = new DeviceManagementProviderServiceImpl(); try { Device device = managementProviderService.getDevice(deviceIdentifier, false); @@ -560,11 +789,21 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid return groupDAO.getGroups(device.getId(), PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); } catch (DeviceManagementException e) { - throw new GroupManagementException("Error occurred while retrieving the device details.", e); + String msg = "Error occurred while retrieving the device details."; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (GroupManagementDAOException e) { - throw new GroupManagementException("Error occurred while retrieving device groups.", e); + String msg = "Error occurred while retrieving device groups."; + log.error(msg,e); + throw new GroupManagementException(msg, e); } catch (SQLException e) { - throw new GroupManagementException("Error occurred while opening database connection.", e); + String msg = "Error occurred while opening database connection."; + log.error(msg,e); + throw new GroupManagementException(msg, e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } finally { GroupManagementDAOFactory.closeConnection(); } @@ -575,7 +814,9 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid */ @Override public DeviceGroup createDefaultGroup(String groupName) throws GroupManagementException { - + if (log.isDebugEnabled()) { + log.debug("Create default group "+groupName); + } DeviceGroup defaultGroup = this.getGroup(groupName); if (defaultGroup == null) { defaultGroup = new DeviceGroup(groupName); @@ -586,10 +827,13 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid this.createGroup(defaultGroup, DeviceGroupConstants.Roles.DEFAULT_ADMIN_ROLE, DeviceGroupConstants.Permissions.DEFAULT_ADMIN_PERMISSIONS); } catch (GroupAlreadyExistException e) { - if (log.isDebugEnabled()) { - log.debug("Default group: " + defaultGroup.getName() + " already exists. Skipping group creation.", - e); - } + String msg = "Default group: " + defaultGroup.getName() + " already exists. Skipping group creation."; + log.error(msg,e); + throw new GroupManagementException(msg,e); + } catch (Exception e) { + String msg = "Error occurred"; + log.error(msg, e); + throw new GroupManagementException(msg, e); } return this.getGroup(groupName); } else { From 78d60b1a925adea223a7587447596cc2e92f1d66 Mon Sep 17 00:00:00 2001 From: GDLMadushanka Date: Tue, 29 Aug 2017 14:15:26 +0530 Subject: [PATCH 3/5] Refactoring --- .../GroupManagementProviderServiceImpl.java | 116 +++++++++--------- 1 file changed, 58 insertions(+), 58 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index 55ec895316..3f8c679503 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -149,7 +149,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public boolean deleteGroup(int groupId) throws GroupManagementException { if (log.isDebugEnabled()) { - log.debug("Delete group " +groupId ); + log.debug("Delete group " + groupId); } DeviceGroup deviceGroup = getGroup(groupId); if (deviceGroup == null) { @@ -166,11 +166,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred while removing group data."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (TransactionManagementException e) { String msg = "Error occurred while initiating transaction."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred"; @@ -195,11 +195,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid deviceGroup = this.groupDAO.getGroup(groupId, CarbonContext.getThreadLocalCarbonContext().getTenantId()); } catch (GroupManagementDAOException e) { String msg = "Error occurred while obtaining group '" + groupId + "'"; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (SQLException e) { String msg = "Error occurred while opening a connection to the data source."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred"; @@ -225,11 +225,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid deviceGroup = this.groupDAO.getGroup(groupName, CarbonContext.getThreadLocalCarbonContext().getTenantId()); } catch (GroupManagementDAOException e) { String msg = "Error occurred while obtaining group with name: '" + groupName + "'"; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (SQLException e) { - String msg= "Error occurred while opening a connection to the data source."; - log.error(msg,e); + String msg = "Error occurred while opening a connection to the data source."; + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred"; @@ -253,7 +253,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid deviceGroups = this.groupDAO.getGroups(tenantId); } catch (GroupManagementDAOException e) { String msg = "Error occurred while retrieving all groups in tenant"; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (SQLException e) { String msg = "Error occurred while opening a connection to the data source."; @@ -282,11 +282,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid deviceGroups = this.groupDAO.getGroups(request, tenantId); } catch (GroupManagementDAOException e) { String msg = "Error occurred while retrieving all groups in tenant"; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (SQLException e) { String msg = "Error occurred while opening a connection to the data source."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred"; @@ -304,7 +304,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public List getGroups(String username) throws GroupManagementException { if (log.isDebugEnabled()) { - log.debug("Get groups of owner "+username); + log.debug("Get groups of owner " + username); } Map groups = new HashMap<>(); UserStoreManager userStoreManager; @@ -324,11 +324,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } } catch (UserStoreException e) { String msg = "Error occurred while getting user store manager."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (SQLException e) { String msg = "Error occurred while retrieving all groups accessible to user."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (GroupManagementDAOException e) { log.error(e); @@ -345,7 +345,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid private List getGroupIds(String username) throws GroupManagementException { if (log.isDebugEnabled()) { - log.debug("Get groups Ids of owner "+username); + log.debug("Get groups Ids of owner " + username); } UserStoreManager userStoreManager; List deviceGroupIds = new ArrayList<>(); @@ -359,11 +359,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid deviceGroupIds.addAll(this.groupDAO.getGroupIds(roleList, tenantId)); } catch (UserStoreException e) { String msg = "Error occurred while getting user store manager."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (SQLException e) { String msg = "Error occurred while retrieving all groups accessible to user."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (GroupManagementDAOException e) { log.error(e); @@ -382,7 +382,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid public PaginationResult getGroups(String currentUser, GroupPaginationRequest request) throws GroupManagementException { if (log.isDebugEnabled()) { - log.debug("Get all groups of user "+currentUser); + log.debug("Get all groups of user " + currentUser); } request = DeviceManagerUtil.validateGroupListPageSize(request); List allDeviceGroupIdsOfUser = getGroupIds(currentUser); @@ -393,11 +393,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid allMatchingGroups = this.groupDAO.getGroups(request, allDeviceGroupIdsOfUser, tenantId); } catch (GroupManagementDAOException e) { String msg = "Error occurred while retrieving all groups in tenant"; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (SQLException e) { String msg = "Error occurred while opening a connection to the data source."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred"; @@ -423,11 +423,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid return groupDAO.getGroupCount(tenantId); } catch (GroupManagementDAOException e) { String msg = "Error occurred while retrieving all groups in tenant"; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (SQLException e) { - String msg= "Error occurred while opening a connection to the data source."; - log.error(msg,e); + String msg = "Error occurred while opening a connection to the data source."; + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred"; @@ -448,11 +448,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid return groupDAO.getGroupCount(request, tenantId); } catch (GroupManagementDAOException e) { String msg = "Error occurred while retrieving all groups in tenant"; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (SQLException e) { String msg = "Error occurred while opening a connection to the data source."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred"; @@ -469,7 +469,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public int getGroupCount(String username) throws GroupManagementException { if (log.isDebugEnabled()) { - log.debug("Get groups count of "+username); + log.debug("Get groups count of " + username); } UserStoreManager userStoreManager; int count; @@ -484,15 +484,15 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid return count; } catch (UserStoreException e) { String msg = "Error occurred while getting user store manager."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (GroupManagementDAOException e) { String msg = "Error occurred while retrieving group count of user '" + username + "'"; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (SQLException e) { String msg = "Error occurred while opening a connection to the data source."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred"; @@ -510,7 +510,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid public void manageGroupSharing(int groupId, List newRoles) throws GroupManagementException, RoleDoesNotExistException { if (log.isDebugEnabled()) { - log.debug("Group sharing for group "+groupId); + log.debug("Group sharing for group " + groupId); } int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); UserStoreManager userStoreManager; @@ -544,7 +544,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid throw new GroupManagementException(e); } catch (UserStoreException e) { String msg = "User store error in updating sharing roles."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (TransactionManagementException e) { log.error(e); @@ -564,7 +564,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public List getRoles(int groupId) throws GroupManagementException { if (log.isDebugEnabled()) { - log.debug("Group roles for group "+groupId); + log.debug("Group roles for group " + groupId); } try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -572,11 +572,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid return groupDAO.getRoles(groupId, tenantId); } catch (GroupManagementDAOException e) { String msg = "Error occurred while retrieving all groups in tenant"; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (SQLException e) { String msg = "Error occurred while opening a connection to the data source."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred"; @@ -594,7 +594,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid public List getDevices(int groupId, int startIndex, int rowCount) throws GroupManagementException { if (log.isDebugEnabled()) { - log.debug("Group devices of group "+groupId); + log.debug("Group devices of group " + groupId); } int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); List devices; @@ -604,15 +604,15 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid devices = this.groupDAO.getDevices(groupId, startIndex, rowCount, tenantId); } catch (GroupManagementDAOException e) { String msg = "Error occurred while getting devices in group."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (SQLException e) { String msg = "Error occurred while opening a connection to the data source."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (DeviceManagementException e) { String msg = "Error occurred while validating the limit of the devices to be returned"; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred"; @@ -630,18 +630,18 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public int getDeviceCount(int groupId) throws GroupManagementException { if (log.isDebugEnabled()) { - log.debug("Group devices count of group "+groupId); + log.debug("Group devices count of group " + groupId); } try { GroupManagementDAOFactory.openConnection(); return groupDAO.getDeviceCount(groupId, CarbonContext.getThreadLocalCarbonContext().getTenantId()); } catch (GroupManagementDAOException e) { String msg = "Error occurred while retrieving all groups in tenant"; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (SQLException e) { String msg = "Error occurred while opening a connection to the data source."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred"; @@ -659,7 +659,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid public void addDevices(int groupId, List deviceIdentifiers) throws GroupManagementException, DeviceNotFoundException { if (log.isDebugEnabled()) { - log.debug("Group devices to the group "+groupId); + log.debug("Group devices to the group " + groupId); } Device device; try { @@ -678,16 +678,16 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid GroupManagementDAOFactory.commitTransaction(); } catch (DeviceManagementException e) { String msg = "Error occurred while retrieving device."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred while adding device to group."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (TransactionManagementException e) { String msg = "Error occurred while initiating transaction."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred"; @@ -705,7 +705,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid public void removeDevice(int groupId, List deviceIdentifiers) throws GroupManagementException, DeviceNotFoundException { if (log.isDebugEnabled()) { - log.debug("Remove devices from the group "+groupId); + log.debug("Remove devices from the group " + groupId); } Device device; try { @@ -722,16 +722,16 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid GroupManagementDAOFactory.commitTransaction(); } catch (DeviceManagementException e) { String msg = "Error occurred while retrieving device."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (TransactionManagementException e) { String msg = "Error occurred while initiating transaction."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred while adding device to group."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred"; @@ -748,7 +748,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public List getGroups(String username, String permission) throws GroupManagementException { if (log.isDebugEnabled()) { - log.debug("Get groups of user "+username); + log.debug("Get groups of user " + username); } List deviceGroups = getGroups(username); Map permittedDeviceGroups = new HashMap<>(); @@ -767,7 +767,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } } catch (UserStoreException e) { String msg = "Error occurred while getting user realm."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred"; @@ -780,7 +780,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public List getGroups(DeviceIdentifier deviceIdentifier) throws GroupManagementException { if (log.isDebugEnabled()) { - log.debug("Get groups of device "+deviceIdentifier.getId()); + log.debug("Get groups of device " + deviceIdentifier.getId()); } DeviceManagementProviderService managementProviderService = new DeviceManagementProviderServiceImpl(); try { @@ -790,15 +790,15 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId()); } catch (DeviceManagementException e) { String msg = "Error occurred while retrieving the device details."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (GroupManagementDAOException e) { String msg = "Error occurred while retrieving device groups."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (SQLException e) { String msg = "Error occurred while opening database connection."; - log.error(msg,e); + log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred"; @@ -815,7 +815,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public DeviceGroup createDefaultGroup(String groupName) throws GroupManagementException { if (log.isDebugEnabled()) { - log.debug("Create default group "+groupName); + log.debug("Create default group " + groupName); } DeviceGroup defaultGroup = this.getGroup(groupName); if (defaultGroup == null) { @@ -828,8 +828,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid DeviceGroupConstants.Permissions.DEFAULT_ADMIN_PERMISSIONS); } catch (GroupAlreadyExistException e) { String msg = "Default group: " + defaultGroup.getName() + " already exists. Skipping group creation."; - log.error(msg,e); - throw new GroupManagementException(msg,e); + log.error(msg, e); + throw new GroupManagementException(msg, e); } catch (Exception e) { String msg = "Error occurred"; log.error(msg, e); From 92d6624bbf247b5ee7bcf5e28992a3e663f772ba Mon Sep 17 00:00:00 2001 From: GDLMadushanka Date: Wed, 30 Aug 2017 11:33:39 +0530 Subject: [PATCH 4/5] refactoring error/debug logs --- .../mgt/common/GroupPaginationRequest.java | 6 + .../device/mgt/common/PaginationRequest.java | 8 + .../DeviceManagementProviderServiceImpl.java | 392 ++++++++++++++---- .../GroupManagementProviderServiceImpl.java | 104 +++-- 4 files changed, 405 insertions(+), 105 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/GroupPaginationRequest.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/GroupPaginationRequest.java index a0dfb9351b..3c17ce32a9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/GroupPaginationRequest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/GroupPaginationRequest.java @@ -65,4 +65,10 @@ public class GroupPaginationRequest { this.groupName = groupName; } + @Override + public String toString() { + return "Group Name '" + this.groupName + "' num of rows: " + this.rowCount + " start index: " + this.startIndex + + " owner' " + this.owner + "'"; + } + } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationRequest.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationRequest.java index 3799b397ba..e71ba71773 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationRequest.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/PaginationRequest.java @@ -129,4 +129,12 @@ public class PaginationRequest { public void setOwnerPattern(String ownerPattern) { this.ownerPattern = ownerPattern; } + + @Override + public String toString() { + return "Device type '" + this.deviceType + "' Device Name '" + this.deviceName + "' row count: " + this.rowCount + + " Owner role '" + this.ownerRole + "' owner pattern '" + this.ownerPattern + "' ownership " + + this.ownership + "' Status '" + this.status + "' owner '" + this.owner + "' groupId: " + this.groupId + + " start index: " + this.startIndex; + } } 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 488818d55f..5428950483 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 @@ -160,8 +160,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean enrollDevice(Device device) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Enrolling the device " + device.getId() + "of type " + device.getType()); + if (device != null) { + if (log.isDebugEnabled()) { + log.debug("Enrolling the device " + device.getId() + "of type '" + device.getType() + "'"); + } + } else { + String msg = "required values are not set for device enrollment"; + log.error(msg); + throw new DeviceManagementException(msg); } boolean status = false; DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); @@ -292,9 +298,16 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean modifyEnrollment(Device device) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Modifying enrollment for device " + device.getDeviceIdentifier() + " of type " + device.getType()); + if (device != null) { + if (log.isDebugEnabled()) { + log.debug("Modifying enrollment for device: " + device.getId() + " of type '" + device.getType() + "'"); + } + } else { + String msg = "required values are not set to modify device enrollment"; + log.error(msg); + throw new DeviceManagementException(msg); } + DeviceManager deviceManager = this.getDeviceManager(device.getType()); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); if (deviceManager == null) { @@ -366,8 +379,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Dis-enrolling device " + deviceId.getId() + " of type " + deviceId.getType()); + if (deviceId != null) { + if (log.isDebugEnabled()) { + log.debug("Dis-enrolling device: " + deviceId.getId() + " of type '" + deviceId.getType() + "'"); + } + } else { + String msg = "required values are not set to dis-enroll device"; + log.error(msg); + throw new DeviceManagementException(msg); } DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); if (deviceManager == null) { @@ -465,8 +484,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getAllDevices(String deviceType, boolean requireDeviceInfo) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Getting all devices of type " + deviceType); + if (deviceType != null) { + if (log.isDebugEnabled()) { + log.debug("Getting all devices of type '" + deviceType + "' and requiredDeviceInfo: " + requireDeviceInfo); + } + } else { + String msg = "Device type is empty for method getAllDevices"; + log.error(msg); + throw new DeviceManagementException(msg); } List allDevices; try { @@ -508,7 +533,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getAllDevices(boolean requireDeviceInfo) throws DeviceManagementException { if (log.isDebugEnabled()) { - log.debug("Getting all devices"); + log.debug("Getting all devices with requiredDeviceInfo: " + requireDeviceInfo); } List allDevices; try { @@ -543,8 +568,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getDevices(Date since, boolean requireDeviceInfo) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Getting all devices since date " + since.toString()); + if (since != null) { + if (log.isDebugEnabled()) { + log.debug("Getting all devices since date '" + since.toString() + "' and required device info: " + + requireDeviceInfo); + } + } else { + String msg = "Given date is empty for method getDevices"; + log.error(msg); + throw new DeviceManagementException(msg); } List allDevices; try { @@ -579,8 +611,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public PaginationResult getDevicesByType(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Get devices with pagination"); + if (request != null) { + if (log.isDebugEnabled()) { + log.debug("Get devices with pagination " + request.toString() + " and required deviceinfo: " + requireDeviceInfo); + } + } else { + String msg = "received incomplete pagination request for getDevicesByType"; + log.error(msg); + throw new DeviceManagementException(msg); } PaginationResult paginationResult = new PaginationResult(); List allDevices = new ArrayList<>(); @@ -627,8 +665,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public PaginationResult getAllDevices(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Get devices with pagination"); + if (request != null) { + if (log.isDebugEnabled()) { + log.debug("Get devices with pagination " + request.toString() + " and requiredDeviceInfo: " + requireDeviceInfo); + } + } else { + String msg = "received incomplete pagination request for method getAllDevices"; + log.error(msg); + throw new DeviceManagementException(msg); } List devicesForRoles = null; PaginationResult paginationResult = new PaginationResult(); @@ -677,8 +721,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public Device getDevice(DeviceIdentifier deviceId, boolean requireDeviceInfo) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Get device by device id " + deviceId.getId() + " of type " + deviceId.getType()); + if (deviceId != null) { + if (log.isDebugEnabled()) { + log.debug("Get device by device id :" + deviceId.getId() + " of type '" + deviceId.getType() + + "' and requiredDeviceInfo: " + requireDeviceInfo); + } + } else { + String msg = "received null device identifier for method getDevice"; + log.error(msg); + throw new DeviceManagementException(msg); } int tenantId = this.getTenantId(); Device device = this.getDeviceFromCache(deviceId); @@ -719,8 +770,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public void sendEnrolmentInvitation(String templateName, EmailMetaInfo metaInfo) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Send enrollment invitation"); + if (metaInfo != null) { + if (log.isDebugEnabled()) { + log.debug("Send enrollment invitation, templateName '" + templateName + "'"); + } + } else { + String msg = "received incomplete data to method sendEnrolmentInvitation"; + log.error(msg); + throw new DeviceManagementException(msg); } Map, Object>> params = new HashMap<>(); Properties props = metaInfo.getProperties(); @@ -751,8 +808,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public void sendRegistrationEmail(EmailMetaInfo metaInfo) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Send registration email"); + if (metaInfo != null) { + if (log.isDebugEnabled()) { + log.debug("Send registration email"); + } + } else { + String msg = "received incomplete request for sendRegistrationEmail"; + log.error(msg); + throw new DeviceManagementException(msg); } Map, Object>> params = new HashMap<>(); params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.FIRST_NAME, @@ -788,8 +851,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public HashMap getTenantedDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Get tenanted device with id " + deviceIdentifier.getId() + " of type " + deviceIdentifier.getType()); + if (deviceIdentifier != null) { + if (log.isDebugEnabled()) { + log.debug("Get tenanted device with id: " + deviceIdentifier.getId() + " of type '" + + deviceIdentifier.getType() + "'"); + } + } else { + String msg = "received null deviceIdentifier for getTenantedDevice"; + log.error(msg); + throw new DeviceManagementException(msg); } HashMap deviceHashMap; try { @@ -827,8 +897,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public Device getDeviceWithTypeProperties(DeviceIdentifier deviceId) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Get tenanted device with type properties"); + if (deviceId != null) { + if (log.isDebugEnabled()) { + log.debug("Get tenanted device with type properties, deviceId: " + deviceId.getId()); + } + } else { + String msg = "received null deviceIdentifier for getDeviceWithTypeProperties"; + log.error(msg); + throw new DeviceManagementException(msg); } Device device = this.getDevice(deviceId, false); @@ -856,9 +932,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public Device getDevice(DeviceIdentifier deviceId, Date since, boolean requireDeviceInfo) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Get device since " + since.toString() + " with identifier " + deviceId.getId() + " and type " - + deviceId.getType()); + if (deviceId != null && since != null) { + if (log.isDebugEnabled()) { + log.debug("Get device since '" + since.toString() + "' with identifier: " + deviceId.getId() + + " and type '" + deviceId.getType() + "'"); + } + } else { + String msg = "received incomplete data for getDevice"; + log.error(msg); + throw new DeviceManagementException(msg); } Device device; try { @@ -900,8 +982,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public Device getDevice(DeviceIdentifier deviceId, EnrolmentInfo.Status status, boolean requireDeviceInfo) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Get device with identifier " + deviceId.getId() + " and type " + deviceId.getType()); + if (deviceId != null) { + if (log.isDebugEnabled()) { + log.debug("Get device with identifier: " + deviceId.getId() + " and type '" + deviceId.getType() + "'"); + } + } else { + String msg = "received null deviceIdentifier for getDevice"; + log.error(msg); + throw new DeviceManagementException(msg); } Device device; try { @@ -994,8 +1082,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean updateDeviceInfo(DeviceIdentifier deviceId, Device device) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Update device info of device " + deviceId.getId()); + if (deviceId != null && device != null) { + if (log.isDebugEnabled()) { + log.debug("Update device info of device: " + deviceId.getId()); + } + } else { + String msg = "received incomplete data for updateDeviceInfo"; + log.error(msg); + throw new DeviceManagementException(msg); } DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); if (deviceManager == null) { @@ -1010,8 +1104,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Set ownership of device " + deviceId.getId()); + if (deviceId != null) { + if (log.isDebugEnabled()) { + log.debug("Set ownership of device: " + deviceId.getId() + " ownership type '" + ownershipType + "'"); + } + } else { + String msg = "received incomplete data for setOwnership"; + log.error(msg); + throw new DeviceManagementException(msg); } DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); if (deviceManager == null) { @@ -1040,8 +1140,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean setStatus(DeviceIdentifier deviceId, String currentOwner, EnrolmentInfo.Status status) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Set status of device " + deviceId.getId()); + if (deviceId != null) { + if (log.isDebugEnabled()) { + log.debug("Set status of device: " + deviceId.getId()); + } + } else { + String msg = "received null deviceIdentifier for setStatus"; + log.error(msg); + throw new DeviceManagementException(msg); } try { boolean success = false; @@ -1120,8 +1226,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public License getLicense(String deviceType, String languageCode) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Get the licence for device type " + deviceType); + if (deviceType != null && languageCode != null) { + if (log.isDebugEnabled()) { + log.debug("Get the licence for device type '" + deviceType + "' languageCode '" + languageCode + "'"); + } + } else { + String msg = "received incomplete data for getLicence"; + log.error(msg); + throw new DeviceManagementException(msg); } DeviceManager deviceManager = this.getDeviceManager(deviceType); License license; @@ -1154,8 +1266,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public void addLicense(String deviceType, License license) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Add the licence for device type " + deviceType); + if (deviceType != null && license != null) { + if (log.isDebugEnabled()) { + log.debug("Add the licence for device type '" + deviceType + "'"); + } + } else { + String msg = "received incomplete data for addLicence"; + log.error(msg); + throw new DeviceManagementException(msg); } DeviceManager deviceManager = this.getDeviceManager(deviceType); if (deviceManager == null) { @@ -1306,8 +1424,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getDevicesOfUser(String username, boolean requireDeviceInfo) throws DeviceManagementException { + if (username == null) { + String msg = "username null in getDevicesOfUser"; + log.error(msg); + throw new DeviceManagementException(msg); + } if (log.isDebugEnabled()) { - log.debug("Get devices of user with username " + username); + log.debug("Get devices of user with username '" + username + "' and requiredDeviceInfo " + requireDeviceInfo); } List userDevices; try { @@ -1344,8 +1467,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getDevicesOfUser(String username, String deviceType, boolean requireDeviceInfo) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Get devices of user with username " + username); + if (username != null && deviceType != null) { + if (log.isDebugEnabled()) { + log.debug("Get '" + deviceType + "' devices of user with username '" + username + "' requiredDeviceInfo: " + + requireDeviceInfo); + } + } else { + String msg = "received incomplete data for getDevicesOfUser"; + log.error(msg); + throw new DeviceManagementException(msg); } List userDevices; try { @@ -1382,8 +1512,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public PaginationResult getDevicesOfUser(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Get paginated results of devices of user " + request.getOwner()); + if (request != null) { + if (log.isDebugEnabled()) { + log.debug("Get paginated results of devices of user " + request.toString() + " and requiredDeviceInfo: " + + requireDeviceInfo); + } + } else { + String msg = "received incomplete pagination request for getDevicesOfUser"; + log.error(msg); + throw new DeviceManagementException(msg); } PaginationResult result = new PaginationResult(); int deviceCount = 0; @@ -1431,8 +1568,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public PaginationResult getDevicesByOwnership(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Get devices by ownership " + request.getOwnership()); + if (request != null) { + if (log.isDebugEnabled()) { + log.debug("Get devices by ownership " + request.toString()); + } + } else { + String msg = "received incomplete data for getDevicesByOwnership"; + log.error(msg); + throw new DeviceManagementException(msg); } PaginationResult result = new PaginationResult(); List allDevices; @@ -1477,8 +1620,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getAllDevicesOfRole(String role, boolean requireDeviceInfo) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Get devices of role " + role); + if (role != null) { + if (log.isDebugEnabled()) { + log.debug("Get devices of role '" + role + "' and requiredDeviceInfo: " + requireDeviceInfo); + } + } else { + String msg = "received empty role for the method getAllDevicesOfRole"; + log.error(msg); + throw new DeviceManagementException(msg); } List devices = new ArrayList<>(); String[] users; @@ -1522,8 +1671,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public int getDeviceCount(String username) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Getting device count of the user " + username); + if (username != null) { + if (log.isDebugEnabled()) { + log.debug("Getting device count of the user '" + username + "'"); + } + } else { + String msg = "received empty username for getDeviceCount"; + log.error(msg); + throw new DeviceManagementException(msg); } try { DeviceManagementDAOFactory.openConnection(); @@ -1573,8 +1728,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getDevicesByNameAndType(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Get devices by name " + request.getDeviceName() + " and type " + request.getDeviceType()); + if (request != null) { + if (log.isDebugEnabled()) { + log.debug("Get devices by name " + request.toString() + " and requiredDeviceInfo: " + requireDeviceInfo); + } + } else { + String msg = "received incomplete data for getDevicesByNameAndType"; + log.error(msg); + throw new DeviceManagementException(msg); } List devices = new ArrayList<>(); List allDevices; @@ -1616,8 +1777,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public PaginationResult getDevicesByName(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Get devices by name " + request.getDeviceName()); + if (request != null) { + if (log.isDebugEnabled()) { + log.debug("Get devices by name " + request.toString() + " requiredDeviceInfo: " + requireDeviceInfo); + } + } else { + String msg = "received incomplete data for getDevicesByName"; + log.error(msg); + throw new DeviceManagementException(msg); } PaginationResult result = new PaginationResult(); int tenantId = this.getTenantId(); @@ -1656,8 +1823,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status status) throws DeviceManagementException { try { - if (log.isDebugEnabled()) { - log.debug("Updating enrolment for device " + device.getDeviceIdentifier() + " of type " + device.getType()); + if (device != null && status != null) { + if (log.isDebugEnabled()) { + log.debug("Updating enrolment for device: " + device.getId() + " of type '" + device.getType() + "'"); + } + } else { + String msg = "received incomplete data for updateDeviceEnrolmentInfo"; + log.error(msg); + throw new DeviceManagementException(msg); } DeviceManagementDAOFactory.beginTransaction(); device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); @@ -1725,7 +1898,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv public List getDevicesByStatus(EnrolmentInfo.Status status, boolean requireDeviceInfo) throws DeviceManagementException { if (log.isDebugEnabled()) { - log.debug("get devices by status"); + log.debug("get devices by status and requiredDeviceInfo: " + requireDeviceInfo); } List allDevices; try { @@ -1759,11 +1932,18 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public PaginationResult getDevicesByStatus(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("get devices by status with pagination"); + if (request != null) { + if (log.isDebugEnabled()) { + log.debug("get devices by status " + request.toString() + " and requiredDeviceInfo: " + + requireDeviceInfo); + } + } else { + String msg = "received incomplete data for getDevicesByStatus"; + log.error(msg); + throw new DeviceManagementException(msg); } PaginationResult result = new PaginationResult(); - List allDevices = new ArrayList<>(); + List allDevices; int tenantId = this.getTenantId(); String status = request.getStatus(); request = DeviceManagerUtil.validateDeviceListPageSize(request); @@ -1827,8 +2007,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean changeDeviceStatus(DeviceIdentifier deviceIdentifier, EnrolmentInfo.Status newStatus) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Change device status of " + deviceIdentifier.getId()); + if (deviceIdentifier != null) { + if (log.isDebugEnabled()) { + log.debug("Change device status of device: " + deviceIdentifier.getId() + " of type '" + + deviceIdentifier.getType() + "'"); + } + } else { + String msg = "received incomplete data for getDevicesByStatus"; + log.error(msg); + throw new DeviceManagementException(msg); } boolean isDeviceUpdated = false; Device device = getDevice(deviceIdentifier, false); @@ -1880,7 +2067,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv private boolean updateEnrollment(int deviceId, EnrolmentInfo enrolmentInfo, int tenantId) throws DeviceManagementException { if (log.isDebugEnabled()) { - log.debug("Update enrollment of device " + deviceId); + log.debug("Update enrollment of device: " + deviceId); } boolean isUpdatedEnrollment = false; boolean isAutoCommit = true; @@ -1943,8 +2130,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv */ private void addDeviceToGroups(DeviceIdentifier deviceIdentifier, EnrolmentInfo.OwnerShip ownership) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Add device " + deviceIdentifier.getId() + " to default group"); + if (deviceIdentifier != null) { + if (log.isDebugEnabled()) { + log.debug("Add device:" + deviceIdentifier.getId() + " to default group"); + } + } else { + String msg = "received incomplete data for addDeviceToGroup"; + log.error(msg); + throw new DeviceManagementException(msg); } GroupManagementProviderService groupManagementProviderService = new GroupManagementProviderServiceImpl(); try { @@ -1970,8 +2163,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } private void addInitialOperations(DeviceIdentifier deviceIdentifier, String deviceType) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Add initial operations to the device " + deviceIdentifier.getId()); + if (deviceIdentifier != null && deviceType != null) { + if (log.isDebugEnabled()) { + log.debug("Add initial operations to the device:" + deviceIdentifier.getId() + " of type '" + + deviceType + "'"); + } + } else { + String msg = "received incomplete data for getDevicesByStatus"; + log.error(msg); + throw new DeviceManagementException(msg); } DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder.getInstance(). getDeviceManagementProvider(); @@ -2018,8 +2218,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv */ private DeviceGroup createDefaultGroup(GroupManagementProviderService service, String groupName) throws GroupManagementException { - if (log.isDebugEnabled()) { - log.debug("Create default group with name " + groupName); + if (service != null && groupName != null) { + if (log.isDebugEnabled()) { + log.debug("Create default group with name '" + groupName + "'"); + } + } else { + String msg = "received incomplete data for createDefaultGroup"; + log.error(msg); + throw new GroupManagementException(msg); } DeviceGroup defaultGroup = service.getGroup(groupName); if (defaultGroup == null) { @@ -2054,8 +2260,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public DeviceType getDeviceType(String deviceType) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("get device type " + deviceType); + if (deviceType != null) { + if (log.isDebugEnabled()) { + log.debug("get device type '" + deviceType + "'"); + } + } else { + String msg = "received null deviceType for getDeviceType"; + log.error(msg); + throw new DeviceManagementException(msg); } try { DeviceManagementDAOFactory.openConnection(); @@ -2113,8 +2325,8 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementService dms = pluginRepository.getDeviceManagementService(deviceIdentifier.getType(), this.getTenantId()); if (dms == null) { - String message = "Device type '" + deviceIdentifier.getType() + "' does not have an associated device management " + - "plugin registered within the framework"; + String message = "Device type '" + deviceIdentifier.getType() + "' does not have an associated " + + "device management plugin registered within the framework"; log.error(message); throw new PullNotificationExecutionFailedException(message); } @@ -2132,8 +2344,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv * Returns all the device-info including location of the given device. */ private DeviceInfo getDeviceInfo(Device device) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Get device info of device " + device.getId() + " of type " + device.getType()); + if (device != null) { + if (log.isDebugEnabled()) { + log.debug("Get device info of device: " + device.getId() + " of type '" + device.getType() + "'"); + } + } else { + String msg = "received incomplete data for getDeviceInfo"; + log.error(msg); + throw new DeviceManagementException(msg); } DeviceInfo info = null; try { @@ -2166,7 +2384,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv */ private List getInstalledApplications(Device device) { if (log.isDebugEnabled()) { - log.debug("Get installed applications of device " + device.getId() + " of type " + device.getType()); + log.debug("Get installed applications of device: " + device.getId() + " of type '" + device.getType() + "'"); } List applications = new ArrayList<>(); try { @@ -2192,8 +2410,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv * of the given device list. */ private List getAllDeviceInfo(List allDevices) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Get all device info of devices"); + if (allDevices.size() > 0) { + if (log.isDebugEnabled()) { + log.debug("Get all device info of devices, num of devices: " + allDevices.size()); + } + } else { + String msg = "received empty device list for getAllDeviceInfo"; + log.error(msg); + throw new DeviceManagementException(msg); } List devices = new ArrayList<>(); if (allDevices != null) { @@ -2226,8 +2450,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv * of a given device. */ private Device getAllDeviceInfo(Device device) throws DeviceManagementException { - if (log.isDebugEnabled()) { - log.debug("Get all device info of device " + device.getId() + " of type " + device.getType()); + if (device != null) { + if (log.isDebugEnabled()) { + log.debug("Get all device info of device: " + device.getId() + " of type '" + device.getType() + "'"); + } + } else { + String msg = "received empty device for getAllDeviceInfo"; + log.error(msg); + throw new DeviceManagementException(msg); } device.setDeviceInfo(this.getDeviceInfo(device)); device.setApplications(this.getInstalledApplications(device)); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index 3f8c679503..592d8a4f65 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -65,8 +65,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public void createGroup(DeviceGroup deviceGroup, String defaultRole, String[] defaultPermissions) throws GroupManagementException, GroupAlreadyExistException { - if (log.isDebugEnabled()) { - log.debug("Creating group" + deviceGroup.getName()); + if (deviceGroup != null) { + if (log.isDebugEnabled()) { + log.debug("Creating group '" + deviceGroup.getName() + "'"); + } + } else { + String msg = "received incomplete data for createGroup"; + log.error(msg); + throw new GroupManagementException(msg); } if (deviceGroup == null) { throw new GroupManagementException("DeviceGroup cannot be null.", new NullPointerException()); @@ -109,8 +115,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public void updateGroup(DeviceGroup deviceGroup, int groupId) throws GroupManagementException, GroupAlreadyExistException { - if (log.isDebugEnabled()) { - log.debug("update group" + deviceGroup.getName()); + if (deviceGroup != null) { + if (log.isDebugEnabled()) { + log.debug("update group '" + deviceGroup.getName() + "'"); + } + } else { + String msg = "received incomplete data for updateGroup"; + log.error(msg); + throw new GroupManagementException(msg); } if (deviceGroup == null) { throw new GroupManagementException("DeviceGroup cannot be null.", new NullPointerException()); @@ -149,7 +161,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public boolean deleteGroup(int groupId) throws GroupManagementException { if (log.isDebugEnabled()) { - log.debug("Delete group " + groupId); + log.debug("Delete group: " + groupId); } DeviceGroup deviceGroup = getGroup(groupId); if (deviceGroup == null) { @@ -187,7 +199,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public DeviceGroup getGroup(int groupId) throws GroupManagementException { if (log.isDebugEnabled()) { - log.debug("Get group by id " + groupId); + log.debug("Get group by id: " + groupId); } DeviceGroup deviceGroup; try { @@ -216,8 +228,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid */ @Override public DeviceGroup getGroup(String groupName) throws GroupManagementException { - if (log.isDebugEnabled()) { - log.debug("Get group by name " + groupName); + if (groupName != null) { + if (log.isDebugEnabled()) { + log.debug("Get group by name '" + groupName + "'"); + } + } else { + String msg = "received empty groupName for getGroup"; + log.error(msg); + throw new GroupManagementException(msg); } DeviceGroup deviceGroup; try { @@ -271,8 +289,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public PaginationResult getGroups(GroupPaginationRequest request) throws GroupManagementException { - if (log.isDebugEnabled()) { - log.debug("Get groups with pagination"); + if (request != null) { + if (log.isDebugEnabled()) { + log.debug("Get groups with pagination " + request.toString()); + } + } else { + String msg = "received incomplete data for getGroup"; + log.error(msg); + throw new GroupManagementException(msg); } request = DeviceManagerUtil.validateGroupListPageSize(request); List deviceGroups = new ArrayList<>(); @@ -303,8 +327,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public List getGroups(String username) throws GroupManagementException { - if (log.isDebugEnabled()) { - log.debug("Get groups of owner " + username); + if (username != null) { + if (log.isDebugEnabled()) { + log.debug("Get groups of owner '" + username + "'"); + } + } else { + String msg = "received null user name for getGroups"; + log.error(msg); + throw new GroupManagementException(msg); } Map groups = new HashMap<>(); UserStoreManager userStoreManager; @@ -344,8 +374,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } private List getGroupIds(String username) throws GroupManagementException { - if (log.isDebugEnabled()) { - log.debug("Get groups Ids of owner " + username); + if (username != null) { + if (log.isDebugEnabled()) { + log.debug("Get groups Ids of owner '" + username + "'"); + } + } else { + String msg = "received empty user name for getGroupIds"; + log.error(msg); + throw new GroupManagementException(msg); } UserStoreManager userStoreManager; List deviceGroupIds = new ArrayList<>(); @@ -381,8 +417,13 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public PaginationResult getGroups(String currentUser, GroupPaginationRequest request) throws GroupManagementException { + if (currentUser == null || request == null) { + String msg = "received incomplete date for getGroups"; + log.error(msg); + throw new GroupManagementException(msg); + } if (log.isDebugEnabled()) { - log.debug("Get all groups of user " + currentUser); + log.debug("Get all groups of user '" + currentUser + "' pagination request " + request.toString()); } request = DeviceManagerUtil.validateGroupListPageSize(request); List allDeviceGroupIdsOfUser = getGroupIds(currentUser); @@ -439,8 +480,13 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } private int getGroupCount(GroupPaginationRequest request) throws GroupManagementException { + if (request == null) { + String msg = "received empty request for getGroupCount"; + log.error(msg); + throw new GroupManagementException(msg); + } if (log.isDebugEnabled()) { - log.debug("Get groups count"); + log.debug("Get groups count, pagination request " + request.toString()); } try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -468,8 +514,13 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid */ @Override public int getGroupCount(String username) throws GroupManagementException { + if (username == null) { + String msg = "received empty user name for getGroupCount"; + log.error(msg); + throw new GroupManagementException(msg); + } if (log.isDebugEnabled()) { - log.debug("Get groups count of " + username); + log.debug("Get groups count of '" + username + "'"); } UserStoreManager userStoreManager; int count; @@ -510,7 +561,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid public void manageGroupSharing(int groupId, List newRoles) throws GroupManagementException, RoleDoesNotExistException { if (log.isDebugEnabled()) { - log.debug("Group sharing for group " + groupId); + log.debug("Manage group sharing for group: " + groupId); } int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); UserStoreManager userStoreManager; @@ -564,7 +615,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public List getRoles(int groupId) throws GroupManagementException { if (log.isDebugEnabled()) { - log.debug("Group roles for group " + groupId); + log.debug("Group roles for group: " + groupId); } try { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); @@ -594,7 +645,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid public List getDevices(int groupId, int startIndex, int rowCount) throws GroupManagementException { if (log.isDebugEnabled()) { - log.debug("Group devices of group " + groupId); + log.debug("Group devices of group: " + groupId + " start index " + startIndex + " row count " + rowCount); } int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); List devices; @@ -630,7 +681,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public int getDeviceCount(int groupId) throws GroupManagementException { if (log.isDebugEnabled()) { - log.debug("Group devices count of group " + groupId); + log.debug("Group devices count of group: " + groupId); } try { GroupManagementDAOFactory.openConnection(); @@ -659,7 +710,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid public void addDevices(int groupId, List deviceIdentifiers) throws GroupManagementException, DeviceNotFoundException { if (log.isDebugEnabled()) { - log.debug("Group devices to the group " + groupId); + log.debug("Group devices to the group: " + groupId); } Device device; try { @@ -705,7 +756,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid public void removeDevice(int groupId, List deviceIdentifiers) throws GroupManagementException, DeviceNotFoundException { if (log.isDebugEnabled()) { - log.debug("Remove devices from the group " + groupId); + log.debug("Remove devices from the group: " + groupId); } Device device; try { @@ -748,7 +799,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public List getGroups(String username, String permission) throws GroupManagementException { if (log.isDebugEnabled()) { - log.debug("Get groups of user " + username); + log.debug("Get groups of user '" + username + "'"); } List deviceGroups = getGroups(username); Map permittedDeviceGroups = new HashMap<>(); @@ -779,6 +830,11 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public List getGroups(DeviceIdentifier deviceIdentifier) throws GroupManagementException { + if (deviceIdentifier == null) { + String msg = "received empty device identifier for getGroups"; + log.error(msg); + throw new GroupManagementException(msg); + } if (log.isDebugEnabled()) { log.debug("Get groups of device " + deviceIdentifier.getId()); } From 10a80fecf81f724134fb6573eab30b30cadc0c0e Mon Sep 17 00:00:00 2001 From: GDLMadushanka Date: Wed, 30 Aug 2017 16:29:08 +0530 Subject: [PATCH 5/5] Refactoring --- .../DeviceManagementProviderServiceImpl.java | 505 +++++++++--------- .../GroupManagementProviderServiceImpl.java | 118 ++-- 2 files changed, 295 insertions(+), 328 deletions(-) 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 5428950483..1b75f2dd4b 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 @@ -160,15 +160,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean enrollDevice(Device device) throws DeviceManagementException { - if (device != null) { - if (log.isDebugEnabled()) { - log.debug("Enrolling the device " + device.getId() + "of type '" + device.getType() + "'"); - } - } else { - String msg = "required values are not set for device enrollment"; + if (device == null) { + String msg = "Received empty device for device enrollment"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Enrolling the device " + device.getId() + "of type '" + device.getType() + "'"); + } boolean status = false; DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); @@ -237,15 +236,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } } catch (DeviceManagementDAOException e) { DeviceManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred while adding enrolment related metadata"; + String msg = "Error occurred while adding enrolment related metadata for device: " + device.getId(); log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (TransactionManagementException e) { - String msg = "Error occurred while initiating transaction"; + String msg = "Error occurred while initiating transaction to enrol device: " + device.getId(); log.error(msg); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred while enrolling device: " + device.getId(); log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -272,7 +271,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred while enrolling device: " + device.getId(); log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -298,16 +297,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean modifyEnrollment(Device device) throws DeviceManagementException { - if (device != null) { - if (log.isDebugEnabled()) { - log.debug("Modifying enrollment for device: " + device.getId() + " of type '" + device.getType() + "'"); - } - } else { - String msg = "required values are not set to modify device enrollment"; + if (device == null) { + String msg = "Required values are not set to modify device enrollment"; log.error(msg); throw new DeviceManagementException(msg); } - + if (log.isDebugEnabled()) { + log.debug("Modifying enrollment for device: " + device.getId() + " of type '" + device.getType() + "'"); + } DeviceManager deviceManager = this.getDeviceManager(device.getType()); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(device.getDeviceIdentifier(), device.getType()); if (deviceManager == null) { @@ -339,11 +336,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg); throw new DeviceManagementException(msg, e); } catch (TransactionManagementException e) { - String msg = "Error occurred while initiating transaction"; + String msg = "Error occurred while initiating transaction to modify device: " + device.getId(); log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred while modifying device: " + device.getId(); log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -354,6 +351,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv private List getEnrollmentsOfUser(int deviceId, String user) throws DeviceManagementException { + if (user == null || user.isEmpty()) { + String msg = "Required values are not set to getEnrollmentsOfUser"; + log.error(msg); + throw new DeviceManagementException(msg); + } + if (log.isDebugEnabled()) { + log.debug("Get enrollments for user '" + user + "' device: " + deviceId); + } List enrolmentInfos = new ArrayList<>(); try { DeviceManagementDAOFactory.openConnection(); @@ -368,7 +373,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getEnrollmentsOfUser user '" + user + "' device: " + deviceId; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -379,15 +384,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean disenrollDevice(DeviceIdentifier deviceId) throws DeviceManagementException { - if (deviceId != null) { - if (log.isDebugEnabled()) { - log.debug("Dis-enrolling device: " + deviceId.getId() + " of type '" + deviceId.getType() + "'"); - } - } else { - String msg = "required values are not set to dis-enroll device"; + if (deviceId == null) { + String msg = "Required values are not set to dis-enroll device"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Dis-enrolling device: " + deviceId.getId() + " of type '" + deviceId.getType() + "'"); + } DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); if (deviceManager == null) { if (log.isDebugEnabled()) { @@ -409,7 +413,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv if (device.getEnrolmentInfo().getStatus().equals(EnrolmentInfo.Status.REMOVED)) { if (log.isDebugEnabled()) { - log.debug("Device has already disenrolled : " + deviceId.getId() + "'"); + log.debug("Device has already dis-enrolled : " + deviceId.getId() + "'"); } return true; } @@ -433,7 +437,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred while dis-enrolling device: " + deviceId.getId(); log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -484,15 +488,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getAllDevices(String deviceType, boolean requireDeviceInfo) throws DeviceManagementException { - if (deviceType != null) { - if (log.isDebugEnabled()) { - log.debug("Getting all devices of type '" + deviceType + "' and requiredDeviceInfo: " + requireDeviceInfo); - } - } else { + if (deviceType == null) { String msg = "Device type is empty for method getAllDevices"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Getting all devices of type '" + deviceType + "' and requiredDeviceInfo: " + requireDeviceInfo); + } List allDevices; try { DeviceManagementDAOFactory.openConnection(); @@ -513,7 +516,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred while getting all devices of device type '" + deviceType + "'"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -548,7 +551,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in get all devices"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -568,16 +571,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getDevices(Date since, boolean requireDeviceInfo) throws DeviceManagementException { - if (since != null) { - if (log.isDebugEnabled()) { - log.debug("Getting all devices since date '" + since.toString() + "' and required device info: " - + requireDeviceInfo); - } - } else { + if (since == null) { String msg = "Given date is empty for method getDevices"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Getting all devices since date '" + since.toString() + "' and required device info: " + + requireDeviceInfo); + } List allDevices; try { DeviceManagementDAOFactory.openConnection(); @@ -591,7 +593,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred get devices since '" + since.toString() + "'"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -611,15 +613,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public PaginationResult getDevicesByType(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { - if (request != null) { - if (log.isDebugEnabled()) { - log.debug("Get devices with pagination " + request.toString() + " and required deviceinfo: " + requireDeviceInfo); - } - } else { - String msg = "received incomplete pagination request for getDevicesByType"; + if (request == null) { + String msg = "Received incomplete pagination request for getDevicesByType"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get devices with pagination " + request.toString() + " and required deviceinfo: " + + requireDeviceInfo); + } PaginationResult paginationResult = new PaginationResult(); List allDevices = new ArrayList<>(); int count = 0; @@ -640,7 +642,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getDeviceByType"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -665,15 +667,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public PaginationResult getAllDevices(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { - if (request != null) { - if (log.isDebugEnabled()) { - log.debug("Get devices with pagination " + request.toString() + " and requiredDeviceInfo: " + requireDeviceInfo); - } - } else { - String msg = "received incomplete pagination request for method getAllDevices"; + if (request == null) { + String msg = "Received incomplete pagination request for method getAllDevices"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get devices with pagination " + request.toString() + " and requiredDeviceInfo: " + requireDeviceInfo); + } List devicesForRoles = null; PaginationResult paginationResult = new PaginationResult(); List allDevices = new ArrayList<>(); @@ -702,7 +703,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getAllDevices"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -721,16 +722,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public Device getDevice(DeviceIdentifier deviceId, boolean requireDeviceInfo) throws DeviceManagementException { - if (deviceId != null) { - if (log.isDebugEnabled()) { - log.debug("Get device by device id :" + deviceId.getId() + " of type '" + deviceId.getType() - + "' and requiredDeviceInfo: " + requireDeviceInfo); - } - } else { - String msg = "received null device identifier for method getDevice"; + if (deviceId == null) { + String msg = "Received null device identifier for method getDevice"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get device by device id :" + deviceId.getId() + " of type '" + deviceId.getType() + + "' and requiredDeviceInfo: " + requireDeviceInfo); + } int tenantId = this.getTenantId(); Device device = this.getDeviceFromCache(deviceId); if (device == null) { @@ -755,7 +755,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getDevice: " + deviceId.getId(); log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -770,15 +770,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public void sendEnrolmentInvitation(String templateName, EmailMetaInfo metaInfo) throws DeviceManagementException { - if (metaInfo != null) { - if (log.isDebugEnabled()) { - log.debug("Send enrollment invitation, templateName '" + templateName + "'"); - } - } else { - String msg = "received incomplete data to method sendEnrolmentInvitation"; + if (metaInfo == null) { + String msg = "Received incomplete data to method sendEnrolmentInvitation"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Send enrollment invitation, templateName '" + templateName + "'"); + } Map, Object>> params = new HashMap<>(); Properties props = metaInfo.getProperties(); Enumeration e = props.propertyNames(); @@ -800,7 +799,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, ex); throw new DeviceManagementException(msg, ex); } catch (Exception ex) { - String msg = "Error occurred"; + String msg = "Error occurred in setEnrollmentInvitation"; log.error(msg, ex); throw new DeviceManagementException(msg, ex); } @@ -808,15 +807,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public void sendRegistrationEmail(EmailMetaInfo metaInfo) throws DeviceManagementException { - if (metaInfo != null) { - if (log.isDebugEnabled()) { - log.debug("Send registration email"); - } - } else { - String msg = "received incomplete request for sendRegistrationEmail"; + if (metaInfo == null) { + String msg = "Received incomplete request for sendRegistrationEmail"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Send registration email"); + } Map, Object>> params = new HashMap<>(); params.put(org.wso2.carbon.device.mgt.core.DeviceManagementConstants.EmailAttributes.FIRST_NAME, new TypedValue, Object>(String.class, metaInfo.getProperty("first-name"))); @@ -843,7 +841,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in sendRegistrationEmail"; log.error(msg, e); throw new DeviceManagementException(msg, e); } @@ -851,16 +849,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public HashMap getTenantedDevice(DeviceIdentifier deviceIdentifier) throws DeviceManagementException { - if (deviceIdentifier != null) { - if (log.isDebugEnabled()) { - log.debug("Get tenanted device with id: " + deviceIdentifier.getId() + " of type '" + - deviceIdentifier.getType() + "'"); - } - } else { - String msg = "received null deviceIdentifier for getTenantedDevice"; + if (deviceIdentifier == null) { + String msg = "Received null deviceIdentifier for getTenantedDevice"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get tenanted device with id: " + deviceIdentifier.getId() + " of type '" + + deviceIdentifier.getType() + "'"); + } HashMap deviceHashMap; try { DeviceManagementDAOFactory.openConnection(); @@ -881,7 +878,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getTenantedDevice device: " + deviceIdentifier.getId(); log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -897,15 +894,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public Device getDeviceWithTypeProperties(DeviceIdentifier deviceId) throws DeviceManagementException { - if (deviceId != null) { - if (log.isDebugEnabled()) { - log.debug("Get tenanted device with type properties, deviceId: " + deviceId.getId()); - } - } else { - String msg = "received null deviceIdentifier for getDeviceWithTypeProperties"; + if (deviceId == null) { + String msg = "Received null deviceIdentifier for getDeviceWithTypeProperties"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get tenanted device with type properties, deviceId: " + deviceId.getId()); + } Device device = this.getDevice(deviceId, false); DeviceManager deviceManager = this.getDeviceManager(device.getType()); @@ -932,16 +928,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public Device getDevice(DeviceIdentifier deviceId, Date since, boolean requireDeviceInfo) throws DeviceManagementException { - if (deviceId != null && since != null) { - if (log.isDebugEnabled()) { - log.debug("Get device since '" + since.toString() + "' with identifier: " + deviceId.getId() - + " and type '" + deviceId.getType() + "'"); - } - } else { - String msg = "received incomplete data for getDevice"; + if (deviceId == null || since == null) { + String msg = "Received incomplete data for getDevice"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get device since '" + since.toString() + "' with identifier: " + deviceId.getId() + + " and type '" + deviceId.getType() + "'"); + } Device device; try { DeviceManagementDAOFactory.openConnection(); @@ -962,7 +957,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getDevice for device: " + deviceId.getId(); log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -982,15 +977,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public Device getDevice(DeviceIdentifier deviceId, EnrolmentInfo.Status status, boolean requireDeviceInfo) throws DeviceManagementException { - if (deviceId != null) { - if (log.isDebugEnabled()) { - log.debug("Get device with identifier: " + deviceId.getId() + " and type '" + deviceId.getType() + "'"); - } - } else { - String msg = "received null deviceIdentifier for getDevice"; + if (deviceId == null) { + String msg = "Received null deviceIdentifier for getDevice"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get device with identifier: " + deviceId.getId() + " and type '" + deviceId.getType() + "'"); + } Device device; try { DeviceManagementDAOFactory.openConnection(); @@ -1011,7 +1005,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getDevice for device: " + deviceId.getId(); log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -1071,7 +1065,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.info(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getAvailableDeviceTypes"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -1082,15 +1076,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean updateDeviceInfo(DeviceIdentifier deviceId, Device device) throws DeviceManagementException { - if (deviceId != null && device != null) { - if (log.isDebugEnabled()) { - log.debug("Update device info of device: " + deviceId.getId()); - } - } else { - String msg = "received incomplete data for updateDeviceInfo"; + if (deviceId == null || device == null) { + String msg = "Received incomplete data for updateDeviceInfo"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Update device info of device: " + deviceId.getId()); + } DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); if (deviceManager == null) { if (log.isDebugEnabled()) { @@ -1104,15 +1097,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean setOwnership(DeviceIdentifier deviceId, String ownershipType) throws DeviceManagementException { - if (deviceId != null) { - if (log.isDebugEnabled()) { - log.debug("Set ownership of device: " + deviceId.getId() + " ownership type '" + ownershipType + "'"); - } - } else { - String msg = "received incomplete data for setOwnership"; + if (deviceId == null) { + String msg = "Received incomplete data for setOwnership"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Set ownership of device: " + deviceId.getId() + " ownership type '" + ownershipType + "'"); + } DeviceManager deviceManager = this.getDeviceManager(deviceId.getType()); if (deviceManager == null) { if (log.isDebugEnabled()) { @@ -1140,15 +1132,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean setStatus(DeviceIdentifier deviceId, String currentOwner, EnrolmentInfo.Status status) throws DeviceManagementException { - if (deviceId != null) { - if (log.isDebugEnabled()) { - log.debug("Set status of device: " + deviceId.getId()); - } - } else { - String msg = "received null deviceIdentifier for setStatus"; + if (deviceId == null) { + String msg = "Received null deviceIdentifier for setStatus"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Set status of device: " + deviceId.getId()); + } try { boolean success = false; int tenantId = this.getTenantId(); @@ -1171,7 +1162,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in setStatus for device :" + deviceId.getId(); log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -1202,7 +1193,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in setStatus"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -1226,15 +1217,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public License getLicense(String deviceType, String languageCode) throws DeviceManagementException { - if (deviceType != null && languageCode != null) { - if (log.isDebugEnabled()) { - log.debug("Get the licence for device type '" + deviceType + "' languageCode '" + languageCode + "'"); - } - } else { - String msg = "received incomplete data for getLicence"; + if (deviceType == null || languageCode == null) { + String msg = "Received incomplete data for getLicence"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get the licence for device type '" + deviceType + "' languageCode '" + languageCode + "'"); + } DeviceManager deviceManager = this.getDeviceManager(deviceType); License license; if (deviceManager == null) { @@ -1258,7 +1248,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getLicence for device type '" + deviceType + "'"; log.error(msg, e); throw new DeviceManagementException(msg, e); } @@ -1266,15 +1256,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public void addLicense(String deviceType, License license) throws DeviceManagementException { - if (deviceType != null && license != null) { - if (log.isDebugEnabled()) { - log.debug("Add the licence for device type '" + deviceType + "'"); - } - } else { - String msg = "received incomplete data for addLicence"; + if (deviceType == null || license == null) { + String msg = "Received incomplete data for addLicence"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Add the licence for device type '" + deviceType + "'"); + } DeviceManager deviceManager = this.getDeviceManager(deviceType); if (deviceManager == null) { if (log.isDebugEnabled()) { @@ -1290,7 +1279,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in addLicence for device type '" + deviceType + "'"; log.error(msg, e); throw new DeviceManagementException(msg, e); } @@ -1425,7 +1414,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getDevicesOfUser(String username, boolean requireDeviceInfo) throws DeviceManagementException { if (username == null) { - String msg = "username null in getDevicesOfUser"; + String msg = "Username null in getDevicesOfUser"; log.error(msg); throw new DeviceManagementException(msg); } @@ -1446,7 +1435,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getDevicesOfUser for username '" + username + "'"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -1467,16 +1456,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getDevicesOfUser(String username, String deviceType, boolean requireDeviceInfo) throws DeviceManagementException { - if (username != null && deviceType != null) { - if (log.isDebugEnabled()) { - log.debug("Get '" + deviceType + "' devices of user with username '" + username + "' requiredDeviceInfo: " - + requireDeviceInfo); - } - } else { - String msg = "received incomplete data for getDevicesOfUser"; + if (username == null || deviceType == null) { + String msg = "Received incomplete data for getDevicesOfUser"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get '" + deviceType + "' devices of user with username '" + username + "' requiredDeviceInfo: " + + requireDeviceInfo); + } List userDevices; try { DeviceManagementDAOFactory.openConnection(); @@ -1491,7 +1479,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getDevicesOfUser for '" + username + "'"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -1512,16 +1500,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public PaginationResult getDevicesOfUser(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { - if (request != null) { - if (log.isDebugEnabled()) { - log.debug("Get paginated results of devices of user " + request.toString() + " and requiredDeviceInfo: " - + requireDeviceInfo); - } - } else { - String msg = "received incomplete pagination request for getDevicesOfUser"; + if (request == null) { + String msg = "Received incomplete pagination request for getDevicesOfUser"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get paginated results of devices of user " + request.toString() + " and requiredDeviceInfo: " + + requireDeviceInfo); + } PaginationResult result = new PaginationResult(); int deviceCount = 0; int tenantId = this.getTenantId(); @@ -1541,7 +1528,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getDevicesOfUser"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -1568,15 +1555,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public PaginationResult getDevicesByOwnership(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { - if (request != null) { - if (log.isDebugEnabled()) { - log.debug("Get devices by ownership " + request.toString()); - } - } else { - String msg = "received incomplete data for getDevicesByOwnership"; + if (request == null) { + String msg = "Received incomplete data for getDevicesByOwnership"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get devices by ownership " + request.toString()); + } PaginationResult result = new PaginationResult(); List allDevices; int deviceCount = 0; @@ -1596,7 +1582,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getDevicesByOwnership"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -1620,15 +1606,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getAllDevicesOfRole(String role, boolean requireDeviceInfo) throws DeviceManagementException { - if (role != null) { - if (log.isDebugEnabled()) { - log.debug("Get devices of role '" + role + "' and requiredDeviceInfo: " + requireDeviceInfo); - } - } else { - String msg = "received empty role for the method getAllDevicesOfRole"; + if (role == null || role.isEmpty()) { + String msg = "Received empty role for the method getAllDevicesOfRole"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get devices of role '" + role + "' and requiredDeviceInfo: " + requireDeviceInfo); + } List devices = new ArrayList<>(); String[] users; int tenantId = this.getTenantId(); @@ -1640,7 +1625,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getAllDevicesOfRole for role '" + role + "'"; log.error(msg, e); throw new DeviceManagementException(msg, e); } @@ -1656,7 +1641,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred getAllDevicesOfRole for role '" + role + "'"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -1671,15 +1656,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public int getDeviceCount(String username) throws DeviceManagementException { - if (username != null) { - if (log.isDebugEnabled()) { - log.debug("Getting device count of the user '" + username + "'"); - } - } else { - String msg = "received empty username for getDeviceCount"; + if (username == null) { + String msg = "Received empty username for getDeviceCount"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Getting device count of the user '" + username + "'"); + } try { DeviceManagementDAOFactory.openConnection(); return deviceDAO.getDeviceCount(username, this.getTenantId()); @@ -1692,7 +1676,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getDeviceCount for username '" + username + "'"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -1717,7 +1701,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getDeviceCount"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -1728,15 +1712,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public List getDevicesByNameAndType(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { - if (request != null) { - if (log.isDebugEnabled()) { - log.debug("Get devices by name " + request.toString() + " and requiredDeviceInfo: " + requireDeviceInfo); - } - } else { - String msg = "received incomplete data for getDevicesByNameAndType"; + if (request == null) { + String msg = "Received incomplete data for getDevicesByNameAndType"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get devices by name " + request.toString() + " and requiredDeviceInfo: " + requireDeviceInfo); + } List devices = new ArrayList<>(); List allDevices; int limit = DeviceManagerUtil.validateDeviceListPageSize(request.getRowCount()); @@ -1756,7 +1739,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getDevicesByNameAndType"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -1777,15 +1760,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public PaginationResult getDevicesByName(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { - if (request != null) { - if (log.isDebugEnabled()) { - log.debug("Get devices by name " + request.toString() + " requiredDeviceInfo: " + requireDeviceInfo); - } - } else { - String msg = "received incomplete data for getDevicesByName"; + if (request == null) { + String msg = "Received incomplete data for getDevicesByName"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get devices by name " + request.toString() + " requiredDeviceInfo: " + requireDeviceInfo); + } PaginationResult result = new PaginationResult(); int tenantId = this.getTenantId(); List allDevices = new ArrayList<>(); @@ -1806,7 +1788,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getDevicesByName"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -1823,15 +1805,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public void updateDeviceEnrolmentInfo(Device device, EnrolmentInfo.Status status) throws DeviceManagementException { try { - if (device != null && status != null) { - if (log.isDebugEnabled()) { - log.debug("Updating enrolment for device: " + device.getId() + " of type '" + device.getType() + "'"); - } - } else { - String msg = "received incomplete data for updateDeviceEnrolmentInfo"; + if (device == null || status == null) { + String msg = "Received incomplete data for updateDeviceEnrolmentInfo"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Updating enrolment for device: " + device.getId() + " of type '" + device.getType() + "'"); + } DeviceManagementDAOFactory.beginTransaction(); device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); device.getEnrolmentInfo().setStatus(status); @@ -1848,7 +1829,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in updateDeviceEnrolmentInfo"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -1868,7 +1849,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv deviceManagementService.getType() + "'"; log.error(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in registerDeviceManagementService"; log.error(msg, e); } } @@ -1884,7 +1865,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error("Error occurred while un-registering device management plugin '" + deviceManagementService.getType() + "'", e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in unregisterDeviceManagementService"; log.error(msg, e); } } @@ -1912,7 +1893,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getDevicesByStatus"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -1932,16 +1913,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public PaginationResult getDevicesByStatus(PaginationRequest request, boolean requireDeviceInfo) throws DeviceManagementException { - if (request != null) { - if (log.isDebugEnabled()) { - log.debug("get devices by status " + request.toString() + " and requiredDeviceInfo: " - + requireDeviceInfo); - } - } else { - String msg = "received incomplete data for getDevicesByStatus"; + if (request == null) { + String msg = "Received incomplete data for getDevicesByStatus"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get devices by status " + request.toString() + " and requiredDeviceInfo: " + + requireDeviceInfo); + } PaginationResult result = new PaginationResult(); List allDevices; int tenantId = this.getTenantId(); @@ -1962,7 +1942,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getDevicesByStatus"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -2007,16 +1987,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv @Override public boolean changeDeviceStatus(DeviceIdentifier deviceIdentifier, EnrolmentInfo.Status newStatus) throws DeviceManagementException { - if (deviceIdentifier != null) { - if (log.isDebugEnabled()) { - log.debug("Change device status of device: " + deviceIdentifier.getId() + " of type '" - + deviceIdentifier.getType() + "'"); - } - } else { - String msg = "received incomplete data for getDevicesByStatus"; + if (deviceIdentifier == null) { + String msg = "Received incomplete data for getDevicesByStatus"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Change device status of device: " + deviceIdentifier.getId() + " of type '" + + deviceIdentifier.getType() + "'"); + } boolean isDeviceUpdated = false; Device device = getDevice(deviceIdentifier, false); int deviceId = device.getId(); @@ -2056,7 +2035,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getDeviceEnrolledTenants"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -2089,7 +2068,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in updateEnrollment for deviceId: " + deviceId; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -2130,15 +2109,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv */ private void addDeviceToGroups(DeviceIdentifier deviceIdentifier, EnrolmentInfo.OwnerShip ownership) throws DeviceManagementException { - if (deviceIdentifier != null) { - if (log.isDebugEnabled()) { - log.debug("Add device:" + deviceIdentifier.getId() + " to default group"); - } - } else { - String msg = "received incomplete data for addDeviceToGroup"; + if (deviceIdentifier == null) { + String msg = "Received incomplete data for addDeviceToGroup"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Add device:" + deviceIdentifier.getId() + " to default group"); + } GroupManagementProviderService groupManagementProviderService = new GroupManagementProviderServiceImpl(); try { DeviceGroup defaultGroup = createDefaultGroup(groupManagementProviderService, ownership.toString()); @@ -2163,16 +2141,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } private void addInitialOperations(DeviceIdentifier deviceIdentifier, String deviceType) throws DeviceManagementException { - if (deviceIdentifier != null && deviceType != null) { - if (log.isDebugEnabled()) { - log.debug("Add initial operations to the device:" + deviceIdentifier.getId() + " of type '" - + deviceType + "'"); - } - } else { - String msg = "received incomplete data for getDevicesByStatus"; + if (deviceIdentifier == null || deviceType == null) { + String msg = "Received incomplete data for getDevicesByStatus"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Add initial operations to the device:" + deviceIdentifier.getId() + " of type '" + + deviceType + "'"); + } DeviceManagementProviderService deviceManagementProviderService = DeviceManagementDataHolder.getInstance(). getDeviceManagementProvider(); DeviceManagementService deviceManagementService = @@ -2218,15 +2195,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv */ private DeviceGroup createDefaultGroup(GroupManagementProviderService service, String groupName) throws GroupManagementException { - if (service != null && groupName != null) { - if (log.isDebugEnabled()) { - log.debug("Create default group with name '" + groupName + "'"); - } - } else { - String msg = "received incomplete data for createDefaultGroup"; + if (service == null || groupName == null) { + String msg = "Received incomplete data for createDefaultGroup"; log.error(msg); throw new GroupManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Create default group with name '" + groupName + "'"); + } DeviceGroup defaultGroup = service.getGroup(groupName); if (defaultGroup == null) { defaultGroup = new DeviceGroup(groupName); @@ -2262,10 +2238,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv public DeviceType getDeviceType(String deviceType) throws DeviceManagementException { if (deviceType != null) { if (log.isDebugEnabled()) { - log.debug("get device type '" + deviceType + "'"); + log.debug("Get device type '" + deviceType + "'"); } } else { - String msg = "received null deviceType for getDeviceType"; + String msg = "Received null deviceType for getDeviceType"; log.error(msg); throw new DeviceManagementException(msg); } @@ -2308,7 +2284,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getDeviceTypes"; log.error(msg, e); throw new DeviceManagementException(msg, e); } finally { @@ -2344,15 +2320,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv * Returns all the device-info including location of the given device. */ private DeviceInfo getDeviceInfo(Device device) throws DeviceManagementException { - if (device != null) { - if (log.isDebugEnabled()) { - log.debug("Get device info of device: " + device.getId() + " of type '" + device.getType() + "'"); - } - } else { - String msg = "received incomplete data for getDeviceInfo"; + if (device == null) { + String msg = "Received incomplete data for getDeviceInfo"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get device info of device: " + device.getId() + " of type '" + device.getType() + "'"); + } DeviceInfo info = null; try { DeviceManagementDAOFactory.openConnection(); @@ -2371,7 +2346,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv log.error(msg, e); throw new DeviceManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getDeviceInfo for device: " + device.getId(); log.error(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); @@ -2397,7 +2372,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } catch (SQLException e) { log.error("Error occurred while opening a connection to the data source", e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getInstalledApplications"; log.error(msg, e); } finally { DeviceManagementDAOFactory.closeConnection(); @@ -2410,15 +2385,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv * of the given device list. */ private List getAllDeviceInfo(List allDevices) throws DeviceManagementException { - if (allDevices.size() > 0) { - if (log.isDebugEnabled()) { - log.debug("Get all device info of devices, num of devices: " + allDevices.size()); - } - } else { - String msg = "received empty device list for getAllDeviceInfo"; + if (allDevices.size() == 0) { + String msg = "Received empty device list for getAllDeviceInfo"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get all device info of devices, num of devices: " + allDevices.size()); + } List devices = new ArrayList<>(); if (allDevices != null) { for (Device device : allDevices) { @@ -2450,15 +2424,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv * of a given device. */ private Device getAllDeviceInfo(Device device) throws DeviceManagementException { - if (device != null) { - if (log.isDebugEnabled()) { - log.debug("Get all device info of device: " + device.getId() + " of type '" + device.getType() + "'"); - } - } else { - String msg = "received empty device for getAllDeviceInfo"; + if (device == null) { + String msg = "Received empty device for getAllDeviceInfo"; log.error(msg); throw new DeviceManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get all device info of device: " + device.getId() + " of type '" + device.getType() + "'"); + } device.setDeviceInfo(this.getDeviceInfo(device)); device.setApplications(this.getInstalledApplications(device)); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index 592d8a4f65..b5a2a4f635 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -65,15 +65,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public void createGroup(DeviceGroup deviceGroup, String defaultRole, String[] defaultPermissions) throws GroupManagementException, GroupAlreadyExistException { - if (deviceGroup != null) { - if (log.isDebugEnabled()) { - log.debug("Creating group '" + deviceGroup.getName() + "'"); - } - } else { - String msg = "received incomplete data for createGroup"; + if (deviceGroup == null) { + String msg = "Received incomplete data for createGroup"; log.error(msg); throw new GroupManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Creating group '" + deviceGroup.getName() + "'"); + } if (deviceGroup == null) { throw new GroupManagementException("DeviceGroup cannot be null.", new NullPointerException()); } @@ -97,7 +96,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in creating group '" + deviceGroup.getName() + "'"; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { @@ -115,15 +114,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public void updateGroup(DeviceGroup deviceGroup, int groupId) throws GroupManagementException, GroupAlreadyExistException { - if (deviceGroup != null) { - if (log.isDebugEnabled()) { - log.debug("update group '" + deviceGroup.getName() + "'"); - } - } else { - String msg = "received incomplete data for updateGroup"; + if (deviceGroup == null) { + String msg = "Received incomplete data for updateGroup"; log.error(msg); throw new GroupManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("update group '" + deviceGroup.getName() + "'"); + } if (deviceGroup == null) { throw new GroupManagementException("DeviceGroup cannot be null.", new NullPointerException()); } @@ -147,7 +145,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in updating group '" + deviceGroup.getName() + "'"; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { @@ -185,7 +183,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in deleting group: " + groupId; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { @@ -214,7 +212,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getGroup for groupId: " + groupId; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { @@ -228,15 +226,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid */ @Override public DeviceGroup getGroup(String groupName) throws GroupManagementException { - if (groupName != null) { - if (log.isDebugEnabled()) { - log.debug("Get group by name '" + groupName + "'"); - } - } else { - String msg = "received empty groupName for getGroup"; + if (groupName == null) { + String msg = "Received empty groupName for getGroup"; log.error(msg); throw new GroupManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get group by name '" + groupName + "'"); + } DeviceGroup deviceGroup; try { GroupManagementDAOFactory.openConnection(); @@ -250,7 +247,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getGroup with name " + groupName; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { @@ -278,7 +275,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(msg); throw new GroupManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getGroups"; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { @@ -289,15 +286,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public PaginationResult getGroups(GroupPaginationRequest request) throws GroupManagementException { - if (request != null) { - if (log.isDebugEnabled()) { - log.debug("Get groups with pagination " + request.toString()); - } - } else { - String msg = "received incomplete data for getGroup"; + if (request == null) { + String msg = "Received incomplete data for getGroup"; log.error(msg); throw new GroupManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get groups with pagination " + request.toString()); + } request = DeviceManagerUtil.validateGroupListPageSize(request); List deviceGroups = new ArrayList<>(); try { @@ -313,7 +309,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getGroups"; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { @@ -327,15 +323,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public List getGroups(String username) throws GroupManagementException { - if (username != null) { - if (log.isDebugEnabled()) { - log.debug("Get groups of owner '" + username + "'"); - } - } else { - String msg = "received null user name for getGroups"; + if (username == null || username.isEmpty()) { + String msg = "Received null user name for getGroups"; log.error(msg); throw new GroupManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get groups of owner '" + username + "'"); + } Map groups = new HashMap<>(); UserStoreManager userStoreManager; try { @@ -364,7 +359,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(e); throw new GroupManagementException(e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getGroups for " + username; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { @@ -374,15 +369,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid } private List getGroupIds(String username) throws GroupManagementException { - if (username != null) { - if (log.isDebugEnabled()) { - log.debug("Get groups Ids of owner '" + username + "'"); - } - } else { - String msg = "received empty user name for getGroupIds"; + if (username == null || username.isEmpty()) { + String msg = "Received empty user name for getGroupIds"; log.error(msg); throw new GroupManagementException(msg); } + if (log.isDebugEnabled()) { + log.debug("Get groups Ids of owner '" + username + "'"); + } UserStoreManager userStoreManager; List deviceGroupIds = new ArrayList<>(); try { @@ -405,7 +399,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(e); throw new GroupManagementException(e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getGroups for username '" + username + "'"; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { @@ -418,7 +412,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid public PaginationResult getGroups(String currentUser, GroupPaginationRequest request) throws GroupManagementException { if (currentUser == null || request == null) { - String msg = "received incomplete date for getGroups"; + String msg = "Received incomplete date for getGroups"; log.error(msg); throw new GroupManagementException(msg); } @@ -441,7 +435,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getGroups"; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { @@ -481,7 +475,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid private int getGroupCount(GroupPaginationRequest request) throws GroupManagementException { if (request == null) { - String msg = "received empty request for getGroupCount"; + String msg = "Received empty request for getGroupCount"; log.error(msg); throw new GroupManagementException(msg); } @@ -501,7 +495,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getGroupCount"; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { @@ -514,8 +508,8 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid */ @Override public int getGroupCount(String username) throws GroupManagementException { - if (username == null) { - String msg = "received empty user name for getGroupCount"; + if (username == null || username.isEmpty()) { + String msg = "Received empty user name for getGroupCount"; log.error(msg); throw new GroupManagementException(msg); } @@ -546,7 +540,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getGroupCount for username '" + username + "'"; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { @@ -601,7 +595,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(e); throw new GroupManagementException(e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in manageGroupSharing for groupId: " + groupId; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { @@ -630,7 +624,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getRoles for groupId: " + groupId; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { @@ -666,7 +660,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getDevices for groupId: " + groupId; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { @@ -695,7 +689,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getDeviceCount for groupId: " + groupId; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { @@ -741,7 +735,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in addDevices for groupId " + groupId; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { @@ -785,7 +779,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in removeDevice for groupId: " + groupId; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { @@ -821,7 +815,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getGroups for username '" + username + "'"; log.error(msg, e); throw new GroupManagementException(msg, e); } @@ -831,7 +825,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid @Override public List getGroups(DeviceIdentifier deviceIdentifier) throws GroupManagementException { if (deviceIdentifier == null) { - String msg = "received empty device identifier for getGroups"; + String msg = "Received empty device identifier for getGroups"; log.error(msg); throw new GroupManagementException(msg); } @@ -857,7 +851,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in getGroups"; log.error(msg, e); throw new GroupManagementException(msg, e); } finally { @@ -887,7 +881,7 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid log.error(msg, e); throw new GroupManagementException(msg, e); } catch (Exception e) { - String msg = "Error occurred"; + String msg = "Error occurred in createDefaultGroup for groupName '" + groupName + "'"; log.error(msg, e); throw new GroupManagementException(msg, e); }