Fix Deprecated ClassMatcher #11767 (#11769)

Fix #11767 by making deprecated ClassMatcher class wrap the util ClassMatcher

Co-authored-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Greg Wilkins 2024-05-21 02:42:33 +02:00 committed by GitHub
parent a1b3acb08a
commit 1b82757854
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 82 additions and 25 deletions

View File

@ -525,6 +525,27 @@ public class ClassMatcher extends AbstractSet<String>
add(pattern); add(pattern);
} }
@Deprecated
protected interface Constructor<T extends ClassMatcher>
{
T construct(Map<String, Entry> entries, IncludeExcludeSet<Entry, String> patterns, IncludeExcludeSet<Entry, URI> 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 <T> 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 extends ClassMatcher> T wrap(ClassMatcher matcher, Constructor<T> constructor)
{
return constructor.construct(matcher._entries, matcher._patterns, matcher._locations);
}
public ClassMatcher asImmutable() public ClassMatcher asImmutable()
{ {
return new ClassMatcher(Map.copyOf(_entries), return new ClassMatcher(Map.copyOf(_entries),

View File

@ -61,4 +61,16 @@ public class ClassMatcher extends org.eclipse.jetty.util.ClassMatcher
_patterns.asImmutable(), _patterns.asImmutable(),
_locations.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<ClassMatcher> constructor = ClassMatcher::new;
return wrap(matcher, constructor);
}
} }

View File

@ -119,7 +119,7 @@ public interface Configuration
@Deprecated(since = "12.0.8", forRemoval = true) @Deprecated(since = "12.0.8", forRemoval = true)
default org.eclipse.jetty.ee10.webapp.ClassMatcher getSystemClasses() 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) @Deprecated(since = "12.0.8", forRemoval = true)
default org.eclipse.jetty.ee10.webapp.ClassMatcher getServerClasses() 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());
} }
/** /**

View File

@ -100,14 +100,14 @@ public class WebAppContext extends ServletContextHandler implements WebAppClassL
*/ */
@Deprecated (forRemoval = true, since = "12.0.9") @Deprecated (forRemoval = true, since = "12.0.9")
public static final org.eclipse.jetty.ee10.webapp.ClassMatcher __dftSystemClasses = 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 use {@link WebAppClassLoading#DEFAULT_HIDDEN_CLASSES}
*/ */
@Deprecated (forRemoval = true, since = "12.0.9") @Deprecated (forRemoval = true, since = "12.0.9")
public static final org.eclipse.jetty.ee10.webapp.ClassMatcher __dftServerClasses = 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 _protectedClasses = new ClassMatcher(WebAppClassLoading.getProtectedClasses(ServletContextHandler.ENVIRONMENT));
private final ClassMatcher _hiddenClasses = new ClassMatcher(WebAppClassLoading.getHiddenClasses(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) @Deprecated(since = "12.0.8", forRemoval = true)
public org.eclipse.jetty.ee10.webapp.ClassMatcher getSystemClassMatcher() 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) @Deprecated(since = "12.0.8", forRemoval = true)
public org.eclipse.jetty.ee10.webapp.ClassMatcher getServerClassMatcher() 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 server The {@link Server} instance to add classes to
* @param patterns the patterns to use * @param patterns the patterns to use
* @see #getHiddenClassMatcher() * @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 server The {@link Server} instance to add classes to
* @param patterns the patterns to use * @param patterns the patterns to use
* @see #getProtectedClassMatcher() * @see #getProtectedClassMatcher()

View File

@ -940,13 +940,13 @@ public class WebAppContextTest
} }
@Test @Test
public void testAddServerClasses() throws Exception public void testAddHiddenClasses() throws Exception
{ {
Server server = newServer(); Server server = newServer();
String testPattern = "org.eclipse.jetty.ee10.webapp.test."; String testPattern = "org.eclipse.jetty.ee10.webapp.test.";
WebAppContext.addServerClasses(server, testPattern); WebAppClassLoading.addHiddenClasses(server, testPattern);
WebAppContext context = new WebAppContext(); WebAppContext context = new WebAppContext();
context.setContextPath("/"); context.setContextPath("/");
@ -957,25 +957,34 @@ public class WebAppContextTest
Path warPath = createWar(testPath, "test.war"); Path warPath = createWar(testPath, "test.war");
context.setBaseResource(context.getResourceFactory().newResource(warPath)); 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.setHandler(context);
server.start(); server.start();
List<String> serverClasses = List.of(context.getHiddenClasses()); List<String> hiddenClasses = List.of(context.getHiddenClasses());
assertThat("Should have environment specific test pattern", serverClasses, hasItem(testPattern)); assertThat("Should have environment specific test pattern", hiddenClasses, hasItem(testPattern));
assertThat("Should have pattern from defaults", serverClasses, hasItem("org.eclipse.jetty.")); assertThat("Should have pattern from defaults", hiddenClasses, hasItem("org.eclipse.jetty."));
assertThat("Should have pattern from JaasConfiguration", serverClasses, hasItem("-org.eclipse.jetty.security.jaas.")); assertThat("Should have pattern from JaasConfiguration", hiddenClasses, hasItem("-org.eclipse.jetty.security.jaas."));
for (String defaultServerClass: WebAppClassLoading.DEFAULT_HIDDEN_CLASSES) 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 @Test
public void testAddSystemClasses() throws Exception public void testAddProtectedClasses() throws Exception
{ {
Server server = newServer(); Server server = newServer();
String testPattern = "org.eclipse.jetty.ee10.webapp.test."; String testPattern = "org.eclipse.jetty.ee10.webapp.test.";
WebAppContext.addSystemClasses(server, testPattern); WebAppClassLoading.addProtectedClasses(server, testPattern);
WebAppContext context = new WebAppContext(); WebAppContext context = new WebAppContext();
context.setContextPath("/"); context.setContextPath("/");
@ -985,15 +994,24 @@ public class WebAppContextTest
Path warPath = createWar(testPath, "test.war"); Path warPath = createWar(testPath, "test.war");
context.setBaseResource(context.getResourceFactory().newResource(warPath)); 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.setHandler(context);
server.start(); server.start();
List<String> systemClasses = List.of(context.getProtectedClasses()); List<String> protectedClasses = List.of(context.getProtectedClasses());
assertThat("Should have environment specific test pattern", systemClasses, hasItem(testPattern)); assertThat("Should have environment specific test pattern", protectedClasses, hasItem(testPattern));
assertThat("Should have pattern from defaults", systemClasses, hasItem("javax.")); assertThat("Should have pattern from defaults", protectedClasses, hasItem("javax."));
assertThat("Should have pattern from defaults", systemClasses, hasItem("jakarta.")); assertThat("Should have pattern from defaults", protectedClasses, hasItem("jakarta."));
assertThat("Should have pattern from JaasConfiguration", systemClasses, hasItem("org.eclipse.jetty.security.jaas.")); assertThat("Should have pattern from JaasConfiguration", protectedClasses, hasItem("org.eclipse.jetty.security.jaas."));
for (String defaultSystemClass: WebAppClassLoading.DEFAULT_PROTECTED_CLASSES) 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."));
} }
} }

