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 6759ef7e5bd..2bcd5e2797e 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; }