Skip to content

Commit

Permalink
Protect against NoSuchMethodException on setReadOnly
Browse files Browse the repository at this point in the history
  • Loading branch information
philwebb committed Feb 19, 2025
1 parent 69abb7d commit c718461
Showing 1 changed file with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ protected void prepareContext(Host host, ServletContextInitializer[] initializer
TomcatEmbeddedContext context = new TomcatEmbeddedContext();
WebResourceRoot resourceRoot = (documentRoot != null) ? new LoaderHidingResourceRoot(context)
: new StandardRoot(context);
resourceRoot.setReadOnly(true);
ignoringNoSuchMethodError(() -> resourceRoot.setReadOnly(true));
context.setResources(resourceRoot);
context.setName(getContextPath());
context.setDisplayName(getDisplayName());
Expand All @@ -253,12 +253,7 @@ protected void prepareContext(Host host, ServletContextInitializer[] initializer
context.setParentClassLoader(parentClassLoader);
resetDefaultLocaleMapping(context);
addLocaleMappings(context);
try {
context.setCreateUploadTargets(true);
}
catch (NoSuchMethodError ex) {
// Tomcat is < 8.5.39. Continue.
}
ignoringNoSuchMethodError(() -> context.setCreateUploadTargets(true));
configureTldPatterns(context);
WebappLoader loader = new WebappLoader();
loader.setLoaderInstance(new TomcatEmbeddedWebappClassLoader(parentClassLoader));
Expand All @@ -278,6 +273,14 @@ protected void prepareContext(Host host, ServletContextInitializer[] initializer
postProcessContext(context);
}

private void ignoringNoSuchMethodError(Runnable method) {
try {
method.run();
}
catch (NoSuchMethodError ex) {
}
}

/**
* Override Tomcat's default locale mappings to align with other servers. See
* {@code org.apache.catalina.util.CharsetMapperDefault.properties}.
Expand Down

0 comments on commit c718461

Please sign in to comment.