From 6426a87c8df7a74c172f11285c187c727f0b9680 Mon Sep 17 00:00:00 2001 From: sinthuja Date: Tue, 6 Dec 2016 16:40:18 +0530 Subject: [PATCH] Fixing 'The cache status is not STARTED' issue whe sending the events from android sense. --- .../authorization/DeviceAccessBasedMQTTAuthorizer.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.mqtt.authorization/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/mqtt/authorization/DeviceAccessBasedMQTTAuthorizer.java b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.mqtt.authorization/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/mqtt/authorization/DeviceAccessBasedMQTTAuthorizer.java index d64a77b0b..0b9806962 100644 --- a/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.mqtt.authorization/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/mqtt/authorization/DeviceAccessBasedMQTTAuthorizer.java +++ b/components/extensions/mb-extensions/org.wso2.carbon.andes.extensions.device.mgt.mqtt.authorization/src/main/java/org/wso2/carbon/andes/extensions/device/mgt/mqtt/authorization/DeviceAccessBasedMQTTAuthorizer.java @@ -64,12 +64,10 @@ public class DeviceAccessBasedMQTTAuthorizer implements IAuthorizer { private static final String CACHE_MANAGER_NAME = "mqttAuthorizationCacheManager"; private static final String CACHE_NAME = "mqttAuthorizationCache"; private static DeviceAccessAuthorizationAdminService deviceAccessAuthorizationAdminService; - private static Cache cache; public DeviceAccessBasedMQTTAuthorizer() { this.MQTTAuthorizationConfiguration = AuthorizationConfigurationManager.getInstance(); - createCache(); deviceAccessAuthorizationAdminService = Feign.builder() .requestInterceptor(new OAuthRequestInterceptor()) .contract(new JAXRSContract()).encoder(new GsonEncoder()).decoder(new GsonDecoder()) @@ -92,6 +90,7 @@ public class DeviceAccessBasedMQTTAuthorizer implements IAuthorizer { if (!tenantDomainFromTopic.equals(authorizationSubject.getTenantDomain())) { return false; } + Cache cache = getCache(); if (topics.length < 3) { AuthorizationCacheKey authorizationCacheKey = new AuthorizationCacheKey(tenantDomainFromTopic , authorizationSubject.getUsername(), "", ""); @@ -207,16 +206,15 @@ public class DeviceAccessBasedMQTTAuthorizer implements IAuthorizer { * This method is used to create the Caches. * @return Cachemanager */ - private void createCache() { + private synchronized Cache getCache() { PrivilegedCarbonContext.startTenantFlow(); PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain( MultitenantConstants.SUPER_TENANT_DOMAIN_NAME, true); try { - CacheManager cacheManager = Caching.getCacheManagerFactory().getCacheManager(CACHE_MANAGER_NAME); if (MQTTAuthorizationConfiguration.getCacheDuration() == 0) { - cache = cacheManager.getCache(CACHE_NAME); + return Caching.getCacheManagerFactory().getCacheManager(CACHE_MANAGER_NAME).getCache(CACHE_NAME); } else { - cache = cacheManager.createCacheBuilder(CACHE_NAME). + return Caching.getCacheManagerFactory().getCacheManager(CACHE_MANAGER_NAME).createCacheBuilder(CACHE_NAME). setExpiry(CacheConfiguration.ExpiryType.MODIFIED, new CacheConfiguration.Duration( TimeUnit.SECONDS, MQTTAuthorizationConfiguration.getCacheDuration())). setStoreByValue(false).build();