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

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


The following commit(s) were added to refs/heads/camel-4.18.x by this push:
     new b48013b65976 [backport camel-4.18.x] CAMEL-27870: Close 
ValidatorFactory created by the endpoint (#31186)
b48013b65976 is described below

commit b48013b65976d07648191ad96544d86de3210610
Author: Guillaume Nodet <[email protected]>
AuthorDate: Tue Dec 1 18:33:10 2026 +0100

    [backport camel-4.18.x] CAMEL-27870: Close ValidatorFactory created by the 
endpoint (#31186)
    
    * [backport camel-4.18.x] CAMEL-27870: Close ValidatorFactory created by 
the endpoint
    
    * CAMEL-27870: fix test import for camel-4.18.x (junit5, not junit6)
---
 .../bean/validator/BeanValidatorEndpoint.java      |  13 ++-
 .../bean/validator/BeanValidatorProducer.java      |  32 ++++++
 .../bean/validator/ValidatorFactoryLeakTest.java   | 110 +++++++++++++++++++++
 3 files changed, 153 insertions(+), 2 deletions(-)

diff --git 
a/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorEndpoint.java
 
b/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorEndpoint.java
index 641fa9e14d6a..36334ba8fdd6 100644
--- 
a/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorEndpoint.java
+++ 
b/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorEndpoint.java
@@ -80,14 +80,23 @@ public class BeanValidatorEndpoint extends DefaultEndpoint {
 
         ValidatorFactory validatorFactory = this.validatorFactory;
         if (validatorFactory == null) {
-            validatorFactory = buildValidatorFactory(getCamelContext(), 
isIgnoreXmlConfiguration(),
-                    validationProviderResolver, messageInterpolator, 
traversableResolver, constraintValidatorFactory);
+            validatorFactory = buildOwnedValidatorFactory();
+            producer.setOwnedValidatorFactory(true);
         }
 
         producer.setValidatorFactory(validatorFactory);
         return producer;
     }
 
+    /**
+     * Builds a new {@link ValidatorFactory} from this endpoint's 
configuration options. Used by
+     * {@link BeanValidatorProducer#doStart()} to rebuild the factory after a 
stop/start cycle.
+     */
+    ValidatorFactory buildOwnedValidatorFactory() {
+        return buildValidatorFactory(getCamelContext(), 
isIgnoreXmlConfiguration(),
+                validationProviderResolver, messageInterpolator, 
traversableResolver, constraintValidatorFactory);
+    }
+
     @Override
     public Consumer createConsumer(Processor processor) throws Exception {
         throw new UnsupportedOperationException("Consumer is not supported");
diff --git 
a/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorProducer.java
 
b/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorProducer.java
index 7ad8f60ba286..2ebf57b93fe5 100644
--- 
a/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorProducer.java
+++ 
b/components/camel-bean-validator/src/main/java/org/apache/camel/component/bean/validator/BeanValidatorProducer.java
@@ -33,6 +33,12 @@ public class BeanValidatorProducer extends DefaultProducer {
 
     private ValidatorFactory validatorFactory;
     private Class<?> group;
+    /**
+     * Whether this producer owns the {@link ValidatorFactory} and must close 
it on stop. This is {@code true} when the
+     * factory was created by {@link BeanValidatorEndpoint#createProducer()} 
because no user-supplied factory was
+     * configured, and {@code false} when the factory was supplied externally 
(the caller owns its lifecycle).
+     */
+    private boolean ownedValidatorFactory;
 
     public BeanValidatorProducer(BeanValidatorEndpoint endpoint) {
         super(endpoint);
@@ -54,6 +60,24 @@ public class BeanValidatorProducer extends DefaultProducer {
         }
     }
 
+    @Override
+    protected void doStart() throws Exception {
+        if (ownedValidatorFactory && validatorFactory == null) {
+            // Factory was closed in a previous doStop(); rebuild it for the 
new start.
+            validatorFactory = ((BeanValidatorEndpoint) 
getEndpoint()).buildOwnedValidatorFactory();
+        }
+        super.doStart();
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        if (ownedValidatorFactory && validatorFactory != null) {
+            validatorFactory.close();
+            validatorFactory = null;
+        }
+        super.doStop();
+    }
+
     public ValidatorFactory getValidatorFactory() {
         return validatorFactory;
     }
@@ -62,6 +86,14 @@ public class BeanValidatorProducer extends DefaultProducer {
         this.validatorFactory = validatorFactory;
     }
 
+    boolean isOwnedValidatorFactory() {
+        return ownedValidatorFactory;
+    }
+
+    void setOwnedValidatorFactory(boolean ownedValidatorFactory) {
+        this.ownedValidatorFactory = ownedValidatorFactory;
+    }
+
     public Class<?> getGroup() {
         return group;
     }
diff --git 
a/components/camel-bean-validator/src/test/java/org/apache/camel/component/bean/validator/ValidatorFactoryLeakTest.java
 
b/components/camel-bean-validator/src/test/java/org/apache/camel/component/bean/validator/ValidatorFactoryLeakTest.java
new file mode 100644
index 000000000000..9bd847672402
--- /dev/null
+++ 
b/components/camel-bean-validator/src/test/java/org/apache/camel/component/bean/validator/ValidatorFactoryLeakTest.java
@@ -0,0 +1,110 @@
+/*
+ * 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.component.bean.validator;
+
+import jakarta.validation.ValidatorFactory;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.support.DefaultExchange;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.DisabledOnOs;
+
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.condition.OS.AIX;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.verify;
+
+/**
+ * Regression test for CAMEL-27870: a {@link ValidatorFactory} built by the 
endpoint must be closed when the producer
+ * stops; a user-supplied factory must not be closed (the caller owns its 
lifecycle).
+ */
+@DisabledOnOs(AIX)
+class ValidatorFactoryLeakTest extends CamelTestSupport {
+
+    /**
+     * When no validatorFactory is configured, the endpoint builds one and the 
producer owns it. The factory must be
+     * closed when the producer stops.
+     */
+    @Test
+    void producerOwnsAndClosesBuiltFactory() throws Exception {
+        BeanValidatorEndpoint endpoint = 
context.getEndpoint("bean-validator:dummy", BeanValidatorEndpoint.class);
+        BeanValidatorProducer producer = (BeanValidatorProducer) 
endpoint.createProducer();
+
+        assertTrue(producer.isOwnedValidatorFactory(), "producer should own 
the built factory");
+
+        ValidatorFactory factory = spy(producer.getValidatorFactory());
+        producer.setValidatorFactory(factory);
+
+        producer.start();
+        producer.stop();
+
+        verify(factory).close();
+    }
+
+    /**
+     * When a validatorFactory is supplied by the user, the producer does not 
own it and must not close it on stop.
+     */
+    @Test
+    void producerDoesNotCloseUserSuppliedFactory() throws Exception {
+        ValidatorFactory userFactory = mock(ValidatorFactory.class);
+
+        BeanValidatorEndpoint endpoint = 
context.getEndpoint("bean-validator:dummy", BeanValidatorEndpoint.class);
+        endpoint.setValidatorFactory(userFactory);
+
+        BeanValidatorProducer producer = (BeanValidatorProducer) 
endpoint.createProducer();
+
+        assertFalse(producer.isOwnedValidatorFactory(), "producer must not own 
a user-supplied factory");
+
+        producer.start();
+        producer.stop();
+
+        verify(userFactory, never()).close();
+    }
+
+    /**
+     * When a producer that owns its factory is stopped and then restarted, it 
must rebuild the factory and be able to
+     * process exchanges without a NullPointerException.
+     */
+    @Test
+    void producerRebuildsFactoryOnRestartAndProcesses() throws Exception {
+        BeanValidatorEndpoint endpoint = 
context.getEndpoint("bean-validator:dummy2", BeanValidatorEndpoint.class);
+        BeanValidatorProducer producer = (BeanValidatorProducer) 
endpoint.createProducer();
+
+        assertTrue(producer.isOwnedValidatorFactory(), "producer should own 
the built factory");
+
+        // First cycle
+        producer.start();
+        producer.stop();
+
+        // Factory must have been closed and nulled; producer must rebuild it 
on restart.
+        producer.start();
+
+        assertNotNull(producer.getValidatorFactory(), "factory must be rebuilt 
after stop/start");
+
+        // A bean without constraints must pass validation without NPE.
+        Exchange exchange = new DefaultExchange(context);
+        exchange.getIn().setBody(new CarWithoutAnnotations("Acme", "ABC-123"));
+        producer.process(exchange);
+
+        producer.stop();
+    }
+}

Reply via email to