This is an automated email from the ASF dual-hosted git repository.
markt-asf pushed a commit to branch 11.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/11.0.x by this push:
new 17c9fd3e20 Review "Report a resource link which omits the global
attribute..."
17c9fd3e20 is described below
commit 17c9fd3e20a5368a2f330c60569c1cef01e21884
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 74383ca540..b3d48bbde4 100644
--- a/java/org/apache/catalina/core/LocalStrings.properties
+++ b/java/org/apache/catalina/core/LocalStrings.properties
@@ -178,6 +178,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]