This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch main in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/main by this push: new b930a040f5 Add support for the java:module namespace b930a040f5 is described below commit b930a040f566623ad81ee74a98c9d7303b1a33d0 Author: Mark Thomas <ma...@apache.org> AuthorDate: Tue May 20 10:19:04 2025 +0100 Add support for the java:module namespace --- .../catalina/core/NamingContextListener.java | 7 +++ test/org/apache/naming/TestNamingContext.java | 55 ++++++++++++++++++++++ webapps/docs/changelog.xml | 4 ++ 3 files changed, 66 insertions(+) diff --git a/java/org/apache/catalina/core/NamingContextListener.java b/java/org/apache/catalina/core/NamingContextListener.java index 2f4ec5bee3..ccf19e8bfe 100644 --- a/java/org/apache/catalina/core/NamingContextListener.java +++ b/java/org/apache/catalina/core/NamingContextListener.java @@ -491,6 +491,13 @@ public class NamingContextListener implements LifecycleListener, PropertyChangeL } else { compCtx = namingContext.createSubcontext("comp"); envCtx = compCtx.createSubcontext("env"); + /* + * Jakarta Platform Specification, 5.2.2: Application Component Environment Namespaces + * + * "java:module" and "java:comp" refer to the same namespace in a web module (i.e. a web application). + * Implement this by binding the "comp" sub-context we just created to the "module" name as well. + */ + namingContext.bind("module", compCtx); } int i; diff --git a/test/org/apache/naming/TestNamingContext.java b/test/org/apache/naming/TestNamingContext.java index 25ea465c1c..64524ec44d 100644 --- a/test/org/apache/naming/TestNamingContext.java +++ b/test/org/apache/naming/TestNamingContext.java @@ -22,6 +22,8 @@ import javax.naming.NamingException; import org.junit.Assert; import org.junit.Test; +import org.apache.catalina.LifecycleListener; +import org.apache.catalina.core.NamingContextListener; import org.apache.catalina.startup.Tomcat; import org.apache.catalina.startup.TomcatBaseTest; import org.apache.naming.factory.ResourceLinkFactory; @@ -31,6 +33,7 @@ import org.apache.tomcat.util.descriptor.web.ContextResourceLink; public class TestNamingContext extends TomcatBaseTest { private static final String COMP_ENV = "comp/env"; + private static final String MODULE_ENV = "module/env"; private static final String GLOBAL_NAME = "global"; private static final String LOCAL_NAME = "local"; private static final String DATA = "Cabbage"; @@ -92,6 +95,58 @@ public class TestNamingContext extends TomcatBaseTest { } + @Test + public void testModuleEquivalentToComp() throws Exception { + Tomcat tomcat = getTomcatInstance(); + tomcat.enableNaming(); + + org.apache.catalina.Context ctx = getProgrammaticRootContext(); + + tomcat.start(); + + // Equivalent to: Context initContext = new InitialContext(); + Context webappInitial = ContextBindings.getContext(ctx); + + // Make it writable (it is normally read-only) + String namingContextName = null; + LifecycleListener[] listeners = ctx.findLifecycleListeners(); + for (LifecycleListener listener : listeners) { + if (listener instanceof NamingContextListener namingListener) { + namingContextName = namingListener.getName(); + break; + } + } + ContextAccessController.setWritable(namingContextName, ctx.getNamingToken()); + + // Nothing created so should be null + Object obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME); + Assert.assertNull(obj); + obj = doLookup(webappInitial, MODULE_ENV + "/" + LOCAL_NAME); + Assert.assertNull(obj); + + // Create in java:comp/env + webappInitial.bind(COMP_ENV + "/" + LOCAL_NAME, DATA); + + // Check it was created in java:comp/env and java:module/env + obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME); + Assert.assertEquals(DATA, obj); + obj = doLookup(webappInitial, MODULE_ENV + "/" + LOCAL_NAME); + Assert.assertEquals(DATA, obj); + + // Remove it + webappInitial.unbind(COMP_ENV + "/" + LOCAL_NAME); + + // Create in java:module/env + webappInitial.bind(MODULE_ENV + "/" + LOCAL_NAME, DATA); + + // Check it was created in java:comp/env and java:module/env + obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME); + Assert.assertEquals(DATA, obj); + obj = doLookup(webappInitial, MODULE_ENV + "/" + LOCAL_NAME); + Assert.assertEquals(DATA, obj); + } + + private Object doLookup(Context context, String name) { Object result = null; try { diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 36fe25e532..a852654c04 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -166,6 +166,10 @@ path. (markt) </scode> <!-- Entries for backport and removal before 12.0.0-M1 below this line --> + <fix> + Add support for the <code>java:module</code> namespace which mirrors + the <code>java:comp</code> namespace. (markt) + </fix> </changelog> </subsection> <subsection name="Coyote"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org