From 534c3d79cee7563b06a9ac2c9a38bea22aacfedc Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Fri, 20 Oct 2023 21:09:46 +0200 Subject: [PATCH 1/5] [MRESOLVER-420] Cache prioritized components --- https://issues.apache.org/jira/browse/MRESOLVER-420 --- .../impl/DefaultLocalRepositoryProvider.java | 8 ++++---- .../impl/DefaultRepositoryConnectorProvider.java | 10 ++++------ .../impl/DefaultRepositoryLayoutProvider.java | 7 +++---- .../impl/DefaultTransporterProvider.java | 7 +++---- .../internal/impl/PrioritizedComponents.java | 16 ++++++++++++++++ .../org/eclipse/aether/internal/impl/Utils.java | 12 ++++++------ 6 files changed, 36 insertions(+), 24 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 084f43ec2..2bafcc9a4 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 @@ -46,6 +46,8 @@ public class DefaultLocalRepositoryProvider implements LocalRepositoryProvider { private static final Logger LOGGER = LoggerFactory.getLogger(DefaultLocalRepositoryProvider.class); + private static final String PRIORITIZED_COMPONENTS = DefaultLocalRepositoryProvider.class.getName() + ".pc"; + private final Map localRepositoryManagerFactories; @Inject @@ -58,10 +60,8 @@ public LocalRepositoryManager newLocalRepositoryManager(RepositorySystemSession throws NoLocalRepositoryManagerException { requireNonNull(session, "session cannot be null"); requireNonNull(repository, "repository cannot be null"); - PrioritizedComponents factories = new PrioritizedComponents<>(session); - for (LocalRepositoryManagerFactory factory : this.localRepositoryManagerFactories.values()) { - factories.add(factory, factory.getPriority()); - } + + PrioritizedComponents factories = PrioritizedComponents.reuseOrCreate(session, PRIORITIZED_COMPONENTS, this.localRepositoryManagerFactories.values(), 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 5aeb1012e..11d9dd0f6 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 @@ -51,6 +51,8 @@ public class DefaultRepositoryConnectorProvider implements RepositoryConnectorPr private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRepositoryConnectorProvider.class); + private static final String PRIORITIZED_COMPONENTS = DefaultRepositoryConnectorProvider.class.getName() + ".pc"; + private final Map connectorFactories; private final RemoteRepositoryFilterManager remoteRepositoryFilterManager; @@ -77,13 +79,9 @@ public RepositoryConnector newRepositoryConnector(RepositorySystemSession sessio } } - RemoteRepositoryFilter filter = remoteRepositoryFilterManager.getRemoteRepositoryFilter(session); - - PrioritizedComponents factories = new PrioritizedComponents<>(session); - for (RepositoryConnectorFactory factory : this.connectorFactories.values()) { - factories.add(factory, factory.getPriority()); - } + PrioritizedComponents factories = PrioritizedComponents.reuseOrCreate(session, PRIORITIZED_COMPONENTS, this.connectorFactories.values(), RepositoryConnectorFactory::getPriority); + RemoteRepositoryFilter filter = remoteRepositoryFilterManager.getRemoteRepositoryFilter(session); List errors = new ArrayList<>(); for (PrioritizedComponent factory : factories.getEnabled()) { try { 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 610680090..7b89df4d2 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 @@ -46,6 +46,8 @@ public final class DefaultRepositoryLayoutProvider implements RepositoryLayoutPr private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRepositoryLayoutProvider.class); + private static final String PRIORITIZED_COMPONENTS = DefaultRepositoryLayoutProvider.class.getName() + ".pc"; + private final Map layoutFactories; @Inject @@ -59,10 +61,7 @@ public RepositoryLayout newRepositoryLayout(RepositorySystemSession session, Rem requireNonNull(session, "session cannot be null"); requireNonNull(repository, "remote repository cannot be null"); - PrioritizedComponents factories = new PrioritizedComponents<>(session); - for (RepositoryLayoutFactory factory : this.layoutFactories.values()) { - factories.add(factory, factory.getPriority()); - } + PrioritizedComponents factories = PrioritizedComponents.reuseOrCreate(session, PRIORITIZED_COMPONENTS, this.layoutFactories.values(), 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 774fbaf52..ec77ea02f 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 @@ -46,6 +46,8 @@ public final class DefaultTransporterProvider implements TransporterProvider { private static final Logger LOGGER = LoggerFactory.getLogger(DefaultTransporterProvider.class); + private static final String PRIORITIZED_COMPONENTS = DefaultTransporterProvider.class.getName() + ".pc"; + private final Map transporterFactories; @Inject @@ -59,10 +61,7 @@ public Transporter newTransporter(RepositorySystemSession session, RemoteReposit requireNonNull(session, "session cannot be null"); requireNonNull(repository, "repository cannot be null"); - PrioritizedComponents factories = new PrioritizedComponents<>(session); - for (TransporterFactory factory : this.transporterFactories.values()) { - factories.add(factory, factory.getPriority()); - } + PrioritizedComponents factories = PrioritizedComponents.reuseOrCreate(session, PRIORITIZED_COMPONENTS, this.transporterFactories.values(), 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 87b65b82a..d06b1490d 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 @@ -19,9 +19,11 @@ package org.eclipse.aether.internal.impl; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.function.Function; import org.eclipse.aether.ConfigurationProperties; import org.eclipse.aether.RepositorySystemSession; @@ -31,6 +33,20 @@ * Helps to sort pluggable components by their priority. */ 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. + * + * @since TBD + */ + @SuppressWarnings( "unchecked" ) + public static PrioritizedComponents reuseOrCreate(RepositorySystemSession session, String key, Collection components, Function priorityFunction) { + return (PrioritizedComponents) session.getData().computeIfAbsent(key, () -> { + PrioritizedComponents newInstance = new PrioritizedComponents<>(session); + components.forEach(c -> newInstance.add(c, priorityFunction.apply(c))); + return components; + }); + } private static final String FACTORY_SUFFIX = "Factory"; 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 0c16abfcf..6f491c33c 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 @@ -31,6 +31,8 @@ import org.eclipse.aether.repository.RemoteRepository; import org.eclipse.aether.resolution.ResolutionErrorPolicy; import org.eclipse.aether.resolution.ResolutionErrorPolicyRequest; +import org.eclipse.aether.spi.connector.RepositoryConnectorFactory; +import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory; import org.eclipse.aether.transfer.RepositoryOfflineException; /** @@ -38,13 +40,11 @@ */ final class Utils { + private static final String PRIORITIZED_COMPONENTS = Utils.class.getName() + ".pc"; + public static PrioritizedComponents sortMetadataGeneratorFactories( - RepositorySystemSession session, Collection factories) { - PrioritizedComponents result = new PrioritizedComponents<>(session); - for (MetadataGeneratorFactory factory : factories) { - result.add(factory, factory.getPriority()); - } - return result; + RepositorySystemSession session, Collection factories) { + return PrioritizedComponents.reuseOrCreate(session, PRIORITIZED_COMPONENTS, factories, MetadataGeneratorFactory::getPriority); } public static List prepareMetadata( From a493c7676638bce84d6e5e8f8289e6ae384f0e76 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Fri, 20 Oct 2023 21:11:48 +0200 Subject: [PATCH 2/5] Reformat --- .../internal/impl/DefaultLocalRepositoryProvider.java | 6 +++++- .../internal/impl/DefaultRepositoryConnectorProvider.java | 6 +++++- .../internal/impl/DefaultRepositoryLayoutProvider.java | 3 ++- .../aether/internal/impl/DefaultTransporterProvider.java | 3 ++- .../aether/internal/impl/PrioritizedComponents.java | 8 ++++++-- .../main/java/org/eclipse/aether/internal/impl/Utils.java | 5 ++--- 6 files changed, 22 insertions(+), 9 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 2bafcc9a4..847c03c3c 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 @@ -61,7 +61,11 @@ public LocalRepositoryManager newLocalRepositoryManager(RepositorySystemSession requireNonNull(session, "session cannot be null"); requireNonNull(repository, "repository cannot be null"); - PrioritizedComponents factories = PrioritizedComponents.reuseOrCreate(session, PRIORITIZED_COMPONENTS, this.localRepositoryManagerFactories.values(), LocalRepositoryManagerFactory::getPriority); + PrioritizedComponents factories = PrioritizedComponents.reuseOrCreate( + session, + PRIORITIZED_COMPONENTS, + this.localRepositoryManagerFactories.values(), + 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 11d9dd0f6..e800cfcd2 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 @@ -79,7 +79,11 @@ public RepositoryConnector newRepositoryConnector(RepositorySystemSession sessio } } - PrioritizedComponents factories = PrioritizedComponents.reuseOrCreate(session, PRIORITIZED_COMPONENTS, this.connectorFactories.values(), RepositoryConnectorFactory::getPriority); + PrioritizedComponents factories = PrioritizedComponents.reuseOrCreate( + session, + PRIORITIZED_COMPONENTS, + this.connectorFactories.values(), + 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 7b89df4d2..184af04dd 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 @@ -61,7 +61,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, PRIORITIZED_COMPONENTS, this.layoutFactories.values(), RepositoryLayoutFactory::getPriority); + PrioritizedComponents factories = PrioritizedComponents.reuseOrCreate( + session, PRIORITIZED_COMPONENTS, this.layoutFactories.values(), 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 ec77ea02f..2fd4d818b 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 @@ -61,7 +61,8 @@ public Transporter newTransporter(RepositorySystemSession session, RemoteReposit requireNonNull(session, "session cannot be null"); requireNonNull(repository, "repository cannot be null"); - PrioritizedComponents factories = PrioritizedComponents.reuseOrCreate(session, PRIORITIZED_COMPONENTS, this.transporterFactories.values(), TransporterFactory::getPriority); + PrioritizedComponents factories = PrioritizedComponents.reuseOrCreate( + session, PRIORITIZED_COMPONENTS, this.transporterFactories.values(), 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 d06b1490d..7b9472c5c 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 @@ -39,8 +39,12 @@ final class PrioritizedComponents { * * @since TBD */ - @SuppressWarnings( "unchecked" ) - public static PrioritizedComponents reuseOrCreate(RepositorySystemSession session, String key, Collection components, Function priorityFunction) { + @SuppressWarnings("unchecked") + public static PrioritizedComponents reuseOrCreate( + RepositorySystemSession session, + String key, + Collection components, + Function priorityFunction) { return (PrioritizedComponents) session.getData().computeIfAbsent(key, () -> { PrioritizedComponents newInstance = new PrioritizedComponents<>(session); components.forEach(c -> newInstance.add(c, priorityFunction.apply(c))); 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 6f491c33c..8a889fd00 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 @@ -31,8 +31,6 @@ import org.eclipse.aether.repository.RemoteRepository; import org.eclipse.aether.resolution.ResolutionErrorPolicy; import org.eclipse.aether.resolution.ResolutionErrorPolicyRequest; -import org.eclipse.aether.spi.connector.RepositoryConnectorFactory; -import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory; import org.eclipse.aether.transfer.RepositoryOfflineException; /** @@ -44,7 +42,8 @@ final class Utils { public static PrioritizedComponents sortMetadataGeneratorFactories( RepositorySystemSession session, Collection factories) { - return PrioritizedComponents.reuseOrCreate(session, PRIORITIZED_COMPONENTS, factories, MetadataGeneratorFactory::getPriority); + return PrioritizedComponents.reuseOrCreate( + session, PRIORITIZED_COMPONENTS, factories, MetadataGeneratorFactory::getPriority); } public static List prepareMetadata( From c4fabec7e945d3bb433452d59406287f77b34754 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Fri, 20 Oct 2023 21:18:50 +0200 Subject: [PATCH 3/5] Bugfix --- .../org/eclipse/aether/internal/impl/PrioritizedComponents.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 7b9472c5c..9b857bd01 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 @@ -48,7 +48,7 @@ public static PrioritizedComponents reuseOrCreate( return (PrioritizedComponents) session.getData().computeIfAbsent(key, () -> { PrioritizedComponents newInstance = new PrioritizedComponents<>(session); components.forEach(c -> newInstance.add(c, priorityFunction.apply(c))); - return components; + return newInstance; }); } From ebbfd078b96246ead668c737a8441f83cf412cd0 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Fri, 20 Oct 2023 21:54:15 +0200 Subject: [PATCH 4/5] Simplify and make dynamic discovery resilient. --- .../aether/internal/impl/DefaultDeployer.java | 2 +- .../aether/internal/impl/DefaultInstaller.java | 2 +- .../impl/DefaultLocalRepositoryProvider.java | 7 +------ .../impl/DefaultRepositoryConnectorProvider.java | 7 +------ .../impl/DefaultRepositoryLayoutProvider.java | 6 ++---- .../internal/impl/DefaultTransporterProvider.java | 6 ++---- .../aether/internal/impl/PrioritizedComponents.java | 13 +++++++------ .../org/eclipse/aether/internal/impl/Utils.java | 8 +++----- 8 files changed, 18 insertions(+), 33 deletions(-) diff --git a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultDeployer.java b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultDeployer.java index d0059f432..20c13407f 100644 --- a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultDeployer.java +++ b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultDeployer.java @@ -234,7 +234,7 @@ private DeployResult deploy(SyncContext syncContext, RepositorySystemSession ses private List getMetadataGenerators( RepositorySystemSession session, DeployRequest request) { PrioritizedComponents factories = - Utils.sortMetadataGeneratorFactories(session, this.metadataFactories.values()); + Utils.sortMetadataGeneratorFactories(session, metadataFactories); List generators = new ArrayList<>(); diff --git a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultInstaller.java b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultInstaller.java index 300e74781..6f24fc5bd 100644 --- a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultInstaller.java +++ b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultInstaller.java @@ -151,7 +151,7 @@ private InstallResult install(SyncContext syncContext, RepositorySystemSession s private List getMetadataGenerators( RepositorySystemSession session, InstallRequest request) { PrioritizedComponents factories = - Utils.sortMetadataGeneratorFactories(session, this.metadataFactories.values()); + Utils.sortMetadataGeneratorFactories(session, metadataFactories); List generators = new ArrayList<>(); 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 847c03c3c..a72c83d30 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 @@ -46,8 +46,6 @@ public class DefaultLocalRepositoryProvider implements LocalRepositoryProvider { private static final Logger LOGGER = LoggerFactory.getLogger(DefaultLocalRepositoryProvider.class); - private static final String PRIORITIZED_COMPONENTS = DefaultLocalRepositoryProvider.class.getName() + ".pc"; - private final Map localRepositoryManagerFactories; @Inject @@ -62,10 +60,7 @@ public LocalRepositoryManager newLocalRepositoryManager(RepositorySystemSession requireNonNull(repository, "repository cannot be null"); PrioritizedComponents factories = PrioritizedComponents.reuseOrCreate( - session, - PRIORITIZED_COMPONENTS, - this.localRepositoryManagerFactories.values(), - LocalRepositoryManagerFactory::getPriority); + session, 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 e800cfcd2..c913dd15e 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 @@ -51,8 +51,6 @@ public class DefaultRepositoryConnectorProvider implements RepositoryConnectorPr private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRepositoryConnectorProvider.class); - private static final String PRIORITIZED_COMPONENTS = DefaultRepositoryConnectorProvider.class.getName() + ".pc"; - private final Map connectorFactories; private final RemoteRepositoryFilterManager remoteRepositoryFilterManager; @@ -80,10 +78,7 @@ public RepositoryConnector newRepositoryConnector(RepositorySystemSession sessio } PrioritizedComponents factories = PrioritizedComponents.reuseOrCreate( - session, - PRIORITIZED_COMPONENTS, - this.connectorFactories.values(), - RepositoryConnectorFactory::getPriority); + session, 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 184af04dd..6671505bb 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 @@ -46,8 +46,6 @@ public final class DefaultRepositoryLayoutProvider implements RepositoryLayoutPr private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRepositoryLayoutProvider.class); - private static final String PRIORITIZED_COMPONENTS = DefaultRepositoryLayoutProvider.class.getName() + ".pc"; - private final Map layoutFactories; @Inject @@ -61,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, PRIORITIZED_COMPONENTS, this.layoutFactories.values(), RepositoryLayoutFactory::getPriority); + PrioritizedComponents factories = + PrioritizedComponents.reuseOrCreate(session, 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 2fd4d818b..dabe36fba 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 @@ -46,8 +46,6 @@ public final class DefaultTransporterProvider implements TransporterProvider { private static final Logger LOGGER = LoggerFactory.getLogger(DefaultTransporterProvider.class); - private static final String PRIORITIZED_COMPONENTS = DefaultTransporterProvider.class.getName() + ".pc"; - private final Map transporterFactories; @Inject @@ -61,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, PRIORITIZED_COMPONENTS, this.transporterFactories.values(), TransporterFactory::getPriority); + PrioritizedComponents factories = + PrioritizedComponents.reuseOrCreate(session, 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 9b857bd01..991f45a4c 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 @@ -19,7 +19,6 @@ package org.eclipse.aether.internal.impl; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; @@ -36,18 +35,20 @@ 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. + *

