From 1e0f012f66ce88e1e6043b41ba83780680af0f29 Mon Sep 17 00:00:00 2001 From: Jan Bartel Date: Fri, 10 May 2024 08:43:48 +0200 Subject: [PATCH] Support jetty-eeX-env.xml, as per jetty-eeX-web.xml (#11752) * Support jetty-eeX-env.xml, as per jetty-eeX-web.xml --------- Co-authored-by: Greg Wilkins --- .../ee10/plus/webapp/EnvConfiguration.java | 55 +++++++++---- .../plus/webapp/EnvConfigurationTest.java | 76 ++++++++++++++++++ .../WEB-INF/jetty-ee10-env.xml | 15 ++++ .../WEB-INF/jetty-ee101-env.xml | 15 ++++ .../WEB-INF/jetty-env.xml | 15 ++++ .../WEB-INF/jetty-env.xml | 15 ++++ .../ee11/plus/webapp/EnvConfiguration.java | 54 +++++++++---- .../plus/webapp/EnvConfigurationTest.java | 76 ++++++++++++++++++ .../WEB-INF/jetty-ee11-env.xml | 15 ++++ .../WEB-INF/jetty-ee111-env.xml | 15 ++++ .../WEB-INF/jetty-env.xml | 15 ++++ .../WEB-INF/jetty-env.xml | 15 ++++ .../WEB-INF/jetty-ee8-env.xml | 15 ++++ .../WEB-INF/jetty-ee88-env.xml | 15 ++++ .../WEB-INF/jetty-env.xml | 15 ++++ .../WEB-INF/jetty-env.xml | 15 ++++ .../ee9/plus/webapp/EnvConfiguration.java | 52 +++++++++---- .../ee9/plus/webapp/EnvConfigurationTest.java | 77 +++++++++++++++++++ .../WEB-INF/jetty-ee9-env.xml | 15 ++++ .../WEB-INF/jetty-ee99-env.xml | 15 ++++ .../WEB-INF/jetty-env.xml | 15 ++++ .../WEB-INF/jetty-env.xml | 15 ++++ 22 files changed, 584 insertions(+), 46 deletions(-) create mode 100644 jetty-ee10/jetty-ee10-plus/src/test/java/org/eclipse/jetty/ee10/plus/webapp/EnvConfigurationTest.java create mode 100644 jetty-ee10/jetty-ee10-plus/src/test/resources/webapp-with-jetty-ee10-env-xml/WEB-INF/jetty-ee10-env.xml create mode 100644 jetty-ee10/jetty-ee10-plus/src/test/resources/webapp-with-jetty-ee10-env-xml/WEB-INF/jetty-ee101-env.xml create mode 100644 jetty-ee10/jetty-ee10-plus/src/test/resources/webapp-with-jetty-ee10-env-xml/WEB-INF/jetty-env.xml create mode 100644 jetty-ee10/jetty-ee10-plus/src/test/resources/webapp-with-jetty-env-xml/WEB-INF/jetty-env.xml create mode 100644 jetty-ee11/jetty-ee11-plus/src/test/java/org/eclipse/jetty/ee11/plus/webapp/EnvConfigurationTest.java create mode 100644 jetty-ee11/jetty-ee11-plus/src/test/resources/webapp-with-jetty-ee11-env-xml/WEB-INF/jetty-ee11-env.xml create mode 100644 jetty-ee11/jetty-ee11-plus/src/test/resources/webapp-with-jetty-ee11-env-xml/WEB-INF/jetty-ee111-env.xml create mode 100644 jetty-ee11/jetty-ee11-plus/src/test/resources/webapp-with-jetty-ee11-env-xml/WEB-INF/jetty-env.xml create mode 100644 jetty-ee11/jetty-ee11-plus/src/test/resources/webapp-with-jetty-env-xml/WEB-INF/jetty-env.xml create mode 100644 jetty-ee8/jetty-ee8-plus/src/test/resources/webapp-with-jetty-ee8-env-xml/WEB-INF/jetty-ee8-env.xml create mode 100644 jetty-ee8/jetty-ee8-plus/src/test/resources/webapp-with-jetty-ee8-env-xml/WEB-INF/jetty-ee88-env.xml create mode 100644 jetty-ee8/jetty-ee8-plus/src/test/resources/webapp-with-jetty-ee8-env-xml/WEB-INF/jetty-env.xml create mode 100644 jetty-ee8/jetty-ee8-plus/src/test/resources/webapp-with-jetty-env-xml/WEB-INF/jetty-env.xml create mode 100644 jetty-ee9/jetty-ee9-plus/src/test/java/org/eclipse/jetty/ee9/plus/webapp/EnvConfigurationTest.java create mode 100644 jetty-ee9/jetty-ee9-plus/src/test/resources/webapp-with-jetty-ee9-env-xml/WEB-INF/jetty-ee9-env.xml create mode 100644 jetty-ee9/jetty-ee9-plus/src/test/resources/webapp-with-jetty-ee9-env-xml/WEB-INF/jetty-ee99-env.xml create mode 100644 jetty-ee9/jetty-ee9-plus/src/test/resources/webapp-with-jetty-ee9-env-xml/WEB-INF/jetty-env.xml create mode 100644 jetty-ee9/jetty-ee9-plus/src/test/resources/webapp-with-jetty-env-xml/WEB-INF/jetty-env.xml diff --git a/jetty-ee10/jetty-ee10-plus/src/main/java/org/eclipse/jetty/ee10/plus/webapp/EnvConfiguration.java b/jetty-ee10/jetty-ee10-plus/src/main/java/org/eclipse/jetty/ee10/plus/webapp/EnvConfiguration.java index 486ad1edd20..96b2e775fbb 100644 --- a/jetty-ee10/jetty-ee10-plus/src/main/java/org/eclipse/jetty/ee10/plus/webapp/EnvConfiguration.java +++ b/jetty-ee10/jetty-ee10-plus/src/main/java/org/eclipse/jetty/ee10/plus/webapp/EnvConfiguration.java @@ -47,6 +47,8 @@ public class EnvConfiguration extends AbstractConfiguration private static final Logger LOG = LoggerFactory.getLogger(EnvConfiguration.class); private static final String JETTY_ENV_BINDINGS = "org.eclipse.jetty.jndi.EnvConfiguration"; + private static final String JETTY_EE10_ENV_XML_FILENAME = "jetty-ee10-env.xml"; + private static final String JETTY_ENV_XML_FILENAME = "jetty-env.xml"; public EnvConfiguration() { @@ -69,25 +71,12 @@ public class EnvConfiguration extends AbstractConfiguration if (LOG.isDebugEnabled()) LOG.debug("Created java:comp/env for webapp {}", context.getContextPath()); - //check to see if an explicit file has been set, if not, - //look in WEB-INF/jetty-env.xml - + //check to see if an explicit file has been set Resource jettyEnvXmlResource = (Resource)context.getAttribute(JETTY_ENV_XML); if (jettyEnvXmlResource == null) { - //look for a file called WEB-INF/jetty-env.xml - //and process it if it exists - Resource webInf = context.getWebInf(); - if (webInf != null && webInf.isDirectory()) - { - // TODO: should never return from WEB-INF/lib/foo.jar!/WEB-INF/jetty-env.xml - // TODO: should also never return from a META-INF/versions/#/WEB-INF/jetty-env.xml location - Resource jettyEnv = webInf.resolve("jetty-env.xml"); - if (Resources.exists(jettyEnv)) - { - jettyEnvXmlResource = jettyEnv; - } - } + //otherwise find jetty-ee10-env.xml or fallback to jetty-env.xml + jettyEnvXmlResource = resolveJettyEnvXml(context.getWebInf()); } if (jettyEnvXmlResource != null) @@ -241,6 +230,40 @@ public class EnvConfiguration extends AbstractConfiguration } } + /** + * Obtain a WEB-INF/jetty-ee10-env.xml, falling back to + * looking for WEB-INF/jetty-env.xml. + * + * @param webInf the WEB-INF of the context to search + * @return the file if it exists or null otherwise + */ + private Resource resolveJettyEnvXml(Resource webInf) + { + try + { + if (webInf == null || !webInf.isDirectory()) + return null; + + //try to find jetty-ee10-env.xml + Resource xmlResource = webInf.resolve(JETTY_EE10_ENV_XML_FILENAME); + if (!Resources.missing(xmlResource)) + return xmlResource; + + //failing that, look for jetty-env.xml + xmlResource = webInf.resolve(JETTY_ENV_XML_FILENAME); + if (!Resources.missing(xmlResource)) + return xmlResource; + + return null; + } + catch (Exception e) + { + if (LOG.isDebugEnabled()) + LOG.debug("Error resolving", e); + return null; + } + } + private static class Dumper extends NamingDump { Dumper(ClassLoader loader, String name) diff --git a/jetty-ee10/jetty-ee10-plus/src/test/java/org/eclipse/jetty/ee10/plus/webapp/EnvConfigurationTest.java b/jetty-ee10/jetty-ee10-plus/src/test/java/org/eclipse/jetty/ee10/plus/webapp/EnvConfigurationTest.java new file mode 100644 index 00000000000..6dd104b62e2 --- /dev/null +++ b/jetty-ee10/jetty-ee10-plus/src/test/java/org/eclipse/jetty/ee10/plus/webapp/EnvConfigurationTest.java @@ -0,0 +1,76 @@ +// +// ======================================================================== +// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License v. 2.0 which is available at +// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// ======================================================================== +// + +package org.eclipse.jetty.ee10.plus.webapp; + +import java.nio.file.Files; +import java.nio.file.Path; + +import org.eclipse.jetty.ee10.webapp.WebAppContext; +import org.eclipse.jetty.plus.jndi.NamingEntryUtil; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.toolchain.test.MavenTestingUtils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class EnvConfigurationTest +{ + Server _server; + + @BeforeEach + public void setUp() + { + _server = new Server(); + } + + @AfterEach + public void tearDown() throws Exception + { + if (_server != null) + _server.stop(); + } + + @Test + public void testWithOnlyJettyWebXml() throws Exception + { + Path testWebappDir = MavenTestingUtils.getTargetPath("test-classes/webapp-with-jetty-env-xml"); + assertTrue(Files.exists(testWebappDir)); + + WebAppContext context = new WebAppContext(); + context.setContextPath("/"); + _server.setHandler(context); + context.setWar(testWebappDir.toFile().getAbsolutePath()); + _server.start(); + assertNotNull(NamingEntryUtil.lookupNamingEntry(context, "apricot")); + } + + @Test + public void testWithJettyEEWebXml() throws Exception + { + Path testWebappDir = MavenTestingUtils.getTargetPath("test-classes/webapp-with-jetty-ee10-env-xml"); + assertTrue(Files.exists(testWebappDir)); + + WebAppContext context = new WebAppContext(); + context.setContextPath("/"); + _server.setHandler(context); + context.setWar(testWebappDir.toFile().getAbsolutePath()); + _server.start(); + assertNotNull(NamingEntryUtil.lookupNamingEntry(context, "peach")); + assertNull(NamingEntryUtil.lookupNamingEntry(context, "cabbage")); + } +} diff --git a/jetty-ee10/jetty-ee10-plus/src/test/resources/webapp-with-jetty-ee10-env-xml/WEB-INF/jetty-ee10-env.xml b/jetty-ee10/jetty-ee10-plus/src/test/resources/webapp-with-jetty-ee10-env-xml/WEB-INF/jetty-ee10-env.xml new file mode 100644 index 00000000000..5ad90b008ff --- /dev/null +++ b/jetty-ee10/jetty-ee10-plus/src/test/resources/webapp-with-jetty-ee10-env-xml/WEB-INF/jetty-ee10-env.xml @@ -0,0 +1,15 @@ + + + + + + + + + peach + 100 + true + + + + diff --git a/jetty-ee10/jetty-ee10-plus/src/test/resources/webapp-with-jetty-ee10-env-xml/WEB-INF/jetty-ee101-env.xml b/jetty-ee10/jetty-ee10-plus/src/test/resources/webapp-with-jetty-ee10-env-xml/WEB-INF/jetty-ee101-env.xml new file mode 100644 index 00000000000..c327a3a8123 --- /dev/null +++ b/jetty-ee10/jetty-ee10-plus/src/test/resources/webapp-with-jetty-ee10-env-xml/WEB-INF/jetty-ee101-env.xml @@ -0,0 +1,15 @@ + + + + + + + + + cabbage + 100 + true + + + + diff --git a/jetty-ee10/jetty-ee10-plus/src/test/resources/webapp-with-jetty-ee10-env-xml/WEB-INF/jetty-env.xml b/jetty-ee10/jetty-ee10-plus/src/test/resources/webapp-with-jetty-ee10-env-xml/WEB-INF/jetty-env.xml new file mode 100644 index 00000000000..fe3891b4686 --- /dev/null +++ b/jetty-ee10/jetty-ee10-plus/src/test/resources/webapp-with-jetty-ee10-env-xml/WEB-INF/jetty-env.xml @@ -0,0 +1,15 @@ + + + + + + + + + apricot + 100 + true + + + + diff --git a/jetty-ee10/jetty-ee10-plus/src/test/resources/webapp-with-jetty-env-xml/WEB-INF/jetty-env.xml b/jetty-ee10/jetty-ee10-plus/src/test/resources/webapp-with-jetty-env-xml/WEB-INF/jetty-env.xml new file mode 100644 index 00000000000..fe3891b4686 --- /dev/null +++ b/jetty-ee10/jetty-ee10-plus/src/test/resources/webapp-with-jetty-env-xml/WEB-INF/jetty-env.xml @@ -0,0 +1,15 @@ + + + + + + + + + apricot + 100 + true + + + + diff --git a/jetty-ee11/jetty-ee11-plus/src/main/java/org/eclipse/jetty/ee11/plus/webapp/EnvConfiguration.java b/jetty-ee11/jetty-ee11-plus/src/main/java/org/eclipse/jetty/ee11/plus/webapp/EnvConfiguration.java index aebf19d4a8c..5119d801b28 100644 --- a/jetty-ee11/jetty-ee11-plus/src/main/java/org/eclipse/jetty/ee11/plus/webapp/EnvConfiguration.java +++ b/jetty-ee11/jetty-ee11-plus/src/main/java/org/eclipse/jetty/ee11/plus/webapp/EnvConfiguration.java @@ -47,6 +47,8 @@ public class EnvConfiguration extends AbstractConfiguration private static final Logger LOG = LoggerFactory.getLogger(EnvConfiguration.class); private static final String JETTY_ENV_BINDINGS = "org.eclipse.jetty.jndi.EnvConfiguration"; + private static final String JETTY_EE11_ENV_XML_FILENAME = "jetty-ee11-env.xml"; + private static final String JETTY_ENV_XML_FILENAME = "jetty-env.xml"; public EnvConfiguration() { @@ -69,25 +71,11 @@ public class EnvConfiguration extends AbstractConfiguration if (LOG.isDebugEnabled()) LOG.debug("Created java:comp/env for webapp {}", context.getContextPath()); - //check to see if an explicit file has been set, if not, - //look in WEB-INF/jetty-env.xml - + //check to see if an explicit file has been set, Resource jettyEnvXmlResource = (Resource)context.getAttribute(JETTY_ENV_XML); if (jettyEnvXmlResource == null) { - //look for a file called WEB-INF/jetty-env.xml - //and process it if it exists - Resource webInf = context.getWebInf(); - if (webInf != null && webInf.isDirectory()) - { - // TODO: should never return from WEB-INF/lib/foo.jar!/WEB-INF/jetty-env.xml - // TODO: should also never return from a META-INF/versions/#/WEB-INF/jetty-env.xml location - Resource jettyEnv = webInf.resolve("jetty-env.xml"); - if (Resources.exists(jettyEnv)) - { - jettyEnvXmlResource = jettyEnv; - } - } + jettyEnvXmlResource = resolveJettyEnvXml(context.getWebInf()); } if (jettyEnvXmlResource != null) @@ -241,6 +229,40 @@ public class EnvConfiguration extends AbstractConfiguration } } + /** + * Obtain a WEB-INF/jetty-ee11-env.xml, falling back to + * looking for WEB-INF/jetty-env.xml. + * + * @param webInf the WEB-INF of the context to search + * @return the file if it exists or null otherwise + */ + private Resource resolveJettyEnvXml(Resource webInf) + { + try + { + if (webInf == null || !webInf.isDirectory()) + return null; + + //try to find jetty-ee10-env.xml + Resource xmlResource = webInf.resolve(JETTY_EE11_ENV_XML_FILENAME); + if (!Resources.missing(xmlResource)) + return xmlResource; + + //failing that, look for jetty-env.xml + xmlResource = webInf.resolve(JETTY_ENV_XML_FILENAME); + if (!Resources.missing(xmlResource)) + return xmlResource; + + return null; + } + catch (Exception e) + { + if (LOG.isDebugEnabled()) + LOG.debug("Error resolving", e); + return null; + } + } + private static class Dumper extends NamingDump { Dumper(ClassLoader loader, String name) diff --git a/jetty-ee11/jetty-ee11-plus/src/test/java/org/eclipse/jetty/ee11/plus/webapp/EnvConfigurationTest.java b/jetty-ee11/jetty-ee11-plus/src/test/java/org/eclipse/jetty/ee11/plus/webapp/EnvConfigurationTest.java new file mode 100644 index 00000000000..501e03d7707 --- /dev/null +++ b/jetty-ee11/jetty-ee11-plus/src/test/java/org/eclipse/jetty/ee11/plus/webapp/EnvConfigurationTest.java @@ -0,0 +1,76 @@ +// +// ======================================================================== +// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License v. 2.0 which is available at +// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// ======================================================================== +// + +package org.eclipse.jetty.ee11.plus.webapp; + +import java.nio.file.Files; +import java.nio.file.Path; + +import org.eclipse.jetty.ee11.webapp.WebAppContext; +import org.eclipse.jetty.plus.jndi.NamingEntryUtil; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.toolchain.test.MavenTestingUtils; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class EnvConfigurationTest +{ + Server _server; + + @BeforeEach + public void setUp() + { + _server = new Server(); + } + + @AfterEach + public void tearDown() throws Exception + { + if (_server != null) + _server.stop(); + } + + @Test + public void testWithOnlyJettyWebXml() throws Exception + { + Path testWebappDir = MavenTestingUtils.getTargetPath("test-classes/webapp-with-jetty-env-xml"); + assertTrue(Files.exists(testWebappDir)); + + WebAppContext context = new WebAppContext(); + context.setContextPath("/"); + _server.setHandler(context); + context.setWar(testWebappDir.toFile().getAbsolutePath()); + _server.start(); + assertNotNull(NamingEntryUtil.lookupNamingEntry(context, "apricot")); + } + + @Test + public void testWithJettyEEWebXml() throws Exception + { + Path testWebappDir = MavenTestingUtils.getTargetPath("test-classes/webapp-with-jetty-ee11-env-xml"); + assertTrue(Files.exists(testWebappDir)); + + WebAppContext context = new WebAppContext(); + context.setContextPath("/"); + _server.setHandler(context); + context.setWar(testWebappDir.toFile().getAbsolutePath()); + _server.start(); + assertNotNull(NamingEntryUtil.lookupNamingEntry(context, "peach")); + assertNull(NamingEntryUtil.lookupNamingEntry(context, "cabbage")); + } +} diff --git a/jetty-ee11/jetty-ee11-plus/src/test/resources/webapp-with-jetty-ee11-env-xml/WEB-INF/jetty-ee11-env.xml b/jetty-ee11/jetty-ee11-plus/src/test/resources/webapp-with-jetty-ee11-env-xml/WEB-INF/jetty-ee11-env.xml new file mode 100644 index 00000000000..754cd7765b1 --- /dev/null +++ b/jetty-ee11/jetty-ee11-plus/src/test/resources/webapp-with-jetty-ee11-env-xml/WEB-INF/jetty-ee11-env.xml @@ -0,0 +1,15 @@ + + + + + + + + + peach + 100 + true + + + + diff --git a/jetty-ee11/jetty-ee11-plus/src/test/resources/webapp-with-jetty-ee11-env-xml/WEB-INF/jetty-ee111-env.xml b/jetty-ee11/jetty-ee11-plus/src/test/resources/webapp-with-jetty-ee11-env-xml/WEB-INF/jetty-ee111-env.xml new file mode 100644 index 00000000000..27f8d71a5d0 --- /dev/null +++ b/jetty-ee11/jetty-ee11-plus/src/test/resources/webapp-with-jetty-ee11-env-xml/WEB-INF/jetty-ee111-env.xml @@ -0,0 +1,15 @@ + + + + + + + + + cabbage + 100 + true + + + + diff --git a/jetty-ee11/jetty-ee11-plus/src/test/resources/webapp-with-jetty-ee11-env-xml/WEB-INF/jetty-env.xml b/jetty-ee11/jetty-ee11-plus/src/test/resources/webapp-with-jetty-ee11-env-xml/WEB-INF/jetty-env.xml new file mode 100644 index 00000000000..8e7028caffe --- /dev/null +++ b/jetty-ee11/jetty-ee11-plus/src/test/resources/webapp-with-jetty-ee11-env-xml/WEB-INF/jetty-env.xml @@ -0,0 +1,15 @@ + + + + + + + + + apricot + 100 + true + + + + diff --git a/jetty-ee11/jetty-ee11-plus/src/test/resources/webapp-with-jetty-env-xml/WEB-INF/jetty-env.xml b/jetty-ee11/jetty-ee11-plus/src/test/resources/webapp-with-jetty-env-xml/WEB-INF/jetty-env.xml new file mode 100644 index 00000000000..8e7028caffe --- /dev/null +++ b/jetty-ee11/jetty-ee11-plus/src/test/resources/webapp-with-jetty-env-xml/WEB-INF/jetty-env.xml @@ -0,0 +1,15 @@ + + + + + + + + + apricot + 100 + true + + + + diff --git a/jetty-ee8/jetty-ee8-plus/src/test/resources/webapp-with-jetty-ee8-env-xml/WEB-INF/jetty-ee8-env.xml b/jetty-ee8/jetty-ee8-plus/src/test/resources/webapp-with-jetty-ee8-env-xml/WEB-INF/jetty-ee8-env.xml new file mode 100644 index 00000000000..d83bd1233a0 --- /dev/null +++ b/jetty-ee8/jetty-ee8-plus/src/test/resources/webapp-with-jetty-ee8-env-xml/WEB-INF/jetty-ee8-env.xml @@ -0,0 +1,15 @@ + + + + + + + + + peach + 100 + true + + + + diff --git a/jetty-ee8/jetty-ee8-plus/src/test/resources/webapp-with-jetty-ee8-env-xml/WEB-INF/jetty-ee88-env.xml b/jetty-ee8/jetty-ee8-plus/src/test/resources/webapp-with-jetty-ee8-env-xml/WEB-INF/jetty-ee88-env.xml new file mode 100644 index 00000000000..3f397d2244b --- /dev/null +++ b/jetty-ee8/jetty-ee8-plus/src/test/resources/webapp-with-jetty-ee8-env-xml/WEB-INF/jetty-ee88-env.xml @@ -0,0 +1,15 @@ + + + + + + + + + cabbage + 100 + true + + + + diff --git a/jetty-ee8/jetty-ee8-plus/src/test/resources/webapp-with-jetty-ee8-env-xml/WEB-INF/jetty-env.xml b/jetty-ee8/jetty-ee8-plus/src/test/resources/webapp-with-jetty-ee8-env-xml/WEB-INF/jetty-env.xml new file mode 100644 index 00000000000..01a352972a1 --- /dev/null +++ b/jetty-ee8/jetty-ee8-plus/src/test/resources/webapp-with-jetty-ee8-env-xml/WEB-INF/jetty-env.xml @@ -0,0 +1,15 @@ + + + + + + + + + apricot + 100 + true + + + + diff --git a/jetty-ee8/jetty-ee8-plus/src/test/resources/webapp-with-jetty-env-xml/WEB-INF/jetty-env.xml b/jetty-ee8/jetty-ee8-plus/src/test/resources/webapp-with-jetty-env-xml/WEB-INF/jetty-env.xml new file mode 100644 index 00000000000..01a352972a1 --- /dev/null +++ b/jetty-ee8/jetty-ee8-plus/src/test/resources/webapp-with-jetty-env-xml/WEB-INF/jetty-env.xml @@ -0,0 +1,15 @@ + + + + + + + + + apricot + 100 + true + + + + diff --git a/jetty-ee9/jetty-ee9-plus/src/main/java/org/eclipse/jetty/ee9/plus/webapp/EnvConfiguration.java b/jetty-ee9/jetty-ee9-plus/src/main/java/org/eclipse/jetty/ee9/plus/webapp/EnvConfiguration.java index 7957901e970..bc4f01dcd39 100644 --- a/jetty-ee9/jetty-ee9-plus/src/main/java/org/eclipse/jetty/ee9/plus/webapp/EnvConfiguration.java +++ b/jetty-ee9/jetty-ee9-plus/src/main/java/org/eclipse/jetty/ee9/plus/webapp/EnvConfiguration.java @@ -14,7 +14,6 @@ package org.eclipse.jetty.ee9.plus.webapp; import java.net.URL; -import java.util.Map; import java.util.Set; import javax.naming.Context; import javax.naming.InitialContext; @@ -50,6 +49,8 @@ public class EnvConfiguration extends AbstractConfiguration private static final Logger LOG = LoggerFactory.getLogger(EnvConfiguration.class); private static final String JETTY_ENV_BINDINGS = "org.eclipse.jetty.jndi.EnvConfiguration"; + private static final String JETTY_ENV_XML = "jetty-env.xml"; + private static final String JETTY_EE9_ENV_XML = "jetty-ee9-env.xml"; private Resource jettyEnvXmlResource; private NamingDump _dumper; private ResourceFactory.Closeable _resourceFactory; @@ -89,19 +90,8 @@ public class EnvConfiguration extends AbstractConfiguration //look in WEB-INF/jetty-env.xml if (jettyEnvXmlResource == null) { - //look for a file called WEB-INF/jetty-env.xml - //and process it if it exists - org.eclipse.jetty.util.resource.Resource webInf = context.getWebInf(); - if (webInf != null && webInf.isDirectory()) - { - // TODO: should never return from WEB-INF/lib/foo.jar!/WEB-INF/jetty-env.xml - // TODO: should also never return from a META-INF/versions/#/WEB-INF/jetty-env.xml location - org.eclipse.jetty.util.resource.Resource jettyEnv = webInf.resolve("jetty-env.xml"); - if (Resources.isReadableFile(jettyEnv)) - { - jettyEnvXmlResource = jettyEnv; - } - } + //look for a configuration file + jettyEnvXmlResource = resolveJettyEnvXml(context.getWebInf()); } if (jettyEnvXmlResource != null) @@ -257,4 +247,38 @@ public class EnvConfiguration extends AbstractConfiguration Thread.currentThread().setContextClassLoader(oldLoader); } } + + /** + * Obtain a WEB-INF/jetty-ee9-env.xml, falling back to + * looking for WEB-INF/jetty-env.xml. + * + * @param webInf the WEB-INF of the context to search + * @return the file if it exists or null otherwise + */ + private Resource resolveJettyEnvXml(Resource webInf) + { + try + { + if (webInf == null || !webInf.isDirectory()) + return null; + + //try to find jetty-ee9-env.xml + Resource xmlResource = webInf.resolve(JETTY_EE9_ENV_XML); + if (!Resources.missing(xmlResource)) + return xmlResource; + + //failing that, look for jetty-env.xml + xmlResource = webInf.resolve(JETTY_ENV_XML); + if (!Resources.missing(xmlResource)) + return xmlResource; + + return null; + } + catch (Exception e) + { + if (LOG.isDebugEnabled()) + LOG.debug("Error resolving", e); + return null; + } + } } diff --git a/jetty-ee9/jetty-ee9-plus/src/test/java/org/eclipse/jetty/ee9/plus/webapp/EnvConfigurationTest.java b/jetty-ee9/jetty-ee9-plus/src/test/java/org/eclipse/jetty/ee9/plus/webapp/EnvConfigurationTest.java new file mode 100644 index 00000000000..c7feca819e8 --- /dev/null +++ b/jetty-ee9/jetty-ee9-plus/src/test/java/org/eclipse/jetty/ee9/plus/webapp/EnvConfigurationTest.java @@ -0,0 +1,77 @@ +// +// ======================================================================== +// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. +// +// This program and the accompanying materials are made available under the +// terms of the Eclipse Public License v. 2.0 which is available at +// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 +// which is available at https://www.apache.org/licenses/LICENSE-2.0. +// +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 +// ======================================================================== +// + +package org.eclipse.jetty.ee9.plus.webapp; + +import java.nio.file.Files; +import java.nio.file.Path; + +import org.eclipse.jetty.ee9.webapp.WebAppContext; +import org.eclipse.jetty.plus.jndi.NamingEntryUtil; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.toolchain.test.MavenTestingUtils; +import org.eclipse.jetty.util.jndi.NamingUtil; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class EnvConfigurationTest +{ + Server _server; + + @BeforeEach + public void setUp() + { + _server = new Server(); + } + + @AfterEach + public void tearDown() throws Exception + { + if (_server != null) + _server.stop(); + } + + @Test + public void testWithOnlyJettyWebXml() throws Exception + { + Path testWebappDir = MavenTestingUtils.getTargetPath("test-classes/webapp-with-jetty-env-xml"); + assertTrue(Files.exists(testWebappDir)); + + WebAppContext context = new WebAppContext(); + context.setContextPath("/"); + _server.setHandler(context); + context.setWar(testWebappDir.toFile().getAbsolutePath()); + _server.start(); + assertNotNull(NamingEntryUtil.lookupNamingEntry(context, "apricot")); + } + + @Test + public void testWithJettyEEWebXml() throws Exception + { + Path testWebappDir = MavenTestingUtils.getTargetPath("test-classes/webapp-with-jetty-ee9-env-xml"); + assertTrue(Files.exists(testWebappDir)); + + WebAppContext context = new WebAppContext(); + context.setContextPath("/"); + _server.setHandler(context); + context.setWar(testWebappDir.toFile().getAbsolutePath()); + _server.start(); + assertNotNull(NamingEntryUtil.lookupNamingEntry(context, "peach")); + assertNull(NamingEntryUtil.lookupNamingEntry(context, "cabbage")); + } +} diff --git a/jetty-ee9/jetty-ee9-plus/src/test/resources/webapp-with-jetty-ee9-env-xml/WEB-INF/jetty-ee9-env.xml b/jetty-ee9/jetty-ee9-plus/src/test/resources/webapp-with-jetty-ee9-env-xml/WEB-INF/jetty-ee9-env.xml new file mode 100644 index 00000000000..c0836565eb9 --- /dev/null +++ b/jetty-ee9/jetty-ee9-plus/src/test/resources/webapp-with-jetty-ee9-env-xml/WEB-INF/jetty-ee9-env.xml @@ -0,0 +1,15 @@ + + + + + + + + + peach + 100 + true + + + + diff --git a/jetty-ee9/jetty-ee9-plus/src/test/resources/webapp-with-jetty-ee9-env-xml/WEB-INF/jetty-ee99-env.xml b/jetty-ee9/jetty-ee9-plus/src/test/resources/webapp-with-jetty-ee9-env-xml/WEB-INF/jetty-ee99-env.xml new file mode 100644 index 00000000000..ee8b6917dbc --- /dev/null +++ b/jetty-ee9/jetty-ee9-plus/src/test/resources/webapp-with-jetty-ee9-env-xml/WEB-INF/jetty-ee99-env.xml @@ -0,0 +1,15 @@ + + + + + + + + + cabbage + 100 + true + + + + diff --git a/jetty-ee9/jetty-ee9-plus/src/test/resources/webapp-with-jetty-ee9-env-xml/WEB-INF/jetty-env.xml b/jetty-ee9/jetty-ee9-plus/src/test/resources/webapp-with-jetty-ee9-env-xml/WEB-INF/jetty-env.xml new file mode 100644 index 00000000000..0e0592571ce --- /dev/null +++ b/jetty-ee9/jetty-ee9-plus/src/test/resources/webapp-with-jetty-ee9-env-xml/WEB-INF/jetty-env.xml @@ -0,0 +1,15 @@ + + + + + + + + + apricot + 100 + true + + + + diff --git a/jetty-ee9/jetty-ee9-plus/src/test/resources/webapp-with-jetty-env-xml/WEB-INF/jetty-env.xml b/jetty-ee9/jetty-ee9-plus/src/test/resources/webapp-with-jetty-env-xml/WEB-INF/jetty-env.xml new file mode 100644 index 00000000000..0e0592571ce --- /dev/null +++ b/jetty-ee9/jetty-ee9-plus/src/test/resources/webapp-with-jetty-env-xml/WEB-INF/jetty-env.xml @@ -0,0 +1,15 @@ + + + + + + + + + apricot + 100 + true + + + +