Skip to content

Commit 4c01388

Browse files
committed
Simplify, add simple UT
1 parent 31aa532 commit 4c01388

File tree

5 files changed

+107
-49
lines changed

5 files changed

+107
-49
lines changed

maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/filter/PrefixesLockingInhibitorFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public Optional<LockingInhibitor> newInstance(RepositorySystemSession session) {
5555
}
5656

5757
@Override
58-
public Optional<Predicate<Metadata>> inhibitMetadataLocking() {
59-
return Optional.of(PREFIX_PREDICATE);
58+
public boolean inhibitMetadataLocking(Metadata metadata) {
59+
return PREFIX_PREDICATE.test(metadata);
6060
}
6161
}

maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/synccontext/named/InhibitingNameMapper.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@
1919
package org.eclipse.aether.internal.impl.synccontext.named;
2020

2121
import java.util.Collection;
22-
import java.util.function.Predicate;
22+
import java.util.List;
2323
import java.util.stream.Collectors;
2424

2525
import org.eclipse.aether.RepositorySystemSession;
2626
import org.eclipse.aether.artifact.Artifact;
2727
import org.eclipse.aether.metadata.Metadata;
2828
import org.eclipse.aether.named.NamedLockKey;
29+
import org.eclipse.aether.spi.locking.LockingInhibitor;
2930

3031
import static java.util.Objects.requireNonNull;
3132

