Skip to content

Commit 97ad5cb

Browse files
committed
Add nullability annotations to module/spring-boot-ldap
See gh-46587
1 parent 30e7d1e commit 97ad5cb

File tree

12 files changed

+78
-43
lines changed

12 files changed

+78
-43
lines changed

module/spring-boot-ldap/src/main/java/org/springframework/boot/ldap/autoconfigure/LdapConnectionDetails.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.ldap.autoconfigure;
1818

19+
import org.jspecify.annotations.Nullable;
20+
1921
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
2022

2123
/**
@@ -36,23 +38,23 @@ public interface LdapConnectionDetails extends ConnectionDetails {
3638
* Base suffix from which all operations should originate.
3739
* @return base suffix
3840
*/
39-
default String getBase() {
41+
default @Nullable String getBase() {
4042
return null;
4143
}
4244

4345
/**
4446
* Login username of the server.
4547
* @return login username
4648
*/
47-
default String getUsername() {
49+
default @Nullable String getUsername() {
4850
return null;
4951
}
5052

5153
/**
5254
* Login password of the server.
5355
* @return login password
5456
*/
55-
default String getPassword() {
57+
default @Nullable String getPassword() {
5658
return null;
5759
}
5860

module/spring-boot-ldap/src/main/java/org/springframework/boot/ldap/autoconfigure/LdapProperties.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.HashMap;
2020
import java.util.Map;
2121

22+
import org.jspecify.annotations.Nullable;
23+
2224
import org.springframework.boot.context.properties.ConfigurationProperties;
2325
import org.springframework.core.env.Environment;
2426
import org.springframework.ldap.ReferralException;
@@ -40,34 +42,34 @@ public class LdapProperties {
4042
/**
4143
* LDAP URLs of the server.
4244
*/
43-
private String[] urls;
45+
private String @Nullable [] urls;
4446

4547
/**
4648
* Base suffix from which all operations should originate.
4749
*/
48-
private String base;
50+
private @Nullable String base;
4951

5052
/**
5153
* Login username of the server.
5254
*/
53-
private String username;
55+
private @Nullable String username;
5456

5557
/**
5658
* Login password of the server.
5759
*/
58-
private String password;
60+
private @Nullable String password;
5961

6062
/**
6163
* Whether read-only operations should use an anonymous environment. Disabled by
6264
* default unless a username is set.
6365
*/
64-
private Boolean anonymousReadOnly;
66+
private @Nullable Boolean anonymousReadOnly;
6567

6668
/**
6769
* Specify how referrals encountered by the service provider are to be processed. If
6870
* not specified, the default is determined by the provider.
6971
*/
70-
private Referral referral;
72+
private @Nullable Referral referral;
7173

7274
/**
7375
* LDAP specification settings.
@@ -76,51 +78,51 @@ public class LdapProperties {
7678

7779
private final Template template = new Template();
7880

79-
public String[] getUrls() {
81+
public String @Nullable [] getUrls() {
8082
return this.urls;
8183
}
8284

83-
public void setUrls(String[] urls) {
85+
public void setUrls(String @Nullable [] urls) {
8486
this.urls = urls;
8587
}
8688

87-
public String getBase() {
89+
public @Nullable String getBase() {
8890
return this.base;
8991
}
9092

91-
public void setBase(String base) {
93+
public void setBase(@Nullable String base) {
9294
this.base = base;
9395
}
9496

95-
public String getUsername() {
97+
public @Nullable String getUsername() {
9698
return this.username;
9799
}
98100

99-
public void setUsername(String username) {
101+
public void setUsername(@Nullable String username) {
100102
this.username = username;
101103
}
102104

103-
public String getPassword() {
105+
public @Nullable String getPassword() {
104106
return this.password;
105107
}
106108

107-
public void setPassword(String password) {
109+
public void setPassword(@Nullable String password) {
108110
this.password = password;
109111
}
110112

111-
public Boolean getAnonymousReadOnly() {
113+
public @Nullable Boolean getAnonymousReadOnly() {
112114
return this.anonymousReadOnly;
113115
}
114116

115-
public void setAnonymousReadOnly(Boolean anonymousReadOnly) {
117+
public void setAnonymousReadOnly(@Nullable Boolean anonymousReadOnly) {
116118
this.anonymousReadOnly = anonymousReadOnly;
117119
}
118120

119-
public Referral getReferral() {
121+
public @Nullable Referral getReferral() {
120122
return this.referral;
121123
}
122124

123-
public void setReferral(Referral referral) {
125+
public void setReferral(@Nullable Referral referral) {
124126
this.referral = referral;
125127
}
126128

module/spring-boot-ldap/src/main/java/org/springframework/boot/ldap/autoconfigure/PropertiesLdapConnectionDetails.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.boot.ldap.autoconfigure;
1818

19+
import org.jspecify.annotations.Nullable;
20+
1921
import org.springframework.core.env.Environment;
2022

2123
/**
@@ -40,17 +42,17 @@ public String[] getUrls() {
4042
}
4143

4244
@Override
43-
public String getBase() {
45+
public @Nullable String getBase() {
4446
return this.properties.getBase();
4547
}
4648

4749
@Override
48-
public String getUsername() {
50+
public @Nullable String getUsername() {
4951
return this.properties.getUsername();
5052
}
5153

5254
@Override
53-
public String getPassword() {
55+
public @Nullable String getPassword() {
5456
return this.properties.getPassword();
5557
}
5658

module/spring-boot-ldap/src/main/java/org/springframework/boot/ldap/autoconfigure/embedded/EmbeddedLdapAutoConfiguration.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.unboundid.ldap.sdk.LDAPException;
2929
import com.unboundid.ldap.sdk.schema.Schema;
3030
import com.unboundid.ldif.LDIFReader;
31+
import org.jspecify.annotations.Nullable;
3132

3233
import org.springframework.aot.hint.RuntimeHints;
3334
import org.springframework.aot.hint.RuntimeHintsRegistrar;
@@ -83,7 +84,7 @@ public final class EmbeddedLdapAutoConfiguration implements DisposableBean {
8384

8485
private final EmbeddedLdapProperties embeddedProperties;
8586

86-
private InMemoryDirectoryServer server;
87+
private @Nullable InMemoryDirectoryServer server;
8788

8889
EmbeddedLdapAutoConfiguration(EmbeddedLdapProperties embeddedProperties) {
8990
this.embeddedProperties = embeddedProperties;
@@ -93,16 +94,17 @@ public final class EmbeddedLdapAutoConfiguration implements DisposableBean {
9394
InMemoryDirectoryServer directoryServer(ApplicationContext applicationContext) throws LDAPException {
9495
String[] baseDn = StringUtils.toStringArray(this.embeddedProperties.getBaseDn());
9596
InMemoryDirectoryServerConfig config = new InMemoryDirectoryServerConfig(baseDn);
96-
if (this.embeddedProperties.getCredential().isAvailable()) {
97-
config.addAdditionalBindCredentials(this.embeddedProperties.getCredential().getUsername(),
98-
this.embeddedProperties.getCredential().getPassword());
97+
String username = this.embeddedProperties.getCredential().getUsername();
98+
String password = this.embeddedProperties.getCredential().getPassword();
99+
if (StringUtils.hasText(username) && StringUtils.hasText(password)) {
100+
config.addAdditionalBindCredentials(username, password);
99101
}
100102
setSchema(config);
101103
InMemoryListenerConfig listenerConfig = InMemoryListenerConfig.createLDAPConfig("LDAP",
102104
this.embeddedProperties.getPort());
103105
config.setListenerConfigs(listenerConfig);
104106
this.server = new InMemoryDirectoryServer(config);
105-
importLdif(applicationContext);
107+
importLdif(this.server, applicationContext);
106108
this.server.startListening();
107109
setPortProperty(applicationContext, this.server.getListenPort());
108110
return this.server;
@@ -123,21 +125,26 @@ private void setSchema(InMemoryDirectoryServerConfig config, Resource resource)
123125
try {
124126
Schema defaultSchema = Schema.getDefaultStandardSchema();
125127
Schema schema = Schema.getSchema(resource.getInputStream());
126-
config.setSchema(Schema.mergeSchemas(defaultSchema, schema));
128+
if (schema == null) {
129+
config.setSchema(defaultSchema);
130+
}
131+
else {
132+
config.setSchema(Schema.mergeSchemas(defaultSchema, schema));
133+
}
127134
}
128135
catch (Exception ex) {
129136
throw new IllegalStateException("Unable to load schema " + resource.getDescription(), ex);
130137
}
131138
}
132139

133-
private void importLdif(ApplicationContext applicationContext) {
140+
private void importLdif(InMemoryDirectoryServer server, ApplicationContext applicationContext) {
134141
String location = this.embeddedProperties.getLdif();
135142
if (StringUtils.hasText(location)) {
136143
try {
137144
Resource resource = applicationContext.getResource(location);
138145
if (resource.exists()) {
139146
try (InputStream inputStream = resource.getInputStream()) {
140-
this.server.importFromLDIF(true, new LDIFReader(inputStream));
147+
server.importFromLDIF(true, new LDIFReader(inputStream));
141148
}
142149
}
143150
}
@@ -221,7 +228,7 @@ LdapContextSource ldapContextSource(Environment environment, LdapProperties prop
221228
static class EmbeddedLdapAutoConfigurationRuntimeHints implements RuntimeHintsRegistrar {
222229

223230
@Override
224-
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
231+
public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader) {
225232
hints.resources()
226233
.registerPatternIfPresent(classLoader, "schema.ldif", (hint) -> hint.includes("schema.ldif"));
227234
}

module/spring-boot-ldap/src/main/java/org/springframework/boot/ldap/autoconfigure/embedded/EmbeddedLdapProperties.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22+
import org.jspecify.annotations.Nullable;
23+
2224
import org.springframework.boot.context.properties.ConfigurationProperties;
2325
import org.springframework.boot.convert.Delimiter;
2426
import org.springframework.core.io.Resource;
@@ -101,26 +103,26 @@ public static class Credential {
101103
/**
102104
* Embedded LDAP username.
103105
*/
104-
private String username;
106+
private @Nullable String username;
105107

106108
/**
107109
* Embedded LDAP password.
108110
*/
109-
private String password;
111+
private @Nullable String password;
110112

111-
public String getUsername() {
113+
public @Nullable String getUsername() {
112114
return this.username;
113115
}
114116

115-
public void setUsername(String username) {
117+
public void setUsername(@Nullable String username) {
116118
this.username = username;
117119
}
118120

119-
public String getPassword() {
121+
public @Nullable String getPassword() {
120122
return this.password;
121123
}
122124

123-
public void setPassword(String password) {
125+
public void setPassword(@Nullable String password) {
124126
this.password = password;
125127
}
126128

@@ -140,7 +142,7 @@ public static class Validation {
140142
/**
141143
* Path to the custom schema.
142144
*/
143-
private Resource schema;
145+
private @Nullable Resource schema;
144146

145147
public boolean isEnabled() {
146148
return this.enabled;
@@ -150,11 +152,11 @@ public void setEnabled(boolean enabled) {
150152
this.enabled = enabled;
151153
}
152154

153-
public Resource getSchema() {
155+
public @Nullable Resource getSchema() {
154156
return this.schema;
155157
}
156158

157-
public void setSchema(Resource schema) {
159+
public void setSchema(@Nullable Resource schema) {
158160
this.schema = schema;
159161
}
160162

module/spring-boot-ldap/src/main/java/org/springframework/boot/ldap/autoconfigure/embedded/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
/**
1818
* Auto-configuration for embedded LDAP.
1919
*/
20+
@NullMarked
2021
package org.springframework.boot.ldap.autoconfigure.embedded;
22+
23+
import org.jspecify.annotations.NullMarked;

module/spring-boot-ldap/src/main/java/org/springframework/boot/ldap/autoconfigure/health/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
/**
1818
* Auto-configuration for LDAP health.
1919
*/
20+
@NullMarked
2021
package org.springframework.boot.ldap.autoconfigure.health;
22+
23+
import org.jspecify.annotations.NullMarked;

module/spring-boot-ldap/src/main/java/org/springframework/boot/ldap/autoconfigure/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
/**
1818
* Auto-configuration for LDAP.
1919
*/
20+
@NullMarked
2021
package org.springframework.boot.ldap.autoconfigure;
22+
23+
import org.jspecify.annotations.NullMarked;

module/spring-boot-ldap/src/main/java/org/springframework/boot/ldap/docker/compose/package-info.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,7 @@
1717
/**
1818
* Support for Docker Compose LDAP service connections.
1919
*/
20+
@NullMarked
2021
package org.springframework.boot.ldap.docker.compose;
22+
23+
import org.jspecify.annotations.NullMarked;

module/spring-boot-ldap/src/main/java/org/springframework/boot/ldap/health/LdapHealthIndicator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import javax.naming.NamingException;
2020
import javax.naming.directory.DirContext;
2121

22+
import org.jspecify.annotations.Nullable;
23+
2224
import org.springframework.boot.health.contributor.AbstractHealthIndicator;
2325
import org.springframework.boot.health.contributor.Health;
2426
import org.springframework.boot.health.contributor.HealthIndicator;
@@ -54,7 +56,7 @@ protected void doHealthCheck(Health.Builder builder) throws Exception {
5456
private static final class VersionContextExecutor implements ContextExecutor<String> {
5557

5658
@Override
57-
public String executeWithContext(DirContext ctx) throws NamingException {
59+
public @Nullable String executeWithContext(DirContext ctx) throws NamingException {
5860
Object version = ctx.getEnvironment().get("java.naming.ldap.version");
5961
if (version != null) {
6062
return (String) version;

0 commit comments

Comments
 (0)