Issue #11514 - Cleanup `jetty.webapp.addServerClasses` property behavior for ee10/ee9/ee8

This commit is contained in:
Joakim Erdfelt 2024-03-13 12:02:20 -05:00
parent 369d9f7e2f
commit c8c6dad752
No known key found for this signature in database
GPG Key ID: 2D0E1FB8FE4B68B4
12 changed files with 151 additions and 38 deletions

View File

@ -158,7 +158,7 @@ public class ResourceAnnotationHandler extends AbstractIntrospectableAnnotationH
//try environment scope next
if (!bound)
bound = NamingEntryUtil.bindToENC(ServletContextHandler.__environment.getName(), name, mappedName);
bound = NamingEntryUtil.bindToENC(ServletContextHandler.ENVIRONMENT.getName(), name, mappedName);
//try Server scope next
if (!bound)
@ -313,7 +313,7 @@ public class ResourceAnnotationHandler extends AbstractIntrospectableAnnotationH
//try the environment's scope
if (!bound)
bound = NamingEntryUtil.bindToENC(ServletContextHandler.__environment.getName(), name, mappedName);
bound = NamingEntryUtil.bindToENC(ServletContextHandler.ENVIRONMENT.getName(), name, mappedName);
//try the server's scope
if (!bound)

View File

@ -13,7 +13,6 @@
package org.eclipse.jetty.ee10.plus.webapp;
import java.util.Map;
import java.util.Set;
import javax.naming.Context;
import javax.naming.InitialContext;
@ -208,8 +207,8 @@ public class EnvConfiguration extends AbstractConfiguration
LOG.debug("Binding env entries from the server scope");
doBindings(envCtx, context.getServer());
LOG.debug("Binding env entries from environment {} scope", ServletContextHandler.__environment.getName());
doBindings(envCtx, ServletContextHandler.__environment.getName());
LOG.debug("Binding env entries from environment {} scope", ServletContextHandler.ENVIRONMENT.getName());
doBindings(envCtx, ServletContextHandler.ENVIRONMENT.getName());
LOG.debug("Binding env entries from the context scope");
doBindings(envCtx, context);

View File

