You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2025/02/24 13:38:03 UTC

(camel) branch camel-4.10.x updated: CAMEL-21782: camel-file - The autoCreate option should be moved to doStart

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

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


The following commit(s) were added to refs/heads/camel-4.10.x by this push:
     new d0217f7f733 CAMEL-21782: camel-file - The autoCreate option should be moved to doStart
d0217f7f733 is described below

commit d0217f7f733112f2b6d73966d009accbc33941ff
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Feb 24 14:36:26 2025 +0100

    CAMEL-21782: camel-file - The autoCreate option should be moved to doStart
---
 .../apache/camel/component/file/FileConsumer.java  | 50 ++++++++++++++++++++++
 .../apache/camel/component/file/FileEndpoint.java  | 36 ----------------
 .../file/FileConsumerAutoCreateDirectoryTest.java  |  8 ++--
 .../file/FileConsumerCustomSchedulerTest.java      |  3 +-
 ...onsumerStartingDirectoryMustHaveAccessTest.java |  9 ++--
 5 files changed, 61 insertions(+), 45 deletions(-)

diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/FileConsumer.java b/components/camel-file/src/main/java/org/apache/camel/component/file/FileConsumer.java
index d2b6dfbd57b..07b5bab373c 100644
--- a/components/camel-file/src/main/java/org/apache/camel/component/file/FileConsumer.java
+++ b/components/camel-file/src/main/java/org/apache/camel/component/file/FileConsumer.java
@@ -17,6 +17,7 @@
 package org.apache.camel.component.file;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -403,9 +404,58 @@ public class FileConsumer extends GenericFileConsumer<File> implements ResumeAwa
             resumeStrategy.loadCache();
         }
 
+        // turn off scheduler first, so autoCreate is handled before scheduler
+        // starts
+        boolean startScheduler = isStartScheduler();
+        setStartScheduler(false);
+        try {
+            super.doStart();
+
+            // auto create starting directory if needed
+            File file = getEndpoint().getFile();
+            if (!file.exists() && !file.isDirectory()) {
+                tryCreateDirectory(file);
+            }
+            // ensure directory can be read
+            tryReadingStartDirectory(file);
+        } finally {
+            if (startScheduler) {
+                setStartScheduler(true);
+                startScheduler();
+            }
+        }
+
         super.doStart();
     }
 
