Skip to content

Commit

Permalink
Reduce dependency on Guava 2 (#1590)
Browse files Browse the repository at this point in the history
Signed-off-by: Marinov Avgustin <Avgustin.Marinov@bosch.com>
  • Loading branch information
avgustinmm committed Feb 2, 2024
1 parent ee5e12a commit 791b87b
Show file tree
Hide file tree
Showing 15 changed files with 81 additions and 59 deletions.
Expand Up @@ -14,15 +14,12 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.eclipse.hawkbit.artifact.repository.model.AbstractDbArtifact;
import org.eclipse.hawkbit.artifact.repository.model.DbArtifactHash;
import org.springframework.validation.annotation.Validated;

import com.google.common.base.Splitter;

/**
* Implementation of the {@link ArtifactRepository} to store artifacts on the
* file-system. The files are stored by their SHA1 hash of the artifact binary.
Expand Down Expand Up @@ -94,9 +91,8 @@ private File getFile(final String tenant, final String sha1) {

private Path getSha1DirectoryPath(final String tenant, final String sha1) {
final int length = sha1.length();
final List<String> folders = Splitter.fixedLength(2).splitToList(sha1.substring(length - 4, length));
final String folder1 = folders.get(0);
final String folder2 = folders.get(1);
final String folder1 = sha1.substring(length - 4, length - 2);
final String folder2 = sha1.substring(length - 2, length);
return Paths.get(artifactResourceProperties.getPath(), sanitizeTenant(tenant), folder1, folder2);
}

Expand Down
Expand Up @@ -9,15 +9,19 @@
*/
package org.eclipse.hawkbit.autoconfigure.scheduling;

import java.util.Locale;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Function;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -34,7 +38,7 @@
import org.springframework.security.concurrent.DelegatingSecurityContextExecutorService;
import org.springframework.security.concurrent.DelegatingSecurityContextScheduledExecutorService;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import static java.util.Objects.requireNonNull;

/**
* Central event processors inside update server.
Expand Down Expand Up @@ -78,7 +82,7 @@ private ThreadPoolExecutor threadPoolExecutor() {
return new ThreadPoolExecutor(asyncConfigurerProperties.getCorethreads(),
asyncConfigurerProperties.getMaxthreads(), asyncConfigurerProperties.getIdletimeout(),
TimeUnit.MILLISECONDS, blockingQueue,
new ThreadFactoryBuilder().setNameFormat("central-executor-pool-%d").build(),
threadFactory("central-executor-pool-%d"),
new PoolSizeExceededPolicy());
}

Expand All @@ -100,7 +104,7 @@ public void rejectedExecution(final Runnable r, final ThreadPoolExecutor executo
public Executor uiExecutor() {
final BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(20);
final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(1, 20, 10000, TimeUnit.MILLISECONDS,
blockingQueue, new ThreadFactoryBuilder().setNameFormat("ui-executor-pool-%d").build());
blockingQueue, threadFactory("ui-executor-pool-%d"));
threadPoolExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return new DelegatingSecurityContextExecutor(threadPoolExecutor);
}
Expand All @@ -114,7 +118,7 @@ public Executor uiExecutor() {
public ScheduledExecutorService scheduledExecutorService() {
return new DelegatingSecurityContextScheduledExecutorService(
Executors.newScheduledThreadPool(asyncConfigurerProperties.getSchedulerThreads(),
new ThreadFactoryBuilder().setNameFormat("central-scheduled-executor-pool-%d").build()));
threadFactory("central-scheduled-executor-pool-%d")));
}

/**
Expand All @@ -126,4 +130,12 @@ public TaskScheduler taskScheduler() {
return new ConcurrentTaskScheduler(scheduledExecutorService());
}

private static ThreadFactory threadFactory(final String format) {
final AtomicLong count = new AtomicLong(0);
return (runnable) -> {
final Thread thread = Executors.defaultThreadFactory().newThread(runnable);
thread.setName(String.format(Locale.ROOT, format, count.getAndIncrement()));
return thread;
};
}
}
7 changes: 5 additions & 2 deletions hawkbit-dmf/hawkbit-dmf-amqp/pom.xml
Expand Up @@ -69,7 +69,11 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
</dependency>


<!-- Test -->
Expand All @@ -90,5 +94,4 @@
<scope>test</scope>
</dependency>
</dependencies>

</project>
Expand Up @@ -25,7 +25,8 @@
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import com.google.common.collect.Iterables;
import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.collections4.ListUtils;
import org.eclipse.hawkbit.api.ApiType;
import org.eclipse.hawkbit.api.ArtifactUrl;
import org.eclipse.hawkbit.api.ArtifactUrlHandler;
Expand Down Expand Up @@ -368,7 +369,7 @@ private static <T, R> List<R> partitionedParallelExecution(final Collection<T> c
// Ensure not exceeding the max value of MAX_PROCESSING_SIZE
if (controllerIds.size() > MAX_PROCESSING_SIZE) {
// Split the provided collection
final Iterable<List<T>> partitions = Iterables.partition(controllerIds, MAX_PROCESSING_SIZE);
final Iterable<List<T>> partitions = ListUtils.partition(IterableUtils.toList(controllerIds), MAX_PROCESSING_SIZE);
// Preserve the security context because it gets lost when executing
// loading calls in new threads
final SecurityContext context = SecurityContextHolder.getContext();
Expand Down
4 changes: 4 additions & 0 deletions hawkbit-repository/hawkbit-repository-jpa/pom.xml
Expand Up @@ -81,6 +81,10 @@
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
</dependency>

<!-- Test -->
<dependency>
Expand Down
Expand Up @@ -33,15 +33,14 @@
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import com.google.common.collect.Lists;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.Root;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;

import org.apache.commons.collections4.ListUtils;
import org.eclipse.hawkbit.repository.ConfirmationManagement;
import org.eclipse.hawkbit.repository.ControllerManagement;
import org.eclipse.hawkbit.repository.EntityFactory;
Expand Down Expand Up @@ -473,7 +472,7 @@ private void flushUpdateQueue() {
private Void updateLastTargetQueries(final String tenant, final List<TargetPoll> polls) {
LOG.debug("Persist {} targetqueries.", polls.size());

final List<List<String>> pollChunks = Lists.partition(
final List<List<String>> pollChunks = ListUtils.partition(
polls.stream().map(TargetPoll::getControllerId).collect(Collectors.toList()),
Constants.MAX_ENTRIES_IN_STATEMENT);

Expand Down
Expand Up @@ -28,7 +28,6 @@
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import com.google.common.collect.Lists;
import jakarta.persistence.EntityManager;
import jakarta.persistence.Query;
import jakarta.persistence.criteria.CriteriaBuilder;
Expand All @@ -37,6 +36,7 @@
import jakarta.persistence.criteria.ListJoin;
import jakarta.persistence.criteria.Root;

import org.apache.commons.collections4.ListUtils;
import org.eclipse.hawkbit.repository.ActionFields;
import org.eclipse.hawkbit.repository.DeploymentManagement;
import org.eclipse.hawkbit.repository.DistributionSetManagement;
Expand Down Expand Up @@ -281,7 +281,7 @@ private void checkForTargetTypeCompatibility(final List<DeploymentRequest> deplo

private void checkCompatibilityForSingleDsAssignment(final Long distSetId, final List<String> controllerIds) {
final DistributionSetType distSetType = distributionSetManagement.getValidAndComplete(distSetId).getType();
final Set<String> incompatibleTargetTypes = Lists.partition(controllerIds, Constants.MAX_ENTRIES_IN_STATEMENT)
final Set<String> incompatibleTargetTypes = ListUtils.partition(controllerIds, Constants.MAX_ENTRIES_IN_STATEMENT)
.stream()
.map(ids -> targetRepository.findAll(TargetSpecifications.hasControllerIdIn(ids)
.and(TargetSpecifications.notCompatibleWithDistributionSetType(distSetType.getId()))))
Expand Down Expand Up @@ -377,7 +377,7 @@ private DistributionSetAssignmentResult assignDistributionSetToTargets(final Str
final List<String> providedTargetIds = targetsWithActionType.stream().map(TargetWithActionType::getControllerId)
.distinct().toList();

final List<String> existingTargetIds = Lists.partition(providedTargetIds, Constants.MAX_ENTRIES_IN_STATEMENT)
final List<String> existingTargetIds = ListUtils.partition(providedTargetIds, Constants.MAX_ENTRIES_IN_STATEMENT)
.stream()
.map(ids -> targetRepository.findAll(AccessController.Operation.UPDATE,
TargetSpecifications.hasControllerIdIn(ids)))
Expand Down Expand Up @@ -442,7 +442,7 @@ private List<JpaAction> doAssignDistributionSetToTargets(final String initiatedB
* statements
*/
private static List<List<Long>> getTargetEntitiesAsChunks(final List<JpaTarget> targetEntities) {
return Lists.partition(targetEntities.stream().map(Target::getId).collect(Collectors.toList()),
return ListUtils.partition(targetEntities.stream().map(Target::getId).collect(Collectors.toList()),
Constants.MAX_ENTRIES_IN_STATEMENT);
}

Expand Down
Expand Up @@ -21,14 +21,14 @@
import java.util.Optional;
import java.util.stream.Collectors;

import com.google.common.collect.Lists;
import jakarta.persistence.EntityManager;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.CriteriaQuery;
import jakarta.persistence.criteria.MapJoin;
import jakarta.persistence.criteria.Root;
import jakarta.validation.constraints.NotEmpty;

import org.apache.commons.collections4.ListUtils;
import org.eclipse.hawkbit.repository.DistributionSetManagement;
import org.eclipse.hawkbit.repository.FilterParams;
import org.eclipse.hawkbit.repository.OffsetBasedPageRequest;
Expand All @@ -40,7 +40,6 @@
import org.eclipse.hawkbit.repository.builder.TargetCreate;
import org.eclipse.hawkbit.repository.builder.TargetUpdate;
import org.eclipse.hawkbit.repository.event.remote.TargetAttributesRequestedEvent;
import org.eclipse.hawkbit.repository.event.remote.TargetDeletedEvent;
import org.eclipse.hawkbit.repository.event.remote.entity.TargetUpdatedEvent;
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
Expand All @@ -49,7 +48,6 @@
import org.eclipse.hawkbit.repository.jpa.builder.JpaTargetCreate;
import org.eclipse.hawkbit.repository.jpa.builder.JpaTargetUpdate;
import org.eclipse.hawkbit.repository.jpa.configuration.Constants;
import org.eclipse.hawkbit.repository.jpa.executor.AfterTransactionCommitExecutor;
import org.eclipse.hawkbit.repository.jpa.model.JpaTarget;
import org.eclipse.hawkbit.repository.jpa.model.JpaTargetMetadata;
import org.eclipse.hawkbit.repository.jpa.model.JpaTargetMetadata_;
Expand Down Expand Up @@ -553,7 +551,7 @@ public TargetTypeAssignmentResult unassignType(final Collection<String> controll

private List<JpaTarget> findTargetsByInSpecification(final Collection<String> controllerIds,
final Specification<JpaTarget> specification) {
return Lists.partition(new ArrayList<>(controllerIds), Constants.MAX_ENTRIES_IN_STATEMENT).stream()
return ListUtils.partition(new ArrayList<>(controllerIds), Constants.MAX_ENTRIES_IN_STATEMENT).stream()
.map(ids -> targetRepository.findAll(TargetSpecifications.hasControllerIdIn(ids).and(specification)))
.flatMap(List::stream).toList();
}
Expand Down
Expand Up @@ -17,7 +17,7 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import com.google.common.collect.Lists;
import org.apache.commons.collections4.ListUtils;
import org.eclipse.hawkbit.repository.QuotaManagement;
import org.eclipse.hawkbit.repository.RepositoryConstants;
import org.eclipse.hawkbit.repository.RepositoryProperties;
Expand Down Expand Up @@ -75,7 +75,7 @@ public List<JpaTarget> findTargetsForAssignment(final List<String> controllerIDs
Arrays.asList(TargetSpecifications.hasControllerIdAndAssignedDistributionSetIdNot(ids, setId),
TargetSpecifications.notEqualToTargetUpdateStatus(TargetUpdateStatus.PENDING))));
}
return Lists.partition(controllerIDs, Constants.MAX_ENTRIES_IN_STATEMENT).stream().map(mapper)
return ListUtils.partition(controllerIDs, Constants.MAX_ENTRIES_IN_STATEMENT).stream().map(mapper)
.flatMap(List::stream).collect(Collectors.toList());
}

