From e2d903382144facd80760d3876ceef7fc7f760ee Mon Sep 17 00:00:00 2001 From: Michael Hirsch Date: Wed, 4 May 2016 19:37:58 +0200 Subject: [PATCH] remove singleton instance wihthin TenantConfigurationManagement Signed-off-by: Michael Hirsch --- .../RepositoryApplicationConfiguration.java | 5 ++- .../TenantConfigurationManagement.java | 14 +----- .../hawkbit/repository/model/TargetInfo.java | 13 +++--- .../TenantConfigurationManagementHolder.java | 44 +++++++++++++++++++ 4 files changed, 56 insertions(+), 20 deletions(-) create mode 100644 hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/helper/TenantConfigurationManagementHolder.java diff --git a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/RepositoryApplicationConfiguration.java b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/RepositoryApplicationConfiguration.java index 8ef427dcf..10100eda1 100644 --- a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/RepositoryApplicationConfiguration.java +++ b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/RepositoryApplicationConfiguration.java @@ -19,6 +19,7 @@ import org.eclipse.hawkbit.repository.model.helper.CacheManagerHolder; import org.eclipse.hawkbit.repository.model.helper.SecurityTokenGeneratorHolder; import org.eclipse.hawkbit.repository.model.helper.SystemManagementHolder; import org.eclipse.hawkbit.repository.model.helper.TenantAwareHolder; +import org.eclipse.hawkbit.repository.model.helper.TenantConfigurationManagementHolder; import org.eclipse.hawkbit.security.SecurityTokenGenerator; import org.eclipse.hawkbit.tenancy.TenantAware; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -54,8 +55,8 @@ public class RepositoryApplicationConfiguration extends JpaBaseConfiguration { * directly, e.g. JPA entities. */ @Bean - public TenantConfigurationManagement tenantConfigurationManagement() { - return TenantConfigurationManagement.getInstance(); + public TenantConfigurationManagementHolder tenantConfigurationManagementHolder() { + return TenantConfigurationManagementHolder.getInstance(); } /** diff --git a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/TenantConfigurationManagement.java b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/TenantConfigurationManagement.java index e311cf288..760942f06 100644 --- a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/TenantConfigurationManagement.java +++ b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/TenantConfigurationManagement.java @@ -24,6 +24,7 @@ import org.springframework.core.convert.support.DefaultConversionService; import org.springframework.core.env.Environment; import org.springframework.data.jpa.repository.Modifying; import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.validation.annotation.Validated; @@ -32,10 +33,9 @@ import org.springframework.validation.annotation.Validated; */ @Transactional(readOnly = true) @Validated +@Service public class TenantConfigurationManagement implements EnvironmentAware { - private static final TenantConfigurationManagement INSTANCE = new TenantConfigurationManagement(); - @Autowired private TenantConfigurationRepository tenantConfigurationRepository; @@ -46,16 +46,6 @@ 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; - } - /** * Retrieves a configuration value from the e.g. tenant overwritten * configuration values or in case the tenant does not a have a specific diff --git a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/TargetInfo.java b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/TargetInfo.java index 47f52579a..e0ffa8f5f 100644 --- a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/TargetInfo.java +++ b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/TargetInfo.java @@ -38,7 +38,7 @@ import javax.persistence.OneToOne; import javax.persistence.Table; import javax.persistence.Transient; -import org.eclipse.hawkbit.repository.TenantConfigurationManagement; +import org.eclipse.hawkbit.repository.model.helper.TenantConfigurationManagementHolder; import org.eclipse.hawkbit.tenancy.configuration.DurationHelper; import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationKey; import org.eclipse.persistence.annotations.CascadeOnDelete; @@ -246,11 +246,12 @@ public class TargetInfo implements Persistable, Serializable { return null; } - final Duration pollTime = DurationHelper.formattedStringToDuration(TenantConfigurationManagement.getInstance() - .getConfigurationValue(TenantConfigurationKey.POLLING_TIME_INTERVAL, String.class).getValue()); - final Duration overdueTime = DurationHelper.formattedStringToDuration(TenantConfigurationManagement - .getInstance().getConfigurationValue(TenantConfigurationKey.POLLING_OVERDUE_TIME_INTERVAL, String.class) - .getValue()); + final Duration pollTime = DurationHelper.formattedStringToDuration( + TenantConfigurationManagementHolder.getInstance().getTenantConfigurationManagement() + .getConfigurationValue(TenantConfigurationKey.POLLING_TIME_INTERVAL, String.class).getValue()); + final Duration overdueTime = DurationHelper.formattedStringToDuration(TenantConfigurationManagementHolder + .getInstance().getTenantConfigurationManagement() + .getConfigurationValue(TenantConfigurationKey.POLLING_OVERDUE_TIME_INTERVAL, String.class).getValue()); final LocalDateTime currentDate = LocalDateTime.now(); final LocalDateTime lastPollDate = LocalDateTime.ofInstant(Instant.ofEpochMilli(lastTargetQuery), ZoneId.systemDefault()); diff --git a/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/helper/TenantConfigurationManagementHolder.java b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/helper/TenantConfigurationManagementHolder.java new file mode 100644 index 000000000..700511db6 --- /dev/null +++ b/hawkbit-repository/src/main/java/org/eclipse/hawkbit/repository/model/helper/TenantConfigurationManagementHolder.java @@ -0,0 +1,44 @@ +/** + * 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.model.helper; + +import org.eclipse.hawkbit.repository.TenantConfigurationManagement; +import org.springframework.beans.factory.annotation.Autowired; + +/** + * A singleton bean which holds {@link TenantConfigurationManagement} service + * and makes it accessible to beans which are not managed by spring, e.g. JPA + * entities. + */ +public final class TenantConfigurationManagementHolder { + + private static final TenantConfigurationManagementHolder INSTANCE = new TenantConfigurationManagementHolder(); + + @Autowired + private TenantConfigurationManagement tenantConfiguration; + + private TenantConfigurationManagementHolder() { + } + + /** + * @return the singleton {@link TenantConfigurationManagementHolder} + * instance + */ + public static TenantConfigurationManagementHolder getInstance() { + return INSTANCE; + } + + /** + * @return the {@link TenantConfigurationManagement} service + */ + public TenantConfigurationManagement getTenantConfigurationManagement() { + return tenantConfiguration; + } + +}