@ -83,13 +83,13 @@ public class PlusConfiguration extends AbstractConfiguration
{
try
{
Transaction.bindTransactionToENC(ServletContextHandler.__environment.getName());
Transaction.bindTransactionToENC(ServletContextHandler.ENVIRONMENT.getName());
}
catch (NameNotFoundException e)
{
try
{
org.eclipse.jetty.plus.jndi.Transaction.bindTransactionToENC(ServletContextHandler.__environment.getName());
org.eclipse.jetty.plus.jndi.Transaction.bindTransactionToENC(ServletContextHandler.ENVIRONMENT.getName());
}
catch (NameNotFoundException x)
{

View File

@ -130,7 +130,12 @@ import static jakarta.servlet.ServletContext.TEMPDIR;
public class ServletContextHandler extends ContextHandler
{
private static final Logger LOG = LoggerFactory.getLogger(ServletContextHandler.class);
public static final Environment __environment = Environment.ensure("ee10");
public static final Environment ENVIRONMENT = Environment.ensure("ee10");
/**
* @deprecated Use {@link ServletContextHandler#ENVIRONMENT} instead. will be removed in Jetty 12.1.0.
*/
@Deprecated(since = "12.0.8", forRemoval = true)
public static final Environment __environment = ENVIRONMENT;
public static final Class<?>[] SERVLET_LISTENER_TYPES =
{
ServletContextListener.class,

View File

@ -3,7 +3,6 @@
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Call class="org.eclipse.jetty.ee10.webapp.WebAppContext" name="addSystemClasses">
<Arg><Ref refid="Server"/></Arg>
<Arg>
<Call class="org.eclipse.jetty.util.StringUtil" name="csvSplit">
<Arg><Property name="jetty.webapp.addSystemClasses"/></Arg>
@ -12,7 +11,6 @@
</Call>
<Call class="org.eclipse.jetty.ee10.webapp.WebAppContext" name="addServerClasses">
<Arg><Ref refid="Server"/></Arg>
<Arg>
<Call class="org.eclipse.jetty.util.StringUtil" name="csvSplit">
<Arg><Property name="jetty.webapp.addServerClasses"/></Arg>

View File

@ -486,7 +486,7 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
protected void doStart() throws Exception
{
ClassLoader old = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(__environment.getClassLoader());
Thread.currentThread().setContextClassLoader(ServletContextHandler.ENVIRONMENT.getClassLoader());
try
{
_metadata.setAllowDuplicateFragmentNames(isAllowDuplicateFragmentNames());
@ -728,7 +728,7 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
{
if (__dftSystemClasses.equals(_systemClasses))
{
Object systemClasses = server.getAttribute(SERVER_SYS_CLASSES);
Object systemClasses = ServletContextHandler.ENVIRONMENT.getAttribute(SERVER_SYS_CLASSES);
if (systemClasses instanceof String[])
systemClasses = new ClassMatcher((String[])systemClasses);
if (systemClasses instanceof ClassMatcher)
@ -737,7 +737,7 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
if (__dftServerClasses.equals(_serverClasses))
{
Object serverClasses = server.getAttribute(SERVER_SRV_CLASSES);
Object serverClasses = ServletContextHandler.ENVIRONMENT.getAttribute(SERVER_SRV_CLASSES);
if (serverClasses instanceof String[])
serverClasses = new ClassMatcher((String[])serverClasses);
if (serverClasses instanceof ClassMatcher)
@ -1383,24 +1383,58 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
return _metadata;
}
public static void addServerClasses(Server server, String... pattern)
/**
* @param server ignored.
* @param patterns the patterns to add
* @deprecated use {@link #addServerClasses(String...)} instead, will be removed in Jetty 12.1.0
*/
@Deprecated(since = "12.0.8", forRemoval = true)
public static void addServerClasses(Server server, String... patterns)
{
addClasses(__dftServerClasses, SERVER_SRV_CLASSES, server, pattern);
addServerClasses(patterns);
}
public static void addSystemClasses(Server server, String... pattern)
/**
* Add a Server Class pattern to use for all ee10 WebAppContexts.
* @param patterns the patterns to use
* @see #getServerClassMatcher()
* @see #getServerClasses()
*/
public static void addServerClasses(String... patterns)
{
addClasses(__dftSystemClasses, SERVER_SYS_CLASSES, server, pattern);
addClasses(__dftServerClasses, SERVER_SRV_CLASSES, patterns);
}
private static void addClasses(ClassMatcher matcher, String attribute, Server server, String... pattern)
/**
* @param server ignored.
* @param patterns the patterns to add
* @deprecated use {@link #addSystemClasses(String...)} instead, will be removed in Jetty 12.1.0
*/
@Deprecated(since = "12.0.8", forRemoval = true)
public static void addSystemClasses(Server server, String... patterns)
{
addSystemClasses(patterns);
}
/**
* Add a System Class pattern to use for all ee10 WebAppContexts.
* @param patterns the patterns to use
* @see #getSystemClassMatcher()
* @see #getSystemClasses()
*/
public static void addSystemClasses(String... patterns)
{
addClasses(__dftSystemClasses, SERVER_SYS_CLASSES, patterns);
}
private static void addClasses(ClassMatcher matcher, String attributeKey, String... pattern)
{
if (pattern == null || pattern.length == 0)
return;
// look for a Server attribute with the list of System classes
// to apply to every web application. If not present, use our defaults.
Object o = server.getAttribute(attribute);
// look for a ClassMatcher attribute with the list of Server / System classes
// to apply to every ee10 web application. If not present, use our defaults.
Object o = ServletContextHandler.ENVIRONMENT.getAttribute(attributeKey);
if (o instanceof ClassMatcher)
{
((ClassMatcher)o).add(pattern);
@ -1415,6 +1449,6 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
int l = classes.length;
classes = Arrays.copyOf(classes, l + pattern.length);
System.arraycopy(pattern, 0, classes, l, pattern.length);
server.setAttribute(attribute, classes);
ServletContextHandler.ENVIRONMENT.setAttribute(attributeKey, classes);
}
}

View File

@ -95,7 +95,7 @@ public class WebDescriptor extends Descriptor
public WebDescriptorParser(boolean validating) throws IOException
{
super(validating);
String catalogName = "catalog-%s.xml".formatted(ServletContextHandler.__environment.getName());
String catalogName = "catalog-%s.xml".formatted(ServletContextHandler.ENVIRONMENT.getName());
URL url = WebDescriptor.class.getResource(catalogName);
if (url == null)
throw new IllegalStateException("Catalog not found: %s/%s".formatted(WebDescriptor.class.getPackageName(), catalogName));

View File

@ -43,6 +43,7 @@ import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.MavenPaths;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDir;
import org.eclipse.jetty.toolchain.test.jupiter.WorkDirExtension;
@ -72,6 +73,7 @@ import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.either;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
@ -802,4 +804,26 @@ public class WebAppContextTest
assertThat(handler.getServer(), sameInstance(server));
}
@Test
public void testAddServerClasses() throws Exception
{
Server server = newServer();
String testPattern = "org.eclipse.jetty.ee10.webapp.test.";
WebAppContext.addServerClasses(server, testPattern);
WebAppContext context = new WebAppContext();
context.setContextPath("/");
Path warPath = MavenPaths.findTestResourceFile("wars/dump.war");
context.setBaseResource(context.getResourceFactory().newResource(warPath));
server.setHandler(context);
server.start();
List<String> serverClasses = List.of(context.getServerClasses());
assertThat(serverClasses, hasItem(testPattern));
}
}

View File

@ -3,7 +3,6 @@
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Call class="org.eclipse.jetty.ee8.webapp.WebAppContext" name="addSystemClasses">
<Arg><Ref refid="Environment"/></Arg>
<Arg>
<Call class="org.eclipse.jetty.util.StringUtil" name="csvSplit">
<Arg><Property name="jetty.webapp.addSystemClasses"/></Arg>
@ -12,7 +11,6 @@
</Call>
<Call class="org.eclipse.jetty.ee8.webapp.WebAppContext" name="addServerClasses">
<Arg><Ref refid="Environment"/></Arg>
<Arg>
<Call class="org.eclipse.jetty.util.StringUtil" name="csvSplit">
<Arg><Property name="jetty.webapp.addServerClasses"/></Arg>

View File

@ -3,7 +3,6 @@
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Call class="org.eclipse.jetty.ee9.webapp.WebAppContext" name="addSystemClasses">
<Arg><Ref refid="Environment"/></Arg>
<Arg>
<Call class="org.eclipse.jetty.util.StringUtil" name="csvSplit">
<Arg><Property name="jetty.webapp.addSystemClasses"/></Arg>
@ -12,7 +11,6 @@
</Call>
<Call class="org.eclipse.jetty.ee9.webapp.WebAppContext" name="addServerClasses">
<Arg><Ref refid="Environment"/></Arg>
<Arg>
<Call class="org.eclipse.jetty.util.StringUtil" name="csvSplit">
<Arg><Property name="jetty.webapp.addServerClasses"/></Arg>

View File

@ -780,7 +780,7 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
{
if (__dftSystemClasses.equals(_systemClasses))
{
Object systemClasses = server.getAttribute(SERVER_SYS_CLASSES);
Object systemClasses = ServletContextHandler.ENVIRONMENT.getAttribute(SERVER_SYS_CLASSES);
if (systemClasses instanceof String[])
systemClasses = new ClassMatcher((String[])systemClasses);
if (systemClasses instanceof ClassMatcher)
@ -789,7 +789,7 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
if (__dftServerClasses.equals(_serverClasses))
{
Object serverClasses = server.getAttribute(SERVER_SRV_CLASSES);
Object serverClasses = ServletContextHandler.ENVIRONMENT.getAttribute(SERVER_SRV_CLASSES);
if (serverClasses instanceof String[])
serverClasses = new ClassMatcher((String[])serverClasses);
if (serverClasses instanceof ClassMatcher)
@ -1472,24 +1472,58 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
return _metadata;
}
public static void addServerClasses(Attributes attributes, String... pattern)
/**
* Add a Server Class pattern to use for all ee9 WebAppContexts.
* @param patterns the patterns to use
* @see #getServerClassMatcher()
* @see #getServerClasses()
*/
public static void addServerClasses(String... patterns)
{
addClasses(__dftServerClasses, SERVER_SRV_CLASSES, attributes, pattern);
addClasses(__dftServerClasses, SERVER_SRV_CLASSES, patterns);
}
public static void addSystemClasses(Attributes attributes, String... pattern)
/**
* @param attributes ignored.
* @param patterns the patterns to add
* @deprecated use {@link #addServerClasses(String...)} instead. will be removed in Jetty 12.1.0
*/
@Deprecated(since = "12.0.8", forRemoval = true)
public static void addServerClasses(Attributes attributes, String... patterns)
{
addClasses(__dftSystemClasses, SERVER_SYS_CLASSES, attributes, pattern);
addServerClasses(patterns);
}
private static void addClasses(ClassMatcher matcher, String attribute, Attributes attributes, String... pattern)
/**
* Add a System Class pattern to use for all ee9 WebAppContexts.
* @param patterns the patterns to use
* @see #getSystemClassMatcher()
* @see #getSystemClasses()
*/
public static void addSystemClasses(String... patterns)
{
addClasses(__dftSystemClasses, SERVER_SYS_CLASSES, patterns);
}
/**
* @param attributes ignored.
* @param patterns the patterns to add
* @deprecated use {@link #addSystemClasses(String...)} instead. will be removed in Jetty 12.1.0
*/
@Deprecated(since = "12.0.8", forRemoval = true)
public static void addSystemClasses(Attributes attributes, String... patterns)
{
addSystemClasses(patterns);
}
private static void addClasses(ClassMatcher matcher, String attributeKey, String... pattern)
{
if (pattern == null || pattern.length == 0)
return;
// look for a Server attribute with the list of System classes
// to apply to every web application. If not present, use our defaults.
Object o = attributes.getAttribute(attribute);
// look for a ClassMatcher attribute with the list of Server / System classes
// to apply to every ee9 web application. If not present, use our defaults.
Object o = ServletContextHandler.ENVIRONMENT.getAttribute(attributeKey);
if (o instanceof ClassMatcher)
{
((ClassMatcher)o).add(pattern);
@ -1504,6 +1538,6 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
int l = classes.length;
classes = Arrays.copyOf(classes, l + pattern.length);
System.arraycopy(pattern, 0, classes, l, pattern.length);
attributes.setAttribute(attribute, classes);
ServletContextHandler.ENVIRONMENT.setAttribute(attributeKey, classes);
}
}

View File

@ -74,6 +74,7 @@ import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
@ -804,4 +805,26 @@ public class WebAppContextTest
extLibs = extLibs.toAbsolutePath();
assertThat("URL[0]", urls[0].toURI(), is(extLibs.toUri()));
}
@Test
public void testAddServerClasses() throws Exception
{
Server server = newServer();
String testPattern = "org.eclipse.jetty.ee9.webapp.test.";
WebAppContext.addServerClasses(testPattern);
WebAppContext context = new WebAppContext();
context.setContextPath("/");
Path warPath = MavenPaths.findTestResourceFile("wars/dump.war");
context.setBaseResource(context.getResourceFactory().newResource(warPath));
server.setHandler(context);
server.start();
List<String> serverClasses = List.of(context.getServerClasses());
assertThat(serverClasses, hasItem(testPattern));
}
}