Expand Down
Expand Up @@ -18,7 +18,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.google.common.collect.Lists;
import org.apache.commons.collections4.ListUtils;
import org.eclipse.hawkbit.repository.QuotaManagement;
import org.eclipse.hawkbit.repository.RepositoryProperties;
import org.eclipse.hawkbit.repository.event.remote.MultiActionAssignEvent;
Expand Down Expand Up @@ -109,7 +109,7 @@ public List<JpaTarget> findTargetsForAssignment(final List<String> controllerIDs
mapper = ids -> targetRepository
.findAll(TargetSpecifications.hasControllerIdAndAssignedDistributionSetIdNot(ids, setId));
}
return Lists.partition(controllerIDs, Constants.MAX_ENTRIES_IN_STATEMENT).stream().map(mapper)
return ListUtils.partition(controllerIDs, Constants.MAX_ENTRIES_IN_STATEMENT).stream().map(mapper)
.flatMap(List::stream).collect(Collectors.toList());
}

Expand Down
Expand Up @@ -14,7 +14,7 @@
import java.util.List;
import java.util.Optional;

import com.google.common.base.Splitter;

import jakarta.persistence.CollectionTable;
import jakarta.persistence.Column;
import jakarta.persistence.ConstraintMode;
Expand Down Expand Up @@ -159,7 +159,20 @@ public final void addMessage(final String message) {
if (messages == null) {
messages = new ArrayList<>((message.length() / MESSAGE_ENTRY_LENGTH) + 1);
}
Splitter.fixedLength(MESSAGE_ENTRY_LENGTH).split(message).forEach(messages::add);
if (message.length() > MESSAGE_ENTRY_LENGTH) {
// split
for (int off = 0; off < message.length();) {
final int end = off + MESSAGE_ENTRY_LENGTH;
if (end < message.length()) {
messages.add(message.substring(off, end));
} else {
messages.add(message.substring(off));
}
off = end;
}
} else {
messages.add(message);
}
}
}

