Olawoyin007 commented on code in PR #1033:
URL: https://github.com/apache/flink-agents/pull/1033#discussion_r3838266273


##########
runtime/src/main/java/org/apache/flink/agents/runtime/skill/repository/LocalUrls.java:
##########
@@ -0,0 +1,70 @@
+/*
+ * 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.flink.agents.runtime.skill.repository;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+
+/**
+ * Centralizes the conversion of local {@code file:} URLs to {@link File} for 
the classpath skill
+ * loading path (direct resource materialization, JAR extraction, and the 
{@code URLClassLoader}
+ * fallback scan). Keeping this in one place ensures the three call sites 
agree on how relative
+ * {@code file:} URLs are resolved.
+ */
+final class LocalUrls {
+
+    private LocalUrls() {}
+
+    /**
+     * Resolve a local {@code file:} URL to a {@link File}.
+     *
+     * <p>A Flink deployment may add user-code JARs relative to the 
TaskManager working directory,
+     * producing relative {@code file:} URLs such as {@code 
file:../../flink/usrlib/job.jar}. Such a
+     * URL parses to an <em>opaque</em> URI (its scheme-specific part is not 
an absolute path),
+     * which {@code new File(URI)} rejects with {@code "URI is not 
hierarchical"}. This method
+     * resolves the opaque URI's decoded scheme-specific part against the 
process working directory
+     * instead.
+     *
+     * <p>Absolute hierarchical {@code file:} URLs keep their existing {@code 
new File(uri)}
+     * behavior. Non-{@code file} URLs are rejected explicitly.
+     *
+     * @throws IOException if the URL is not a {@code file:} URL, or is a 
malformed URI.
+     */
+    static File toLocalFile(URL url) throws IOException {
+        if (!"file".equals(url.getProtocol())) {
+            throw new IOException("Not a local file URL: " + url);
+        }
+        final URI uri;
+        try {
+            uri = url.toURI();
+        } catch (URISyntaxException e) {
+            throw new IOException("Malformed file URL: " + url, e);
+        }
+        if (uri.isOpaque()) {
+            // Relative file: URL (e.g. file:../../flink/usrlib/job.jar). new 
File(URI) rejects
+            // opaque URIs, so resolve the decoded scheme-specific part 
relative to the working
+            // directory, which is exactly how a relative File is interpreted.
+            return new File(uri.getSchemeSpecificPart());
+        }
+        return new File(uri);

Review Comment:
   Good catch, fixed in 05b4a463. `new File(uri)` is now inside the `try`, and 
the `catch` handles both `URISyntaxException` and `IllegalArgumentException`, 
wrapping them as `IOException`. So a hierarchical-but-non-representable URL 
like `file://host/share/job.jar` now surfaces as `IOException` and is skipped 
by the fallback scan, restoring the previous `copyJarEntries` contract instead 
of escaping unchecked. Added a `LocalUrlsTest` case asserting the 
authority-component URL comes back as `IOException`.



##########
runtime/src/test/java/org/apache/flink/agents/runtime/skill/ClasspathSkillRepositoryTest.java:
##########
@@ -89,6 +90,29 @@ void loadFromJarResource(@TempDir Path tempDir) throws 
IOException {
                         .collect(Collectors.toList()));
     }
 
+    @Test
+    void loadFromRelativeJarUrl(@TempDir Path tempDir) throws IOException {

Review Comment:
   Agreed, added in 05b4a463. Two new regression tests feed the URLs through 
`getResources()` directly (via a non-`URLClassLoader` loader, so the fallback 
scan is bypassed and each path is exercised in isolation): 
`loadFromDirectRelativeJarUrl` returns a relative `jar:file:` URL to drive 
`SkillMaterializer.copyJarEntries`, and `loadFromDirectRelativeDirectoryUrl` 
returns a relative `file:` directory URL to drive 
`ClasspathSkillRepository.materializeFileUrl`. Together with the existing 
fallback-scan test, all three affected call paths are now covered.



-- 
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