This is an automated email from the ASF dual-hosted git repository. markt 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 de33f61aaa Add support for the java:module namespace de33f61aaa is described below commit de33f61aaa7815b2bd487f1d0aa523eef130ae6b 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 | 8 ++++ 3 files changed, 70 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 896dc94306..92ee2b845b 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -105,6 +105,14 @@ issues do not "pop up" wrt. others). --> <section name="Tomcat 11.0.8 (markt)" rtext="in development"> + <subsection name="Catalina"> + <changelog> + <fix> + Add support for the <code>java:module</code> namespace which mirrors + the <code>java:comp</code> namespace. (markt) + </fix> + </changelog> + </subsection> <subsection name="Web applications"> <changelog> <fix> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org