+    private void tryCreateDirectory(File file) throws FileNotFoundException {
+        if (getEndpoint().isAutoCreate()) {
+            doCreateStartDirectory(file);
+        } else if (getEndpoint().isStartingDirectoryMustExist()) {
+            throw new FileNotFoundException("Starting directory does not exist: " + file);
+        }
+    }
+
+    private void doCreateStartDirectory(File file) {
+        LOG.debug("Creating non existing starting directory: {}", file);
+        boolean absolute = FileUtil.isAbsolute(file);
+        boolean created = operations.buildDirectory(file.getPath(), absolute);
+        if (!created) {
+            LOG.warn("Cannot auto create starting directory: {}", file);
+        }
+    }
+
+    private void tryReadingStartDirectory(File file) throws IOException {
+        if (!getEndpoint().isStartingDirectoryMustExist() && getEndpoint().isStartingDirectoryMustHaveAccess()) {
+            throw new IllegalArgumentException(
+                    "You cannot set startingDirectoryMustHaveAccess=true without setting startingDirectoryMustExist=true");
+        } else if (getEndpoint().isStartingDirectoryMustExist() && getEndpoint().isStartingDirectoryMustHaveAccess()) {
+            if (!file.canRead() || !file.canWrite()) {
+                throw new IOException("Starting directory permission denied: " + file);
+            }
+        }
+    }
+
     @Override
     public ResumeStrategy getResumeStrategy() {
         return resumeStrategy;
diff --git a/components/camel-file/src/main/java/org/apache/camel/component/file/FileEndpoint.java b/components/camel-file/src/main/java/org/apache/camel/component/file/FileEndpoint.java
index 3b2b803ad07..8efebcf1b50 100644
--- a/components/camel-file/src/main/java/org/apache/camel/component/file/FileEndpoint.java
+++ b/components/camel-file/src/main/java/org/apache/camel/component/file/FileEndpoint.java
@@ -17,8 +17,6 @@
 package org.apache.camel.component.file;
 
 import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.attribute.PosixFilePermission;
 import java.util.Arrays;
@@ -102,12 +100,6 @@ public class FileEndpoint extends GenericFileEndpoint<File> {
         ObjectHelper.notNull(operations, PARAM_OPERATIONS);
         ObjectHelper.notNull(file, "file");
 
-        // auto create starting directory if needed
-        if (!file.exists() && !file.isDirectory()) {
-            tryCreateDirectory();
-        }
-        tryReadingStartDirectory();
-
         FileConsumer result = newFileConsumer(processor, operations);
 
         if (isDelete() && getMove() != null) {
@@ -138,17 +130,6 @@ public class FileEndpoint extends GenericFileEndpoint<File> {
         return result;
     }
 
-    private void tryReadingStartDirectory() throws IOException {
-        if (!isStartingDirectoryMustExist() && isStartingDirectoryMustHaveAccess()) {
-            throw new IllegalArgumentException(
-                    "You cannot set startingDirectoryMustHaveAccess=true without setting startingDirectoryMustExist=true");
-        } else if (isStartingDirectoryMustExist() && isStartingDirectoryMustHaveAccess()) {
-            if (!file.canRead() || !file.canWrite()) {
-                throw new IOException("Starting directory permission denied: " + file);
-            }
-        }
-    }
-
     private void readLockCheck() {
         // check if it's valid
         String valid = "none,markerFile,fileLock,rename,changed,idempotent,idempotent-changed,idempotent-rename";
@@ -159,23 +140,6 @@ public class FileEndpoint extends GenericFileEndpoint<File> {
         }
     }
 
-    private void tryCreateDirectory() throws FileNotFoundException {
-        if (isAutoCreate()) {
-            doCreateStartDirectory();
-        } else if (isStartingDirectoryMustExist()) {
-            throw new FileNotFoundException("Starting directory does not exist: " + file);
-        }
-    }
-
-    private void doCreateStartDirectory() {
-        LOG.debug("Creating non existing starting directory: {}", file);
-        boolean absolute = FileUtil.isAbsolute(file);
-        boolean created = operations.buildDirectory(file.getPath(), absolute);
-        if (!created) {
-            LOG.warn("Cannot auto create starting directory: {}", file);
-        }
-    }
-
     @Override
     public PollingConsumer createPollingConsumer() throws Exception {
         ObjectHelper.notNull(operations, PARAM_OPERATIONS);
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerAutoCreateDirectoryTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerAutoCreateDirectoryTest.java
index 6b702ad667f..5d9441bfc73 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerAutoCreateDirectoryTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerAutoCreateDirectoryTest.java
@@ -16,7 +16,6 @@
  */
 package org.apache.camel.component.file;
 
-import java.io.FileNotFoundException;
 import java.nio.file.Files;
 
 import org.apache.camel.Consumer;
@@ -106,14 +105,15 @@ public class FileConsumerAutoCreateDirectoryTest extends ContextTestSupport {
     public void testStartingDirectoryMustExistDirectory() {
         Endpoint endpoint = context.getEndpoint(fileUri("foo?autoCreate=false&startingDirectoryMustExist=true"));
 
-        FileNotFoundException e = assertThrows(FileNotFoundException.class,
+        Exception e = assertThrows(Exception.class,
                 () -> {
-                    endpoint.createConsumer(exchange -> {
+                    Consumer c = endpoint.createConsumer(exchange -> {
                         // noop
                     });
+                    c.start();
                 }, "Should have thrown an exception");
 
-        assertTrue(e.getMessage().startsWith("Starting directory does not exist"));
+        assertTrue(e.getCause().getMessage().startsWith("Starting directory does not exist"));
 
         // the directory should NOT exists
         assertFalse(Files.exists(testDirectory("foo")), "Directory should NOT be created");
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCustomSchedulerTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCustomSchedulerTest.java
index 5455d03be58..150f70d484e 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCustomSchedulerTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerCustomSchedulerTest.java
@@ -53,7 +53,8 @@ public class FileConsumerCustomSchedulerTest extends ContextTestSupport {
         assertMockEndpointsSatisfied();
 
         // the scheduler is only run once, and we can configure its properties
-        assertEquals(1, scheduler.getCounter());
+        // (camel run the scheduler once during startup so the value is +1)
+        assertEquals(2, scheduler.getCounter());
         assertEquals("bar", scheduler.getFoo());
     }
 
diff --git a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerStartingDirectoryMustHaveAccessTest.java b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerStartingDirectoryMustHaveAccessTest.java
index 420b44963e7..10722f77b42 100644
--- a/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerStartingDirectoryMustHaveAccessTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/component/file/FileConsumerStartingDirectoryMustHaveAccessTest.java
@@ -17,8 +17,8 @@
 package org.apache.camel.component.file;
 
 import java.io.File;
-import java.io.IOException;
 
+import org.apache.camel.Consumer;
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Endpoint;
 import org.junit.jupiter.api.AfterEach;
@@ -52,13 +52,14 @@ public class FileConsumerStartingDirectoryMustHaveAccessTest extends ContextTest
         Endpoint endpoint = context.getEndpoint(
                 fileUri("noAccess?autoCreate=false&startingDirectoryMustExist=true&startingDirectoryMustHaveAccess=true"));
 
-        IOException e = assertThrows(IOException.class,
+        Exception e = assertThrows(Exception.class,
                 () -> {
-                    endpoint.createConsumer(exchange -> {
+                    Consumer c = endpoint.createConsumer(exchange -> {
                         // noop
                     });
+                    c.start();
                 }, "Should have thrown an exception");
 
-        assertTrue(e.getMessage().startsWith("Starting directory permission denied"), e.getMessage());
+        assertTrue(e.getCause().getMessage().startsWith("Starting directory permission denied"), e.getMessage());
     }
 }