From c32b8c9236389ee04ab5d0aac3d206f8bb1b2f36 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Thu, 14 Nov 2024 11:46:18 +0100 Subject: [PATCH] [MRESOLVER-628] Explicit cache key for prioritized components (#601) As original solution using map.hashCode may clash, and in that case runtime "just in time" class clast issue happens. Rather provide explicit discriminator class "what is in maps" and use that to create key to cache in session. --- https://issues.apache.org/jira/browse/MRESOLVER-628 --- .../impl/DefaultLocalRepositoryProvider.java | 5 ++++- .../impl/DefaultRepositoryConnectorProvider.java | 2 +- .../impl/DefaultRepositoryLayoutProvider.java | 4 ++-- .../internal/impl/DefaultTransporterProvider.java | 4 ++-- .../aether/internal/impl/PrioritizedComponents.java | 12 ++++++++---- .../java/org/eclipse/aether/internal/impl/Utils.java | 9 ++++++--- 6 files changed, 23 insertions(+), 13 deletions(-) diff --git a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultLocalRepositoryProvider.java b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultLocalRepositoryProvider.java index e2c23e658..3556cab9d 100644 --- a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultLocalRepositoryProvider.java +++ b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultLocalRepositoryProvider.java @@ -60,7 +60,10 @@ public LocalRepositoryManager newLocalRepositoryManager(RepositorySystemSession requireNonNull(repository, "repository cannot be null"); PrioritizedComponents factories = PrioritizedComponents.reuseOrCreate( - session, localRepositoryManagerFactories, LocalRepositoryManagerFactory::getPriority); + session, + LocalRepositoryManagerFactory.class, + localRepositoryManagerFactories, + LocalRepositoryManagerFactory::getPriority); List errors = new ArrayList<>(); for (PrioritizedComponent factory : factories.getEnabled()) { diff --git a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultRepositoryConnectorProvider.java b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultRepositoryConnectorProvider.java index c913dd15e..859d06cb0 100644 --- a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultRepositoryConnectorProvider.java +++ b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultRepositoryConnectorProvider.java @@ -78,7 +78,7 @@ public RepositoryConnector newRepositoryConnector(RepositorySystemSession sessio } PrioritizedComponents factories = PrioritizedComponents.reuseOrCreate( - session, connectorFactories, RepositoryConnectorFactory::getPriority); + session, RepositoryConnectorFactory.class, connectorFactories, RepositoryConnectorFactory::getPriority); RemoteRepositoryFilter filter = remoteRepositoryFilterManager.getRemoteRepositoryFilter(session); List errors = new ArrayList<>(); diff --git a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultRepositoryLayoutProvider.java b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultRepositoryLayoutProvider.java index 6671505bb..354830846 100644 --- a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultRepositoryLayoutProvider.java +++ b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultRepositoryLayoutProvider.java @@ -59,8 +59,8 @@ public RepositoryLayout newRepositoryLayout(RepositorySystemSession session, Rem requireNonNull(session, "session cannot be null"); requireNonNull(repository, "remote repository cannot be null"); - PrioritizedComponents factories = - PrioritizedComponents.reuseOrCreate(session, layoutFactories, RepositoryLayoutFactory::getPriority); + PrioritizedComponents factories = PrioritizedComponents.reuseOrCreate( + session, RepositoryLayoutFactory.class, layoutFactories, RepositoryLayoutFactory::getPriority); List errors = new ArrayList<>(); for (PrioritizedComponent factory : factories.getEnabled()) { diff --git a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultTransporterProvider.java b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultTransporterProvider.java index dabe36fba..b494a1bf4 100644 --- a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultTransporterProvider.java +++ b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultTransporterProvider.java @@ -59,8 +59,8 @@ public Transporter newTransporter(RepositorySystemSession session, RemoteReposit requireNonNull(session, "session cannot be null"); requireNonNull(repository, "repository cannot be null"); - PrioritizedComponents factories = - PrioritizedComponents.reuseOrCreate(session, transporterFactories, TransporterFactory::getPriority); + PrioritizedComponents factories = PrioritizedComponents.reuseOrCreate( + session, TransporterFactory.class, transporterFactories, TransporterFactory::getPriority); List errors = new ArrayList<>(); for (PrioritizedComponent factory : factories.getEnabled()) { diff --git a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/PrioritizedComponents.java b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/PrioritizedComponents.java index 8a5c917ce..3fe020f95 100644 --- a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/PrioritizedComponents.java +++ b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/PrioritizedComponents.java @@ -35,8 +35,9 @@ */ public final class PrioritizedComponents { /** - * Reuses or creates and stores (if session data does not contain yet) prioritized components under this key into - * given session. Same session is used to configure prioritized components. + * Reuses or creates and stores (if session data does not contain yet) prioritized components under certain key into + * given session. Same session is used to configure prioritized components, so priority sorted components during + * session are immutable and reusable. *

* The {@code components} are expected to be Sisu injected {@link Map}-like component maps. There is a * simple "change detection" in place, as injected maps are dynamic, they are atomically expanded or contracted @@ -46,11 +47,14 @@ public final class PrioritizedComponents { */ @SuppressWarnings("unchecked") public static PrioritizedComponents reuseOrCreate( - RepositorySystemSession session, Map components, Function priorityFunction) { + RepositorySystemSession session, + Class discriminator, + Map components, + Function priorityFunction) { boolean cached = ConfigUtils.getBoolean( session, ConfigurationProperties.DEFAULT_CACHED_PRIORITIES, ConfigurationProperties.CACHED_PRIORITIES); if (cached) { - String key = PrioritizedComponents.class.getName() + ".pc" + Integer.toHexString(components.hashCode()); + String key = PrioritizedComponents.class.getName() + ".pc." + discriminator.getName(); return (PrioritizedComponents) session.getData().computeIfAbsent(key, () -> create(session, components, priorityFunction)); } else { diff --git a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/Utils.java b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/Utils.java index dbebe6e58..87cb48ae1 100644 --- a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/Utils.java +++ b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/Utils.java @@ -48,7 +48,8 @@ private Utils() {} private static PrioritizedComponents sortArtifactDecoratorFactories( RepositorySystemSession session, Map factories) { - return PrioritizedComponents.reuseOrCreate(session, factories, ArtifactDecoratorFactory::getPriority); + return PrioritizedComponents.reuseOrCreate( + session, ArtifactDecoratorFactory.class, factories, ArtifactDecoratorFactory::getPriority); } public static List getArtifactDecorators( @@ -67,7 +68,8 @@ public static List getArtifactDecorators( private static PrioritizedComponents sortArtifactGeneratorFactories( RepositorySystemSession session, Map factories) { - return PrioritizedComponents.reuseOrCreate(session, factories, ArtifactGeneratorFactory::getPriority); + return PrioritizedComponents.reuseOrCreate( + session, ArtifactGeneratorFactory.class, factories, ArtifactGeneratorFactory::getPriority); } private static List doGetArtifactGenerators( @@ -107,7 +109,8 @@ public static List getArtifactGenerators( private static PrioritizedComponents sortMetadataGeneratorFactories( RepositorySystemSession session, Map factories) { - return PrioritizedComponents.reuseOrCreate(session, factories, MetadataGeneratorFactory::getPriority); + return PrioritizedComponents.reuseOrCreate( + session, MetadataGeneratorFactory.class, factories, MetadataGeneratorFactory::getPriority); } private static List doGetMetadataGenerators(