From 1b8275785441aa9ffe10fd2a81487118b2647e66 Mon Sep 17 00:00:00 2001 From: Greg Wilkins Date: Tue, 21 May 2024 02:42:33 +0200 Subject: [PATCH] Fix Deprecated ClassMatcher #11767 (#11769) Fix #11767 by making deprecated ClassMatcher class wrap the util ClassMatcher Co-authored-by: Joakim Erdfelt --- .../org/eclipse/jetty/util/ClassMatcher.java | 21 ++++++++ .../jetty/ee10/webapp/ClassMatcher.java | 12 +++++ .../jetty/ee10/webapp/Configuration.java | 4 +- .../jetty/ee10/webapp/WebAppContext.java | 12 ++--- .../jetty/ee10/webapp/WebAppContextTest.java | 48 +++++++++++++------ .../jetty/ee9/webapp/WebAppContextTest.java | 10 +++- 6 files changed, 82 insertions(+), 25 deletions(-) diff --git a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/ClassMatcher.java b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/ClassMatcher.java index 2fe9fe754c8..4a17ed67e03 100644 --- a/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/ClassMatcher.java +++ b/jetty-core/jetty-util/src/main/java/org/eclipse/jetty/util/ClassMatcher.java @@ -525,6 +525,27 @@ public class ClassMatcher extends AbstractSet add(pattern); } + @Deprecated + protected interface Constructor + { + T construct(Map entries, IncludeExcludeSet patterns, IncludeExcludeSet locations); + } + + /** + * Wrap an instance of a {@link ClassMatcher} using a constructor of an extended {@code ClassMatcher} + * that needs access to the internal fields of the passed matcher. + * @param matcher The matcher to wrap + * @param constructor The constructor to build the API specific wrapper + * @param The type of the API specific wrapper + * @return A wrapper of the {@code matcher}, sharing internal state. + * @deprecated use {@link ClassMatcher} directly. + */ + @Deprecated + protected static T wrap(ClassMatcher matcher, Constructor constructor) + { + return constructor.construct(matcher._entries, matcher._patterns, matcher._locations); + } + public ClassMatcher asImmutable() { return new ClassMatcher(Map.copyOf(_entries), diff --git a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/ClassMatcher.java b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/ClassMatcher.java index 3eb611146b6..8e0146ff8df 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/ClassMatcher.java +++ b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/ClassMatcher.java @@ -61,4 +61,16 @@ public class ClassMatcher extends org.eclipse.jetty.util.ClassMatcher _patterns.asImmutable(), _locations.asImmutable()); } + + /** + * Create a {@link ClassMatcher webapp ClassMatcher} that wraps a {@link org.eclipse.jetty.util.ClassMatcher util ClassMatcher} + * for deprecated API usage. + * @param matcher The util {@link org.eclipse.jetty.util.ClassMatcher} to wrap + * @return A {@link ClassMatcher webapp ClassMatcher} + */ + static ClassMatcher wrap(org.eclipse.jetty.util.ClassMatcher matcher) + { + Constructor constructor = ClassMatcher::new; + return wrap(matcher, constructor); + } } diff --git a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/Configuration.java b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/Configuration.java index 1f172c43fe3..508afa46e3a 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/Configuration.java +++ b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/Configuration.java @@ -119,7 +119,7 @@ public interface Configuration @Deprecated(since = "12.0.8", forRemoval = true) default org.eclipse.jetty.ee10.webapp.ClassMatcher getSystemClasses() { - return new org.eclipse.jetty.ee10.webapp.ClassMatcher(getProtectedClasses()); + return org.eclipse.jetty.ee10.webapp.ClassMatcher.wrap(getProtectedClasses()); } /** @@ -128,7 +128,7 @@ public interface Configuration @Deprecated(since = "12.0.8", forRemoval = true) default org.eclipse.jetty.ee10.webapp.ClassMatcher getServerClasses() { - return new org.eclipse.jetty.ee10.webapp.ClassMatcher(getHiddenClasses()); + return org.eclipse.jetty.ee10.webapp.ClassMatcher.wrap(getHiddenClasses()); } /** diff --git a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/WebAppContext.java b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/WebAppContext.java index 53081d51dc3..65d8cbea3cf 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/WebAppContext.java +++ b/jetty-ee10/jetty-ee10-webapp/src/main/java/org/eclipse/jetty/ee10/webapp/WebAppContext.java @@ -100,14 +100,14 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL */ @Deprecated (forRemoval = true, since = "12.0.9") public static final org.eclipse.jetty.ee10.webapp.ClassMatcher __dftSystemClasses = - new org.eclipse.jetty.ee10.webapp.ClassMatcher(WebAppClassLoading.DEFAULT_PROTECTED_CLASSES); + org.eclipse.jetty.ee10.webapp.ClassMatcher.wrap(WebAppClassLoading.DEFAULT_PROTECTED_CLASSES); /** * @deprecated use {@link WebAppClassLoading#DEFAULT_HIDDEN_CLASSES} */ @Deprecated (forRemoval = true, since = "12.0.9") public static final org.eclipse.jetty.ee10.webapp.ClassMatcher __dftServerClasses = - new org.eclipse.jetty.ee10.webapp.ClassMatcher(WebAppClassLoading.DEFAULT_HIDDEN_CLASSES); + org.eclipse.jetty.ee10.webapp.ClassMatcher.wrap(WebAppClassLoading.DEFAULT_HIDDEN_CLASSES); private final ClassMatcher _protectedClasses = new ClassMatcher(WebAppClassLoading.getProtectedClasses(ServletContextHandler.ENVIRONMENT)); private final ClassMatcher _hiddenClasses = new ClassMatcher(WebAppClassLoading.getHiddenClasses(ServletContextHandler.ENVIRONMENT)); @@ -765,7 +765,7 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL @Deprecated(since = "12.0.8", forRemoval = true) public org.eclipse.jetty.ee10.webapp.ClassMatcher getSystemClassMatcher() { - return new org.eclipse.jetty.ee10.webapp.ClassMatcher(getProtectedClassMatcher()); + return org.eclipse.jetty.ee10.webapp.ClassMatcher.wrap(getProtectedClassMatcher()); } /** @@ -774,7 +774,7 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL @Deprecated(since = "12.0.8", forRemoval = true) public org.eclipse.jetty.ee10.webapp.ClassMatcher getServerClassMatcher() { - return new org.eclipse.jetty.ee10.webapp.ClassMatcher(getHiddenClassMatcher()); + return org.eclipse.jetty.ee10.webapp.ClassMatcher.wrap(getHiddenClassMatcher()); } /** @@ -1492,7 +1492,7 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL } /** - * Add a Server Class pattern to use for all ee9 WebAppContexts. + * Add a Server Class pattern to use for all WebAppContexts. * @param server The {@link Server} instance to add classes to * @param patterns the patterns to use * @see #getHiddenClassMatcher() @@ -1506,7 +1506,7 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL } /** - * Add a System Class pattern to use for all ee9 WebAppContexts. + * Add a System Class pattern to use for all WebAppContexts. * @param server The {@link Server} instance to add classes to * @param patterns the patterns to use * @see #getProtectedClassMatcher() diff --git a/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/WebAppContextTest.java b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/WebAppContextTest.java index 03b6cb835bf..3a7b38673c9 100644 --- a/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/WebAppContextTest.java +++ b/jetty-ee10/jetty-ee10-webapp/src/test/java/org/eclipse/jetty/ee10/webapp/WebAppContextTest.java @@ -940,13 +940,13 @@ public class WebAppContextTest } @Test - public void testAddServerClasses() throws Exception + public void testAddHiddenClasses() throws Exception { Server server = newServer(); String testPattern = "org.eclipse.jetty.ee10.webapp.test."; - WebAppContext.addServerClasses(server, testPattern); + WebAppClassLoading.addHiddenClasses(server, testPattern); WebAppContext context = new WebAppContext(); context.setContextPath("/"); @@ -957,25 +957,34 @@ public class WebAppContextTest Path warPath = createWar(testPath, "test.war"); context.setBaseResource(context.getResourceFactory().newResource(warPath)); + // Check context specific + context.getHiddenClassMatcher().add("org.context.specific."); + + // Check old API + context.getServerClassMatcher().add("org.deprecated.api."); + server.setHandler(context); server.start(); - List serverClasses = List.of(context.getHiddenClasses()); - assertThat("Should have environment specific test pattern", serverClasses, hasItem(testPattern)); - assertThat("Should have pattern from defaults", serverClasses, hasItem("org.eclipse.jetty.")); - assertThat("Should have pattern from JaasConfiguration", serverClasses, hasItem("-org.eclipse.jetty.security.jaas.")); + List hiddenClasses = List.of(context.getHiddenClasses()); + assertThat("Should have environment specific test pattern", hiddenClasses, hasItem(testPattern)); + assertThat("Should have pattern from defaults", hiddenClasses, hasItem("org.eclipse.jetty.")); + assertThat("Should have pattern from JaasConfiguration", hiddenClasses, hasItem("-org.eclipse.jetty.security.jaas.")); for (String defaultServerClass: WebAppClassLoading.DEFAULT_HIDDEN_CLASSES) - assertThat("Should have default patterns", serverClasses, hasItem(defaultServerClass)); + assertThat("Should have default patterns", hiddenClasses, hasItem(defaultServerClass)); + + assertThat("context API", hiddenClasses, hasItem("org.context.specific.")); + assertThat("deprecated API", hiddenClasses, hasItem("org.deprecated.api.")); } @Test - public void testAddSystemClasses() throws Exception + public void testAddProtectedClasses() throws Exception { Server server = newServer(); String testPattern = "org.eclipse.jetty.ee10.webapp.test."; - WebAppContext.addSystemClasses(server, testPattern); + WebAppClassLoading.addProtectedClasses(server, testPattern); WebAppContext context = new WebAppContext(); context.setContextPath("/"); @@ -985,15 +994,24 @@ public class WebAppContextTest Path warPath = createWar(testPath, "test.war"); context.setBaseResource(context.getResourceFactory().newResource(warPath)); + // Check context specific + context.getProtectedClassMatcher().add("org.context.specific."); + + // Check old API is a wrapper + context.getSystemClassMatcher().add("org.deprecated.api."); + server.setHandler(context); server.start(); - List systemClasses = List.of(context.getProtectedClasses()); - assertThat("Should have environment specific test pattern", systemClasses, hasItem(testPattern)); - assertThat("Should have pattern from defaults", systemClasses, hasItem("javax.")); - assertThat("Should have pattern from defaults", systemClasses, hasItem("jakarta.")); - assertThat("Should have pattern from JaasConfiguration", systemClasses, hasItem("org.eclipse.jetty.security.jaas.")); + List protectedClasses = List.of(context.getProtectedClasses()); + assertThat("Should have environment specific test pattern", protectedClasses, hasItem(testPattern)); + assertThat("Should have pattern from defaults", protectedClasses, hasItem("javax.")); + assertThat("Should have pattern from defaults", protectedClasses, hasItem("jakarta.")); + assertThat("Should have pattern from JaasConfiguration", protectedClasses, hasItem("org.eclipse.jetty.security.jaas.")); for (String defaultSystemClass: WebAppClassLoading.DEFAULT_PROTECTED_CLASSES) - assertThat("Should have default patterns", systemClasses, hasItem(defaultSystemClass)); + assertThat("Should have default patterns", protectedClasses, hasItem(defaultSystemClass)); + + assertThat("context API", protectedClasses, hasItem("org.context.specific.")); + assertThat("deprecated API", protectedClasses, hasItem("org.deprecated.api.")); } } diff --git a/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/WebAppContextTest.java b/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/WebAppContextTest.java index 9e9ee0bd01b..d186d6d7a48 100644 --- a/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/WebAppContextTest.java +++ b/jetty-ee9/jetty-ee9-webapp/src/test/java/org/eclipse/jetty/ee9/webapp/WebAppContextTest.java @@ -867,6 +867,9 @@ public class WebAppContextTest Path warPath = createWar(testPath, "test.war"); context.setBaseResource(context.getResourceFactory().newResource(warPath)); + // Add test for old/original API (replaced with getHiddenClassMatcher() in ee10) + context.getServerClassMatcher().add("org.deprecated.api."); + server.setHandler(context); server.start(); @@ -876,6 +879,7 @@ public class WebAppContextTest assertThat("Should have pattern from JaasConfiguration", serverClasses, hasItem("-org.eclipse.jetty.security.jaas.")); for (String defaultServerClass: WebAppClassLoading.DEFAULT_HIDDEN_CLASSES) assertThat("Should have default patterns", serverClasses, hasItem(defaultServerClass)); + assertThat("deprecated API", serverClasses, hasItem("org.deprecated.api.")); } @Test @@ -895,6 +899,9 @@ public class WebAppContextTest Path warPath = createWar(testPath, "test.war"); context.setBaseResource(context.getResourceFactory().newResource(warPath)); + // Add test for old/original API (replaced with getProtectedClassMatcher() in ee10) + context.getSystemClassMatcher().add("org.deprecated.api."); + server.setHandler(context); server.start(); @@ -904,8 +911,7 @@ public class WebAppContextTest assertThat("Should have pattern from defaults", systemClasses, hasItem("jakarta.")); assertThat("Should have pattern from JaasConfiguration", systemClasses, hasItem("org.eclipse.jetty.security.jaas.")); for (String defaultSystemClass : WebAppClassLoading.DEFAULT_PROTECTED_CLASSES) - { assertThat("Should have default patterns", systemClasses, hasItem(defaultSystemClass)); - } + assertThat("deprecated API", systemClasses, hasItem("org.deprecated.api.")); } }