Skip to content

Commit e4d4140

Browse files
authored
[MRESOLVER-303] Make checksum detection reusable (#227)
Expose it on selector.. --- https://issues.apache.org/jira/browse/MRESOLVER-303
1 parent ce54f7f commit e4d4140

File tree

6 files changed

+47
-11
lines changed

6 files changed

+47
-11
lines changed

maven-resolver-connector-basic/src/test/java/org/eclipse/aether/connector/basic/TestChecksumAlgorithmSelector.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ public List<ChecksumAlgorithmFactory> selectList( Collection<String> algorithmNa
9898
.collect( toList() );
9999
}
100100

101+
@Override
102+
public boolean isChecksumExtension( String extension )
103+
{
104+
throw new RuntimeException( "not implemented" );
105+
}
106+
101107
private static class MessageDigestChecksumAlgorithmFactory
102108
extends ChecksumAlgorithmFactorySupport
103109
{

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public RepositoryLayout newInstance( RepositorySystemSession session, RemoteRepo
134134
}
135135

136136
return new Maven2RepositoryLayout(
137-
new ArrayList<>( checksumAlgorithmFactorySelector.getChecksumAlgorithmFactories() ),
137+
checksumAlgorithmFactorySelector,
138138
checksumsAlgorithms,
139139
omitChecksumsForExtensions
140140
);
@@ -143,17 +143,17 @@ public RepositoryLayout newInstance( RepositorySystemSession session, RemoteRepo
143143
private static class Maven2RepositoryLayout
144144
implements RepositoryLayout
145145
{
146-
private final List<ChecksumAlgorithmFactory> allChecksumAlgorithms;
146+
private final ChecksumAlgorithmFactorySelector checksumAlgorithmFactorySelector;
147147

148148
private final List<ChecksumAlgorithmFactory> configuredChecksumAlgorithms;
149149

150150
private final Set<String> extensionsWithoutChecksums;
151151

152-
private Maven2RepositoryLayout( List<ChecksumAlgorithmFactory> allChecksumAlgorithms,
152+
private Maven2RepositoryLayout( ChecksumAlgorithmFactorySelector checksumAlgorithmFactorySelector,
153153
List<ChecksumAlgorithmFactory> configuredChecksumAlgorithms,
154154
Set<String> extensionsWithoutChecksums )
155155
{
156-
this.allChecksumAlgorithms = Collections.unmodifiableList( allChecksumAlgorithms );
156+
this.checksumAlgorithmFactorySelector = requireNonNull( checksumAlgorithmFactorySelector );
157157
this.configuredChecksumAlgorithms = Collections.unmodifiableList( configuredChecksumAlgorithms );
158158
this.extensionsWithoutChecksums = requireNonNull( extensionsWithoutChecksums );
159159
}
@@ -269,7 +269,7 @@ private List<ChecksumLocation> getChecksumLocations( URI location )
269269

270270
private boolean isChecksum( String extension )
271271
{
272-
return allChecksumAlgorithms.stream().anyMatch( a -> extension.endsWith( "." + a.getFileExtension() ) );
272+
return checksumAlgorithmFactorySelector.isChecksumExtension( extension );
273273
}
274274
}
275275
}

maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/checksum/DefaultChecksumAlgorithmFactorySelector.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
import javax.inject.Named;
2424
import javax.inject.Singleton;
2525

26-
import java.util.ArrayList;
2726
import java.util.Collection;
27+
import java.util.Collections;
2828
import java.util.HashMap;
2929
import java.util.List;
3030
import java.util.Map;
@@ -94,8 +94,22 @@ public List<ChecksumAlgorithmFactory> selectList( Collection<String> algorithmNa
9494
}
9595

9696
@Override
97-
public List<ChecksumAlgorithmFactory> getChecksumAlgorithmFactories()
97+
public Collection<ChecksumAlgorithmFactory> getChecksumAlgorithmFactories()
9898
{
99-
return new ArrayList<>( factories.values() );
99+
return Collections.unmodifiableCollection( factories.values() );
100+
}
101+
102+
@Override
103+
public boolean isChecksumExtension( String extension )
104+
{
105+
requireNonNull( extension );
106+
if ( extension.contains( "." ) )
107+
{
108+
return factories.values().stream().anyMatch( a -> extension.endsWith( "." + a.getFileExtension() ) );
109+
}
110+
else
111+
{
112+
return factories.values().stream().anyMatch( a -> extension.equals( a.getFileExtension() ) );
113+
}
100114
}
101115
}

maven-resolver-impl/src/test/java/org/eclipse/aether/internal/impl/resolution/TrustedChecksumsArtifactResolverPostProcessorTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ public Collection<ChecksumAlgorithmFactory> getChecksumAlgorithmFactories()
108108
{
109109
return Collections.singletonList( checksumAlgorithmFactory );
110110
}
111+
112+
@Override
113+
public boolean isChecksumExtension( String extension )
114+
{
115+
throw new RuntimeException( "not implemented" );
116+
}
111117
};
112118
subject = new TrustedChecksumsArtifactResolverPostProcessor( selector,
113119
Collections.singletonMap( TRUSTED_SOURCE_NAME, this ) );

maven-resolver-spi/src/main/java/org/eclipse/aether/spi/connector/checksum/ChecksumAlgorithmFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public interface ChecksumAlgorithmFactory
3737
/**
3838
* Returns the file extension to be used for given checksum file (without leading dot), never {@code null}. The
3939
* extension should be file and URL path friendly, and may differ from value returned by {@link #getName()}.
40+
* The checksum extension SHOULD NOT contain dot (".") character.
4041
* Example: "sha1".
4142
*/
4243
String getFileExtension();

maven-resolver-spi/src/main/java/org/eclipse/aether/spi/connector/checksum/ChecksumAlgorithmFactorySelector.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public interface ChecksumAlgorithmFactorySelector
4141
ChecksumAlgorithmFactory select( String algorithmName );
4242

4343
/**
44-
* Returns a list of factories for given algorithm names in order as collection is ordered, or throws if any of the
44+
* Returns a list of factories in same order as algorithm names are ordered, or throws if any of the
4545
* algorithm name is not supported. The returned list has equal count of elements as passed in collection of names,
4646
* and if names contains duplicated elements, the returned list of algorithms will have duplicates as well.
4747
*
@@ -52,10 +52,19 @@ public interface ChecksumAlgorithmFactorySelector
5252
List<ChecksumAlgorithmFactory> selectList( Collection<String> algorithmNames );
5353

5454
/**
55-
* Returns a collection of supported algorithms. This set represents ALL the algorithms supported by Resolver,
56-
* and is NOT in any relation to given repository layout used checksums, returned by method {@link
55+
* Returns immutable collection of all supported algorithms. This set represents ALL the algorithms supported by
56+
* Resolver, and is NOT in any relation to given repository layout used checksums, returned by method {@link
5757
* org.eclipse.aether.spi.connector.layout.RepositoryLayout#getChecksumAlgorithmFactories()} (in fact, is super set
5858
* of it).
5959
*/
6060
Collection<ChecksumAlgorithmFactory> getChecksumAlgorithmFactories();
61+
62+
/**
63+
* Returns {@code true} if passed in extension matches any known checksum extension. The extension string may
64+
* start or contain dot ("."), but does not have to. In former case "ends with" is checked (i.e. "jar.sha1" -> true;
65+
* ".sha1" -> true) while in latter equality (i.e. "sha1" -> true).
66+
*
67+
* @since 1.9.3
68+
*/
69+
boolean isChecksumExtension( String extension );
6170
}

0 commit comments

Comments
 (0)