@@ -36,14 +37,11 @@
3637
*/
3738
public class InhibitingNameMapper implements NameMapper {
3839
private final NameMapper delegate;
39-
private final Predicate<Artifact> artifactPredicate;
40-
private final Predicate<Metadata> metadataPredicate;
40+
private final List<LockingInhibitor> lockingInhibitors;
4141

42-
public InhibitingNameMapper(
43-
NameMapper delegate, Predicate<Artifact> artifactPredicate, Predicate<Metadata> metadataPredicate) {
42+
public InhibitingNameMapper(NameMapper delegate, List<LockingInhibitor> lockingInhibitors) {
4443
this.delegate = requireNonNull(delegate);
45-
this.artifactPredicate = artifactPredicate;
46-
this.metadataPredicate = metadataPredicate;
44+
this.lockingInhibitors = requireNonNull(lockingInhibitors);
4745
}
4846

4947
@Override
@@ -56,13 +54,15 @@ public Collection<NamedLockKey> nameLocks(
5654
RepositorySystemSession session,
5755
Collection<? extends Artifact> artifacts,
5856
Collection<? extends Metadata> metadatas) {
59-
if (artifacts != null && artifactPredicate != null) {
60-
artifacts =
61-
artifacts.stream().filter(a -> !artifactPredicate.test(a)).collect(Collectors.toList());
57+
if (artifacts != null) {
58+
artifacts = artifacts.stream()
59+
.filter(a -> lockingInhibitors.stream().noneMatch(i -> i.inhibitArtifactLocking(a)))
60+
.collect(Collectors.toList());
6261
}
63-
if (metadatas != null && metadataPredicate != null) {
64-
metadatas =
65-
metadatas.stream().filter(m -> !metadataPredicate.test(m)).collect(Collectors.toList());
62+
if (metadatas != null) {
63+
metadatas = metadatas.stream()
64+
.filter(m -> lockingInhibitors.stream().noneMatch(i -> i.inhibitMetadataLocking(m)))
65+
.collect(Collectors.toList());
6666
}
6767
return delegate.nameLocks(session, artifacts, metadatas);
6868
}

maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/synccontext/named/NamedLockFactoryAdapterFactoryImpl.java

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@
2424

2525
import java.util.ArrayList;
2626
import java.util.Map;
27-
import java.util.function.Predicate;
2827

2928
import org.eclipse.aether.MultiRuntimeException;
3029
import org.eclipse.aether.RepositorySystemSession;
31-
import org.eclipse.aether.artifact.Artifact;
3230
import org.eclipse.aether.impl.RepositorySystemLifecycle;
33-
import org.eclipse.aether.metadata.Metadata;
3431
import org.eclipse.aether.named.NamedLockFactory;
3532
import org.eclipse.aether.named.providers.FileLockNamedLockFactory;
33+
import org.eclipse.aether.spi.locking.LockingInhibitor;
3634
import org.eclipse.aether.spi.locking.LockingInhibitorFactory;
3735
import org.eclipse.aether.util.ConfigUtils;
3836
import org.slf4j.Logger;
@@ -196,32 +194,12 @@ protected NameMapper selectNameMapper(final RepositorySystemSession session, fin
196194
"Unknown NameMapper name: '" + nameMapperName + "', known ones: " + nameMappers.keySet());
197195
}
198196
if (!lockingInhibitorFactories.isEmpty()) {
199-
ArrayList<Predicate<Artifact>> artifactPredicates = new ArrayList<>();
200-
ArrayList<Predicate<Metadata>> metadataPredicates = new ArrayList<>();
197+
ArrayList<LockingInhibitor> inhibitors = new ArrayList<>();
201198
for (LockingInhibitorFactory factory : lockingInhibitorFactories.values()) {
202-
factory.newInstance(session).ifPresent(i -> {
203-
i.inhibitArtifactLocking().ifPresent(artifactPredicates::add);
204-
i.inhibitMetadataLocking().ifPresent(metadataPredicates::add);
205-
});
199+
factory.newInstance(session).ifPresent(inhibitors::add);
206200
}
207-
if (!artifactPredicates.isEmpty() || !metadataPredicates.isEmpty()) {
208-
Predicate<Artifact> ap = null;
209-
Predicate<Metadata> md = null;
210-
for (Predicate<Artifact> predicate : artifactPredicates) {
211-
if (ap == null) {
212-
ap = predicate;
213-
} else {
214-
ap = ap.or(predicate);
215-
}
216-
}
217-
for (Predicate<Metadata> predicate : metadataPredicates) {
218-
if (md == null) {
219-
md = predicate;
220-
} else {
221-
md = md.or(predicate);
222-
}
223-
}
224-
return new InhibitingNameMapper(nameMapper, ap, md);
201+
if (!inhibitors.isEmpty()) {
202+
return new InhibitingNameMapper(nameMapper, inhibitors);
225203
}
226204
}
227205
return nameMapper;
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.eclipse.aether.internal.impl.synccontext.named;
20+
21+
import java.util.ArrayList;
22+
import java.util.Arrays;
23+
import java.util.Collection;
24+
25+
import org.eclipse.aether.artifact.Artifact;
26+
import org.eclipse.aether.artifact.DefaultArtifact;
27+
import org.eclipse.aether.metadata.DefaultMetadata;
28+
import org.eclipse.aether.metadata.Metadata;
29+
import org.eclipse.aether.named.NamedLockKey;
30+
import org.eclipse.aether.spi.locking.LockingInhibitor;
31+
import org.junit.jupiter.api.Test;
32+
33+
import static java.util.Collections.singletonList;
34+
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
36+
public class InhibitingNameMapperTest extends NameMapperTestSupport {
37+
NameMapper mapper = new InhibitingNameMapper(
38+
NameMappers.gavNameMapper(true),
39+
Arrays.asList(
40+
new LockingInhibitor() {
41+
@Override
42+
public boolean inhibitArtifactLocking(Artifact artifact) {
43+
return "no.lock".equals(artifact.getGroupId());
44+
}
45+
},
46+
new LockingInhibitor() {
47+
@Override
48+
public boolean inhibitMetadataLocking(Metadata metadata) {
49+
return "no.lock".equals(metadata.getGroupId());
50+
}
51+
}));
52+
53+
@Test
54+
void singleArtifactWithLocking() {
55+
DefaultArtifact artifact = new DefaultArtifact("group:artifact:1.0");
56+
Collection<NamedLockKey> names = mapper.nameLocks(session, singletonList(artifact), null);
57+
assertEquals(1, names.size());
58+
assertEquals("artifact~group~artifact~1.0.lock", names.iterator().next().name());
59+
}
60+
61+
@Test
62+
void singleArtifactWithoutLocking() {
63+
DefaultArtifact artifact = new DefaultArtifact("no.lock:artifact:1.0");
64+
Collection<NamedLockKey> names = mapper.nameLocks(session, singletonList(artifact), null);
65+
assertEquals(0, names.size());
66+
}
67+
68+
@Test
69+
void combined() {
70+
Collection<NamedLockKey> names = mapper.nameLocks(
71+
session,
72+
Arrays.asList(new DefaultArtifact("group:artifact:1.0"), new DefaultArtifact("no.lock:artifact:1.0")),
73+
Arrays.asList(
74+
new DefaultMetadata(
75+
"group", "artifact", "maven-metadata.xml", Metadata.Nature.RELEASE_OR_SNAPSHOT),
76+
new DefaultMetadata(
77+
"no.lock", "artifact", "maven-metadata.xml", Metadata.Nature.RELEASE_OR_SNAPSHOT)));
78+
assertEquals(2, names.size());
79+
ArrayList<NamedLockKey> keys = new ArrayList<>(names);
80+
assertEquals("artifact~group~artifact~1.0.lock", keys.get(0).name());
81+
assertEquals("metadata~group~artifact.lock", keys.get(1).name());
82+
}
83+
}

maven-resolver-spi/src/main/java/org/eclipse/aether/spi/locking/LockingInhibitor.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
*/
1919
package org.eclipse.aether.spi.locking;
2020

21-
import java.util.Optional;
22-
import java.util.function.Predicate;
23-
2421
import org.eclipse.aether.artifact.Artifact;
2522
import org.eclipse.aether.metadata.Metadata;
2623

@@ -34,14 +31,14 @@ public interface LockingInhibitor {
3431
* May provide a predicate for artifacts that needs lock inhibition. <em>Warning: you do not want to override
3532
* this method, or if you do, think twice.</em>
3633
*/
37-
default Optional<Predicate<Artifact>> inhibitArtifactLocking() {
38-
return Optional.empty();
34+
default boolean inhibitArtifactLocking(Artifact artifact) {
35+
return false;
3936
}
4037

4138
/**
4239
* May provide a predicate for metadata that needs lock inhibition.
4340
*/
44-
default Optional<Predicate<Metadata>> inhibitMetadataLocking() {
45-
return Optional.empty();
41+
default boolean inhibitMetadataLocking(Metadata metadata) {
42+
return false;
4643
}
4744
}

0 commit comments

Comments
 (0)