|
| 1 | +/* |
| 2 | + * Copyright 2012-2023 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.util.List; |
| 20 | + |
| 21 | +import org.springframework.boot.autoconfigure.data.redis.RedisConnectionConfiguration.ConnectionInfo; |
| 22 | + |
| 23 | +/** |
| 24 | + * Adapts {@link RedisProperties} to {@link RedisConnectionDetails}. |
| 25 | + * |
| 26 | + * @author Moritz Halbritter |
| 27 | + * @author Andy Wilkinson |
| 28 | + * @author Phillip Webb |
| 29 | + */ |
| 30 | +class PropertiesRedisConnectionDetails implements RedisConnectionDetails { |
| 31 | + |
| 32 | + private final RedisProperties properties; |
| 33 | + |
| 34 | + PropertiesRedisConnectionDetails(RedisProperties properties) { |
| 35 | + this.properties = properties; |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public String getUsername() { |
| 40 | + if (this.properties.getUrl() != null) { |
| 41 | + ConnectionInfo connectionInfo = connectionInfo(this.properties.getUrl()); |
| 42 | + String userInfo = connectionInfo.getUri().getUserInfo(); |
| 43 | + int index = (userInfo != null) ? userInfo.indexOf(':') : -1; |
| 44 | + if (index != -1) { |
| 45 | + return userInfo.substring(0, index); |
| 46 | + } |
| 47 | + } |
| 48 | + return this.properties.getUsername(); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public String getPassword() { |
| 53 | + if (this.properties.getUrl() != null) { |
| 54 | + ConnectionInfo connectionInfo = connectionInfo(this.properties.getUrl()); |
| 55 | + String userInfo = connectionInfo.getUri().getUserInfo(); |
| 56 | + int index = (userInfo != null) ? userInfo.indexOf(':') : -1; |
| 57 | + if (index != -1) { |
| 58 | + return userInfo.substring(index + 1); |
| 59 | + } |
| 60 | + } |
| 61 | + return this.properties.getPassword(); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Standalone getStandalone() { |
| 66 | + if (this.properties.getUrl() != null) { |
| 67 | + ConnectionInfo connectionInfo = connectionInfo(this.properties.getUrl()); |
| 68 | + return Standalone.of(connectionInfo.getUri().getHost(), connectionInfo.getUri().getPort(), |
| 69 | + this.properties.getDatabase()); |
| 70 | + } |
| 71 | + return Standalone.of(this.properties.getHost(), this.properties.getPort(), this.properties.getDatabase()); |
| 72 | + } |
| 73 | + |
| 74 | + private ConnectionInfo connectionInfo(String url) { |
| 75 | + return (url != null) ? RedisConnectionConfiguration.parseUrl(url) : null; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public Sentinel getSentinel() { |
| 80 | + org.springframework.boot.autoconfigure.data.redis.RedisProperties.Sentinel sentinel = this.properties |
| 81 | + .getSentinel(); |
| 82 | + if (sentinel == null) { |
| 83 | + return null; |
| 84 | + } |
| 85 | + return new Sentinel() { |
| 86 | + |
| 87 | + @Override |
| 88 | + public int getDatabase() { |
| 89 | + return PropertiesRedisConnectionDetails.this.properties.getDatabase(); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public String getMaster() { |
| 94 | + return sentinel.getMaster(); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public List<Node> getNodes() { |
| 99 | + return sentinel.getNodes().stream().map(PropertiesRedisConnectionDetails.this::asNode).toList(); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public String getUsername() { |
| 104 | + return sentinel.getUsername(); |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public String getPassword() { |
| 109 | + return sentinel.getPassword(); |
| 110 | + } |
| 111 | + |
| 112 | + }; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public Cluster getCluster() { |
| 117 | + RedisProperties.Cluster cluster = this.properties.getCluster(); |
| 118 | + List<Node> nodes = (cluster != null) ? cluster.getNodes().stream().map(this::asNode).toList() : null; |
| 119 | + return (nodes != null) ? () -> nodes : null; |
| 120 | + } |
| 121 | + |
| 122 | + private Node asNode(String node) { |
| 123 | + String[] components = node.split(":"); |
| 124 | + return new Node(components[0], Integer.parseInt(components[1])); |
| 125 | + } |
| 126 | + |
| 127 | +} |
0 commit comments