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

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


The following commit(s) were added to refs/heads/main by this push:
     new b22a80b341 Use JndiHelper in AbstractMessageListenerContainer (#2918)
b22a80b341 is described below

commit b22a80b341bf7e24d2df6cb95a6e01e78d3ff7aa
Author: Colm O hEigeartaigh <[email protected]>
AuthorDate: Fri Mar 6 14:04:00 2026 +0000

    Use JndiHelper in AbstractMessageListenerContainer (#2918)
---
 .../jms/util/AbstractMessageListenerContainer.java |  3 ++-
 .../apache/cxf/transport/jms/util/JndiHelper.java  |  6 ++++-
 .../transport/jms/util/MessageListenerTest.java    | 26 ++++++++++++++++++++++
 3 files changed, 33 insertions(+), 2 deletions(-)

diff --git 
a/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/AbstractMessageListenerContainer.java
 
b/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/AbstractMessageListenerContainer.java
index 8b0a977b68..ad17f4ca7b 100644
--- 
a/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/AbstractMessageListenerContainer.java
+++ 
b/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/AbstractMessageListenerContainer.java
@@ -106,7 +106,8 @@ public abstract class AbstractMessageListenerContainer 
implements JMSListenerCon
     public InitialContext createInitialContext() {
         if (jndiEnvironment != null) {
             try {
-                return new InitialContext(this.jndiEnvironment);
+                JndiHelper helper = new JndiHelper(this.jndiEnvironment);
+                return helper.createInitialContext();
             } catch (NamingException e) {
                 LOG.log(Level.SEVERE, "Could not expose JNDI environment to 
JMS thread context", e);
             }
diff --git 
a/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/JndiHelper.java
 
b/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/JndiHelper.java
index ef98b6b3e5..0f86ead147 100644
--- 
a/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/JndiHelper.java
+++ 
b/rt/transports/jms/src/main/java/org/apache/cxf/transport/jms/util/JndiHelper.java
@@ -78,7 +78,7 @@ public class JndiHelper {
 
     @SuppressWarnings("unchecked")
     public <T> T lookup(final String name, Class<T> requiredType) throws 
NamingException {
-        Context ctx = new InitialContext(this.environment);
+        Context ctx = createInitialContext();
         try {
             Object located = ctx.lookup(name);
             if (located == null) {
@@ -90,4 +90,8 @@ public class JndiHelper {
         }
     }
 
+    public InitialContext createInitialContext() throws NamingException {
+        return new InitialContext(this.environment);
+    }
+
 }
diff --git 
a/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/util/MessageListenerTest.java
 
b/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/util/MessageListenerTest.java
index 82acb5c906..e743176446 100644
--- 
a/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/util/MessageListenerTest.java
+++ 
b/rt/transports/jms/src/test/java/org/apache/cxf/transport/jms/util/MessageListenerTest.java
@@ -19,8 +19,10 @@
 package org.apache.cxf.transport.jms.util;
 
 import java.util.Enumeration;
+import java.util.Properties;
 import java.util.Timer;
 
+import javax.naming.Context;
 import javax.transaction.xa.XAException;
 
 import jakarta.jms.Connection;
@@ -53,6 +55,7 @@ import 
org.apache.activemq.artemis.core.settings.impl.AddressSettings;
 import org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory;
 import org.apache.activemq.artemis.junit.EmbeddedActiveMQResource;
 import org.apache.activemq.artemis.ra.ActiveMQResourceAdapter;
+import org.apache.cxf.transport.jms.JMSConfiguration;
 import org.awaitility.Awaitility;
 import org.jboss.narayana.jta.jms.ConnectionFactoryProxy;
 import org.jboss.narayana.jta.jms.TransactionHelperImpl;
@@ -64,6 +67,7 @@ import static org.hamcrest.CoreMatchers.containsString;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
 
 
 public class MessageListenerTest {
@@ -94,6 +98,28 @@ public class MessageListenerTest {
         assertEquals("Connection is closed", ex.getMessage());
     }
 
+    @Test
+    public void testJndiForbiddenProtocolForPollingContainer() {
+        Properties env = new Properties();
+        env.put(Context.INITIAL_CONTEXT_FACTORY, 
"com.sun.jndi.ldap.LdapCtxFactory");
+        env.put(Context.PROVIDER_URL, "ldap://127.0.0.1:12345";);
+        env.put(Context.REFERRAL, "follow");
+
+        JMSConfiguration jmsConfig = new JMSConfiguration();
+        jmsConfig.setJndiEnvironment(env);
+
+        PollingMessageListenerContainer container =
+            new PollingMessageListenerContainer(jmsConfig, false, message -> { 
});
+        container.setJndiEnvironment(env);
+
+        try {
+            container.createInitialContext();
+            org.junit.Assert.fail("JNDI context creation should have failed 
for unsafe LDAP protocol");
+        } catch (IllegalArgumentException ex) {
+            assertTrue(ex.getMessage().contains("Unsafe protocol in JNDI 
URL"));
+        }
+    }
+
     @Test
     public void testConnectionProblemXA() throws JMSException, XAException, 
ResourceException {
         TransactionManager transactionManager = 
com.arjuna.ats.jta.TransactionManager.transactionManager();

Reply via email to