Refactoring/Improving source: repository & fix log() (#1601) (#1602)

Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2024-02-03 17:25:30 +02:00
committed by GitHub
parent a7f7b0fea8
commit ba685ef429
14 changed files with 61 additions and 98 deletions

View File

@@ -9,13 +9,14 @@
*/
package org.eclipse.hawkbit.api;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
/**
* Utility class for Base10 to Base62 conversion and vice versa. Base62 has the
* benefit of being shorter in ASCII representation than Base10.
*/
@NoArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class Base62Util {
private static final String BASE62_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

View File

@@ -9,8 +9,7 @@
*/
package org.eclipse.hawkbit.event;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.bus.event.RemoteApplicationEvent;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
@@ -33,12 +32,11 @@ import io.protostuff.runtime.RuntimeSchema;
* message header information will get lost. So in this implementation the
* information about the event-type is encoded in the payload of the message
* directly using the encoded values of {@link EventType}.
*
*/
@Slf4j
public class BusProtoStuffMessageConverter extends AbstractMessageConverter {
public static final MimeType APPLICATION_BINARY_PROTOSTUFF = new MimeType("application", "binary+protostuff");
private static final Logger LOG = LoggerFactory.getLogger(BusProtoStuffMessageConverter.class);
/**
* The length of the class type length of the payload.
*/
@@ -60,9 +58,7 @@ public class BusProtoStuffMessageConverter extends AbstractMessageConverter {
public Object convertFromInternal(final Message<?> message, final Class<?> targetClass,
final Object conversionHint) {
final Object objectPayload = message.getPayload();
if (objectPayload instanceof byte[]) {
final byte[] payload = (byte[]) objectPayload;
if (objectPayload instanceof byte[] payload) {
final byte[] clazzHeader = extractClazzHeader(payload);
final byte[] content = extraxtContent(payload);
@@ -86,7 +82,7 @@ public class BusProtoStuffMessageConverter extends AbstractMessageConverter {
private static Object readContent(final EventType eventType, final byte[] content) {
final Class<?> targetClass = eventType.getTargetClass();
if (targetClass == null) {
LOG.error("Cannot read clazz header for given EventType value {}, missing mapping", eventType.getValue());
log.error("Cannot read clazz header for given EventType value {}, missing mapping", eventType.getValue());
throw new MessageConversionException("Missing mapping of EventType for value " + eventType.getValue());
}
@SuppressWarnings("unchecked")
@@ -133,7 +129,7 @@ public class BusProtoStuffMessageConverter extends AbstractMessageConverter {
private static byte[] writeClassHeader(final Class<?> clazz) {
final EventType clazzEventType = EventType.from(clazz);
if (clazzEventType == null) {
LOG.error("There is no mapping to EventType for the given class {}", clazz);
log.error("There is no mapping to EventType for the given class {}", clazz);
throw new MessageConversionException("Missing EventType for given class : " + clazz);
}
@SuppressWarnings("unchecked")

View File

@@ -14,6 +14,10 @@ import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import lombok.AccessLevel;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.eclipse.hawkbit.repository.event.remote.DistributionSetDeletedEvent;
import org.eclipse.hawkbit.repository.event.remote.DistributionSetTagDeletedEvent;
import org.eclipse.hawkbit.repository.event.remote.DistributionSetTypeDeletedEvent;
@@ -70,6 +74,10 @@ import org.eclipse.hawkbit.repository.event.remote.entity.TenantConfigurationUpd
* between the actual class and the corresponding integer value which is the
* encoded value in the byte-payload.
*/
// for marshalling and unmarshalling.
@NoArgsConstructor(access = AccessLevel.PUBLIC)
@Getter
@EqualsAndHashCode
public class EventType {
private static final Map<Integer, Class<?>> TYPES = new HashMap<>();
@@ -79,7 +87,6 @@ public class EventType {
* declared. Otherwise messages cannot correctly de-serialized.
*/
static {
// target
TYPES.put(1, TargetCreatedEvent.class);
TYPES.put(2, TargetUpdatedEvent.class);
@@ -163,13 +170,6 @@ public class EventType {
private int value;
/**
* Constructor.
*/
public EventType() {
// for marshalling and unmarshalling.
}
/**
* Constructor.
*
@@ -180,10 +180,6 @@ public class EventType {
this.value = value;
}
public int getValue() {
return value;
}
public Class<?> getTargetClass() {
return TYPES.get(value);
}
@@ -203,4 +199,4 @@ public class EventType {
return foundEventType.map(EventType::new).orElse(null);
}
}
}

View File

@@ -16,6 +16,8 @@ import java.util.stream.Collectors;
import jakarta.validation.ValidationException;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.eclipse.hawkbit.repository.exception.AssignmentQuotaExceededException;
import org.eclipse.hawkbit.repository.exception.RolloutIllegalStateException;
import org.eclipse.hawkbit.repository.model.Rollout;
@@ -26,11 +28,9 @@ import org.springframework.util.StringUtils;
/**
* A collection of static helper methods for the {@link RolloutManagement}
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class RolloutHelper {
private RolloutHelper() {
}
/**
* Verifies that the required success condition and action are actually set.
*
@@ -279,4 +279,4 @@ public final class RolloutHelper {
public static String getIdFromRetriedTargetFilter(final String targetFilter) {
return targetFilter.substring("failedrollout==".length());
}
}
}

View File

@@ -38,9 +38,11 @@ import com.github.benmanes.caffeine.cache.Caffeine;
*
*/
public class RolloutStatusCache {
private static final String CACHE_RO_NAME = "RolloutStatus";
private static final String CACHE_GR_NAME = "RolloutGroupStatus";
private static final long DEFAULT_SIZE = 50_000;
private final TenancyCacheManager cacheManager;
private final TenantAware tenantAware;

View File

@@ -12,6 +12,8 @@ package org.eclipse.hawkbit.repository;
import java.time.Duration;
import java.time.Instant;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.eclipse.hawkbit.repository.model.helper.SystemSecurityContextHolder;
import org.eclipse.hawkbit.repository.model.helper.TenantConfigurationManagementHolder;
import org.eclipse.hawkbit.security.SystemSecurityContext;
@@ -22,13 +24,10 @@ import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationProperties.T
* Calculates non-persistent timestamps , e.g. the point a time a target is
* declared as overdue.<br>
* Therefore tenant specific configuration may be considered.
*
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class TimestampCalculator {
private TimestampCalculator() {
}
/**
* Calculates the overdue timestamp (<em>overdue_ts</em>) based on the
* current timestamp and the intervals for polling and poll-overdue:
@@ -62,4 +61,4 @@ public final class TimestampCalculator {
private static TenantConfigurationManagement getTenantConfigurationManagement() {
return TenantConfigurationManagementHolder.getInstance().getTenantConfigurationManagement();
}
}
}

View File

@@ -9,6 +9,9 @@
*/
package org.eclipse.hawkbit.repository.model.helper;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.bus.BusProperties;
import org.springframework.cloud.bus.ServiceMatcher;
@@ -19,10 +22,12 @@ import org.springframework.context.ApplicationEventPublisher;
* order to publish remote application events. It can be used in beans not
* instantiated by spring e.g. JPA entities which cannot be auto-wired.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class EventPublisherHolder {
private static final EventPublisherHolder SINGLETON = new EventPublisherHolder();
@Getter
@Autowired
private ApplicationEventPublisher eventPublisher;
@@ -32,9 +37,6 @@ public final class EventPublisherHolder {
@Autowired
private BusProperties bus;
private EventPublisherHolder() {
}
/**
* @return the event publisher holder singleton instance
*/
@@ -42,13 +44,6 @@ public final class EventPublisherHolder {
return SINGLETON;
}
/**
* @return the eventPublisher
*/
public ApplicationEventPublisher getEventPublisher() {
return eventPublisher;
}
/**
* @return the service origin Id coming either from {@link ServiceMatcher}
* when available or {@link BusProperties} otherwise.
@@ -63,4 +58,4 @@ public final class EventPublisherHolder {
}
return id;
}
}
}

View File

@@ -9,26 +9,26 @@
*/
package org.eclipse.hawkbit.repository.model.helper;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.eclipse.hawkbit.repository.SystemManagement;
import org.springframework.beans.factory.annotation.Autowired;
/**
* A singleton bean which holds {@link SystemManagement} service and makes it
* accessible to beans which are not managed by spring, e.g. JPA entities.
*
*
*
**/
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SystemManagementHolder {
private static final SystemManagementHolder INSTANCE = new SystemManagementHolder();
@Getter @Setter
@Autowired
private SystemManagement systemManagement;
private SystemManagementHolder() {
}
/**
* @return the singleton {@link SystemManagementHolder} instance
*/
@@ -36,26 +36,10 @@ public final class SystemManagementHolder {
return INSTANCE;
}
/**
* @return the systemManagement
*/
public SystemManagement getSystemManagement() {
return systemManagement;
}
/**
* @param systemManagement
* the systemManagement to set
*/
public void setSystemManagement(final SystemManagement systemManagement) {
this.systemManagement = systemManagement;
}
/**
* @return the {@link SystemManagement#currentTenant()}.
*/
public String currentTenant() {
return systemManagement.currentTenant();
}
}
}

View File

@@ -9,6 +9,9 @@
*/
package org.eclipse.hawkbit.repository.model.helper;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.eclipse.hawkbit.security.SystemSecurityContext;
import org.springframework.beans.factory.annotation.Autowired;
@@ -16,27 +19,19 @@ import org.springframework.beans.factory.annotation.Autowired;
* A singleton bean which holds {@link SystemSecurityContext} service and makes
* it accessible to beans which are not managed by spring, e.g. JPA entities.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SystemSecurityContextHolder {
private static final SystemSecurityContextHolder INSTANCE = new SystemSecurityContextHolder();
@Getter
@Autowired
private SystemSecurityContext systemSecurityContext;
private SystemSecurityContextHolder() {
}
/**
* @return the singleton {@link SystemSecurityContextHolder} instance
*/
public static SystemSecurityContextHolder getInstance() {
return INSTANCE;
}
/**
* @return the {@link SystemSecurityContext} service
*/
public SystemSecurityContext getSystemSecurityContext() {
return systemSecurityContext;
}
}
}

View File

@@ -9,6 +9,9 @@
*/
package org.eclipse.hawkbit.repository.model.helper;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.eclipse.hawkbit.repository.TenantConfigurationManagement;
import org.springframework.beans.factory.annotation.Autowired;
@@ -17,29 +20,19 @@ import org.springframework.beans.factory.annotation.Autowired;
* and makes it accessible to beans which are not managed by spring, e.g. JPA
* entities.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class TenantConfigurationManagementHolder {
private static final TenantConfigurationManagementHolder INSTANCE = new TenantConfigurationManagementHolder();
@Getter
@Autowired
private TenantConfigurationManagement tenantConfiguration;
private TenantConfigurationManagementHolder() {
}
private TenantConfigurationManagement tenantConfigurationManagement;
/**
* @return the singleton {@link TenantConfigurationManagementHolder}
* instance
* @return the singleton {@link TenantConfigurationManagementHolder} instance
*/
public static TenantConfigurationManagementHolder getInstance() {
return INSTANCE;
}
/**
* @return the {@link TenantConfigurationManagement} service
*/
public TenantConfigurationManagement getTenantConfigurationManagement() {
return tenantConfiguration;
}
}
}

View File

@@ -67,5 +67,4 @@ public class VirtualPropertyResolver extends StrLookup<String> implements Virtua
}
return substitutor.replace(input);
}
}
}

View File

@@ -9,12 +9,13 @@
*/
package org.eclipse.hawkbit.security;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
/**
* Constants related to security.
*/
@NoArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class SecurityConstants {
/**

View File

@@ -16,6 +16,7 @@ import java.util.regex.Pattern;
import jakarta.servlet.http.HttpServletRequest;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.eclipse.hawkbit.security.HawkbitSecurityProperties;
@@ -23,7 +24,7 @@ import org.eclipse.hawkbit.security.HawkbitSecurityProperties;
* A utility which determines the correct IP of a connected {@link Target}. E.g
* from a {@link HttpServletRequest}.
*/
@NoArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE)
// Exception squid:S2083 - false positive, file paths not handled here
@SuppressWarnings("squid:S2083")
public final class IpUtil {

View File

@@ -9,12 +9,13 @@
*/
package org.eclipse.hawkbit.util;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.web.util.UriUtils;
import java.nio.charset.StandardCharsets;
@NoArgsConstructor
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class UrlUtils {
public static String decodeUriValue(String value) {