This is an automated email from the ASF dual-hosted git repository.

cstamas pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-resolver.git


The following commit(s) were added to refs/heads/master by this push:
     new 77e9b91f [MRESOLVER-645] Remove repeated LRM interaction (#611)
77e9b91f is described below

commit 77e9b91f2cead907615fcdfba208a3213c316901
Author: Tamas Cservenak <ta...@cservenak.net>
AuthorDate: Thu Dec 12 18:29:59 2024 +0100

    [MRESOLVER-645] Remove repeated LRM interaction (#611)
    
    Pull in repeated snippets under LRM.
    
    ---
    
    https://issues.apache.org/jira/browse/MRESOLVER-645
---
 .../aether/repository/LocalRepositoryManager.java  |  62 +++++++++++++
 .../internal/impl/DefaultArtifactResolver.java     |   5 +-
 .../aether/internal/impl/DefaultDeployer.java      |   4 +-
 .../aether/internal/impl/DefaultInstaller.java     |   4 +-
 .../internal/impl/DefaultMetadataResolver.java     |  14 +--
 .../impl/EnhancedLocalRepositoryManager.java       |  14 +--
 .../impl/SimpleLocalRepositoryManager.java         |   7 +-
 .../repository/ChainedLocalRepositoryManager.java  | 102 ++++++++++++++++++---
 8 files changed, 167 insertions(+), 45 deletions(-)

diff --git 
a/maven-resolver-api/src/main/java/org/eclipse/aether/repository/LocalRepositoryManager.java
 
b/maven-resolver-api/src/main/java/org/eclipse/aether/repository/LocalRepositoryManager.java
index 14651cb0..6d23deb7 100644
--- 
a/maven-resolver-api/src/main/java/org/eclipse/aether/repository/LocalRepositoryManager.java
+++ 
b/maven-resolver-api/src/main/java/org/eclipse/aether/repository/LocalRepositoryManager.java
@@ -18,6 +18,8 @@
  */
 package org.eclipse.aether.repository;
 
+import java.nio.file.Path;
+
 import org.eclipse.aether.RepositorySystemSession;
 import org.eclipse.aether.artifact.Artifact;
 import org.eclipse.aether.metadata.Metadata;
@@ -37,6 +39,18 @@ public interface LocalRepositoryManager {
      */
     LocalRepository getRepository();
 
+    /**
+     * Gets the absolute path for a locally installed artifact. Note that the 
artifact need not actually exist yet at
+     * the returned location, the path merely indicates where the artifact 
would eventually be stored.
+     *
+     * @param artifact The artifact for which to determine the path, must not 
be {@code null}.
+     * @return The path, relative to the local repository's base directory.
+     * @since 2.0.5
+     */
+    default Path getAbsolutePathForLocalArtifact(Artifact artifact) {
+        return 
getRepository().getBasePath().resolve(getPathForLocalArtifact(artifact));
+    }
+
     /**
      * Gets the relative path for a locally installed artifact. Note that the 
artifact need not actually exist yet at
      * the returned location, the path merely indicates where the artifact 
would eventually be stored. The path uses the
@@ -44,9 +58,25 @@ public interface LocalRepositoryManager {
      *
      * @param artifact The artifact for which to determine the path, must not 
be {@code null}.
      * @return The path, relative to the local repository's base directory.
+     * @deprecated See {@link #getAbsolutePathForLocalArtifact(Artifact)}
      */
+    @Deprecated
     String getPathForLocalArtifact(Artifact artifact);
 
+    /**
+     * Gets the absolute path for an artifact cached from a remote repository. 
Note that the artifact need not actually
+     * exist yet at the returned location, the path merely indicates where the 
artifact would eventually be stored.
+     *
+     * @param artifact The artifact for which to determine the path, must not 
be {@code null}.
+     * @param repository The source repository of the artifact, must not be 
{@code null}.
+     * @param context The resolution context in which the artifact is being 
requested, may be {@code null}.
+     * @return The path, relative to the local repository's base directory.
+     * @since 2.0.5
+     */
+    default Path getAbsolutePathForRemoteArtifact(Artifact artifact, 
RemoteRepository repository, String context) {
+        return 
getRepository().getBasePath().resolve(getPathForRemoteArtifact(artifact, 
repository, context));
+    }
+
     /**
      * Gets the relative path for an artifact cached from a remote repository. 
Note that the artifact need not actually
      * exist yet at the returned location, the path merely indicates where the 
artifact would eventually be stored. The
@@ -56,9 +86,23 @@ public interface LocalRepositoryManager {
      * @param repository The source repository of the artifact, must not be 
{@code null}.
      * @param context The resolution context in which the artifact is being 
requested, may be {@code null}.
      * @return The path, relative to the local repository's base directory.
+     * @deprecated See {@link #getAbsolutePathForRemoteArtifact(Artifact, 
RemoteRepository, String)}
      */
+    @Deprecated
     String getPathForRemoteArtifact(Artifact artifact, RemoteRepository 
repository, String context);
 
+    /**
+     * Gets the absolute path for locally installed metadata. Note that the 
metadata need not actually exist yet at the
+     * returned location, the path merely indicates where the metadata would 
eventually be stored.
+     *
+     * @param metadata The metadata for which to determine the path, must not 
be {@code null}.
+     * @return The path, relative to the local repository's base directory.
+     * @since 2.0.5
+     */
+    default Path getAbsolutePathForLocalMetadata(Metadata metadata) {
+        return 
getRepository().getBasePath().resolve(getPathForLocalMetadata(metadata));
+    }
+
     /**
      * Gets the relative path for locally installed metadata. Note that the 
metadata need not actually exist yet at the
      * returned location, the path merely indicates where the metadata would 
eventually be stored. The path uses the
@@ -66,9 +110,25 @@ public interface LocalRepositoryManager {
      *
      * @param metadata The metadata for which to determine the path, must not 
be {@code null}.
      * @return The path, relative to the local repository's base directory.
+     * @deprecated See {@link #getAbsolutePathForLocalMetadata(Metadata)}
      */
+    @Deprecated
     String getPathForLocalMetadata(Metadata metadata);
 
+    /**
+     * Gets the absolute path for metadata cached from a remote repository. 
Note that the metadata need not actually
+     * exist yet at the returned location, the path merely indicates where the 
metadata would eventually be stored.
+     *
+     * @param metadata The metadata for which to determine the path, must not 
be {@code null}.
+     * @param repository The source repository of the metadata, must not be 
{@code null}.
+     * @param context The resolution context in which the metadata is being 
requested, may be {@code null}.
+     * @return The path, relative to the local repository's base directory.
+     * @since 2.0.5
+     */
+    default Path getAbsolutePathForRemoteMetadata(Metadata metadata, 
RemoteRepository repository, String context) {
+        return 
getRepository().getBasePath().resolve(getPathForRemoteMetadata(metadata, 
repository, context));
+    }
+
     /**
      * Gets the relative path for metadata cached from a remote repository. 
Note that the metadata need not actually
      * exist yet at the returned location, the path merely indicates where the 
metadata would eventually be stored. The
@@ -78,7 +138,9 @@ public interface LocalRepositoryManager {
      * @param repository The source repository of the metadata, must not be 
{@code null}.
      * @param context The resolution context in which the metadata is being 
requested, may be {@code null}.
      * @return The path, relative to the local repository's base directory.
+     * @deprecated See {@link #getAbsolutePathForRemoteMetadata(Metadata, 
RemoteRepository, String)}
      */
+    @Deprecated
     String getPathForRemoteMetadata(Metadata metadata, RemoteRepository 
repository, String context);
 
     /**
diff --git 
a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultArtifactResolver.java
 
b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultArtifactResolver.java
index f4312691..6c614d92 100644
--- 
a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultArtifactResolver.java
+++ 
b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultArtifactResolver.java
@@ -519,9 +519,8 @@ public class DefaultArtifactResolver implements 
ArtifactResolver {
                 download.setPath(item.local.getPath());
                 download.setExistenceCheck(true);
             } else {
-                String path =
-                        lrm.getPathForRemoteArtifact(artifact, 
group.repository, item.request.getRequestContext());
-                
download.setPath(lrm.getRepository().getBasePath().resolve(path));
+                download.setPath(lrm.getAbsolutePathForRemoteArtifact(
+                        artifact, group.repository, 
item.request.getRequestContext()));
             }
 
             boolean snapshot = artifact.isSnapshot();
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 873d121d..361e809c 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
@@ -285,9 +285,7 @@ public class DefaultDeployer implements Deployer {
             EventCatapult catapult)
             throws DeploymentException {
         LocalRepositoryManager lrm = session.getLocalRepositoryManager();
-        Path basePath = lrm.getRepository().getBasePath();
-
-        Path dstPath = basePath.resolve(lrm.getPathForRemoteMetadata(metadata, 
repository, ""));
+        Path dstPath = lrm.getAbsolutePathForRemoteMetadata(metadata, 
repository, "");
 
         if (metadata instanceof MergeableMetadata) {
             if (!((MergeableMetadata) metadata).isMerged()) {
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 fba22928..5533c480 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
@@ -195,7 +195,7 @@ public class DefaultInstaller implements Installer {
             throws InstallationException {
         final LocalRepositoryManager lrm = session.getLocalRepositoryManager();
         final Path srcPath = artifact.getPath();
-        final Path dstPath = 
lrm.getRepository().getBasePath().resolve(lrm.getPathForLocalArtifact(artifact));
+        final Path dstPath = lrm.getAbsolutePathForLocalArtifact(artifact);
 
         artifactInstalling(session, trace, artifact, dstPath);
 
@@ -219,7 +219,7 @@ public class DefaultInstaller implements Installer {
             throws InstallationException {
         LocalRepositoryManager lrm = session.getLocalRepositoryManager();
 
-        Path dstPath = 
lrm.getRepository().getBasePath().resolve(lrm.getPathForLocalMetadata(metadata));
+        Path dstPath = lrm.getAbsolutePathForLocalMetadata(metadata);
 
         metadataInstalling(session, trace, metadata, dstPath);
 
diff --git 
a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultMetadataResolver.java
 
b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultMetadataResolver.java
index 28a43ae1..305655b2 100644
--- 
a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultMetadataResolver.java
+++ 
b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/DefaultMetadataResolver.java
@@ -257,10 +257,8 @@ public class DefaultMetadataResolver implements 
MetadataResolver {
                         check.setItem(metadata);
 
                         // use 'main' installation file for the check (-> use 
requested repository)
-                        Path checkPath = session.getLocalRepository()
-                                .getBasePath()
-                                .resolve(session.getLocalRepositoryManager()
-                                        .getPathForRemoteMetadata(metadata, 
repository, request.getRequestContext()));
+                        Path checkPath = session.getLocalRepositoryManager()
+                                .getAbsolutePathForRemoteMetadata(metadata, 
repository, request.getRequestContext());
                         check.setPath(checkPath);
                         check.setRepository(repository);
                         check.setAuthoritativeRepository(repo);
@@ -283,11 +281,9 @@ public class DefaultMetadataResolver implements 
MetadataResolver {
                         RepositoryPolicy policy = getPolicy(session, 
repository, metadata.getNature());
 
                         // install path may be different from lookup path
-                        Path installPath = session.getLocalRepository()
-                                .getBasePath()
-                                .resolve(session.getLocalRepositoryManager()
-                                        .getPathForRemoteMetadata(
-                                                metadata, 
request.getRepository(), request.getRequestContext()));
+                        Path installPath = session.getLocalRepositoryManager()
+                                .getAbsolutePathForRemoteMetadata(
+                                        metadata, request.getRepository(), 
request.getRequestContext());
 
                         ResolveTask task = new ResolveTask(
                                 session, trace, result, installPath, checks, 
policy.getChecksumPolicy());
diff --git 
a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/EnhancedLocalRepositoryManager.java
 
b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/EnhancedLocalRepositoryManager.java
index a414c858..4fd5ec29 100644
--- 
a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/EnhancedLocalRepositoryManager.java
+++ 
b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/EnhancedLocalRepositoryManager.java
@@ -119,20 +119,17 @@ class EnhancedLocalRepositoryManager extends 
SimpleLocalRepositoryManager {
         Artifact artifact = request.getArtifact();
         LocalArtifactResult result = new LocalArtifactResult(request);
 
-        String path;
         Path filePath;
 
         // Local repository CANNOT have timestamped installed, they are 
created only during deploy
         if (Objects.equals(artifact.getVersion(), artifact.getBaseVersion())) {
-            path = getPathForLocalArtifact(artifact);
-            filePath = getRepository().getBasePath().resolve(path);
+            filePath = getAbsolutePathForLocalArtifact(artifact);
             checkFind(filePath, result);
         }
 
         if (!result.isAvailable()) {
             for (RemoteRepository repository : request.getRepositories()) {
-                path = getPathForRemoteArtifact(artifact, repository, 
request.getContext());
-                filePath = getRepository().getBasePath().resolve(path);
+                filePath = getAbsolutePathForRemoteArtifact(artifact, 
repository, request.getContext());
 
                 checkFind(filePath, result);
 
@@ -208,10 +205,9 @@ class EnhancedLocalRepositoryManager extends 
SimpleLocalRepositoryManager {
     private void addArtifact(
             Artifact artifact, Collection<String> repositories, 
RemoteRepository repository, String context) {
         requireNonNull(artifact, "artifact cannot be null");
-        String path = repository == null
-                ? getPathForLocalArtifact(artifact)
-                : getPathForRemoteArtifact(artifact, repository, context);
-        Path file = getRepository().getBasePath().resolve(path);
+        Path file = repository == null
+                ? getAbsolutePathForLocalArtifact(artifact)
+                : getAbsolutePathForRemoteArtifact(artifact, repository, 
context);
         addRepo(file, repositories);
     }
 
diff --git 
a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/SimpleLocalRepositoryManager.java
 
b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/SimpleLocalRepositoryManager.java
index ee393140..6c5e2a88 100644
--- 
a/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/SimpleLocalRepositoryManager.java
+++ 
b/maven-resolver-impl/src/main/java/org/eclipse/aether/internal/impl/SimpleLocalRepositoryManager.java
@@ -132,13 +132,11 @@ class SimpleLocalRepositoryManager implements 
LocalRepositoryManager {
         Artifact artifact = request.getArtifact();
         LocalArtifactResult result = new LocalArtifactResult(request);
 
-        String path;
         Path filePath;
 
         // Local repository CANNOT have timestamped installed, they are 
created only during deploy
         if (Objects.equals(artifact.getVersion(), artifact.getBaseVersion())) {
-            path = getPathForLocalArtifact(artifact);
-            filePath = getRepository().getBasePath().resolve(path);
+            filePath = getAbsolutePathForLocalArtifact(artifact);
             if (Files.isRegularFile(filePath)) {
                 result.setPath(filePath);
                 result.setAvailable(true);
@@ -147,8 +145,7 @@ class SimpleLocalRepositoryManager implements 
LocalRepositoryManager {
 
         if (!result.isAvailable()) {
             for (RemoteRepository repository : request.getRepositories()) {
-                path = getPathForRemoteArtifact(artifact, repository, 
request.getContext());
-                filePath = getRepository().getBasePath().resolve(path);
+                filePath = getAbsolutePathForRemoteArtifact(artifact, 
repository, request.getContext());
                 if (Files.isRegularFile(filePath)) {
                     result.setPath(filePath);
                     result.setAvailable(true);
diff --git 
a/maven-resolver-util/src/main/java/org/eclipse/aether/util/repository/ChainedLocalRepositoryManager.java
 
b/maven-resolver-util/src/main/java/org/eclipse/aether/util/repository/ChainedLocalRepositoryManager.java
index 6b32ab2f..1223c457 100644
--- 
a/maven-resolver-util/src/main/java/org/eclipse/aether/util/repository/ChainedLocalRepositoryManager.java
+++ 
b/maven-resolver-util/src/main/java/org/eclipse/aether/util/repository/ChainedLocalRepositoryManager.java
@@ -70,19 +70,52 @@ public final class ChainedLocalRepositoryManager implements 
LocalRepositoryManag
 
     private final boolean ignoreTailAvailability;
 
+    private final int installTarget;
+
+    private final int cacheTarget;
+
     public ChainedLocalRepositoryManager(
             LocalRepositoryManager head, List<LocalRepositoryManager> tail, 
boolean ignoreTailAvailability) {
-        this.head = requireNonNull(head, "head cannot be null");
-        this.tail = requireNonNull(tail, "tail cannot be null");
-        this.ignoreTailAvailability = ignoreTailAvailability;
+        this(head, tail, ignoreTailAvailability, 0, 0);
     }
 
     public ChainedLocalRepositoryManager(
             LocalRepositoryManager head, List<LocalRepositoryManager> tail, 
RepositorySystemSession session) {
+        this(
+                head,
+                tail,
+                ConfigUtils.getBoolean(session, 
DEFAULT_IGNORE_TAIL_AVAILABILITY, CONFIG_PROP_IGNORE_TAIL_AVAILABILITY),
+                0,
+                0);
+    }
+
+    /**
+     * Warning: this is experimental feature of chained, is not recommended to 
be used/integrated into plain Maven.
+     *
+     * @param head The head LRM
+     * @param tail The tail LRMs
+     * @param ignoreTailAvailability Whether tail availability should be 
ignored (usually you do want this)
+     * @param installTarget The installation LRM index, integer from 0 to size 
of tail.
+     * @param cacheTarget The cache LRM index, integer from 0 to size of tail.
+     * @since 2.0.5
+     */
+    public ChainedLocalRepositoryManager(
+            LocalRepositoryManager head,
+            List<LocalRepositoryManager> tail,
+            boolean ignoreTailAvailability,
+            int installTarget,
+            int cacheTarget) {
         this.head = requireNonNull(head, "head cannot be null");
         this.tail = requireNonNull(tail, "tail cannot be null");
-        this.ignoreTailAvailability =
-                ConfigUtils.getBoolean(session, 
DEFAULT_IGNORE_TAIL_AVAILABILITY, CONFIG_PROP_IGNORE_TAIL_AVAILABILITY);
+        this.ignoreTailAvailability = ignoreTailAvailability;
+        if (installTarget < 0 || installTarget > tail.size()) {
+            throw new IllegalArgumentException("Illegal installTarget value");
+        }
+        this.installTarget = installTarget;
+        if (cacheTarget < 0 || cacheTarget > tail.size()) {
+            throw new IllegalArgumentException("Illegal cacheTarget value");
+        }
+        this.cacheTarget = cacheTarget;
     }
 
     @Override
@@ -90,24 +123,60 @@ public final class ChainedLocalRepositoryManager 
implements LocalRepositoryManag
         return head.getRepository();
     }
 
+    private LocalRepositoryManager getInstallTarget() {
+        if (installTarget == 0) {
+            return head;
+        } else {
+            return tail.get(installTarget - 1);
+        }
+    }
+
+    private LocalRepositoryManager getCacheTarget() {
+        if (cacheTarget == 0) {
+            return head;
+        } else {
+            return tail.get(cacheTarget - 1);
+        }
+    }
+
+    @Override
+    public Path getAbsolutePathForLocalArtifact(Artifact artifact) {
+        return getInstallTarget().getAbsolutePathForLocalArtifact(artifact);
+    }
+
+    @Override
+    public Path getAbsolutePathForRemoteArtifact(Artifact artifact, 
RemoteRepository repository, String context) {
+        return getCacheTarget().getAbsolutePathForRemoteArtifact(artifact, 
repository, context);
+    }
+
+    @Override
+    public Path getAbsolutePathForLocalMetadata(Metadata metadata) {
+        return getInstallTarget().getAbsolutePathForLocalMetadata(metadata);
+    }
+
+    @Override
+    public Path getAbsolutePathForRemoteMetadata(Metadata metadata, 
RemoteRepository repository, String context) {
+        return getCacheTarget().getAbsolutePathForRemoteMetadata(metadata, 
repository, context);
+    }
+
     @Override
     public String getPathForLocalArtifact(Artifact artifact) {
-        return head.getPathForLocalArtifact(artifact);
+        return getInstallTarget().getPathForLocalArtifact(artifact);
     }
 
     @Override
     public String getPathForRemoteArtifact(Artifact artifact, RemoteRepository 
repository, String context) {
-        return head.getPathForRemoteArtifact(artifact, repository, context);
+        return getCacheTarget().getPathForRemoteArtifact(artifact, repository, 
context);
     }
 
     @Override
     public String getPathForLocalMetadata(Metadata metadata) {
-        return head.getPathForLocalMetadata(metadata);
+        return getInstallTarget().getPathForLocalMetadata(metadata);
     }
 
     @Override
     public String getPathForRemoteMetadata(Metadata metadata, RemoteRepository 
repository, String context) {
-        return head.getPathForRemoteMetadata(metadata, repository, context);
+        return getCacheTarget().getPathForRemoteMetadata(metadata, repository, 
context);
     }
 
     @Override
@@ -134,15 +203,17 @@ public final class ChainedLocalRepositoryManager 
implements LocalRepositoryManag
     @Override
     public void add(RepositorySystemSession session, LocalArtifactRegistration 
request) {
         String artifactPath;
+        LocalRepositoryManager target;
         if (request.getRepository() != null) {
             artifactPath = getPathForRemoteArtifact(request.getArtifact(), 
request.getRepository(), "check");
+            target = getCacheTarget();
         } else {
             artifactPath = getPathForLocalArtifact(request.getArtifact());
+            target = getInstallTarget();
         }
-
-        Path file = head.getRepository().getBasePath().resolve(artifactPath);
+        Path file = target.getRepository().getBasePath().resolve(artifactPath);
         if (Files.isRegularFile(file)) {
-            head.add(session, request);
+            target.add(session, request);
         }
     }
 
@@ -165,15 +236,18 @@ public final class ChainedLocalRepositoryManager 
implements LocalRepositoryManag
     @Override
     public void add(RepositorySystemSession session, LocalMetadataRegistration 
request) {
         String metadataPath;
+        LocalRepositoryManager target;
         if (request.getRepository() != null) {
             metadataPath = getPathForRemoteMetadata(request.getMetadata(), 
request.getRepository(), "check");
+            target = getCacheTarget();
         } else {
             metadataPath = getPathForLocalMetadata(request.getMetadata());
+            target = getInstallTarget();
         }
 
-        Path file = head.getRepository().getBasePath().resolve(metadataPath);
+        Path file = target.getRepository().getBasePath().resolve(metadataPath);
         if (Files.isRegularFile(file)) {
-            head.add(session, request);
+            target.add(session, request);
         }
     }
 

Reply via email to