Expand Down
Expand Up @@ -13,6 +13,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -36,9 +37,6 @@
import org.springframework.orm.jpa.JpaSystemException;
import org.springframework.util.CollectionUtils;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

import cz.jirutka.rsql.parser.ParseException;
import cz.jirutka.rsql.parser.RSQLParserException;

Expand Down Expand Up @@ -254,22 +252,14 @@ private static String getCustomMessage(final String message, final List<SuggestT
// sensitive help on search query.
private static final class TokenDescription {

private static final Multimap<Integer, String> TOKEN_MAP = ArrayListMultimap.create();
private static final Map<Integer, List<String>> TOKEN_MAP = new HashMap<>();

private static final int LOGICAL_OP = 8;
private static final int COMPARATOR = 12;

static {
TOKEN_MAP.put(LOGICAL_OP, "and");
TOKEN_MAP.put(LOGICAL_OP, "or");
TOKEN_MAP.put(COMPARATOR, "==");
TOKEN_MAP.put(COMPARATOR, "!=");
TOKEN_MAP.put(COMPARATOR, "=ge=");
TOKEN_MAP.put(COMPARATOR, "=le=");
TOKEN_MAP.put(COMPARATOR, "=gt=");
TOKEN_MAP.put(COMPARATOR, "=lt=");
TOKEN_MAP.put(COMPARATOR, "=in=");
TOKEN_MAP.put(COMPARATOR, "=out=");
TOKEN_MAP.put(LOGICAL_OP, List.of("and", "or"));
TOKEN_MAP.put(COMPARATOR, List.of("==", "!=", "=ge=", "=le=", "=gt=", "=lt=", "=in=", "=out="));
}

private TokenDescription() {
Expand All @@ -279,7 +269,6 @@ private TokenDescription() {
private static Collection<String> getTokenImage(final int tokenIndex) {
return TOKEN_MAP.get(tokenIndex);
}

}

private static final class FieldNameDescription {
Expand Down
Expand Up @@ -38,7 +38,6 @@
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import com.google.common.collect.Lists;
import com.google.common.primitives.Ints;

import io.qameta.allure.Description;
Expand Down

0 comments on commit 791b87b

Please sign in to comment.