From 4bed9cb1ffd28ee03f506bb3846521ae2e14d413 Mon Sep 17 00:00:00 2001 From: Menaka Jayawardena Date: Thu, 26 Oct 2017 00:18:56 +0530 Subject: [PATCH] Code formatting and refactoring for Permission Update Listeners of device types. --- .../AndroidSensePermissionUpdateListener.java | 39 +++++++------------ .../ArduinoPermissionUpdateListener.java | 28 +++++-------- .../RaspberryPIPermissionUpdateListener.java | 27 +++++-------- ...tualFireAlarmPermissionUpdateListener.java | 29 ++++++-------- 4 files changed, 46 insertions(+), 77 deletions(-) diff --git a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/listener/AndroidSensePermissionUpdateListener.java b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/listener/AndroidSensePermissionUpdateListener.java index 4cb0ccc9e..b29458e95 100644 --- a/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/listener/AndroidSensePermissionUpdateListener.java +++ b/components/device-types/androidsense-plugin/org.wso2.carbon.device.mgt.iot.androidsense.api/src/main/java/org/wso2/carbon/device/mgt/iot/androidsense/service/impl/listener/AndroidSensePermissionUpdateListener.java @@ -32,10 +32,13 @@ import javax.servlet.ServletContextListener; public class AndroidSensePermissionUpdateListener implements ServletContextListener { private static Log log = LogFactory.getLog(AndroidSensePermissionUpdateListener.class); + private static PrivilegedCarbonContext threadLocalCarbonContext; + private static RealmService realmService; @Override public void contextInitialized(ServletContextEvent servletContextEvent) { - + threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + realmService = (RealmService) threadLocalCarbonContext.getOSGiService(RealmService.class, null); UserStoreManager userStoreManager = getUserStoreManager(); try { if (userStoreManager != null) { @@ -47,7 +50,8 @@ public class AndroidSensePermissionUpdateListener implements ServletContextListe getAuthorizationManager().authorizeRole(AndroidSenseConstants.ROLE_NAME, AndroidSenseConstants.PERM_OWNING_DEVICE_VIEW, CarbonConstants.UI_PERMISSION_ACTION); } - } } catch (UserStoreException e) { + } + } catch (UserStoreException e) { log.error("Error while creating a role and adding a user for Android Sense.", e); } } @@ -57,57 +61,44 @@ public class AndroidSensePermissionUpdateListener implements ServletContextListe } - public static UserStoreManager getUserStoreManager() { - RealmService realmService; + private UserStoreManager getUserStoreManager() { UserStoreManager userStoreManager; try { - PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - realmService = (RealmService) ctx.getOSGiService(RealmService.class, null); if (realmService == null) { String msg = "Realm service has not initialized."; - log.error(msg); throw new IllegalStateException(msg); } - int tenantId = ctx.getTenantId(); + int tenantId = threadLocalCarbonContext.getTenantId(); userStoreManager = realmService.getTenantUserRealm(tenantId).getUserStoreManager(); realmService.getTenantUserRealm(tenantId).getAuthorizationManager(); } catch (UserStoreException e) { String msg = "Error occurred while retrieving current user store manager"; - log.error(msg, e); throw new IllegalStateException(msg); } return userStoreManager; } - public static AuthorizationManager getAuthorizationManager() { - RealmService realmService; + private AuthorizationManager getAuthorizationManager() { AuthorizationManager authorizationManager; try { - PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - realmService = (RealmService) ctx.getOSGiService(RealmService.class, null); if (realmService == null) { String msg = "Realm service has not initialized."; - log.error(msg); throw new IllegalStateException(msg); } - int tenantId = ctx.getTenantId(); + int tenantId = threadLocalCarbonContext.getTenantId(); authorizationManager = realmService.getTenantUserRealm(tenantId).getAuthorizationManager(); } catch (UserStoreException e) { String msg = "Error occurred while retrieving current user store manager"; - log.error(msg, e); throw new IllegalStateException(msg); } return authorizationManager; } private Permission[] getPermissions() { - - Permission androidSense = new Permission(AndroidSenseConstants.PERM_ENROLL_ANDROID_SENSE, - CarbonConstants.UI_PERMISSION_ACTION); - Permission view = new Permission(AndroidSenseConstants.PERM_OWNING_DEVICE_VIEW, CarbonConstants - .UI_PERMISSION_ACTION); - - return new Permission[]{androidSense, view}; + Permission androidSense = new Permission(AndroidSenseConstants.PERM_ENROLL_ANDROID_SENSE, + CarbonConstants.UI_PERMISSION_ACTION); + Permission view = new Permission(AndroidSenseConstants.PERM_OWNING_DEVICE_VIEW, + CarbonConstants.UI_PERMISSION_ACTION); + return new Permission[]{androidSense, view}; } - } diff --git a/components/device-types/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/listener/ArduinoPermissionUpdateListener.java b/components/device-types/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/listener/ArduinoPermissionUpdateListener.java index d45bc1a16..d06a7dda9 100644 --- a/components/device-types/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/listener/ArduinoPermissionUpdateListener.java +++ b/components/device-types/arduino-plugin/org.wso2.carbon.device.mgt.iot.arduino.api/src/main/java/org/wso2/carbon/device/mgt/iot/arduino/service/impl/listener/ArduinoPermissionUpdateListener.java @@ -32,9 +32,13 @@ import javax.servlet.ServletContextListener; public class ArduinoPermissionUpdateListener implements ServletContextListener { private static Log log = LogFactory.getLog(ArduinoPermissionUpdateListener.class); + private static PrivilegedCarbonContext threadLocalCarbonContext; + private static RealmService realmService; @Override public void contextInitialized(ServletContextEvent servletContextEvent) { + threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + realmService = (RealmService) threadLocalCarbonContext.getOSGiService(RealmService.class, null); UserStoreManager userStoreManager = getUserStoreManager(); try { if (userStoreManager != null) { @@ -57,44 +61,34 @@ public class ArduinoPermissionUpdateListener implements ServletContextListener { } - public static UserStoreManager getUserStoreManager() { - RealmService realmService; + private UserStoreManager getUserStoreManager() { UserStoreManager userStoreManager; try { - PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - realmService = (RealmService) ctx.getOSGiService(RealmService.class, null); if (realmService == null) { String msg = "Realm service has not initialized."; - log.error(msg); throw new IllegalStateException(msg); } - int tenantId = ctx.getTenantId(); + int tenantId = threadLocalCarbonContext.getTenantId(); userStoreManager = realmService.getTenantUserRealm(tenantId).getUserStoreManager(); realmService.getTenantUserRealm(tenantId).getAuthorizationManager(); } catch (UserStoreException e) { String msg = "Error occurred while retrieving current user store manager"; - log.error(msg, e); throw new IllegalStateException(msg); } return userStoreManager; } - public static AuthorizationManager getAuthorizationManager() { - RealmService realmService; + private AuthorizationManager getAuthorizationManager() { AuthorizationManager authorizationManager; try { - PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - realmService = (RealmService) ctx.getOSGiService(RealmService.class, null); if (realmService == null) { String msg = "Realm service has not initialized."; - log.error(msg); throw new IllegalStateException(msg); } - int tenantId = ctx.getTenantId(); + int tenantId = threadLocalCarbonContext.getTenantId(); authorizationManager = realmService.getTenantUserRealm(tenantId).getAuthorizationManager(); } catch (UserStoreException e) { String msg = "Error occurred while retrieving current user store manager"; - log.error(msg, e); throw new IllegalStateException(msg); } return authorizationManager; @@ -103,10 +97,8 @@ public class ArduinoPermissionUpdateListener implements ServletContextListener { private Permission[] getPermissions() { Permission androidSense = new Permission(ArduinoConstants.PERM_ENROLL_ARDUINO, CarbonConstants.UI_PERMISSION_ACTION); - Permission view = new Permission(ArduinoConstants.PERM_OWNING_DEVICE_VIEW, CarbonConstants - .UI_PERMISSION_ACTION); - + Permission view = new Permission(ArduinoConstants.PERM_OWNING_DEVICE_VIEW, + CarbonConstants.UI_PERMISSION_ACTION); return new Permission[]{androidSense, view}; } - } diff --git a/components/device-types/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/listener/RaspberryPIPermissionUpdateListener.java b/components/device-types/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/listener/RaspberryPIPermissionUpdateListener.java index 5404d0e39..704ef518f 100644 --- a/components/device-types/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/listener/RaspberryPIPermissionUpdateListener.java +++ b/components/device-types/raspberrypi-plugin/org.wso2.carbon.device.mgt.iot.raspberrypi.api/src/main/java/org/wso2/carbon/device/mgt/iot/raspberrypi/service/impl/listener/RaspberryPIPermissionUpdateListener.java @@ -32,9 +32,13 @@ import javax.servlet.ServletContextListener; public class RaspberryPIPermissionUpdateListener implements ServletContextListener { private static Log log = LogFactory.getLog(RaspberryPIPermissionUpdateListener.class); + private static PrivilegedCarbonContext threadLocalCarbonContext; + private static RealmService realmService; @Override public void contextInitialized(ServletContextEvent servletContextEvent) { + threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + realmService = (RealmService) threadLocalCarbonContext.getOSGiService(RealmService.class, null); UserStoreManager userStoreManager = getUserStoreManager(); try { if (userStoreManager != null) { @@ -57,44 +61,34 @@ public class RaspberryPIPermissionUpdateListener implements ServletContextListen } - public static UserStoreManager getUserStoreManager() { - RealmService realmService; + private UserStoreManager getUserStoreManager() { UserStoreManager userStoreManager; try { - PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - realmService = (RealmService) ctx.getOSGiService(RealmService.class, null); if (realmService == null) { String msg = "Realm service has not initialized."; - log.error(msg); throw new IllegalStateException(msg); } - int tenantId = ctx.getTenantId(); + int tenantId = threadLocalCarbonContext.getTenantId(); userStoreManager = realmService.getTenantUserRealm(tenantId).getUserStoreManager(); realmService.getTenantUserRealm(tenantId).getAuthorizationManager(); } catch (UserStoreException e) { String msg = "Error occurred while retrieving current user store manager"; - log.error(msg, e); throw new IllegalStateException(msg); } return userStoreManager; } - public static AuthorizationManager getAuthorizationManager() { - RealmService realmService; + private AuthorizationManager getAuthorizationManager() { AuthorizationManager authorizationManager; try { - PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - realmService = (RealmService) ctx.getOSGiService(RealmService.class, null); if (realmService == null) { String msg = "Realm service has not initialized."; - log.error(msg); throw new IllegalStateException(msg); } - int tenantId = ctx.getTenantId(); + int tenantId = threadLocalCarbonContext.getTenantId(); authorizationManager = realmService.getTenantUserRealm(tenantId).getAuthorizationManager(); } catch (UserStoreException e) { String msg = "Error occurred while retrieving current user store manager"; - log.error(msg, e); throw new IllegalStateException(msg); } return authorizationManager; @@ -103,9 +97,8 @@ public class RaspberryPIPermissionUpdateListener implements ServletContextListen private Permission[] getPermissions() { Permission androidSense = new Permission(RaspberrypiConstants.PERM_ENROLL_RASPBERRYPI, CarbonConstants.UI_PERMISSION_ACTION); - Permission view = new Permission(RaspberrypiConstants.PERM_OWNING_DEVICE_VIEW, CarbonConstants - .UI_PERMISSION_ACTION); + Permission view = new Permission(RaspberrypiConstants.PERM_OWNING_DEVICE_VIEW, + CarbonConstants.UI_PERMISSION_ACTION); return new Permission[]{androidSense, view}; } - } diff --git a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/listener/VirtualFireAlarmPermissionUpdateListener.java b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/listener/VirtualFireAlarmPermissionUpdateListener.java index 92000ad00..16a6897b3 100644 --- a/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/listener/VirtualFireAlarmPermissionUpdateListener.java +++ b/components/device-types/virtual-fire-alarm-plugin/org.wso2.carbon.device.mgt.iot.virtualfirealarm.api/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/impl/listener/VirtualFireAlarmPermissionUpdateListener.java @@ -32,9 +32,13 @@ import javax.servlet.ServletContextListener; public class VirtualFireAlarmPermissionUpdateListener implements ServletContextListener { private static Log log = LogFactory.getLog(VirtualFireAlarmPermissionUpdateListener.class); + private static PrivilegedCarbonContext threadLocalCarbonContext; + private static RealmService realmService; @Override public void contextInitialized(ServletContextEvent servletContextEvent) { + threadLocalCarbonContext = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + realmService = (RealmService) threadLocalCarbonContext.getOSGiService(RealmService.class, null); UserStoreManager userStoreManager = getUserStoreManager(); try { if (userStoreManager != null) { @@ -57,44 +61,34 @@ public class VirtualFireAlarmPermissionUpdateListener implements ServletContextL } - public static UserStoreManager getUserStoreManager() { - RealmService realmService; + private UserStoreManager getUserStoreManager() { UserStoreManager userStoreManager; try { - PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - realmService = (RealmService) ctx.getOSGiService(RealmService.class, null); if (realmService == null) { String msg = "Realm service has not initialized."; - log.error(msg); throw new IllegalStateException(msg); } - int tenantId = ctx.getTenantId(); + int tenantId = threadLocalCarbonContext.getTenantId(); userStoreManager = realmService.getTenantUserRealm(tenantId).getUserStoreManager(); realmService.getTenantUserRealm(tenantId).getAuthorizationManager(); } catch (UserStoreException e) { String msg = "Error occurred while retrieving current user store manager"; - log.error(msg, e); throw new IllegalStateException(msg); } return userStoreManager; } - public static AuthorizationManager getAuthorizationManager() { - RealmService realmService; + private AuthorizationManager getAuthorizationManager() { AuthorizationManager authorizationManager; try { - PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); - realmService = (RealmService) ctx.getOSGiService(RealmService.class, null); if (realmService == null) { String msg = "Realm service has not initialized."; - log.error(msg); throw new IllegalStateException(msg); } - int tenantId = ctx.getTenantId(); + int tenantId = threadLocalCarbonContext.getTenantId(); authorizationManager = realmService.getTenantUserRealm(tenantId).getAuthorizationManager(); } catch (UserStoreException e) { String msg = "Error occurred while retrieving current user store manager"; - log.error(msg, e); throw new IllegalStateException(msg); } return authorizationManager; @@ -103,9 +97,8 @@ public class VirtualFireAlarmPermissionUpdateListener implements ServletContextL private Permission[] getPermissions() { Permission androidSense = new Permission(VirtualFireAlarmConstants.PERM_ENROLL_FIRE_ALARM, CarbonConstants.UI_PERMISSION_ACTION); - Permission view = new Permission(VirtualFireAlarmConstants.PERM_OWNING_DEVICE_VIEW, CarbonConstants - .UI_PERMISSION_ACTION); + Permission view = new Permission(VirtualFireAlarmConstants.PERM_OWNING_DEVICE_VIEW, + CarbonConstants.UI_PERMISSION_ACTION); return new Permission[]{androidSense, view}; } - -} +} \ No newline at end of file