Skip to content

Commit 598ee98

Browse files
committed
Add nullability annotations to module/spring-boot-tomcat
See gh-46587
1 parent e27009d commit 598ee98

22 files changed

+130
-85
lines changed

module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/CompressionConnectorCustomizer.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.apache.catalina.connector.Connector;
2020
import org.apache.coyote.ProtocolHandler;
2121
import org.apache.coyote.http11.AbstractHttp11Protocol;
22+
import org.jspecify.annotations.Nullable;
2223

2324
import org.springframework.boot.web.server.Compression;
2425
import org.springframework.util.StringUtils;
@@ -32,9 +33,9 @@
3233
*/
3334
public class CompressionConnectorCustomizer implements TomcatConnectorCustomizer {
3435

35-
private final Compression compression;
36+
private final @Nullable Compression compression;
3637

37-
public CompressionConnectorCustomizer(Compression compression) {
38+
public CompressionConnectorCustomizer(@Nullable Compression compression) {
3839
this.compression = compression;
3940
}
4041

@@ -43,18 +44,17 @@ public void customize(Connector connector) {
4344
if (this.compression != null && this.compression.getEnabled()) {
4445
ProtocolHandler handler = connector.getProtocolHandler();
4546
if (handler instanceof AbstractHttp11Protocol<?> abstractHttp11Protocol) {
46-
customize(abstractHttp11Protocol);
47+
customize(abstractHttp11Protocol, this.compression);
4748
}
4849
}
4950
}
5051

51-
private void customize(AbstractHttp11Protocol<?> protocol) {
52-
Compression compression = this.compression;
52+
private void customize(AbstractHttp11Protocol<?> protocol, Compression compression) {
5353
protocol.setCompression("on");
5454
protocol.setCompressionMinSize(getMinResponseSize(compression));
5555
protocol.setCompressibleMimeType(getMimeTypes(compression));
56-
if (this.compression.getExcludedUserAgents() != null) {
57-
protocol.setNoCompressionUserAgents(getExcludedUserAgents());
56+
if (compression.getExcludedUserAgents() != null) {
57+
protocol.setNoCompressionUserAgents(getExcludedUserAgents(compression));
5858
}
5959
}
6060

@@ -66,8 +66,8 @@ private String getMimeTypes(Compression compression) {
6666
return StringUtils.arrayToCommaDelimitedString(compression.getMimeTypes());
6767
}
6868

69-
private String getExcludedUserAgents() {
70-
return StringUtils.arrayToCommaDelimitedString(this.compression.getExcludedUserAgents());
69+
private String getExcludedUserAgents(Compression compression) {
70+
return StringUtils.arrayToCommaDelimitedString(compression.getExcludedUserAgents());
7171
}
7272

7373
}

module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/ConfigurableTomcatWebServerFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.catalina.Engine;
2424
import org.apache.catalina.Valve;
2525
import org.apache.catalina.connector.Connector;
26+
import org.jspecify.annotations.Nullable;
2627

2728
import org.springframework.boot.tomcat.reactive.TomcatReactiveWebServerFactory;
2829
import org.springframework.boot.tomcat.servlet.TomcatServletWebServerFactory;
@@ -42,7 +43,7 @@ public interface ConfigurableTomcatWebServerFactory extends ConfigurableWebServe
4243
* Set the Tomcat base directory. If not specified a temporary directory will be used.
4344
* @param baseDirectory the tomcat base directory
4445
*/
45-
void setBaseDirectory(File baseDirectory);
46+
void setBaseDirectory(@Nullable File baseDirectory);
4647

4748
/**
4849
* Sets the background processor delay in seconds.

module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/SslConnectorCustomizer.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.tomcat.util.net.SSLHostConfig;
2626
import org.apache.tomcat.util.net.SSLHostConfigCertificate;
2727
import org.apache.tomcat.util.net.SSLHostConfigCertificate.Type;
28+
import org.jspecify.annotations.Nullable;
2829

2930
import org.springframework.boot.ssl.SslBundle;
3031
import org.springframework.boot.ssl.SslBundleKey;
@@ -48,17 +49,17 @@ public class SslConnectorCustomizer {
4849

4950
private final Log logger;
5051

51-
private final ClientAuth clientAuth;
52+
private final @Nullable ClientAuth clientAuth;
5253

5354
private final Connector connector;
5455

55-
public SslConnectorCustomizer(Log logger, Connector connector, ClientAuth clientAuth) {
56+
public SslConnectorCustomizer(Log logger, Connector connector, @Nullable ClientAuth clientAuth) {
5657
this.logger = logger;
5758
this.clientAuth = clientAuth;
5859
this.connector = connector;
5960
}
6061

61-
public void update(String serverName, SslBundle updatedSslBundle) {
62+
public void update(@Nullable String serverName, SslBundle updatedSslBundle) {
6263
AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol<?>) this.connector.getProtocolHandler();
6364
String host = (serverName != null) ? serverName : protocol.getDefaultSSLHostConfigName();
6465
this.logger.debug("SSL Bundle for host " + host + " has been updated, reloading SSL configuration");
@@ -80,7 +81,7 @@ public void customize(SslBundle sslBundle, Map<String, SslBundle> serverNameSslB
8081
* @param sslBundle the SSL bundle
8182
* @param serverNameSslBundles the SSL bundles for specific SNI host names
8283
*/
83-
private void configureSsl(AbstractHttp11Protocol<?> protocol, SslBundle sslBundle,
84+
private void configureSsl(AbstractHttp11Protocol<?> protocol, @Nullable SslBundle sslBundle,
8485
Map<String, SslBundle> serverNameSslBundles) {
8586
protocol.setSSLEnabled(true);
8687
if (sslBundle != null) {

module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/TomcatEmbeddedContext.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.catalina.core.StandardContext;
3131
import org.apache.catalina.core.StandardWrapper;
3232
import org.apache.catalina.session.ManagerBase;
33+
import org.jspecify.annotations.Nullable;
3334

3435
import org.springframework.boot.web.server.MimeMappings;
3536
import org.springframework.boot.web.server.WebServerException;
@@ -45,9 +46,9 @@
4546
*/
4647
public class TomcatEmbeddedContext extends StandardContext {
4748

48-
private TomcatStarter starter;
49+
private @Nullable TomcatStarter starter;
4950

50-
private MimeMappings mimeMappings;
51+
private @Nullable MimeMappings mimeMappings;
5152

5253
@Override
5354
public boolean loadOnStartup(Container[] children) {
@@ -102,7 +103,7 @@ private void load(Wrapper wrapper) {
102103
* @param classLoader the class loader to use
103104
* @param code the code to run
104105
*/
105-
private void doWithThreadContextClassLoader(ClassLoader classLoader, Runnable code) {
106+
private void doWithThreadContextClassLoader(@Nullable ClassLoader classLoader, Runnable code) {
106107
ClassLoader existingLoader = (classLoader != null) ? ClassUtils.overrideThreadContextClassLoader(classLoader)
107108
: null;
108109
try {
@@ -115,11 +116,11 @@ private void doWithThreadContextClassLoader(ClassLoader classLoader, Runnable co
115116
}
116117
}
117118

118-
public void setStarter(TomcatStarter starter) {
119+
public void setStarter(@Nullable TomcatStarter starter) {
119120
this.starter = starter;
120121
}
121122

122-
TomcatStarter getStarter() {
123+
@Nullable TomcatStarter getStarter() {
123124
return this.starter;
124125
}
125126

@@ -137,7 +138,7 @@ public String[] findMimeMappings() {
137138
}
138139

139140
@Override
140-
public String findMimeMapping(String extension) {
141+
public @Nullable String findMimeMapping(String extension) {
141142
String mimeMapping = super.findMimeMapping(extension);
142143
if (mimeMapping != null) {
143144
return mimeMapping;

module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/TomcatEmbeddedWebappClassLoader.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.commons.logging.Log;
2626
import org.apache.commons.logging.LogFactory;
2727
import org.apache.tomcat.util.compat.JreCompat;
28+
import org.jspecify.annotations.Nullable;
2829

2930
/**
3031
* Extension of Tomcat's {@link ParallelWebappClassLoader} that does not consider the
@@ -49,12 +50,12 @@ public class TomcatEmbeddedWebappClassLoader extends ParallelWebappClassLoader {
4950
public TomcatEmbeddedWebappClassLoader() {
5051
}
5152

52-
public TomcatEmbeddedWebappClassLoader(ClassLoader parent) {
53+
public TomcatEmbeddedWebappClassLoader(@Nullable ClassLoader parent) {
5354
super(parent);
5455
}
5556

5657
@Override
57-
public URL findResource(String name) {
58+
public @Nullable URL findResource(String name) {
5859
return null;
5960
}
6061

@@ -75,13 +76,13 @@ public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundExce
7576
}
7677
}
7778

78-
private Class<?> findExistingLoadedClass(String name) {
79+
private @Nullable Class<?> findExistingLoadedClass(String name) {
7980
Class<?> resultClass = findLoadedClass0(name);
8081
resultClass = (resultClass != null || JreCompat.isGraalAvailable()) ? resultClass : findLoadedClass(name);
8182
return resultClass;
8283
}
8384

84-
private Class<?> doLoadClass(String name) {
85+
private @Nullable Class<?> doLoadClass(String name) {
8586
if ((this.delegate || filter(name, true))) {
8687
Class<?> result = loadFromParent(name);
8788
return (result != null) ? result : findClassIgnoringNotFound(name);
@@ -105,7 +106,7 @@ protected void addURL(URL url) {
105106
}
106107
}
107108

108-
private Class<?> loadFromParent(String name) {
109+
private @Nullable Class<?> loadFromParent(String name) {
109110
if (this.parent == null) {
110111
return null;
111112
}
@@ -117,7 +118,7 @@ private Class<?> loadFromParent(String name) {
117118
}
118119
}
119120

120-
private Class<?> findClassIgnoringNotFound(String name) {
121+
private @Nullable Class<?> findClassIgnoringNotFound(String name) {
121122
try {
122123
return findClass(name);
123124
}

module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/TomcatStarter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import jakarta.servlet.ServletException;
2424
import org.apache.commons.logging.Log;
2525
import org.apache.commons.logging.LogFactory;
26+
import org.jspecify.annotations.Nullable;
2627

2728
import org.springframework.boot.web.servlet.ServletContextInitializer;
2829

@@ -40,7 +41,7 @@ public class TomcatStarter implements ServletContainerInitializer {
4041

4142
private final Iterable<ServletContextInitializer> initializers;
4243

43-
private volatile Exception startUpException;
44+
private volatile @Nullable Exception startUpException;
4445

4546
public TomcatStarter(Iterable<ServletContextInitializer> initializers) {
4647
this.initializers = initializers;
@@ -64,7 +65,7 @@ public void onStartup(Set<Class<?>> classes, ServletContext servletContext) thro
6465
}
6566
}
6667

67-
Exception getStartUpException() {
68+
@Nullable Exception getStartUpException() {
6869
return this.startUpException;
6970
}
7071

module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/TomcatWebServer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.apache.commons.logging.Log;
3939
import org.apache.commons.logging.LogFactory;
4040
import org.apache.naming.ContextBindings;
41+
import org.jspecify.annotations.Nullable;
4142

4243
import org.springframework.boot.tomcat.reactive.TomcatReactiveWebServerFactory;
4344
import org.springframework.boot.tomcat.servlet.TomcatServletWebServerFactory;
@@ -73,7 +74,7 @@ public class TomcatWebServer implements WebServer {
7374

7475
private final boolean autoStart;
7576

76-
private final GracefulShutdown gracefulShutdown;
77+
private final @Nullable GracefulShutdown gracefulShutdown;
7778

7879
private volatile boolean started;
7980

@@ -412,7 +413,7 @@ public int getPort() {
412413
return -1;
413414
}
414415

415-
private String getContextPath() {
416+
private @Nullable String getContextPath() {
416417
String contextPath = Arrays.stream(this.tomcat.getHost().findChildren())
417418
.filter(TomcatEmbeddedContext.class::isInstance)
418419
.map(TomcatEmbeddedContext.class::cast)

module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/TomcatWebServerFactory.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@
4040
import org.apache.coyote.ProtocolHandler;
4141
import org.apache.coyote.http2.Http2Protocol;
4242
import org.apache.tomcat.util.modeler.Registry;
43+
import org.jspecify.annotations.Nullable;
4344

45+
import org.springframework.boot.ssl.SslBundles;
4446
import org.springframework.boot.util.LambdaSafe;
4547
import org.springframework.boot.web.server.AbstractConfigurableWebServerFactory;
4648
import org.springframework.boot.web.server.Ssl;
@@ -66,7 +68,7 @@ public class TomcatWebServerFactory extends AbstractConfigurableWebServerFactory
6668

6769
private final Log logger = LogFactory.getLog(getClass());
6870

69-
private File baseDirectory;
71+
private @Nullable File baseDirectory;
7072

7173
private int backgroundProcessorDelay;
7274

@@ -108,11 +110,11 @@ private List<LifecycleListener> getDefaultServerLifecycleListeners() {
108110
}
109111

110112
@Override
111-
public void setBaseDirectory(File baseDirectory) {
113+
public void setBaseDirectory(@Nullable File baseDirectory) {
112114
this.baseDirectory = baseDirectory;
113115
}
114116

115-
public File getBaseDirectory() {
117+
public @Nullable File getBaseDirectory() {
116118
return this.baseDirectory;
117119
}
118120

@@ -406,8 +408,9 @@ protected void customizeConnector(Connector connector) {
406408
if (getHttp2() != null && getHttp2().isEnabled()) {
407409
connector.addUpgradeProtocol(new Http2Protocol());
408410
}
409-
if (Ssl.isEnabled(getSsl())) {
410-
customizeSsl(connector);
411+
Ssl ssl = getSsl();
412+
if (Ssl.isEnabled(ssl)) {
413+
customizeSsl(connector, ssl);
411414
}
412415
TomcatConnectorCustomizer compression = new CompressionConnectorCustomizer(getCompression());
413416
compression.customize(connector);
@@ -429,20 +432,21 @@ private void invokeProtocolHandlerCustomizers(ProtocolHandler protocolHandler) {
429432
.invoke((customizer) -> customizer.customize(protocolHandler));
430433
}
431434

432-
private void customizeSsl(Connector connector) {
433-
SslConnectorCustomizer customizer = new SslConnectorCustomizer(this.logger, connector,
434-
getSsl().getClientAuth());
435+
private void customizeSsl(Connector connector, Ssl ssl) {
436+
SslConnectorCustomizer customizer = new SslConnectorCustomizer(this.logger, connector, ssl.getClientAuth());
435437
customizer.customize(getSslBundle(), getServerNameSslBundles());
436-
addBundleUpdateHandler(null, getSsl().getBundle(), customizer);
437-
getSsl().getServerNameBundles()
438+
addBundleUpdateHandler(null, ssl.getBundle(), customizer);
439+
ssl.getServerNameBundles()
438440
.forEach((serverNameSslBundle) -> addBundleUpdateHandler(serverNameSslBundle.serverName(),
439441
serverNameSslBundle.bundle(), customizer));
440442
}
441443

442-
private void addBundleUpdateHandler(String serverName, String sslBundleName, SslConnectorCustomizer customizer) {
444+
private void addBundleUpdateHandler(@Nullable String serverName, @Nullable String sslBundleName,
445+
SslConnectorCustomizer customizer) {
443446
if (StringUtils.hasText(sslBundleName)) {
444-
getSslBundles().addBundleUpdateHandler(sslBundleName,
445-
(sslBundle) -> customizer.update(serverName, sslBundle));
447+
SslBundles sslBundles = getSslBundles();
448+
Assert.state(sslBundles != null, "'sslBundles' must not be null");
449+
sslBundles.addBundleUpdateHandler(sslBundleName, (sslBundle) -> customizer.update(serverName, sslBundle));
446450
}
447451
}
448452

0 commit comments

Comments
 (0)