From b8f085da20cdf13e61e2aeae5b767ddcd18a5cd8 Mon Sep 17 00:00:00 2001 From: Harshan Liyanage Date: Mon, 19 Jun 2017 16:58:42 +0530 Subject: [PATCH] Added null checks for device cache in-case cache is disabled. --- .../cache/impl/DeviceCacheManagerImpl.java | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/impl/DeviceCacheManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/impl/DeviceCacheManagerImpl.java index 6759ef7e5b..2bcd5e2797 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/impl/DeviceCacheManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/cache/impl/DeviceCacheManagerImpl.java @@ -55,29 +55,35 @@ public class DeviceCacheManagerImpl implements DeviceCacheManager { @Override public void addDeviceToCache(DeviceIdentifier deviceIdentifier, Device device, int tenantId) { Cache lCache = DeviceManagerUtil.getDeviceCache(); - DeviceCacheKey cacheKey = getCacheKey(deviceIdentifier, tenantId); - if (lCache.containsKey(cacheKey)) { - this.updateDeviceInCache(deviceIdentifier, device, tenantId); - } else { - lCache.put(cacheKey, device); + if (lCache != null) { + DeviceCacheKey cacheKey = getCacheKey(deviceIdentifier, tenantId); + if (lCache.containsKey(cacheKey)) { + this.updateDeviceInCache(deviceIdentifier, device, tenantId); + } else { + lCache.put(cacheKey, device); + } } } @Override public void removeDeviceFromCache(DeviceIdentifier deviceIdentifier, int tenantId) { Cache lCache = DeviceManagerUtil.getDeviceCache(); - DeviceCacheKey cacheKey = getCacheKey(deviceIdentifier, tenantId); - if (lCache.containsKey(cacheKey)) { - lCache.remove(cacheKey); + if (lCache != null) { + DeviceCacheKey cacheKey = getCacheKey(deviceIdentifier, tenantId); + if (lCache.containsKey(cacheKey)) { + lCache.remove(cacheKey); + } } } @Override public void removeDevicesFromCache(List deviceList) { Cache lCache = DeviceManagerUtil.getDeviceCache(); - for (DeviceCacheKey cacheKey : deviceList) { - if (lCache.containsKey(cacheKey)) { - lCache.remove(cacheKey); + if (lCache != null) { + for (DeviceCacheKey cacheKey : deviceList) { + if (lCache.containsKey(cacheKey)) { + lCache.remove(cacheKey); + } } } } @@ -85,16 +91,21 @@ public class DeviceCacheManagerImpl implements DeviceCacheManager { @Override public void updateDeviceInCache(DeviceIdentifier deviceIdentifier, Device device, int tenantId) { Cache lCache = DeviceManagerUtil.getDeviceCache(); - DeviceCacheKey cacheKey = getCacheKey(deviceIdentifier, tenantId); - if (lCache.containsKey(cacheKey)) { - lCache.replace(cacheKey, device); + if (lCache != null) { + DeviceCacheKey cacheKey = getCacheKey(deviceIdentifier, tenantId); + if (lCache.containsKey(cacheKey)) { + lCache.replace(cacheKey, device); + } } } @Override public Device getDeviceFromCache(DeviceIdentifier deviceIdentifier, int tenantId) { Cache lCache = DeviceManagerUtil.getDeviceCache(); - return lCache.get(getCacheKey(deviceIdentifier, tenantId)); + if (lCache != null) { + return lCache.get(getCacheKey(deviceIdentifier, tenantId)); + } + return null; }