View File

@ -867,6 +867,9 @@ public class WebAppContextTest
Path warPath = createWar(testPath, "test.war"); Path warPath = createWar(testPath, "test.war");
context.setBaseResource(context.getResourceFactory().newResource(warPath)); 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.setHandler(context);
server.start(); server.start();
@ -876,6 +879,7 @@ public class WebAppContextTest
assertThat("Should have pattern from JaasConfiguration", serverClasses, hasItem("-org.eclipse.jetty.security.jaas.")); assertThat("Should have pattern from JaasConfiguration", serverClasses, hasItem("-org.eclipse.jetty.security.jaas."));
for (String defaultServerClass: WebAppClassLoading.DEFAULT_HIDDEN_CLASSES) for (String defaultServerClass: WebAppClassLoading.DEFAULT_HIDDEN_CLASSES)
assertThat("Should have default patterns", serverClasses, hasItem(defaultServerClass)); assertThat("Should have default patterns", serverClasses, hasItem(defaultServerClass));
assertThat("deprecated API", serverClasses, hasItem("org.deprecated.api."));
} }
@Test @Test
@ -895,6 +899,9 @@ public class WebAppContextTest
Path warPath = createWar(testPath, "test.war"); Path warPath = createWar(testPath, "test.war");
context.setBaseResource(context.getResourceFactory().newResource(warPath)); 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.setHandler(context);
server.start(); server.start();
@ -904,8 +911,7 @@ public class WebAppContextTest
assertThat("Should have pattern from defaults", systemClasses, hasItem("jakarta.")); assertThat("Should have pattern from defaults", systemClasses, hasItem("jakarta."));
assertThat("Should have pattern from JaasConfiguration", systemClasses, hasItem("org.eclipse.jetty.security.jaas.")); assertThat("Should have pattern from JaasConfiguration", systemClasses, hasItem("org.eclipse.jetty.security.jaas."));
for (String defaultSystemClass : WebAppClassLoading.DEFAULT_PROTECTED_CLASSES) for (String defaultSystemClass : WebAppClassLoading.DEFAULT_PROTECTED_CLASSES)
{
assertThat("Should have default patterns", systemClasses, hasItem(defaultSystemClass)); assertThat("Should have default patterns", systemClasses, hasItem(defaultSystemClass));
} assertThat("deprecated API", systemClasses, hasItem("org.deprecated.api."));
} }
} }