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

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 9aec06e92e0d CAMEL-26912: camel-jbang - exports in one directory run 
one at a time, so they do not mix up their projects (#29812)
9aec06e92e0d is described below

commit 9aec06e92e0dffb332fdb9e912cb44ef8fd5cd85
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Nov 13 16:40:30 2026 +0100

    CAMEL-26912: camel-jbang - exports in one directory run one at a time, so 
they do not mix up their projects (#29812)
    
    Every camel export in a directory shares the .camel-jbang work directory,
    which an export deletes when it starts: exports at the same time mixed up
    their projects and still reported success.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    Signed-off-by: Claus Ibsen <[email protected]>
---
 .../camel/dsl/jbang/run/export/ExportRequest.java  |   4 +-
 .../dsl/jbang/run/export/ProjectExporter.java      |   6 +-
 .../camel/dsl/jbang/run/export/WorkDirLock.java    | 109 +++++++++++++++++++++
 .../dsl/jbang/run/export/ProjectExporterTest.java  |  46 ++++++++-
 4 files changed, 160 insertions(+), 5 deletions(-)

diff --git 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/ExportRequest.java
 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/ExportRequest.java
index 658d6d6d02e2..beec2e024182 100644
--- 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/ExportRequest.java
+++ 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/ExportRequest.java
@@ -206,8 +206,8 @@ public class ExportRequest {
     /**
      * The working directory of the export: the run settings of the dependency 
resolution and the project being built
      * are kept there, and the directory is deleted and created again on each 
export. The default is the .camel-jbang
-     * directory in the current directory, as with camel export; use a 
directory of its own for each export that runs at
-     * the same time.
+     * directory in the current directory, as with camel export. Exports that 
use the same work directory run one at a
+     * time; use a directory of its own for each export that runs at the same 
time.
      */
     public Path workDir = CamelHome.getWorkDir();
 
diff --git 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/ProjectExporter.java
 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/ProjectExporter.java
index 8c35b102f7b1..c3b65f1127e8 100644
--- 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/ProjectExporter.java
+++ 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/ProjectExporter.java
@@ -184,7 +184,11 @@ public class ProjectExporter {
             printer().printErr("Unknown runtime: " + request.runtime);
             return 1;
         }
-        return exporter.export();
+        // one export at a time in a work directory: the .camel-jbang 
directory is shared by the exports of the current
+        // directory, and an export without files reuses the run data that the 
last run or export left there
+        try (WorkDirLock lock = WorkDirLock.acquire(copy.workDir, printer())) {
+            return exporter.export();
+        }
     }
 
     /**
diff --git 
a/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/WorkDirLock.java
 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/WorkDirLock.java
new file mode 100644
index 000000000000..df4655bc4101
--- /dev/null
+++ 
b/dsl/camel-jbang/camel-jbang-run/src/main/java/org/apache/camel/dsl/jbang/run/export/WorkDirLock.java
@@ -0,0 +1,109 @@
+/*
+ * 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.run.export;
+
+import java.io.IOException;
+import java.nio.channels.FileChannel;
+import java.nio.channels.FileLock;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.StandardOpenOption;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.locks.ReentrantLock;
+
+import org.apache.camel.dsl.jbang.run.common.CamelHome;
+import org.apache.camel.dsl.jbang.run.common.Printer;
+
+/**
+ * Lets one export at a time use a work directory, such as the .camel-jbang 
directory of the current directory, which
+ * every export there shares: an export deletes it when it starts, and builds 
the project and keeps the run settings in
+ * it, so exports at the same time would mix up their projects (CAMEL-26912).
+ * <p/>
+ * The exports of other processes are kept out by a file lock in the Camel 
directory of the user (not in the work
+ * directory, which an export deletes), and the exports of this process, which 
a file lock does not tell apart, by a
+ * lock of its own.
+ */
+final class WorkDirLock implements AutoCloseable {
+
+    private static final Map<Path, ReentrantLock> LOCKS = new 
ConcurrentHashMap<>();
+
+    private final ReentrantLock lock;
+    private final FileChannel channel;
+    private final FileLock fileLock;
+
+    private WorkDirLock(ReentrantLock lock, FileChannel channel, FileLock 
fileLock) {
+        this.lock = lock;
+        this.channel = channel;
+        this.fileLock = fileLock;
+    }
+
+    /**
+     * Waits until no other export uses the work directory, and locks it.
+     *
+     * @param workDir the work directory
+     * @param printer where to say that the export waits for another one
+     */
+    static WorkDirLock acquire(Path workDir, Printer printer) throws 
IOException {
+        Path dir = workDir.toAbsolutePath().normalize();
+        ReentrantLock lock = LOCKS.computeIfAbsent(dir, d -> new 
ReentrantLock());
+        boolean waiting = false;
+        if (!lock.tryLock()) {
+            waiting = waiting(printer, dir);
+            lock.lock();
+        }
+        FileChannel channel = null;
+        try {
+            Path camelDir = CamelHome.getCamelDir();
+            Files.createDirectories(camelDir);
+            // the lock file is kept: deleting it would let an export lock a 
new file while another holds the old one
+            String name = "export-" + 
UUID.nameUUIDFromBytes(dir.toString().getBytes(StandardCharsets.UTF_8)) + 
".lock";
+            channel = FileChannel.open(camelDir.resolve(name), 
StandardOpenOption.CREATE, StandardOpenOption.WRITE);
+            FileLock fileLock = channel.tryLock();
+            if (fileLock == null) {
+                if (!waiting) {
+                    waiting(printer, dir);
+                }
+                fileLock = channel.lock();
+            }
+            return new WorkDirLock(lock, channel, fileLock);
+        } catch (IOException | RuntimeException e) {
+            if (channel != null) {
+                channel.close();
+            }
+            lock.unlock();
+            throw e;
+        }
+    }
+
+    private static boolean waiting(Printer printer, Path dir) {
+        printer.println("Waiting for another export that uses " + dir + " to 
finish");
+        return true;
+    }
+
+    @Override
+    public void close() throws IOException {
+        try {
+            fileLock.release();
+            channel.close();
+        } finally {
+            lock.unlock();
+        }
+    }
+}
diff --git 
a/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/export/ProjectExporterTest.java
 
b/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/export/ProjectExporterTest.java
index 36d08d729f9d..c7089deea347 100644
--- 
a/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/export/ProjectExporterTest.java
+++ 
b/dsl/camel-jbang/camel-jbang-run/src/test/java/org/apache/camel/dsl/jbang/run/export/ProjectExporterTest.java
@@ -30,9 +30,12 @@ import java.util.stream.Stream;
 import org.apache.camel.dsl.jbang.run.common.CamelHome;
 import org.apache.camel.dsl.jbang.run.common.ExportHelper;
 import org.apache.camel.dsl.jbang.run.common.MavenResolver;
+import org.apache.camel.dsl.jbang.run.common.PathUtils;
 import org.apache.camel.dsl.jbang.run.common.QuarkusPlatform;
 import org.apache.camel.dsl.jbang.run.common.RuntimeType;
 import org.apache.camel.dsl.jbang.run.common.StringPrinter;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.io.TempDir;
 import org.junitpioneer.jupiter.SetSystemProperty;
@@ -45,6 +48,11 @@ import static org.assertj.core.api.Assertions.assertThat;
  */
 class ProjectExporterTest {
 
+    // the home of the CLI, where an export locks its work directory
+    @TempDir
+    Path home;
+    private Path previousHome;
+
     @TempDir
     Path dir;
 
@@ -62,6 +70,17 @@ class ProjectExporterTest {
         return 0;
     };
 
+    @BeforeEach
+    void useHome() {
+        previousHome = CamelHome.getHomeDir();
+        CamelHome.useHomeDir(home.toString());
+    }
+
+    @AfterEach
+    void restoreHome() {
+        CamelHome.useHomeDir(previousHome.toString());
+    }
+
     @Test
     void exportsCamelMainProject() throws Exception {
         Path route = writeRoute("hello");
@@ -158,11 +177,36 @@ class ProjectExporterTest {
     void exportsInParallelWithWorkDirsOfTheirOwn() throws Exception {
         List<String> before = listWorkDir();
 
+        exportInParallel(false);
+
+        // nothing is written to the .camel-jbang directory of the current 
directory
+        assertThat(listWorkDir()).isEqualTo(before);
+    }
+
+    @Test
+    void exportsInParallelWithTheDefaultWorkDir() throws Exception {
+        List<String> before = listWorkDir();
+
+        try {
+            // as camel export does: the .camel-jbang directory, which every 
export in the directory shares, so they
+            // run one at a time
+            exportInParallel(true);
+        } finally {
+            if (before.isEmpty()) {
+                PathUtils.deleteDirectory(CamelHome.getWorkDir());
+            }
+        }
+    }
+
+    private void exportInParallel(boolean defaultWorkDir) throws Exception {
         int count = 4;
         List<Callable<Integer>> tasks = new ArrayList<>();
         for (int i = 0; i < count; i++) {
             Path route = writeRoute("hello" + i);
             ExportRequest request = request(route, dir.resolve("export" + i));
+            if (defaultWorkDir) {
+                request.workDir = new ExportRequest().workDir;
+            }
             tasks.add(() -> new ProjectExporter(
                     new ExportContext().withPrinter(new 
StringPrinter()).withDependencyResolver(resolver))
                     .export(request));
@@ -184,8 +228,6 @@ class ProjectExporterTest {
             
assertThat(exportDir.resolve("src/main/resources/camel")).isDirectoryContaining("glob:**/"
 + route)
                     .isDirectoryNotContaining(p -> 
!p.getFileName().toString().equals(route));
         }
-        // nothing is written to the .camel-jbang directory of the current 
directory
-        assertThat(listWorkDir()).isEqualTo(before);
     }
 
     @Test

Reply via email to