Made duration helper a static utility class
This commit is contained in:
@@ -22,6 +22,10 @@ import java.time.temporal.TemporalAccessor;
|
|||||||
*/
|
*/
|
||||||
public class DurationHelper {
|
public class DurationHelper {
|
||||||
|
|
||||||
|
private DurationHelper() {
|
||||||
|
// utility class
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Format of the String expected in configuration file and in the database.
|
* Format of the String expected in configuration file and in the database.
|
||||||
*/
|
*/
|
||||||
@@ -35,7 +39,7 @@ public class DurationHelper {
|
|||||||
* @return String in the duration format, specified at
|
* @return String in the duration format, specified at
|
||||||
* {@link #DURATION_FORMAT}
|
* {@link #DURATION_FORMAT}
|
||||||
*/
|
*/
|
||||||
public String durationToFormattedString(final Duration duration) {
|
public static String durationToFormattedString(final Duration duration) {
|
||||||
if (duration == null) {
|
if (duration == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -52,7 +56,7 @@ public class DurationHelper {
|
|||||||
* @throws DateTimeParseException
|
* @throws DateTimeParseException
|
||||||
* when String is in wrong format
|
* when String is in wrong format
|
||||||
*/
|
*/
|
||||||
public Duration formattedStringToDuration(final String formattedDuration) throws DateTimeParseException {
|
public static Duration formattedStringToDuration(final String formattedDuration) {
|
||||||
if (formattedDuration == null) {
|
if (formattedDuration == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -72,11 +76,11 @@ public class DurationHelper {
|
|||||||
* count of seconds
|
* count of seconds
|
||||||
* @return duration
|
* @return duration
|
||||||
*/
|
*/
|
||||||
public Duration getDurationByTimeValues(final long hours, final long minutes, final long seconds) {
|
public static Duration getDurationByTimeValues(final long hours, final long minutes, final long seconds) {
|
||||||
return Duration.ofHours(hours).plusMinutes(minutes).plusSeconds(seconds);
|
return Duration.ofHours(hours).plusMinutes(minutes).plusSeconds(seconds);
|
||||||
}
|
}
|
||||||
|
|
||||||
public DurationRangeValidator durationRangeValidator(final Duration min, final Duration max) {
|
public static DurationRangeValidator durationRangeValidator(final Duration min, final Duration max) {
|
||||||
return new DurationRangeValidator(min, max);
|
return new DurationRangeValidator(min, max);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -22,8 +22,6 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||||||
*/
|
*/
|
||||||
public class TenantConfigurationPollingDurationValidator implements TenantConfigurationValidator {
|
public class TenantConfigurationPollingDurationValidator implements TenantConfigurationValidator {
|
||||||
|
|
||||||
private final DurationHelper durationHelper = new DurationHelper();
|
|
||||||
|
|
||||||
private final Duration minDuration;
|
private final Duration minDuration;
|
||||||
|
|
||||||
private final Duration maxDuration;
|
private final Duration maxDuration;
|
||||||
@@ -36,8 +34,8 @@ public class TenantConfigurationPollingDurationValidator implements TenantConfig
|
|||||||
*/
|
*/
|
||||||
@Autowired
|
@Autowired
|
||||||
public TenantConfigurationPollingDurationValidator(final ControllerPollProperties properties) {
|
public TenantConfigurationPollingDurationValidator(final ControllerPollProperties properties) {
|
||||||
minDuration = durationHelper.formattedStringToDuration(properties.getMinPollingTime());
|
minDuration = DurationHelper.formattedStringToDuration(properties.getMinPollingTime());
|
||||||
maxDuration = durationHelper.formattedStringToDuration(properties.getMaxPollingTime());
|
maxDuration = DurationHelper.formattedStringToDuration(properties.getMaxPollingTime());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -47,18 +45,19 @@ public class TenantConfigurationPollingDurationValidator implements TenantConfig
|
|||||||
|
|
||||||
final Duration tenantConfigurationValue;
|
final Duration tenantConfigurationValue;
|
||||||
try {
|
try {
|
||||||
tenantConfigurationValue = durationHelper.formattedStringToDuration(tenantConfigurationString);
|
tenantConfigurationValue = DurationHelper.formattedStringToDuration(tenantConfigurationString);
|
||||||
} catch (final DateTimeParseException ex) {
|
} catch (final DateTimeParseException ex) {
|
||||||
throw new TenantConfigurationValidatorException(
|
throw new TenantConfigurationValidatorException(
|
||||||
String.format("The given configuration value is expected as a string in the format %s.",
|
String.format("The given configuration value is expected as a string in the format %s.",
|
||||||
DurationHelper.DURATION_FORMAT));
|
DurationHelper.DURATION_FORMAT),
|
||||||
|
ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!durationHelper.durationRangeValidator(minDuration, maxDuration).isWithinRange(tenantConfigurationValue)) {
|
if (!DurationHelper.durationRangeValidator(minDuration, maxDuration).isWithinRange(tenantConfigurationValue)) {
|
||||||
throw new TenantConfigurationValidatorException(
|
throw new TenantConfigurationValidatorException(
|
||||||
String.format("The given configuration value is not in the allowed range from %s to %s.",
|
String.format("The given configuration value is not in the allowed range from %s to %s.",
|
||||||
durationHelper.durationToFormattedString(minDuration),
|
DurationHelper.durationToFormattedString(minDuration),
|
||||||
durationHelper.durationToFormattedString(maxDuration)));
|
DurationHelper.durationToFormattedString(maxDuration)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
* which accompanies this distribution, and is available at
|
* which accompanies this distribution, and is available at
|
||||||
* http://www.eclipse.org/legal/epl-v10.html
|
* http://www.eclipse.org/legal/epl-v10.html
|
||||||
*/
|
*/
|
||||||
package org.eclipse.hawkbit;
|
package org.eclipse.hawkbit.repository;
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
@@ -10,7 +10,6 @@ package org.eclipse.hawkbit.repository;
|
|||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.eclipse.hawkbit.RolloutProperties;
|
|
||||||
import org.eclipse.hawkbit.security.SystemSecurityContext;
|
import org.eclipse.hawkbit.security.SystemSecurityContext;
|
||||||
import org.eclipse.hawkbit.tenancy.TenantAware;
|
import org.eclipse.hawkbit.tenancy.TenantAware;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
|||||||
@@ -122,9 +122,6 @@ public class TargetInfo implements Persistable<Long>, Serializable {
|
|||||||
@Column(name = "request_controller_attributes", nullable = false)
|
@Column(name = "request_controller_attributes", nullable = false)
|
||||||
private boolean requestControllerAttributes = true;
|
private boolean requestControllerAttributes = true;
|
||||||
|
|
||||||
@Transient
|
|
||||||
private final DurationHelper durationHelper = new DurationHelper();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor for {@link TargetStatus}.
|
* Constructor for {@link TargetStatus}.
|
||||||
*
|
*
|
||||||
@@ -324,9 +321,9 @@ public class TargetInfo implements Persistable<Long>, Serializable {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
final Duration pollTime = durationHelper.formattedStringToDuration(TenantConfigurationManagement.getInstance()
|
final Duration pollTime = DurationHelper.formattedStringToDuration(TenantConfigurationManagement.getInstance()
|
||||||
.getConfigurationValue(TenantConfigurationKey.POLLING_TIME_INTERVAL, String.class).getValue());
|
.getConfigurationValue(TenantConfigurationKey.POLLING_TIME_INTERVAL, String.class).getValue());
|
||||||
final Duration overdueTime = durationHelper.formattedStringToDuration(TenantConfigurationManagement
|
final Duration overdueTime = DurationHelper.formattedStringToDuration(TenantConfigurationManagement
|
||||||
.getInstance().getConfigurationValue(TenantConfigurationKey.POLLING_OVERDUE_TIME_INTERVAL, String.class)
|
.getInstance().getConfigurationValue(TenantConfigurationKey.POLLING_OVERDUE_TIME_INTERVAL, String.class)
|
||||||
.getValue());
|
.getValue());
|
||||||
final LocalDateTime currentDate = LocalDateTime.now();
|
final LocalDateTime currentDate = LocalDateTime.now();
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ public class PollingConfigurationView extends BaseConfigurationView
|
|||||||
private I18N i18n;
|
private I18N i18n;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ControllerPollProperties controllerPollProperties;
|
private transient ControllerPollProperties controllerPollProperties;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private transient TenantConfigurationManagement tenantConfigurationManagement;
|
private transient TenantConfigurationManagement tenantConfigurationManagement;
|
||||||
@@ -59,31 +59,29 @@ public class PollingConfigurationView extends BaseConfigurationView
|
|||||||
private Duration tenantPollTime = null;
|
private Duration tenantPollTime = null;
|
||||||
private Duration tenantOverdueTime = null;
|
private Duration tenantOverdueTime = null;
|
||||||
|
|
||||||
private final DurationHelper durationHelper = new DurationHelper();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initialize Authentication Configuration layout.
|
* Initialize Authentication Configuration layout.
|
||||||
*/
|
*/
|
||||||
@PostConstruct
|
@PostConstruct
|
||||||
public void init() {
|
public void init() {
|
||||||
|
|
||||||
minDuration = durationHelper.formattedStringToDuration(controllerPollProperties.getMinPollingTime());
|
minDuration = DurationHelper.formattedStringToDuration(controllerPollProperties.getMinPollingTime());
|
||||||
maxDuration = durationHelper.formattedStringToDuration(controllerPollProperties.getMaxPollingTime());
|
maxDuration = DurationHelper.formattedStringToDuration(controllerPollProperties.getMaxPollingTime());
|
||||||
globalPollTime = durationHelper.formattedStringToDuration(tenantConfigurationManagement
|
globalPollTime = DurationHelper.formattedStringToDuration(tenantConfigurationManagement
|
||||||
.getGlobalConfigurationValue(TenantConfigurationKey.POLLING_TIME_INTERVAL, String.class));
|
.getGlobalConfigurationValue(TenantConfigurationKey.POLLING_TIME_INTERVAL, String.class));
|
||||||
globalOverdueTime = durationHelper.formattedStringToDuration(tenantConfigurationManagement
|
globalOverdueTime = DurationHelper.formattedStringToDuration(tenantConfigurationManagement
|
||||||
.getGlobalConfigurationValue(TenantConfigurationKey.POLLING_OVERDUE_TIME_INTERVAL, String.class));
|
.getGlobalConfigurationValue(TenantConfigurationKey.POLLING_OVERDUE_TIME_INTERVAL, String.class));
|
||||||
|
|
||||||
final TenantConfigurationValue<String> pollTimeConfValue = tenantConfigurationManagement
|
final TenantConfigurationValue<String> pollTimeConfValue = tenantConfigurationManagement
|
||||||
.getConfigurationValue(TenantConfigurationKey.POLLING_TIME_INTERVAL, String.class);
|
.getConfigurationValue(TenantConfigurationKey.POLLING_TIME_INTERVAL, String.class);
|
||||||
if (!pollTimeConfValue.isGlobal()) {
|
if (!pollTimeConfValue.isGlobal()) {
|
||||||
tenantPollTime = durationHelper.formattedStringToDuration(pollTimeConfValue.getValue());
|
tenantPollTime = DurationHelper.formattedStringToDuration(pollTimeConfValue.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
final TenantConfigurationValue<String> overdueTimeConfValue = tenantConfigurationManagement
|
final TenantConfigurationValue<String> overdueTimeConfValue = tenantConfigurationManagement
|
||||||
.getConfigurationValue(TenantConfigurationKey.POLLING_OVERDUE_TIME_INTERVAL, String.class);
|
.getConfigurationValue(TenantConfigurationKey.POLLING_OVERDUE_TIME_INTERVAL, String.class);
|
||||||
if (!overdueTimeConfValue.isGlobal()) {
|
if (!overdueTimeConfValue.isGlobal()) {
|
||||||
tenantOverdueTime = durationHelper.formattedStringToDuration(overdueTimeConfValue.getValue());
|
tenantOverdueTime = DurationHelper.formattedStringToDuration(overdueTimeConfValue.getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
final Panel rootPanel = new Panel();
|
final Panel rootPanel = new Panel();
|
||||||
@@ -133,7 +131,7 @@ public class PollingConfigurationView extends BaseConfigurationView
|
|||||||
tenantConfigurationManagement.deleteConfiguration(key);
|
tenantConfigurationManagement.deleteConfiguration(key);
|
||||||
} else {
|
} else {
|
||||||
tenantConfigurationManagement.addOrUpdateConfiguration(key,
|
tenantConfigurationManagement.addOrUpdateConfiguration(key,
|
||||||
durationHelper.durationToFormattedString(duration));
|
DurationHelper.durationToFormattedString(duration));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user