Added REST interfaces for tenant configuration
- added GET, DELETE and PUT function on configuration value interfaces - changed Exception in TenantConfigurationManagement for correct mapping to HTTP Status - added function to get global configurations from TenantConfigurationManagement, to have only one class for handling these configuration values Signed-off-by: Nonnenmacher Fabian <fabian.nonnenmacher@bosch-si.com>
This commit is contained in:
committed by
Nonnenmacher Fabian
parent
4be880587a
commit
ec79e9bd19
@@ -3,6 +3,7 @@ package org.eclipse.hawkbit.repository;
|
||||
import org.eclipse.hawkbit.repository.model.TenantConfiguration;
|
||||
import org.eclipse.hawkbit.repository.model.TenantConfigurationValue;
|
||||
import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationKey;
|
||||
import org.eclipse.hawkbit.tenancy.configuration.validator.TenantConfigurationValidatorException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cache.annotation.CacheEvict;
|
||||
import org.springframework.cache.annotation.Cacheable;
|
||||
@@ -16,6 +17,9 @@ import org.springframework.data.jpa.repository.Modifying;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
/**
|
||||
* Central tenant configuration management operations of the SP server.
|
||||
*/
|
||||
@Transactional(readOnly = true)
|
||||
@Validated
|
||||
public class TenantConfigurationManagement implements EnvironmentAware {
|
||||
@@ -32,6 +36,12 @@ public class TenantConfigurationManagement implements EnvironmentAware {
|
||||
|
||||
private Environment environment;
|
||||
|
||||
/**
|
||||
* Get Singleton instance, needed for classes which are not managed in
|
||||
* Spring context
|
||||
*
|
||||
* @return singleton instance of TenantConfigurationManagement
|
||||
*/
|
||||
public static TenantConfigurationManagement getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
@@ -42,7 +52,7 @@ public class TenantConfigurationManagement implements EnvironmentAware {
|
||||
* configuration the global default value hold in the {@link Environment}.
|
||||
*
|
||||
* @param <T>
|
||||
*
|
||||
* the type of the configuration value
|
||||
* @param configurationKey
|
||||
* the key of the configuration
|
||||
* @param propertyType
|
||||
@@ -52,17 +62,21 @@ public class TenantConfigurationManagement implements EnvironmentAware {
|
||||
* configuration stored or from the fallback default values or
|
||||
* {@code null} in case key has not been configured and not default
|
||||
* value exists
|
||||
* @throws TenantConfigurationValidatorException
|
||||
* if the {@code propertyType} and the value in general does not
|
||||
* match the expected type and format defined by the Key
|
||||
* @throws ConversionFailedException
|
||||
* if the property cannot be converted to the given
|
||||
* {@code propertyType}
|
||||
*/
|
||||
@Cacheable(value = "tenantConfiguration", key = "#configurationKey.getKeyName()")
|
||||
public <T> TenantConfigurationValue<T> getConfigurationValue(final TenantConfigurationKey configurationKey,
|
||||
final Class<T> propertyType) {
|
||||
final Class<T> propertyType) throws TenantConfigurationValidatorException {
|
||||
|
||||
if (configurationKey.getDataType() != propertyType) {
|
||||
throw new IllegalAccessError(String.format("The key %s does not handle values in the type %s.",
|
||||
configurationKey.getKeyName(), propertyType));
|
||||
if (!configurationKey.getDataType().isAssignableFrom(propertyType)) {
|
||||
throw new TenantConfigurationValidatorException(
|
||||
String.format("Cannot parse the database value of type %s into the type %s.",
|
||||
configurationKey.getDataType(), propertyType));
|
||||
}
|
||||
|
||||
final TenantConfiguration tenantConfiguration = tenantConfigurationRepository
|
||||
@@ -76,42 +90,132 @@ public class TenantConfigurationManagement implements EnvironmentAware {
|
||||
.value(conversionService.convert(tenantConfiguration.getValue(), propertyType)).build();
|
||||
|
||||
} else if (configurationKey.getDefaultKeyName() != null) {
|
||||
final T valueInProperties = environment.getProperty(configurationKey.getDefaultKeyName(), propertyType);
|
||||
|
||||
return TenantConfigurationValue.<T> builder().isGlobal(true).createdBy(null).createdAt(null)
|
||||
.lastModifiedAt(null).lastModifiedBy(null).value(valueInProperties != null ? valueInProperties
|
||||
: conversionService.convert(configurationKey.getDefaultValue(), propertyType))
|
||||
.build();
|
||||
.lastModifiedAt(null).lastModifiedBy(null)
|
||||
.value(getGlobalConfigurationValue(configurationKey, propertyType)).build();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds or updates a specific configuration for a specific tenant.
|
||||
*
|
||||
* @param tenantConf
|
||||
* the tenant configuration object which contains the key and
|
||||
* value of the specific configuration to update
|
||||
* @return the added or updated TenantConfiguration
|
||||
* Retrieves a configuration value from the e.g. tenant overwritten
|
||||
* configuration values or in case the tenant does not a have a specific
|
||||
* configuration the global default value hold in the {@link Environment}.
|
||||
*
|
||||
* @param configurationKey
|
||||
* the key of the configuration
|
||||
* @return the converted configuration value either from the tenant specific
|
||||
* configuration stored or from the fallback default values or
|
||||
* {@code null} in case key has not been configured and not default
|
||||
* value exists
|
||||
* @throws TenantConfigurationValidatorException
|
||||
* if the {@code propertyType} and the value in general does not
|
||||
* match the expected type and format defined by the Key
|
||||
* @throws ConversionFailedException
|
||||
* if the property cannot be converted to the given
|
||||
* {@code propertyType}
|
||||
*/
|
||||
@CacheEvict(value = "tenantConfiguration", key = "#tenantConf.getKey()")
|
||||
@Transactional
|
||||
@Modifying
|
||||
public void addOrUpdateConfiguration(final TenantConfigurationKey tenantConfkey, final Object value) {
|
||||
|
||||
tenantConfkey.validate(applicationContext, value);
|
||||
|
||||
TenantConfiguration tenantConfiguration = tenantConfigurationRepository.findByKey(tenantConfkey.getKeyName());
|
||||
if (tenantConfiguration != null) {
|
||||
tenantConfiguration.setValue(value.toString());
|
||||
} else {
|
||||
tenantConfiguration = new TenantConfiguration(tenantConfkey.getKeyName(), value.toString());
|
||||
}
|
||||
tenantConfigurationRepository.save(tenantConfiguration);
|
||||
public TenantConfigurationValue<?> getConfigurationValue(final TenantConfigurationKey configurationKey)
|
||||
throws TenantConfigurationValidatorException {
|
||||
return getConfigurationValue(configurationKey, configurationKey.getDataType());
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a specific configuration for the current tenant.
|
||||
* returns the global configuration property either defined in the property
|
||||
* file or an default value otherwise.
|
||||
*
|
||||
* @param <T>
|
||||
* the type of the configuration value
|
||||
* @param configurationKey
|
||||
* the key of the configuration
|
||||
* @param propertyType
|
||||
* the type of the configuration value, e.g. {@code String.class}
|
||||
* , {@code Integer.class}, etc
|
||||
* @return the global configured value
|
||||
* @throws TenantConfigurationValidatorException
|
||||
* if the {@code propertyType} and the value in the property
|
||||
* file or the default value does not match the expected type
|
||||
* and format defined by the Key
|
||||
* @throws ConversionFailedException
|
||||
* if the property cannot be converted to the given
|
||||
* {@code propertyType}
|
||||
*/
|
||||
@Cacheable(value = "tenantConfiguration", key = "#configurationKey.getKeyName()")
|
||||
public <T> T getGlobalConfigurationValue(final TenantConfigurationKey configurationKey, final Class<T> propertyType)
|
||||
throws TenantConfigurationValidatorException {
|
||||
|
||||
if (!configurationKey.getDataType().isAssignableFrom(propertyType)) {
|
||||
throw new TenantConfigurationValidatorException(
|
||||
String.format("Cannot parse the database value of type %s into the type %s.",
|
||||
configurationKey.getDataType(), propertyType));
|
||||
}
|
||||
|
||||
final T valueInProperties = environment.getProperty(configurationKey.getDefaultKeyName(), propertyType);
|
||||
|
||||
if (valueInProperties != null) {
|
||||
return valueInProperties;
|
||||
}
|
||||
return conversionService.convert(configurationKey.getDefaultValue(), propertyType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds or updates a specific configuration for a specific tenant.
|
||||
*
|
||||
*
|
||||
* @param configurationKey
|
||||
* the key of the configuration
|
||||
* @param value
|
||||
* the configuration value which will be written into the
|
||||
* database.
|
||||
* @return the configuration value which was just written into the database.
|
||||
* @throws TenantConfigurationValidatorException
|
||||
* if the {@code propertyType} and the value in general does not
|
||||
* match the expected type and format defined by the Key
|
||||
* @throws ConversionFailedException
|
||||
* if the property cannot be converted to the given
|
||||
*/
|
||||
@CacheEvict(value = "tenantConfiguration", key = "#configurationKey.getKeyName()")
|
||||
@Transactional
|
||||
@Modifying
|
||||
public <T> TenantConfigurationValue<T> addOrUpdateConfiguration(final TenantConfigurationKey configurationKey,
|
||||
final T value) {
|
||||
|
||||
if (!configurationKey.getDataType().isAssignableFrom(value.getClass())) {
|
||||
throw new TenantConfigurationValidatorException(String.format(
|
||||
"Cannot parse the value %s of type %s into the type %s defined by the configuration key.", value,
|
||||
value.getClass(), configurationKey.getDataType()));
|
||||
}
|
||||
|
||||
configurationKey.validate(applicationContext, value);
|
||||
|
||||
TenantConfiguration tenantConfiguration = tenantConfigurationRepository
|
||||
.findByKey(configurationKey.getKeyName());
|
||||
if (tenantConfiguration != null)
|
||||
|
||||
{
|
||||
tenantConfiguration.setValue(value.toString());
|
||||
} else
|
||||
|
||||
{
|
||||
tenantConfiguration = new TenantConfiguration(configurationKey.getKeyName(), value.toString());
|
||||
}
|
||||
|
||||
final TenantConfiguration updatedTenantConfiguration = tenantConfigurationRepository.save(tenantConfiguration);
|
||||
|
||||
final Class<T> clazzT = (Class<T>) value.getClass();
|
||||
|
||||
return TenantConfigurationValue.<T> builder().isGlobal(false)
|
||||
.createdBy(updatedTenantConfiguration.getCreatedBy())
|
||||
.createdAt(updatedTenantConfiguration.getCreatedAt())
|
||||
.lastModifiedAt(updatedTenantConfiguration.getLastModifiedAt())
|
||||
.lastModifiedBy(updatedTenantConfiguration.getLastModifiedBy())
|
||||
.value(conversionService.convert(updatedTenantConfiguration.getValue(), clazzT)).build();
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a specific configuration for the current tenant. Does nothing in
|
||||
* case there is no tenant specific configuration value.
|
||||
*
|
||||
* @param configurationKey
|
||||
* the configuration key to be deleted
|
||||
@@ -123,12 +227,6 @@ public class TenantConfigurationManagement implements EnvironmentAware {
|
||||
tenantConfigurationRepository.deleteByKey(configurationKey.getKeyName());
|
||||
}
|
||||
|
||||
// @Transactional
|
||||
// public List<TenantConfiguration> getTenantConfigurations() {
|
||||
//
|
||||
// return tenantConfigurationRepository.findAll();
|
||||
// }
|
||||
|
||||
@Override
|
||||
public void setEnvironment(final Environment environment) {
|
||||
this.environment = environment;
|
||||
|
||||
Reference in New Issue
Block a user