Skip to content

Commit

Permalink
Fix Deprecated ClassMatcher #11767 (#11769)
Browse files Browse the repository at this point in the history
Fix #11767 by making deprecated ClassMatcher class wrap the util ClassMatcher

Co-authored-by: Joakim Erdfelt <[email protected]>
  • Loading branch information
gregw and joakime authored May 21, 2024
1 parent a1b3acb commit 1b82757
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,27 @@ public ClassMatcher(String 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()
{
return new ClassMatcher(Map.copyOf(_entries),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,16 @@ public ClassMatcher asImmutable()
_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<ClassMatcher> constructor = ClassMatcher::new;
return wrap(matcher, constructor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ default ClassMatcher getHiddenClasses()
@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());
}

/**
Expand All @@ -128,7 +128,7 @@ default org.eclipse.jetty.ee10.webapp.ClassMatcher getSystemClasses()
@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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -765,7 +765,7 @@ public void addSystemClassMatcher(org.eclipse.jetty.ee10.webapp.ClassMatcher sys
@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());
}

/**
Expand All @@ -774,7 +774,7 @@ public org.eclipse.jetty.ee10.webapp.ClassMatcher getSystemClassMatcher()
@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());
}

/**
Expand Down Expand Up @@ -1492,7 +1492,7 @@ public MetaData getMetaData()
}

/**
* 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()
Expand All @@ -1506,7 +1506,7 @@ public static void addServerClasses(Server server, String... patterns)
}

/**
* 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -940,13 +940,13 @@ public void testSetServerPropagation()
}

@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("/");
Expand All @@ -957,25 +957,34 @@ public void testAddServerClasses() throws Exception
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<String> 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<String> 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("/");
Expand All @@ -985,15 +994,24 @@ public void testAddSystemClasses() throws Exception
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<String> 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<String> 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."));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,9 @@ public void testAddServerClasses() throws Exception
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();

Expand All @@ -876,6 +879,7 @@ public void testAddServerClasses() throws Exception
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
Expand All @@ -895,6 +899,9 @@ public void testAddSystemClasses() throws Exception
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();

Expand All @@ -904,8 +911,7 @@ public void testAddSystemClasses() throws Exception
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."));
}
}

0 comments on commit 1b82757

Please sign in to comment.