gnodet-bot commented on code in PR #30564:
URL: https://github.com/apache/camel/pull/30564#discussion_r4654472578


##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/outdated/DependencyCheck.java:
##########
@@ -0,0 +1,229 @@
+/*
+ * 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.camel.dsl.jbang.core.common.outdated;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+
+import org.apache.camel.tooling.maven.MavenArtifact;
+import org.apache.camel.tooling.maven.MavenDownloader;
+import org.apache.camel.tooling.maven.MavenGav;
+
+/**
+ * The check of the dependencies of an integration (CAMEL-27351): how the 
version of each relates to the one the Camel
+ * dependencies bring, which ones are loaded in another version or twice, and 
which newer versions there are. Shared by
+ * {@code camel dependency outdated} and the Maven Dependencies screen of the 
TUI.
+ */
+public final class DependencyCheck {
+
+    private DependencyCheck() {
+    }
+
+    /**
+     * Sets the status of each row.
+     *
+     * @param rows         the dependencies, declared and transitive
+     * @param camelVersion the Camel version of the integration, null when not 
known
+     * @param camelBrings  {@code groupId:artifactId} to the version the Camel 
dependencies bring, empty when not
+     *                     resolved
+     * @param loaded       {@code groupId:artifactId} to the versions on the 
classpath of the running integration, null
+     *                     when not known
+     */
+    public static void classify(
+            List<DependencyRow> rows, String camelVersion, Map<String, String> 
camelBrings,
+            Map<String, ? extends Collection<String>> loaded) {
+        for (DependencyRow row : rows) {
+            Collection<String> versions = loaded != null ? 
loaded.get(row.ga()) : null;
+            row.loadedVersions().clear();
+            if (versions != null) {
+                row.loadedVersions().addAll(new TreeSet<>(versions));
+            }
+            if (row.camel()) {
+                row.setCamelVersion(camelVersion);
+            } else {
+                row.setCamelVersion(camelBrings != null ? 
camelBrings.get(row.ga()) : null);
+            }
+            row.setStatus(statusOf(row, camelVersion));
+        }
+    }
+
+    private static DependencyRow.Status statusOf(DependencyRow row, String 
camelVersion) {
+        List<String> loaded = row.loadedVersions();
+        if (loaded.size() > 1 || (row.version() != null && !loaded.isEmpty() 
&& !loaded.contains(row.version()))) {
+            return DependencyRow.Status.CONFLICT;
+        }
+        if (row.camel()) {
+            if (DependencyRow.sameVersionAsCamel(row.groupId()) && 
camelVersion != null && row.version() != null
+                    && !camelVersion.equals(row.version())) {
+                return DependencyRow.Status.MIXED;
+            }
+            return DependencyRow.Status.CAMEL;
+        }
+        String brings = row.camelVersion();
+        if (brings == null || row.version() == null) {
+            return DependencyRow.Status.NONE;
+        }
+        if (brings.equals(row.version())) {
+            return DependencyRow.Status.MANAGED;
+        }
+        // a transitive one in another version is not the project's choice: 
the classpath tells which one wins
+        return row.transitive() ? DependencyRow.Status.NONE : 
DependencyRow.Status.OVERRIDE;
+    }
+
+    /** Sets the newer versions of each row that is not a Camel JAR, from the 
available versions per GA. */
+    public static void applyAvailable(List<DependencyRow> rows, Map<String, 
List<String>> available) {
+        for (DependencyRow row : rows) {
+            if (row.camel() || row.version() == null) {
+                continue;
+            }
+            List<String> versions = available.get(row.ga());
+            if (versions != null) {
+                row.setNewer(Versions.newerPatch(row.version(), versions), 
Versions.latest(row.version(), versions));
+            }
+        }
+    }
+
+    /**
+     * The versions of the libraries the Camel dependencies bring: the Camel 
GAVs resolved with their transitive
+     * dependencies, without the other dependencies of the project, so a 
version the project declares does not hide the
+     * one of Camel.
+     *
+     * @return {@code groupId:artifactId} to version, of the JARs that are not 
Camel JARs
+     */
+    public static Map<String, String> camelBrings(MavenDownloader downloader, 
Collection<String> camelGavs)
+            throws Exception {
+        Map<String, String> result = new HashMap<>();
+        if (camelGavs.isEmpty()) {
+            return result;
+        }
+        boolean snapshot = camelGavs.stream().anyMatch(g -> 
g.contains("SNAPSHOT"));
+        for (MavenArtifact ma : downloader.resolveArtifacts(new 
ArrayList<>(camelGavs), Set.of(), true, snapshot)) {
+            MavenGav g = ma.getGav();
+            if (g.getVersion() != null && !Versions.isCamel(g.getGroupId())) {
+                result.putIfAbsent(g.getGroupId() + ":" + g.getArtifactId(), 
g.getVersion());
+            }
+        }
+        return result;
+    }
+
+    /**
+     * The available versions of each {@code groupId:artifactId}, from the 
remote repositories of the Maven settings
+     * (Maven Central, mirrors, profiles), a few at a time.
+     *
+     * @param  error set to the message of a lookup that failed (the others 
are still returned)
+     * @return       {@code groupId:artifactId} to its versions; one that 
failed is left out
+     */
+    public static Map<String, List<String>> availableVersions(
+            MavenDownloader downloader, Collection<String> gas, 
AtomicReference<String> error) {
+        Map<String, List<String>> result = new ConcurrentHashMap<>();
+        ExecutorService pool = Executors.newFixedThreadPool(Math.max(1, 
Math.min(8, gas.size())), r -> {
+            Thread t = new Thread(r, "CamelDependencyVersions");
+            t.setDaemon(true);
+            return t;
+        });
+        try {
+            List<Future<?>> futures = new ArrayList<>();
+            for (String ga : new TreeSet<>(gas)) {
+                int colon = ga.indexOf(':');
+                if (colon <= 0) {
+                    continue;
+                }
+                futures.add(pool.submit(() -> {
+                    try {
+                        List<String> versions = new ArrayList<>();
+                        for (MavenGav g : downloader.resolveAvailableVersions(
+                                ga.substring(0, colon), ga.substring(colon + 
1), null, false)) {
+                            versions.add(g.getVersion());
+                        }
+                        result.put(ga, versions);
+                    } catch (Exception e) {
+                        if (error != null) {
+                            error.compareAndSet(null, e.getMessage());
+                        }
+                    }
+                }));
+            }
+            for (Future<?> f : futures) {
+                try {
+                    f.get(60, TimeUnit.SECONDS);
+                } catch (Exception e) {
+                    if (error != null) {
+                        error.compareAndSet(null, "timeout looking up the 
available versions");
+                    }
+                }
+            }
+        } finally {
+            pool.shutdownNow();

Review Comment:
   ⚠️ **`shutdownNow()` does not wait for running threads to terminate** — 
still not addressed.
   
   After `shutdownNow()`, threads executing 
`downloader.resolveAvailableVersions()` may still be in progress when the 
method returns. For the CLI this is benign (JVM exits), but the TUI calls this 
method from a live session and may invoke it repeatedly — leaving ghost threads 
competing for I/O and downloader resources. Add `awaitTermination` before 
returning:
   
   ```suggestion
               pool.shutdownNow();
               pool.awaitTermination(5, TimeUnit.SECONDS);
   ```



##########
dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/common/outdated/DependencyRow.java:
##########
@@ -0,0 +1,217 @@
+/*
+ * 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.camel.dsl.jbang.core.common.outdated;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+
+import org.apache.camel.util.json.JsonArray;
+import org.apache.camel.util.json.JsonObject;
+
+/**
+ * One dependency of an integration, with how its version relates to the one 
Camel brings, the versions loaded, and the
+ * newer versions available.
+ */
+public final class DependencyRow {
+
+    /** How the version of a dependency relates to Camel. */
+    public enum Status {
+        /** Loaded in more than one version, or in another version than the 
one declared. */
+        CONFLICT,
+        /** A Camel JAR of another Camel version than the one of the 
integration. */
+        MIXED,
+        /** Declared by the project in another version than the one the Camel 
dependencies bring. */
+        OVERRIDE,
+        /** The version the Camel dependencies bring. */
+        MANAGED,
+        /** A Camel JAR of the Camel version of the integration. */
+        CAMEL,
+        /** Camel does not bring this library. */
+        NONE;
+
+        /** The text of the STATUS column, empty for NONE. */
+        public String label() {
+            return this == NONE ? "" : name().toLowerCase(Locale.ROOT);
+        }
+
+        /** Whether the status is one to act on. */
+        public boolean attention() {
+            return this == CONFLICT || this == MIXED || this == OVERRIDE;
+        }
+    }
+
+    private final String groupId;
+    private final String artifactId;
+    private final String version;
+    private final boolean transitive;
+    private final String via;
+    private Status status = Status.NONE;
+    private String camelVersion;
+    private final List<String> loadedVersions = new ArrayList<>();
+    private String newer;
+    private String latest;
+
+    public DependencyRow(String groupId, String artifactId, String version, 
boolean transitive, String via) {
+        this.groupId = groupId;
+        this.artifactId = artifactId;
+        this.version = version;
+        this.transitive = transitive;
+        this.via = via;
+    }
+
+    public String groupId() {
+        return groupId;
+    }
+
+    public String artifactId() {
+        return artifactId;
+    }
+
+    public String version() {
+        return version;
+    }
+
+    /** {@code groupId:artifactId}. */
+    public String ga() {
+        return groupId + ":" + artifactId;
+    }
+
+    /** {@code groupId:artifactId:version}, or {@code groupId:artifactId} 
without a version. */
+    public String gav() {
+        return version != null ? ga() + ":" + version : ga();
+    }
+
+    public boolean transitive() {
+        return transitive;
+    }
+
+    /** The dependency of the project that brings a transitive one in, null 
for a declared one. */
+    public String via() {
+        return via;
+    }
+
+    public boolean camel() {
+        return Versions.isCamel(groupId);
+    }
+
+    public Status status() {
+        return status;
+    }
+
+    void setStatus(Status status) {
+        this.status = status;
+    }
+
+    /** The version the Camel dependencies bring of this library; null when 
they do not bring it. */
+    public String camelVersion() {
+        return camelVersion;
+    }
+
+    void setCamelVersion(String camelVersion) {
+        this.camelVersion = camelVersion;
+    }
+
+    /** The versions of this artifact on the classpath of the running 
integration; empty when not known. */
+    public List<String> loadedVersions() {
+        return loadedVersions;

Review Comment:
   💡 **`loadedVersions()` returns the internal mutable `ArrayList` directly** — 
still not addressed.
   
   `DependencyCheck.classify()` mutates it via `row.loadedVersions().clear()` + 
`addAll()`, which is intentional internally, but the accessor is `public` on a 
`public final` class. Any external caller (TUI code, tests, future 
integrations) can accidentally corrupt the row's state. Return an unmodifiable 
view from the public accessor:
   
   ```suggestion
       public List<String> loadedVersions() {
           return Collections.unmodifiableList(loadedVersions);
       }
   ```
   
   (Add `import java.util.Collections;` and change `classify()` to call a 
package-private `mutableLoadedVersions()` that returns the raw list.)



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