Skip to content

Commit bdbba08

Browse files
DATAREDIS-1046 - Polishing.
Guard tests to allow execution against single node instance for quick dev turnaround and replace usage of Optional with null and @nullable annotations. Original Pull Request: #558
1 parent 717a196 commit bdbba08

13 files changed

+168
-69
lines changed

src/main/java/org/springframework/data/redis/connection/RedisClusterConfiguration.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.HashMap;
2424
import java.util.LinkedHashSet;
2525
import java.util.Map;
26-
import java.util.Optional;
2726
import java.util.Set;
2827

2928
import org.springframework.core.env.MapPropertySource;
@@ -50,7 +49,7 @@ public class RedisClusterConfiguration implements RedisConfiguration, ClusterCon
5049

5150
private Set<RedisNode> clusterNodes;
5251
private @Nullable Integer maxRedirects;
53-
private Optional<String> username = Optional.empty();
52+
private @Nullable String username = null;
5453
private RedisPassword password = RedisPassword.none();
5554

5655
/**
@@ -189,16 +188,17 @@ private void appendClusterNodes(Set<String> hostAndPorts) {
189188
* @see org.springframework.data.redis.connection.RedisConfiguration.WithAuthentication#setUsername(String)
190189
*/
191190
@Override
192-
public void setUsername(String username) {
193-
this.username = Optional.of(username);
191+
public void setUsername(@Nullable String username) {
192+
this.username = username;
194193
}
195194

196195
/*
197196
* (non-Javadoc)
198197
* @see org.springframework.data.redis.connection.RedisConfiguration.WithAuthentication#getUsername()
199198
*/
199+
@Nullable
200200
@Override
201-
public Optional<String> getUsername() {
201+
public String getUsername() {
202202
return this.username;
203203
}
204204

src/main/java/org/springframework/data/redis/connection/RedisConfiguration.java

+10-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
import java.util.Collections;
1919
import java.util.List;
20-
import java.util.Optional;
2120
import java.util.Set;
2221
import java.util.function.IntSupplier;
2322
import java.util.function.Supplier;
@@ -138,11 +137,11 @@ static Integer getDatabaseOrElse(@Nullable RedisConfiguration configuration, Sup
138137
* @param configuration can be {@literal null}.
139138
* @param other a {@code Supplier} whose result is returned if given {@link RedisConfiguration} is not
140139
* {@link #isAuthenticationAware(RedisConfiguration) password aware}.
141-
* @return never {@literal null}.
140+
* @return can be {@literal null}.
142141
* @throws IllegalArgumentException if {@code other} is {@literal null}.
143142
*/
144-
static Optional<String> getUsernameOrElse(@Nullable RedisConfiguration configuration,
145-
Supplier<Optional<String>> other) {
143+
@Nullable
144+
static String getUsernameOrElse(@Nullable RedisConfiguration configuration, Supplier<String> other) {
146145

147146
Assert.notNull(other, "Other must not be null!");
148147
return isAuthenticationAware(configuration) ? ((WithAuthentication) configuration).getUsername() : other.get();
@@ -203,7 +202,7 @@ interface WithAuthentication {
203202
*
204203
* @param username the username.
205204
*/
206-
void setUsername(String username);
205+
void setUsername(@Nullable String username);
207206

208207
/**
209208
* Create and set a {@link RedisPassword} for given {@link String}.
@@ -233,9 +232,10 @@ default void setPassword(@Nullable char[] password) {
233232
/**
234233
* Get the username to use when connecting.
235234
*
236-
* @return {@link Optional#empty()} if none set.
235+
* @return {@literal null} if none set.
237236
*/
238-
Optional<String> getUsername();
237+
@Nullable
238+
String getUsername();
239239

240240
/**
241241
* Get the RedisPassword to use when connecting.
@@ -381,10 +381,11 @@ default void setMaster(final String name) {
381381
/**
382382
* Get the username used when authenticating with a Redis Server.
383383
*
384-
* @return never {@literal null}.
384+
* @return can be {@literal null} if not set.
385385
* @since 2.4
386386
*/
387-
default Optional<String> getDataNodeUsername() {
387+
@Nullable
388+
default String getDataNodeUsername() {
388389
return getUsername();
389390
}
390391

src/main/java/org/springframework/data/redis/connection/RedisSentinelConfiguration.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.HashMap;
2222
import java.util.LinkedHashSet;
2323
import java.util.Map;
24-
import java.util.Optional;
2524
import java.util.Set;
2625

2726
import org.springframework.core.env.MapPropertySource;
@@ -51,7 +50,7 @@ public class RedisSentinelConfiguration implements RedisConfiguration, SentinelC
5150
private Set<RedisNode> sentinels;
5251
private int database;
5352

54-
private Optional<String> dataNodeUsername = Optional.empty();
53+
private @Nullable String dataNodeUsername = null;
5554
private RedisPassword dataNodePassword = RedisPassword.none();
5655
private RedisPassword sentinelPassword = RedisPassword.none();
5756

@@ -236,16 +235,17 @@ public void setDatabase(int index) {
236235
* @see org.springframework.data.redis.connection.RedisConfiguration.WithAuthentication#setUsername(String)
237236
*/
238237
@Override
239-
public void setUsername(String username) {
240-
this.dataNodeUsername = Optional.of(username);
238+
public void setUsername(@Nullable String username) {
239+
this.dataNodeUsername = username;
241240
}
242241

243242
/*
244243
* (non-Javadoc)
245244
* @see org.springframework.data.redis.connection.RedisConfiguration.WithAuthentication#getUsername()
246245
*/
246+
@Nullable
247247
@Override
248-
public Optional<String> getUsername() {
248+
public String getUsername() {
249249
return this.dataNodeUsername;
250250
}
251251

src/main/java/org/springframework/data/redis/connection/RedisSocketConfiguration.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
*/
1616
package org.springframework.data.redis.connection;
1717

18-
import java.util.Optional;
19-
2018
import org.springframework.data.redis.connection.RedisConfiguration.DomainSocketConfiguration;
19+
import org.springframework.lang.Nullable;
2120
import org.springframework.util.Assert;
2221

2322
/**
@@ -34,7 +33,7 @@ public class RedisSocketConfiguration implements RedisConfiguration, DomainSocke
3433

3534
private String socket = DEFAULT_SOCKET;
3635
private int database;
37-
private Optional<String> username = Optional.empty();
36+
private @Nullable String username = null;
3837
private RedisPassword password = RedisPassword.none();
3938

4039
/**
@@ -100,16 +99,17 @@ public void setDatabase(int index) {
10099
* @see org.springframework.data.redis.connection.RedisConfiguration.WithAuthentication#setUsername(String)
101100
*/
102101
@Override
103-
public void setUsername(String username) {
104-
this.username = Optional.of(username);
102+
public void setUsername(@Nullable String username) {
103+
this.username = username;
105104
}
106105

107106
/*
108107
* (non-Javadoc)
109108
* @see org.springframework.data.redis.connection.RedisConfiguration.WithAuthentication#getUsername()
110109
*/
110+
@Nullable
111111
@Override
112-
public Optional<String> getUsername() {
112+
public String getUsername() {
113113
return this.username;
114114
}
115115

src/main/java/org/springframework/data/redis/connection/RedisStandaloneConfiguration.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,10 @@
1515
*/
1616
package org.springframework.data.redis.connection;
1717

18-
import java.util.Optional;
19-
2018
import org.springframework.data.redis.connection.RedisConfiguration.WithDatabaseIndex;
2119
import org.springframework.data.redis.connection.RedisConfiguration.WithHostAndPort;
2220
import org.springframework.data.redis.connection.RedisConfiguration.WithPassword;
21+
import org.springframework.lang.Nullable;
2322
import org.springframework.util.Assert;
2423

2524
/**
@@ -39,7 +38,7 @@ public class RedisStandaloneConfiguration
3938
private String hostName = DEFAULT_HOST;
4039
private int port = DEFAULT_PORT;
4140
private int database;
42-
private Optional<String> username = Optional.empty();
41+
private @Nullable String username = null;
4342
private RedisPassword password = RedisPassword.none();
4443

4544
/**
@@ -133,16 +132,17 @@ public void setDatabase(int index) {
133132
* @see org.springframework.data.redis.connection.RedisConfiguration.WithAuthentication#setUsername(String)
134133
*/
135134
@Override
136-
public void setUsername(String username) {
137-
this.username = Optional.of(username);
135+
public void setUsername(@Nullable String username) {
136+
this.username = username;
138137
}
139138

140139
/*
141140
* (non-Javadoc)
142141
* @see org.springframework.data.redis.connection.RedisConfiguration.WithAuthentication#getUsername()
143142
*/
143+
@Nullable
144144
@Override
145-
public Optional<String> getUsername() {
145+
public String getUsername() {
146146
return this.username;
147147
}
148148

src/main/java/org/springframework/data/redis/connection/RedisStaticMasterReplicaConfiguration.java

+8-7
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818
import java.util.ArrayList;
1919
import java.util.Collections;
2020
import java.util.List;
21-
import java.util.Optional;
2221

2322
import org.springframework.data.redis.connection.RedisConfiguration.StaticMasterReplicaConfiguration;
23+
import org.springframework.lang.Nullable;
2424
import org.springframework.util.Assert;
2525

2626
/**
2727
* Configuration class used for setting up {@link RedisConnection} via {@link RedisConnectionFactory} using the provided
2828
* Master / Replica configuration to nodes know to not change address. Eg. when connecting to
2929
* <a href="https://aws.amazon.com/documentation/elasticache/">AWS ElastiCache with Read Replicas</a>. <br/>
30-
* Note: Redis is undergoing a nomenclature change where the term replica is used synonymously to slave.
31-
* Please also note that a Master/Replica connection cannot be used for Pub/Sub operations.
30+
* Note: Redis is undergoing a nomenclature change where the term replica is used synonymously to slave. Please also
31+
* note that a Master/Replica connection cannot be used for Pub/Sub operations.
3232
*
3333
* @author Mark Paluch
3434
* @author Christoph Strobl
@@ -41,7 +41,7 @@ public class RedisStaticMasterReplicaConfiguration implements RedisConfiguration
4141

4242
private List<RedisStandaloneConfiguration> nodes = new ArrayList<>();
4343
private int database;
44-
private Optional<String> username = Optional.empty();
44+
private @Nullable String username = null;
4545
private RedisPassword password = RedisPassword.none();
4646

4747
/**
@@ -137,16 +137,17 @@ public void setDatabase(int index) {
137137
* @see org.springframework.data.redis.connection.RedisConfiguration.WithAuthentication#setUsername(String)
138138
*/
139139
@Override
140-
public void setUsername(String username) {
141-
this.username = Optional.of(username);
140+
public void setUsername(@Nullable String username) {
141+
this.username = username;
142142
}
143143

144144
/*
145145
* (non-Javadoc)
146146
* @see org.springframework.data.redis.connection.RedisConfiguration.WithAuthentication#getUsername()
147147
*/
148+
@Nullable
148149
@Override
149-
public Optional<String> getUsername() {
150+
public String getUsername() {
150151
return this.username;
151152
}
152153

src/main/java/org/springframework/data/redis/connection/jedis/JedisConnectionFactory.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.springframework.util.Assert;
5959
import org.springframework.util.ClassUtils;
6060
import org.springframework.util.CollectionUtils;
61+
import org.springframework.util.StringUtils;
6162

6263
/**
6364
* Connection factory creating <a href="https://github.com/xetorthio/jedis">Jedis</a> based connections.
@@ -328,7 +329,10 @@ public void afterPropertiesSet() {
328329
clientConfiguration.getHostnameVerifier().orElse(null));
329330

330331
getRedisPassword().map(String::new).ifPresent(shardInfo::setPassword);
331-
getRedisUsername().ifPresent(shardInfo::setUser);
332+
String username = getRedisUsername();
333+
if (StringUtils.hasText(username)) {
334+
shardInfo.setUser(username);
335+
}
332336

333337
int readTimeout = getReadTimeout();
334338

@@ -375,8 +379,8 @@ protected Pool<Jedis> createRedisSentinelPool(RedisSentinelConfiguration config)
375379
String sentinelPassword = config.getSentinelPassword().toOptional().map(String::new).orElse(null);
376380

377381
return new JedisSentinelPool(config.getMaster().getName(), convertToJedisSentinelSet(config.getSentinels()),
378-
poolConfig, getConnectTimeout(), getReadTimeout(), getUsername(), getPassword(), getDatabase(),
379-
getClientName(), getConnectTimeout(), getReadTimeout(), sentinelUser, sentinelPassword, getClientName());
382+
poolConfig, getConnectTimeout(), getReadTimeout(), getUsername(), getPassword(), getDatabase(), getClientName(),
383+
getConnectTimeout(), getReadTimeout(), sentinelUser, sentinelPassword, getClientName());
380384
}
381385

382386
/**
@@ -556,7 +560,7 @@ public void setUseSsl(boolean useSsl) {
556560
*/
557561
@Nullable
558562
private String getUsername() {
559-
return getRedisUsername().orElse(null);
563+
return getRedisUsername();
560564
}
561565

562566
/**
@@ -569,7 +573,8 @@ public String getPassword() {
569573
return getRedisPassword().map(String::new).orElse(null);
570574
}
571575

572-
private Optional<String> getRedisUsername() {
576+
@Nullable
577+
private String getRedisUsername() {
573578
return RedisConfiguration.getUsernameOrElse(this.configuration, standaloneConfig::getUsername);
574579
}
575580

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConnectionFactory.java

+7-6
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@
4444

4545
import org.apache.commons.logging.Log;
4646
import org.apache.commons.logging.LogFactory;
47-
4847
import org.springframework.beans.factory.DisposableBean;
4948
import org.springframework.beans.factory.InitializingBean;
5049
import org.springframework.dao.DataAccessException;
@@ -62,6 +61,7 @@
6261
import org.springframework.lang.Nullable;
6362
import org.springframework.util.Assert;
6463
import org.springframework.util.ClassUtils;
64+
import org.springframework.util.StringUtils;
6565

6666
/**
6767
* Connection factory creating <a href="https://github.com/mp911de/lettuce">Lettuce</a>-based connections.
@@ -788,9 +788,11 @@ public void setClientName(@Nullable String clientName) {
788788
this.getMutableConfiguration().setClientName(clientName);
789789
}
790790

791-
private Optional<String> getRedisUsername() {
791+
@Nullable
792+
private String getRedisUsername() {
792793
return RedisConfiguration.getUsernameOrElse(configuration, standaloneConfig::getUsername);
793794
}
795+
794796
/**
795797
* Returns the password used for authenticating with the Redis server.
796798
*
@@ -1164,11 +1166,10 @@ private RedisURI createRedisSocketURIAndApplySettings(String socketPath) {
11641166

11651167
private void applyAuthentication(RedisURI.Builder builder) {
11661168

1167-
Optional<String> username = getRedisUsername();
1168-
if (username.isPresent()) {
1169+
String username = getRedisUsername();
1170+
if (StringUtils.hasText(username)) {
11691171
// See https://github.com/lettuce-io/lettuce-core/issues/1404
1170-
username.ifPresent(
1171-
it -> builder.withAuthentication(it, new String(getRedisPassword().toOptional().orElse(new char[0]))));
1172+
builder.withAuthentication(username, new String(getRedisPassword().toOptional().orElse(new char[0])));
11721173
} else {
11731174
getRedisPassword().toOptional().ifPresent(builder::withPassword);
11741175
}

src/main/java/org/springframework/data/redis/connection/lettuce/LettuceConverters.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,6 @@ private Set<Flag> parseFlags(Set<NodeFlag> source) {
309309

310310
TRANSACTION_RESULT_UNWRAPPER = transactionResult -> transactionResult.stream().collect(Collectors.toList());
311311

312-
313312
}
314313

315314
public static List<Tuple> toTuple(List<byte[]> list) {
@@ -653,12 +652,12 @@ public static RedisURI sentinelConfigurationToRedisURI(RedisSentinelConfiguratio
653652
builder.withSentinel(sentinelBuilder.build());
654653
}
655654

656-
Optional<String> username = sentinelConfiguration.getUsername();
655+
String username = sentinelConfiguration.getUsername();
657656
RedisPassword password = sentinelConfiguration.getPassword();
658657

659-
if (username.isPresent()) {
658+
if (StringUtils.hasText(username)) {
660659
// See https://github.com/lettuce-io/lettuce-core/issues/1404
661-
builder.withAuthentication(username.get(), new String(password.toOptional().orElse(new char[0])));
660+
builder.withAuthentication(username, new String(password.toOptional().orElse(new char[0])));
662661
} else {
663662
password.toOptional().ifPresent(builder::withPassword);
664663
}

0 commit comments

Comments
 (0)