diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaSystemManagement.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaSystemManagement.java index 1cee5b892..ea6c99708 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaSystemManagement.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/JpaSystemManagement.java @@ -8,7 +8,6 @@ */ package org.eclipse.hawkbit.repository.jpa; -import java.lang.reflect.Method; import java.math.BigDecimal; import java.util.List; import java.util.stream.Collectors; @@ -34,8 +33,7 @@ import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.interceptor.KeyGenerator; -import org.springframework.cache.interceptor.SimpleKeyGenerator; -import org.springframework.context.annotation.Bean; +import org.springframework.context.ApplicationContext; import org.springframework.data.jpa.repository.Modifying; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; @@ -106,7 +104,11 @@ public class JpaSystemManagement implements CurrentTenantCacheKeyGenerator, Syst @Autowired private TenancyCacheManager cacheManager; - private final ThreadLocal createInitialTenant = new ThreadLocal<>(); + @Autowired + private SystemManagementCacheKeyGenerator currentTenantCacheKeyGenerator; + + @Autowired + private ApplicationContext applicationContext; @Override public SystemUsageReport getSystemUsageStatistics() { @@ -152,9 +154,8 @@ public class JpaSystemManagement implements CurrentTenantCacheKeyGenerator, Syst @Override @Transactional(propagation = Propagation.SUPPORTS) - @Bean public KeyGenerator currentTenantKeyGenerator() { - return new CurrentTenantKeyGenerator(); + return currentTenantCacheKeyGenerator.currentTenantKeyGenerator(); } @Override @@ -167,11 +168,12 @@ public class JpaSystemManagement implements CurrentTenantCacheKeyGenerator, Syst // Create if it does not exist if (result == null) { try { - createInitialTenant.set(tenant); + currentTenantCacheKeyGenerator.getCreateInitialTenant().set(tenant); cacheManager.getCache("currentTenant").evict(currentTenantKeyGenerator().generate(null, null)); + applicationContext.getBean("currentTenantKeyGenerator"); return tenantMetaDataRepository.save(new JpaTenantMetaData(createStandardSoftwareDataSetup(), tenant)); } finally { - createInitialTenant.remove(); + currentTenantCacheKeyGenerator.getCreateInitialTenant().remove(); } } @@ -237,7 +239,7 @@ public class JpaSystemManagement implements CurrentTenantCacheKeyGenerator, Syst // tenant is not cached anyway already. @Transactional(propagation = Propagation.NOT_SUPPORTED, isolation = Isolation.READ_UNCOMMITTED) public String currentTenant() { - final String initialTenantCreation = createInitialTenant.get(); + final String initialTenantCreation = currentTenantCacheKeyGenerator.getCreateInitialTenant().get(); if (initialTenantCreation == null) { final TenantMetaData findByTenant = tenantMetaDataRepository .findByTenantIgnoreCase(tenantAware.getCurrentTenant()); @@ -258,29 +260,6 @@ public class JpaSystemManagement implements CurrentTenantCacheKeyGenerator, Syst return tenantMetaDataRepository.save((JpaTenantMetaData) metaData); } - /** - * A implementation of the {@link KeyGenerator} to generate a key based on - * either the {@code createInitialTenant} thread local and the - * {@link TenantAware}, but in case we are in a tenant creation with its - * default types we need to use the tenant the current tenant which is - * currently created and not the one currently in the {@link TenantAware}. - * - */ - public class CurrentTenantKeyGenerator implements KeyGenerator { - @Override - // Exception squid:S923 - override - @SuppressWarnings({ "squid:S923" }) - public Object generate(final Object target, final Method method, final Object... params) { - final String initialTenantCreation = createInitialTenant.get(); - if (initialTenantCreation == null) { - return SimpleKeyGenerator.generateKey(tenantAware.getCurrentTenant().toUpperCase(), - tenantAware.getCurrentTenant().toUpperCase()); - } - return SimpleKeyGenerator.generateKey(initialTenantCreation.toUpperCase(), - initialTenantCreation.toUpperCase()); - } - } - private DistributionSetType createStandardSoftwareDataSetup() { final SoftwareModuleType app = softwareModuleTypeRepository .save(new JpaSoftwareModuleType(Constants.SMT_DEFAULT_APP_KEY, Constants.SMT_DEFAULT_APP_NAME, diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/SystemManagementCacheKeyGenerator.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/SystemManagementCacheKeyGenerator.java new file mode 100644 index 000000000..9ca8ea7fa --- /dev/null +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/SystemManagementCacheKeyGenerator.java @@ -0,0 +1,65 @@ +/** + * Copyright (c) 2015 Bosch Software Innovations GmbH and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + */ +package org.eclipse.hawkbit.repository.jpa; + +import java.lang.reflect.Method; + +import org.eclipse.hawkbit.tenancy.TenantAware; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.interceptor.KeyGenerator; +import org.springframework.cache.interceptor.SimpleKeyGenerator; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Service; + +/** + * Implementation of {@link CurrentTenantCacheKeyGenerator}. + * + */ +@Service +public class SystemManagementCacheKeyGenerator implements CurrentTenantCacheKeyGenerator { + + @Autowired + private TenantAware tenantAware; + + private final ThreadLocal createInitialTenant = new ThreadLocal<>(); + + /** + * A implementation of the {@link KeyGenerator} to generate a key based on + * either the {@code createInitialTenant} thread local and the + * {@link TenantAware}, but in case we are in a tenant creation with its + * default types we need to use the tenant the current tenant which is + * currently created and not the one currently in the {@link TenantAware}. + * + */ + public class CurrentTenantKeyGenerator implements KeyGenerator { + @Override + // Exception squid:S923 - override + @SuppressWarnings({ "squid:S923" }) + public Object generate(final Object target, final Method method, final Object... params) { + final String initialTenantCreation = createInitialTenant.get(); + if (initialTenantCreation == null) { + return SimpleKeyGenerator.generateKey(tenantAware.getCurrentTenant().toUpperCase(), + tenantAware.getCurrentTenant().toUpperCase()); + } + return SimpleKeyGenerator.generateKey(initialTenantCreation.toUpperCase(), + initialTenantCreation.toUpperCase()); + } + } + + @Override + @Bean + public KeyGenerator currentTenantKeyGenerator() { + return new CurrentTenantKeyGenerator(); + } + + ThreadLocal getCreateInitialTenant() { + return createInitialTenant; + } + +} \ No newline at end of file