jbonofre commented on code in PR #2928:
URL: https://github.com/apache/karaf/pull/2928#discussion_r4191867490


##########
features/core/src/main/java/org/apache/karaf/features/internal/download/impl/LocalMavenResolver.java:
##########
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.karaf.features.internal.download.impl;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.apache.karaf.features.spi.MavenResolver;
+import org.apache.karaf.util.maven.Parser;
+
+/**
+ * Resolves Maven coordinates from the Karaf distribution's local system 
repository only.
+ */
+public class LocalMavenResolver implements MavenResolver {
+
+    private final Path systemRepository;
+
+    public LocalMavenResolver(Path systemRepository) {
+        this.systemRepository = systemRepository.toAbsolutePath().normalize();
+    }
+
+    /**
+     * Create a resolver for the repository configured by the running Karaf 
distribution.
+     *
+     * @return a resolver rooted at {@code 
karaf.home/karaf.default.repository}.
+     */
+    public static LocalMavenResolver forKarafSystem() {
+        Path home = Paths.get(System.getProperty("karaf.home", "karaf"));
+        Path repository = 
Paths.get(System.getProperty("karaf.default.repository", "system"));
+        if (!repository.isAbsolute()) {
+            repository = home.resolve(repository);
+        }
+        return new LocalMavenResolver(repository);
+    }
+
+    @Override
+    public File resolve(String url) throws IOException {
+        if (url == null || !url.startsWith("mvn:")) {
+            throw new MalformedURLException("Expected a mvn: URI: " + url);
+        }
+        String artifactPath = Parser.pathFromMaven(url);
+        Path artifact = systemRepository.resolve(artifactPath).normalize();
+        if (!artifact.startsWith(systemRepository)) {
+            throw new IOException("Maven artifact path is outside the Karaf 
system repository: " + url);
+        }
+        if (!artifact.toFile().isFile()) {
+            throw new FileNotFoundException("Maven artifact " + url + " was 
not found in the Karaf system repository "
+                    + systemRepository);
+        }
+        return artifact.toFile();
+    }
+
+    @Override
+    public File resolve(String url, Exception previousException) throws 
IOException {
+        return resolve(url);

Review Comment:
   `resolve(url, previousException)` is not a retry-only method. It is the 
entry point the download path uses for every attempt:
   - first attempt: `MavenDownloadTask.download()` calls `resolver.resolve(url, 
previousException)` with `previousException = null`. Throwing there would make 
the Pax URL free fallback resolve nothing at all.
   - retries: because `isRetryableException` returns `NEVER`, 
`AbstractRetryableDownloadTask` sets the retry count to 0 and never reachable. 
A guard on a non-null `previousException` would be dead code.
   - contract: the SPI Javadoc descriobes the exception as a hint the 
implementation may use, so ignoring it is legitimate.
   
   I understand your confusion though, because the SPI has two abstract 
`resolve` methods where on would do. `LocalMavenResolver` and the stub in 
`TestMavenResolverFactory` both carry the same `return resolve(url);` 
boilerplate.
   
   I would make the hint overload a default method in `MavenResolver` that 
delegates to `resolve(url)`, as `resolve(g, a, c, e, v)` and 
`isRetryableException` already are. The Pax URL, lazy and reactor resolvers 
keep their overrides, and the line you commented on disappears. The SPI is new 
in this PR, so this is the cheapest time to change it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to