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

markt-asf pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit efe32e294602a3bd6886816dac49577989fc96f6
Author: Mark Thomas <[email protected]>
AuthorDate: Fri Nov 6 18:39:53 2026 +0000

    Review "Report a resource link which omits the global attribute..."
    
    Validate links before adding them
    Add clean-up to test
---
 .../apache/catalina/core/LocalStrings.properties   |  1 +
 .../catalina/core/NamingContextListener.java       |  6 +++++
 test/org/apache/naming/TestNamingContext.java      | 31 ++++++++++++++++++++++
 3 files changed, 38 insertions(+)

diff --git a/java/org/apache/catalina/core/LocalStrings.properties 
b/java/org/apache/catalina/core/LocalStrings.properties
index d1b2d3b8d5..1951c7c91e 100644
--- a/java/org/apache/catalina/core/LocalStrings.properties
+++ b/java/org/apache/catalina/core/LocalStrings.properties
@@ -175,6 +175,7 @@ naming.bind=Bind naming context to container [{0}]
 naming.bindFailed=Failed to bind object: [{0}]
 naming.invalidEnvEntryType=Environment entry [{0}] has an invalid type
 naming.invalidEnvEntryValue=Environment entry [{0}] has an invalid value
+naming.invalidResourceLink=The resource link [{0}] that refers to global 
resource [{1}] does not specify the required name or global attribute
 naming.jmxRegistrationFailed=Failed to register in JMX: [{0}]
 naming.namingContextCreationFailed=Creation of the naming context failed: [{0}]
 naming.unbindFailed=Failed to unbind object: [{0}]
diff --git a/java/org/apache/catalina/core/NamingContextListener.java 
b/java/org/apache/catalina/core/NamingContextListener.java
index 3dfb781b41..e68624ad0b 100644
--- a/java/org/apache/catalina/core/NamingContextListener.java
+++ b/java/org/apache/catalina/core/NamingContextListener.java
@@ -1200,6 +1200,12 @@ public class NamingContextListener implements 
LifecycleListener, PropertyChangeL
      */
     public void addResourceLink(ContextResourceLink resourceLink) {
 
+        // Reject a link which cannot be registered before anything is bound
+        if (resourceLink.getName() == null || resourceLink.getGlobal() == 
null) {
+            throw new IllegalArgumentException(
+                    sm.getString("naming.invalidResourceLink", 
resourceLink.getName(), resourceLink.getGlobal()));
+        }
+
         // Create a reference to the resource.
         Reference ref =
                 new ResourceLinkRef(resourceLink.getType(), 
resourceLink.getGlobal(), resourceLink.getFactory(), null);
diff --git a/test/org/apache/naming/TestNamingContext.java 
b/test/org/apache/naming/TestNamingContext.java
index bb39181824..cb1e1a190f 100644
--- a/test/org/apache/naming/TestNamingContext.java
+++ b/test/org/apache/naming/TestNamingContext.java
@@ -21,6 +21,7 @@ import java.util.Hashtable;
 import javax.naming.Context;
 import javax.naming.NamingException;
 
+import org.junit.After;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -97,6 +98,36 @@ public class TestNamingContext extends TomcatBaseTest {
     }
 
 
+    @After
+    public void resetResourceLinkGlobalContext() {
+        // The test below installs a private global context in a static field
+        ResourceLinkFactory.setGlobalContext(null);
+    }
+
+
+    @Test
+    public void testResourceLinkWithoutGlobalIsRejectedBeforeBinding() throws 
Exception {
+        Tomcat tomcat = getTomcatInstance();
+        tomcat.enableNaming();
+
+        org.apache.catalina.Context ctx = getProgrammaticRootContext();
+        tomcat.start();
+
+        ContextResourceLink crl = new ContextResourceLink();
+        crl.setName(LOCAL_NAME);
+        crl.setType(DATA.getClass().getName());
+        try {
+            ctx.getNamingResources().addResourceLink(crl);
+            Assert.fail("Expected an IllegalArgumentException for the missing 
global attribute");
+        } catch (IllegalArgumentException iae) {
+            Assert.assertTrue(iae.getMessage(), 
iae.getMessage().contains(LOCAL_NAME));
+        }
+
+        // The rejected link must not have been bound
+        Assert.assertNull(doLookup(ContextBindings.getContext(ctx), COMP_ENV + 
"/" + LOCAL_NAME));
+    }
+
+
     @Test
     public void testResourceLinkWithoutGlobalName() {
         // Use a private context to keep the global context validation out of


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to