Skip to content

Commit 7545bed

Browse files
committed
Polish
1 parent cce509c commit 7545bed

File tree

5 files changed

+190
-152
lines changed

5 files changed

+190
-152
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/JvmMetricsAutoConfiguration.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,9 @@ public JvmCompilationMetrics jvmCompilationMetrics() {
100100
@ConditionalOnMissingBean(type = VIRTUAL_THREAD_METRICS_CLASS)
101101
@ImportRuntimeHints(VirtualThreadMetricsRuntimeHintsRegistrar.class)
102102
MeterBinder virtualThreadMetrics() throws ClassNotFoundException {
103-
Class<?> clazz = ClassUtils.forName(VIRTUAL_THREAD_METRICS_CLASS, getClass().getClassLoader());
104-
return (MeterBinder) BeanUtils.instantiateClass(clazz);
103+
Class<?> virtualThreadMetricsClass = ClassUtils.forName(VIRTUAL_THREAD_METRICS_CLASS,
104+
getClass().getClassLoader());
105+
return (MeterBinder) BeanUtils.instantiateClass(virtualThreadMetricsClass);
105106
}
106107

107108
static final class VirtualThreadMetricsRuntimeHintsRegistrar implements RuntimeHintsRegistrar {

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/PropertiesRedisConnectionDetails.java

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import java.util.List;
2020

21-
import org.springframework.boot.autoconfigure.data.redis.RedisConnectionConfiguration.ConnectionInfo;
22-
2321
/**
2422
* Adapts {@link RedisProperties} to {@link RedisConnectionDetails}.
2523
*
@@ -28,6 +26,7 @@
2826
* @author Phillip Webb
2927
* @author Scott Frederick
3028
* @author Yanming Zhou
29+
* @author Phillip Webb
3130
*/
3231
class PropertiesRedisConnectionDetails implements RedisConnectionDetails {
3332

@@ -39,81 +38,91 @@ class PropertiesRedisConnectionDetails implements RedisConnectionDetails {
3938

4039
@Override
4140
public String getUsername() {
42-
if (this.properties.getUrl() != null) {
43-
ConnectionInfo connectionInfo = ConnectionInfo.of(this.properties.getUrl());
44-
return connectionInfo.getUsername();
45-
}
46-
return this.properties.getUsername();
41+
RedisUrl redisUrl = getRedisUrl();
42+
return (redisUrl != null) ? redisUrl.credentials().username() : this.properties.getUsername();
4743
}
4844

4945
@Override
5046
public String getPassword() {
51-
if (this.properties.getUrl() != null) {
52-
ConnectionInfo connectionInfo = ConnectionInfo.of(this.properties.getUrl());
53-
return connectionInfo.getPassword();
54-
}
55-
return this.properties.getPassword();
47+
RedisUrl redisUrl = getRedisUrl();
48+
return (redisUrl != null) ? redisUrl.credentials().password() : this.properties.getPassword();
5649
}
5750

5851
@Override
5952
public Standalone getStandalone() {
60-
if (this.properties.getUrl() != null) {
61-
ConnectionInfo connectionInfo = ConnectionInfo.of(this.properties.getUrl());
62-
return Standalone.of(connectionInfo.getUri().getHost(), connectionInfo.getUri().getPort(),
63-
connectionInfo.getDatabase());
64-
}
65-
return Standalone.of(this.properties.getHost(), this.properties.getPort(), this.properties.getDatabase());
53+
RedisUrl redisUrl = getRedisUrl();
54+
return (redisUrl != null)
55+
? Standalone.of(redisUrl.uri().getHost(), redisUrl.uri().getPort(), redisUrl.database())
56+
: Standalone.of(this.properties.getHost(), this.properties.getPort(), this.properties.getDatabase());
6657
}
6758

6859
@Override
6960
public Sentinel getSentinel() {
70-
org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel sentinel = this.properties
71-
.getSentinel();
72-
if (sentinel == null) {
73-
return null;
74-
}
75-
return new Sentinel() {
76-
77-
@Override
78-
public int getDatabase() {
79-
return getStandalone().getDatabase();
80-
}
81-
82-
@Override
83-
public String getMaster() {
84-
return sentinel.getMaster();
85-
}
86-
87-
@Override
88-
public List<Node> getNodes() {
89-
return sentinel.getNodes().stream().map(PropertiesRedisConnectionDetails.this::asNode).toList();
90-
}
91-
92-
@Override
93-
public String getUsername() {
94-
return sentinel.getUsername();
95-
}
96-
97-
@Override
98-
public String getPassword() {
99-
return sentinel.getPassword();
100-
}
101-
102-
};
61+
RedisProperties.Sentinel sentinel = this.properties.getSentinel();
62+
return (sentinel != null) ? new PropertiesSentinel(getStandalone().getDatabase(), sentinel) : null;
10363
}
10464

10565
@Override
10666
public Cluster getCluster() {
10767
RedisProperties.Cluster cluster = this.properties.getCluster();
108-
List<Node> nodes = (cluster != null) ? cluster.getNodes().stream().map(this::asNode).toList() : null;
68+
List<Node> nodes = (cluster != null) ? asNodes(cluster.getNodes()) : null;
10969
return (nodes != null) ? () -> nodes : null;
11070
}
11171

72+
private RedisUrl getRedisUrl() {
73+
return RedisUrl.of(this.properties.getUrl());
74+
}
75+
76+
private List<Node> asNodes(List<String> nodes) {
77+
return nodes.stream().map(this::asNode).toList();
78+
}
79+
11280
private Node asNode(String node) {
11381
int portSeparatorIndex = node.lastIndexOf(':');
11482
String host = node.substring(0, portSeparatorIndex);
11583
int port = Integer.parseInt(node.substring(portSeparatorIndex + 1));
11684
return new Node(host, port);
11785
}
11886

87+
/**
88+
* {@link Sentinel} implementation backed by properties.
89+
*/
90+
private class PropertiesSentinel implements Sentinel {
91+
92+
private final int database;
93+
94+
private final RedisProperties.Sentinel properties;
95+
96+
PropertiesSentinel(int database, RedisProperties.Sentinel properties) {
97+
this.database = database;
98+
this.properties = properties;
99+
}
100+
101+
@Override
102+
public int getDatabase() {
103+
return this.database;
104+
}
105+
106+
@Override
107+
public String getMaster() {
108+
return this.properties.getMaster();
109+
}
110+
111+
@Override
112+
public List<Node> getNodes() {
113+
return asNodes(this.properties.getNodes());
114+
}
115+
116+
@Override
117+
public String getUsername() {
118+
return this.properties.getUsername();
119+
}
120+
121+
@Override
122+
public String getPassword() {
123+
return this.properties.getPassword();
124+
}
125+
126+
}
127+
119128
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/redis/RedisConnectionConfiguration.java

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

1717
package org.springframework.boot.autoconfigure.data.redis;
1818

19-
import java.net.URI;
20-
import java.net.URISyntaxException;
2119
import java.util.ArrayList;
2220
import java.util.List;
2321

@@ -33,7 +31,6 @@
3331
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
3432
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
3533
import org.springframework.util.ClassUtils;
36-
import org.springframework.util.StringUtils;
3734

3835
/**
3936
* Base Redis connection configuration.
@@ -152,14 +149,18 @@ protected final RedisProperties getProperties() {
152149
return this.properties;
153150
}
154151

155-
protected SslBundles getSslBundles() {
152+
protected final SslBundles getSslBundles() {
156153
return this.sslBundles;
157154
}
158155

159-
protected boolean isSslEnabled() {
156+
protected final boolean isSslEnabled() {
160157
return getProperties().getSsl().isEnabled();
161158
}
162159

160+
protected final boolean urlUsesSsl() {
161+
return RedisUrl.of(this.properties.getUrl()).useSsl();
162+
}
163+
163164
protected boolean isPoolEnabled(Pool pool) {
164165
Boolean enabled = pool.getEnabled();
165166
return (enabled != null) ? enabled : COMMONS_POOL2_AVAILABLE;
@@ -173,89 +174,8 @@ private List<RedisNode> createSentinels(Sentinel sentinel) {
173174
return nodes;
174175
}
175176

176-
protected final boolean urlUsesSsl() {
177-
return ConnectionInfo.of(this.properties.getUrl()).isUseSsl();
178-
}
179-
180177
protected final RedisConnectionDetails getConnectionDetails() {
181178
return this.connectionDetails;
182179
}
183180

184-
static final class ConnectionInfo {
185-
186-
private final URI uri;
187-
188-
private final boolean useSsl;
189-
190-
private final String username;
191-
192-
private final String password;
193-
194-
private final int database;
195-
196-
private ConnectionInfo(URI uri, boolean useSsl, String username, String password, int database) {
197-
this.uri = uri;
198-
this.useSsl = useSsl;
199-
this.username = username;
200-
this.password = password;
201-
this.database = database;
202-
}
203-
204-
URI getUri() {
205-
return this.uri;
206-
}
207-
208-
boolean isUseSsl() {
209-
return this.useSsl;
210-
}
211-
212-
String getUsername() {
213-
return this.username;
214-
}
215-
216-
String getPassword() {
217-
return this.password;
218-
}
219-
220-
int getDatabase() {
221-
return this.database;
222-
}
223-
224-
static ConnectionInfo of(String url) {
225-
try {
226-
URI uri = new URI(url);
227-
String scheme = uri.getScheme();
228-
if (!"redis".equals(scheme) && !"rediss".equals(scheme)) {
229-
throw new RedisUrlSyntaxException(url);
230-
}
231-
boolean useSsl = ("rediss".equals(scheme));
232-
String username = null;
233-
String password = null;
234-
if (uri.getUserInfo() != null) {
235-
String candidate = uri.getUserInfo();
236-
int index = candidate.indexOf(':');
237-
if (index >= 0) {
238-
username = candidate.substring(0, index);
239-
password = candidate.substring(index + 1);
240-
}
241-
else {
242-
password = candidate;
243-
}
244-
}
245-
int database = 0;
246-
if (StringUtils.hasText(uri.getPath())) {
247-
String[] pathSplit = uri.getPath().split("/", 2);
248-
if (pathSplit.length > 1 && !pathSplit[1].isEmpty()) {
249-
database = Integer.parseInt(pathSplit[1]);
250-
}
251-
}
252-
return new ConnectionInfo(uri, useSsl, username, password, database);
253-
}
254-
catch (URISyntaxException ex) {
255-
throw new RedisUrlSyntaxException(url, ex);
256-
}
257-
}
258-
259-
}
260-
261181
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.data.redis;
18+
19+
import java.net.URI;
20+
import java.net.URISyntaxException;
21+
22+
import org.springframework.util.StringUtils;
23+
24+
/**
25+
* A parsed URL used to connect to Redis.
26+
*
27+
* @param uri the source URI
28+
* @param useSsl if SSL is used to connect
29+
* @param credentials the connection credentials
30+
* @param database the database index
31+
* @author Mark Paluch
32+
* @author Stephane Nicoll
33+
* @author Alen Turkovic
34+
* @author Scott Frederick
35+
* @author Eddú Meléndez
36+
* @author Moritz Halbritter
37+
* @author Andy Wilkinson
38+
* @author Phillip Webb
39+
* @author Yanming Zhou
40+
* @author Phillip Webb
41+
*/
42+
record RedisUrl(URI uri, boolean useSsl, Credentials credentials, int database) {
43+
44+
static RedisUrl of(String url) {
45+
return (url != null) ? of(toUri(url)) : null;
46+
}
47+
48+
private static RedisUrl of(URI uri) {
49+
boolean useSsl = ("rediss".equals(uri.getScheme()));
50+
Credentials credentials = Credentials.fromUserInfo(uri.getUserInfo());
51+
int database = getDatabase(uri);
52+
return new RedisUrl(uri, useSsl, credentials, database);
53+
}
54+
55+
private static int getDatabase(URI uri) {
56+
String path = uri.getPath();
57+
String[] split = (!StringUtils.hasText(path)) ? new String[0] : path.split("/", 2);
58+
return (split.length > 1 && !split[1].isEmpty()) ? Integer.parseInt(split[1]) : 0;
59+
}
60+
61+
private static URI toUri(String url) {
62+
try {
63+
URI uri = new URI(url);
64+
String scheme = uri.getScheme();
65+
if (!"redis".equals(scheme) && !"rediss".equals(scheme)) {
66+
throw new RedisUrlSyntaxException(url);
67+
}
68+
return uri;
69+
}
70+
catch (URISyntaxException ex) {
71+
throw new RedisUrlSyntaxException(url, ex);
72+
}
73+
}
74+
75+
/**
76+
* Redis connection credentials.
77+
*
78+
* @param username the username or {@code null}
79+
* @param password the password
80+
*/
81+
record Credentials(String username, String password) {
82+
83+
private static final Credentials NONE = new Credentials(null, null);
84+
85+
private static Credentials fromUserInfo(String userInfo) {
86+
if (userInfo == null) {
87+
return NONE;
88+
}
89+
int index = userInfo.indexOf(':');
90+
if (index != -1) {
91+
return new Credentials(userInfo.substring(0, index), userInfo.substring(index + 1));
92+
}
93+
return new Credentials(null, userInfo);
94+
}
95+
96+
}
97+
98+
}

0 commit comments

Comments
 (0)