+ * 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 + * as components are dynamically discovered or unloaded. * * @since TBD */ @SuppressWarnings("unchecked") public static PrioritizedComponents reuseOrCreate( - RepositorySystemSession session, - String key, - Collection components, - Function priorityFunction) { + RepositorySystemSession session, Map components, Function priorityFunction) { + String key = PrioritizedComponents.class.getName() + ".pc" + Integer.toHexString(components.hashCode()); return (PrioritizedComponents) session.getData().computeIfAbsent(key, () -> { PrioritizedComponents newInstance = new PrioritizedComponents<>(session); - components.forEach(c -> newInstance.add(c, priorityFunction.apply(c))); + components.values().forEach(c -> newInstance.add(c, priorityFunction.apply(c))); return newInstance; }); } 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 8a889fd00..95165663e 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 @@ -21,6 +21,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Map; import org.eclipse.aether.RepositorySystemSession; import org.eclipse.aether.artifact.Artifact; @@ -38,12 +39,9 @@ */ final class Utils { - private static final String PRIORITIZED_COMPONENTS = Utils.class.getName() + ".pc"; - public static PrioritizedComponents sortMetadataGeneratorFactories( - RepositorySystemSession session, Collection factories) { - return PrioritizedComponents.reuseOrCreate( - session, PRIORITIZED_COMPONENTS, factories, MetadataGeneratorFactory::getPriority); + RepositorySystemSession session, Map factories) { + return PrioritizedComponents.reuseOrCreate(session, factories, MetadataGeneratorFactory::getPriority); } public static List prepareMetadata( From 4a0dbda40284a8e6364844554b288697f5fc2417 Mon Sep 17 00:00:00 2001 From: Tamas Cservenak Date: Fri, 20 Oct 2023 22:10:18 +0200 Subject: [PATCH 5/5] Make this logic enabled by default. --- .../aether/ConfigurationProperties.java | 15 +++++++++++++ .../internal/impl/PrioritizedComponents.java | 22 ++++++++++++++----- src/site/markdown/configuration.md | 1 + 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/maven-resolver-api/src/main/java/org/eclipse/aether/ConfigurationProperties.java b/maven-resolver-api/src/main/java/org/eclipse/aether/ConfigurationProperties.java index c117c1d5b..279e707cd 100644 --- a/maven-resolver-api/src/main/java/org/eclipse/aether/ConfigurationProperties.java +++ b/maven-resolver-api/src/main/java/org/eclipse/aether/ConfigurationProperties.java @@ -55,6 +55,21 @@ public final class ConfigurationProperties { */ public static final boolean DEFAULT_IMPLICIT_PRIORITIES = false; + /** + * A flag indicating whether the created ordered components should be cached or not. + * + * @see #DEFAULT_CACHED_PRIORITIES + * @since TBD + */ + public static final String CACHED_PRIORITIES = PREFIX_PRIORITY + "cached"; + + /** + * The default caching of priority components if {@link #CACHED_PRIORITIES} isn't set. Default value is {@code true}. + * + * @since TBD + */ + public static final boolean DEFAULT_CACHED_PRIORITIES = true; + /** * A flag indicating whether interaction with the user is allowed. * 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 991f45a4c..fad550241 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 @@ -45,12 +45,22 @@ final class PrioritizedComponents { @SuppressWarnings("unchecked") public static PrioritizedComponents reuseOrCreate( RepositorySystemSession session, Map components, Function priorityFunction) { - String key = PrioritizedComponents.class.getName() + ".pc" + Integer.toHexString(components.hashCode()); - return (PrioritizedComponents) session.getData().computeIfAbsent(key, () -> { - PrioritizedComponents newInstance = new PrioritizedComponents<>(session); - components.values().forEach(c -> newInstance.add(c, priorityFunction.apply(c))); - return newInstance; - }); + boolean cached = ConfigUtils.getBoolean( + session, ConfigurationProperties.DEFAULT_CACHED_PRIORITIES, ConfigurationProperties.CACHED_PRIORITIES); + if (cached) { + String key = PrioritizedComponents.class.getName() + ".pc" + Integer.toHexString(components.hashCode()); + return (PrioritizedComponents) + session.getData().computeIfAbsent(key, () -> create(session, components, priorityFunction)); + } else { + return create(session, components, priorityFunction); + } + } + + private static PrioritizedComponents create( + RepositorySystemSession session, Map components, Function priorityFunction) { + PrioritizedComponents newInstance = new PrioritizedComponents<>(session); + components.values().forEach(c -> newInstance.add(c, priorityFunction.apply(c))); + return newInstance; } private static final String FACTORY_SUFFIX = "Factory"; diff --git a/src/site/markdown/configuration.md b/src/site/markdown/configuration.md index dc1cb64dd..d8bffd8e0 100644 --- a/src/site/markdown/configuration.md +++ b/src/site/markdown/configuration.md @@ -85,6 +85,7 @@ Option | Type | Description | Default Value | Supports Repo ID Suffix `aether.metadataResolver.threads` | int | Number of threads to use in parallel for resolving metadata. | `4` | no `aether.offline.protocols` | String | Comma-separated list of protocols which are supposed to be resolved offline. | - | no `aether.offline.hosts` | String | Comma-separated list of hosts which are supposed to be resolved offline. | - | no +`aether.priority.cached` | boolean | Whether the created ordered list of components should be cached (in session) or not. | `true` | no `aether.priority.` | float | The priority to use for a certain extension class. `class` can either be the fully qualified name or the simple name stands for fully qualified class name. If the class name ends with `Factory` that suffix could optionally be left out. | - | no `aether.priority.implicit` | boolean | Flag indicating whether the priorities of pluggable extensions are implicitly given by their iteration order such that the first extension has the highest priority. If set, an extension's built-in priority as well as any corresponding `aether.priority.` configuration properties are ignored when searching for a suitable implementation among the available extensions. This priority mode is meant for cases where the application will present/inject extensions in the desired search order. | `false` | no `aether.remoteRepositoryFilter.groupId` | boolean | Enable `groupId` remote repository filter. | `false` | no