2222import javax .inject .Named ;
2323import javax .inject .Singleton ;
2424
25+ import java .net .URI ;
2526import java .nio .file .Files ;
2627import java .nio .file .Path ;
2728import java .util .Collections ;
29+ import java .util .List ;
2830import java .util .concurrent .ConcurrentHashMap ;
31+ import java .util .concurrent .ConcurrentMap ;
2932import java .util .function .Supplier ;
3033
3134import org .eclipse .aether .DefaultRepositorySystemSession ;
4043import org .eclipse .aether .repository .RemoteRepository ;
4144import org .eclipse .aether .resolution .MetadataRequest ;
4245import org .eclipse .aether .resolution .MetadataResult ;
46+ import org .eclipse .aether .spi .connector .checksum .ChecksumAlgorithmFactory ;
4347import org .eclipse .aether .spi .connector .filter .RemoteRepositoryFilter ;
4448import org .eclipse .aether .spi .connector .layout .RepositoryLayout ;
4549import org .eclipse .aether .spi .connector .layout .RepositoryLayoutProvider ;
@@ -191,10 +195,6 @@ public final class PrefixesRemoteRepositoryFilterSource extends RemoteRepository
191195
192196 private final RepositoryLayoutProvider repositoryLayoutProvider ;
193197
194- private final ConcurrentHashMap <RemoteRepository , PrefixTree > prefixes ;
195-
196- private final ConcurrentHashMap <RemoteRepository , RepositoryLayout > layouts ;
197-
198198 @ Inject
199199 public PrefixesRemoteRepositoryFilterSource (
200200 Supplier <MetadataResolver > metadataResolver ,
@@ -203,8 +203,18 @@ public PrefixesRemoteRepositoryFilterSource(
203203 this .metadataResolver = requireNonNull (metadataResolver );
204204 this .remoteRepositoryManager = requireNonNull (remoteRepositoryManager );
205205 this .repositoryLayoutProvider = requireNonNull (repositoryLayoutProvider );
206- this .prefixes = new ConcurrentHashMap <>();
207- this .layouts = new ConcurrentHashMap <>();
206+ }
207+
208+ @ SuppressWarnings ("unchecked" )
209+ private ConcurrentMap <RemoteRepository , PrefixTree > prefixes (RepositorySystemSession session ) {
210+ return (ConcurrentMap <RemoteRepository , PrefixTree >)
211+ session .getData ().computeIfAbsent (getClass ().getName () + ".prefixes" , ConcurrentHashMap ::new );
212+ }
213+
214+ @ SuppressWarnings ("unchecked" )
215+ private ConcurrentMap <RemoteRepository , RepositoryLayout > layouts (RepositorySystemSession session ) {
216+ return (ConcurrentMap <RemoteRepository , RepositoryLayout >)
217+ session .getData ().computeIfAbsent (getClass ().getName () + ".layouts" , ConcurrentHashMap ::new );
208218 }
209219
210220 @ Override
@@ -238,25 +248,26 @@ public RemoteRepositoryFilter getRemoteRepositoryFilter(RepositorySystemSession
238248 }
239249
240250 /**
241- * Caches layout instances for remote repository. In case of unknown layout it returns {@code null }.
251+ * Caches layout instances for remote repository. In case of unknown layout it returns {@link #NOT_SUPPORTED }.
242252 *
243- * @return the layout instance of {@code null } if layout not supported.
253+ * @return the layout instance or {@link #NOT_SUPPORTED } if layout not supported.
244254 */
245255 private RepositoryLayout cacheLayout (RepositorySystemSession session , RemoteRepository remoteRepository ) {
246- return layouts .computeIfAbsent (normalizeRemoteRepository (session , remoteRepository ), r -> {
256+ return layouts ( session ) .computeIfAbsent (normalizeRemoteRepository (session , remoteRepository ), r -> {
247257 try {
248258 return repositoryLayoutProvider .newRepositoryLayout (session , remoteRepository );
249259 } catch (NoRepositoryLayoutException e ) {
250- return null ;
260+ return NOT_SUPPORTED ;
251261 }
252262 });
253263 }
254264
255265 private PrefixTree cachePrefixTree (
256266 RepositorySystemSession session , Path basedir , RemoteRepository remoteRepository ) {
257- return prefixes .computeIfAbsent (
258- normalizeRemoteRepository (session , remoteRepository ),
259- r -> loadPrefixTree (session , basedir , remoteRepository ));
267+ return prefixes (session )
268+ .computeIfAbsent (
269+ normalizeRemoteRepository (session , remoteRepository ),
270+ r -> loadPrefixTree (session , basedir , remoteRepository ));
260271 }
261272
262273 private PrefixTree loadPrefixTree (
@@ -339,16 +350,15 @@ private Path resolvePrefixesFromRemoteRepository(
339350 session , Collections .emptyList (), Collections .singletonList (remoteRepository ), true )
340351 .get (0 );
341352 // retrieve prefix as metadata from repository
342- MetadataRequest request =
343- new MetadataRequest (new DefaultMetadata (PREFIX_FILE_TYPE , Metadata .Nature .RELEASE_OR_SNAPSHOT ));
344- request .setRepository (prepared );
345- request .setDeleteLocalCopyIfMissing (true );
346- request .setFavorLocalRepository (true );
347353 MetadataResult result = mr .resolveMetadata (
348354 new DefaultRepositorySystemSession (session )
349355 .setTransferListener (null )
350356 .setConfigProperty (CONFIG_PROP_SKIPPED , Boolean .TRUE .toString ()),
351- Collections .singleton (request ))
357+ Collections .singleton (new MetadataRequest (
358+ new DefaultMetadata (PREFIX_FILE_TYPE , Metadata .Nature .RELEASE_OR_SNAPSHOT ))
359+ .setRepository (prepared )
360+ .setDeleteLocalCopyIfMissing (true )
361+ .setFavorLocalRepository (true )))
352362 .get (0 );
353363 if (result .isResolved ()) {
354364 return result .getMetadata ().getPath ();
@@ -371,7 +381,7 @@ private PrefixesFilter(RepositorySystemSession session, Path basedir) {
371381 @ Override
372382 public Result acceptArtifact (RemoteRepository remoteRepository , Artifact artifact ) {
373383 RepositoryLayout repositoryLayout = cacheLayout (session , remoteRepository );
374- if (repositoryLayout == null ) {
384+ if (repositoryLayout == NOT_SUPPORTED ) {
375385 return new SimpleResult (true , "Unsupported layout: " + remoteRepository );
376386 }
377387 return acceptPrefix (
@@ -382,7 +392,7 @@ public Result acceptArtifact(RemoteRepository remoteRepository, Artifact artifac
382392 @ Override
383393 public Result acceptMetadata (RemoteRepository remoteRepository , Metadata metadata ) {
384394 RepositoryLayout repositoryLayout = cacheLayout (session , remoteRepository );
385- if (repositoryLayout == null ) {
395+ if (repositoryLayout == NOT_SUPPORTED ) {
386396 return new SimpleResult (true , "Unsupported layout: " + remoteRepository );
387397 }
388398 return acceptPrefix (
@@ -392,7 +402,7 @@ public Result acceptMetadata(RemoteRepository remoteRepository, Metadata metadat
392402
393403 private Result acceptPrefix (RemoteRepository repository , String path ) {
394404 PrefixTree prefixTree = cachePrefixTree (session , basedir , repository );
395- if (PrefixTree . SENTINEL == prefixTree ) {
405+ if (prefixTree == PrefixTree . SENTINEL ) {
396406 return NOT_PRESENT_RESULT ;
397407 }
398408 if (prefixTree .acceptedPath (path )) {
@@ -405,4 +415,36 @@ private Result acceptPrefix(RemoteRepository repository, String path) {
405415
406416 private static final RemoteRepositoryFilter .Result NOT_PRESENT_RESULT =
407417 new SimpleResult (true , "Prefix file not present" );
418+
419+ private static final RepositoryLayout NOT_SUPPORTED = new RepositoryLayout () {
420+ @ Override
421+ public List <ChecksumAlgorithmFactory > getChecksumAlgorithmFactories () {
422+ throw new UnsupportedOperationException ();
423+ }
424+
425+ @ Override
426+ public boolean hasChecksums (Artifact artifact ) {
427+ throw new UnsupportedOperationException ();
428+ }
429+
430+ @ Override
431+ public URI getLocation (Artifact artifact , boolean upload ) {
432+ throw new UnsupportedOperationException ();
433+ }
434+
435+ @ Override
436+ public URI getLocation (Metadata metadata , boolean upload ) {
437+ throw new UnsupportedOperationException ();
438+ }
439+
440+ @ Override
441+ public List <ChecksumLocation > getChecksumLocations (Artifact artifact , boolean upload , URI location ) {
442+ throw new UnsupportedOperationException ();
443+ }
444+
445+ @ Override
446+ public List <ChecksumLocation > getChecksumLocations (Metadata metadata , boolean upload , URI location ) {
447+ throw new UnsupportedOperationException ();
448+ }
449+ };
408450}
0 commit comments