Skip to content

Commit

Permalink
Jetty 12.0.x 11774 backport env web xml (#11966)
Browse files Browse the repository at this point in the history
* Jetty 12.0.x jetty ee web xml (PR #11746)
* Resolve jetty-eeX-web.xml before jetty-web.xml
* Support jetty-eeX-env.xml, as per jetty-eeX-web.xml
* Support jetty-eeX-env.xml, as per jetty-eeX-web.xml

---------

Co-authored-by: Greg Wilkins <[email protected]>
  • Loading branch information
janbartel and gregw authored Jun 27, 2024
1 parent 2caf9c1 commit 893911a
Show file tree
Hide file tree
Showing 28 changed files with 822 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -69,25 +71,12 @@ public void configure(WebAppContext context) throws Exception
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)
Expand Down Expand Up @@ -241,6 +230,40 @@ protected void createEnvContext(WebAppContext wac)
}
}

/**
* 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">

<Configure id='wac' class="org.eclipse.jetty.ee10.webapp.WebAppContext">

<!-- Add an EnvEntry only valid for this webapp -->
<New id="gargle" class="org.eclipse.jetty.plus.jndi.EnvEntry">
<Arg><Ref refid='wac'/></Arg>
<Arg>peach</Arg>
<Arg type="java.lang.Double">100</Arg>
<Arg type="boolean">true</Arg>
</New>

</Configure>

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">

<Configure id='wac' class="org.eclipse.jetty.ee10.webapp.WebAppContext">

<!-- Add an EnvEntry only valid for this webapp -->
<New id="gargle" class="org.eclipse.jetty.plus.jndi.EnvEntry">
<Arg><Ref refid='wac'/></Arg>
<Arg>cabbage</Arg>
<Arg type="java.lang.Double">100</Arg>
<Arg type="boolean">true</Arg>
</New>

</Configure>

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">

<Configure id='wac' class="org.eclipse.jetty.ee10.webapp.WebAppContext">

<!-- Add an EnvEntry only valid for this webapp -->
<New id="gargle" class="org.eclipse.jetty.plus.jndi.EnvEntry">
<Arg><Ref refid='wac'/></Arg>
<Arg>apricot</Arg>
<Arg type="java.lang.Double">100</Arg>
<Arg type="boolean">true</Arg>
</New>

</Configure>

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "https://www.eclipse.org/jetty/configure_10_0.dtd">

<Configure id='wac' class="org.eclipse.jetty.ee10.webapp.WebAppContext">

<!-- Add an EnvEntry only valid for this webapp -->
<New id="gargle" class="org.eclipse.jetty.plus.jndi.EnvEntry">
<Arg><Ref refid='wac'/></Arg>
<Arg>apricot</Arg>
<Arg type="java.lang.Double">100</Arg>
<Arg type="boolean">true</Arg>
</New>

</Configure>

Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class JettyWebXmlConfiguration extends AbstractConfiguration
public static final String PROPERTY_WEB_INF = "web-inf";
public static final String XML_CONFIGURATION = "org.eclipse.jetty.webapp.JettyWebXmlConfiguration";
public static final String JETTY_WEB_XML = "jetty-web.xml";
public static final String JETTY_EE10_WEB_XML = "jetty-ee10-web.xml";

public JettyWebXmlConfiguration()
{
Expand All @@ -55,44 +56,71 @@ public void configure(WebAppContext context) throws Exception
LOG.debug("Configuring web-jetty.xml");

Resource webInf = context.getWebInf();
// handle any WEB-INF descriptors
if (webInf != null && webInf.isDirectory())
// get the jetty-ee10-web.xml or jetty-web.xml
Resource jetty = resolveJettyWebXml(webInf);
if (Resources.isReadableFile(jetty))
{
// Attempt to load ancient jetty8-web.xml file
Resource jetty = webInf.resolve("jetty8-web.xml");
if (Resources.missing(jetty))
jetty = webInf.resolve(JETTY_WEB_XML);
if (Resources.missing(jetty))
jetty = webInf.resolve("web-jetty.xml");

if (Resources.isReadableFile(jetty))
{
if (LOG.isDebugEnabled())
LOG.debug("Configure: {}", jetty);
if (LOG.isDebugEnabled())
LOG.debug("Configure: {}", jetty);

Object xmlAttr = context.getAttribute(XML_CONFIGURATION);
context.removeAttribute(XML_CONFIGURATION);
final XmlConfiguration jetty_config = xmlAttr instanceof XmlConfiguration ? (XmlConfiguration)xmlAttr : new XmlConfiguration(jetty);
Object xmlAttr = context.getAttribute(XML_CONFIGURATION);
context.removeAttribute(XML_CONFIGURATION);
final XmlConfiguration jetty_config = xmlAttr instanceof XmlConfiguration ? (XmlConfiguration)xmlAttr : new XmlConfiguration(jetty);

setupXmlConfiguration(context, jetty_config, webInf);
setupXmlConfiguration(context, jetty_config, webInf);

try
{
WebAppClassLoader.runWithServerClassAccess(() ->
{
jetty_config.configure(context);
return null;
});
}
catch (Exception e)
try
{
WebAppClassLoader.runWithServerClassAccess(() ->
{
LOG.warn("Error applying {}", jetty);
throw e;
}
jetty_config.configure(context);
return null;
});
}
catch (Exception e)
{
LOG.warn("Error applying {}", jetty);
throw e;
}
}
}

/**
* Obtain a WEB-INF/jetty-ee9-web.xml, falling back to
* looking for WEB-INF/jetty-web.xml.
*
* @param webInf the WEB-INF of the context to search
* @return the file if it exists or null otherwise
*/
private Resource resolveJettyWebXml(Resource webInf)
{
String xmlFile = JETTY_EE10_WEB_XML;
try
{
if (webInf == null || !webInf.isDirectory())
return null;

//try to find jetty-ee10-web.xml
Resource jetty = webInf.resolve(xmlFile);
if (!Resources.missing(jetty))
return jetty;

xmlFile = JETTY_WEB_XML;
//failing that, look for jetty-web.xml
jetty = webInf.resolve(xmlFile);
if (!Resources.missing(jetty))
return jetty;

return null;
}
catch (Exception e)
{
if (LOG.isDebugEnabled())
LOG.debug("Error resolving WEB-INF/" + xmlFile, e);
return null;
}
}

/**
* Configures some well-known properties before the XmlConfiguration reads
* the configuration.
Expand Down
Loading

0 comments on commit 893911a

Please sign in to comment.