|
40 | 40 | import java.util.Date;
|
41 | 41 | import java.util.EnumSet;
|
42 | 42 | import java.util.HashMap;
|
| 43 | +import java.util.HashSet; |
43 | 44 | import java.util.LinkedHashMap;
|
44 | 45 | import java.util.List;
|
45 | 46 | import java.util.Locale;
|
|
65 | 66 | import jakarta.servlet.FilterChain;
|
66 | 67 | import jakarta.servlet.FilterConfig;
|
67 | 68 | import jakarta.servlet.GenericServlet;
|
| 69 | +import jakarta.servlet.ServletConfig; |
68 | 70 | import jakarta.servlet.ServletContext;
|
69 | 71 | import jakarta.servlet.ServletContextEvent;
|
70 | 72 | import jakarta.servlet.ServletContextListener;
|
@@ -1382,6 +1384,26 @@ void startedLogMessageWithMultiplePorts() {
|
1382 | 1384 | + " \\(http(/1.1)?\\), [0-9]+ \\(http(/1.1)?\\)( with context path '(/)?')?");
|
1383 | 1385 | }
|
1384 | 1386 |
|
| 1387 | + @Test |
| 1388 | + void servletComponentsAreInitializedWithTheSameThreadContextClassLoader() { |
| 1389 | + AbstractServletWebServerFactory factory = getFactory(); |
| 1390 | + ThreadContextClassLoaderCapturingServlet servlet = new ThreadContextClassLoaderCapturingServlet(); |
| 1391 | + ThreadContextClassLoaderCapturingFilter filter = new ThreadContextClassLoaderCapturingFilter(); |
| 1392 | + ThreadContextClassLoaderCapturingListener listener = new ThreadContextClassLoaderCapturingListener(); |
| 1393 | + this.webServer = factory.getWebServer((context) -> { |
| 1394 | + context.addServlet("tcclCapturingServlet", servlet).setLoadOnStartup(0); |
| 1395 | + context.addFilter("tcclCapturingFilter", filter); |
| 1396 | + context.addListener(listener); |
| 1397 | + }); |
| 1398 | + this.webServer.start(); |
| 1399 | + assertThat(servlet.contextClassLoader).isNotNull(); |
| 1400 | + assertThat(filter.contextClassLoader).isNotNull(); |
| 1401 | + assertThat(listener.contextClassLoader).isNotNull(); |
| 1402 | + assertThat(new HashSet<>( |
| 1403 | + Arrays.asList(servlet.contextClassLoader, filter.contextClassLoader, listener.contextClassLoader))) |
| 1404 | + .hasSize(1); |
| 1405 | + } |
| 1406 | + |
1385 | 1407 | protected Future<Object> initiateGetRequest(int port, String path) {
|
1386 | 1408 | return initiateGetRequest(HttpClients.createMinimal(), port, path);
|
1387 | 1409 | }
|
@@ -1455,7 +1477,7 @@ private String setUpFactoryForCompression(int contentSize, String[] mimeTypes, S
|
1455 | 1477 | compression.setExcludedUserAgents(excludedUserAgents);
|
1456 | 1478 | }
|
1457 | 1479 | factory.setCompression(compression);
|
1458 |
| - factory.addInitializers(new ServletRegistrationBean<HttpServlet>(new HttpServlet() { |
| 1480 | + factory.addInitializers(new ServletRegistrationBean<>(new HttpServlet() { |
1459 | 1481 |
|
1460 | 1482 | @Override
|
1461 | 1483 | protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
|
@@ -1833,4 +1855,43 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
|
1833 | 1855 |
|
1834 | 1856 | }
|
1835 | 1857 |
|
| 1858 | + static class ThreadContextClassLoaderCapturingServlet extends HttpServlet { |
| 1859 | + |
| 1860 | + private ClassLoader contextClassLoader; |
| 1861 | + |
| 1862 | + @Override |
| 1863 | + public void init(ServletConfig config) throws ServletException { |
| 1864 | + this.contextClassLoader = Thread.currentThread().getContextClassLoader(); |
| 1865 | + } |
| 1866 | + |
| 1867 | + } |
| 1868 | + |
| 1869 | + static class ThreadContextClassLoaderCapturingListener implements ServletContextListener { |
| 1870 | + |
| 1871 | + private ClassLoader contextClassLoader; |
| 1872 | + |
| 1873 | + @Override |
| 1874 | + public void contextInitialized(ServletContextEvent sce) { |
| 1875 | + this.contextClassLoader = Thread.currentThread().getContextClassLoader(); |
| 1876 | + } |
| 1877 | + |
| 1878 | + } |
| 1879 | + |
| 1880 | + static class ThreadContextClassLoaderCapturingFilter implements Filter { |
| 1881 | + |
| 1882 | + private ClassLoader contextClassLoader; |
| 1883 | + |
| 1884 | + @Override |
| 1885 | + public void init(FilterConfig filterConfig) throws ServletException { |
| 1886 | + this.contextClassLoader = Thread.currentThread().getContextClassLoader(); |
| 1887 | + } |
| 1888 | + |
| 1889 | + @Override |
| 1890 | + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
| 1891 | + throws IOException, ServletException { |
| 1892 | + chain.doFilter(request, response); |
| 1893 | + } |
| 1894 | + |
| 1895 | + } |
| 1896 | + |
1836 | 1897 | }
|
0 commit comments