|
1 | 1 | package redis.clients.jedis; |
| 2 | + |
2 | 3 | import com.google.gson.Gson; |
3 | 4 | import com.google.gson.reflect.TypeToken; |
4 | 5 | import redis.clients.jedis.util.JedisURIHelper; |
5 | 6 |
|
6 | 7 | import java.io.FileReader; |
7 | 8 | import java.net.URI; |
8 | | -import java.util.HashMap; |
9 | | -import java.util.List; |
| 9 | +import java.util.*; |
10 | 10 |
|
11 | 11 | public class EndpointConfig { |
12 | 12 |
|
13 | | - private boolean tls; |
14 | | - private String username; |
15 | | - private String password; |
16 | | - private int bdbId; |
17 | | - private Object rawEndpoints; |
18 | | - |
19 | | - private List<URI> endpoints; |
20 | | - |
21 | | - public EndpointConfig(boolean tls, String username, String password, int bdbId, Object rawEndpoints) { |
22 | | - this.tls = tls; |
23 | | - this.username = username; |
24 | | - this.password = password; |
25 | | - this.bdbId = bdbId; |
26 | | - this.rawEndpoints = rawEndpoints; |
27 | | - } |
28 | | - |
29 | | - public HostAndPort getHostAndPort() { |
30 | | - return JedisURIHelper.getHostAndPort(endpoints.get(0)); |
31 | | - } |
32 | | - |
33 | | - public String getPassword() { |
34 | | - return password; |
35 | | - } |
36 | | - |
37 | | - public String getUsername() { |
38 | | - return username; |
39 | | - } |
40 | | - |
41 | | - public String getHost() { |
42 | | - return getHostAndPort().getHost(); |
43 | | - } |
44 | | - |
45 | | - public int getPort() { |
46 | | - return getHostAndPort().getPort(); |
47 | | - } |
48 | | - |
49 | | - public URI getURI() { |
50 | | - return endpoints.get(0); |
51 | | - } |
52 | | - |
53 | | - public URI getCustomizedURI(boolean withCredentials, String path) |
54 | | - { |
55 | | - return getCustomizedURI(withCredentials ? username : "", withCredentials ? password : "", path); |
56 | | - } |
57 | | - |
58 | | - public URI getCustomizedURI(String u, String p, String path) |
59 | | - { |
60 | | - String userInfo = !(u.isEmpty() && p.isEmpty()) ? u + ":" + p + "@" : ""; |
61 | | - return URI.create((tls ? "rediss" : "redis") + "://" + userInfo + getHost() + ":" + getPort() + path); |
62 | | - } |
63 | | - |
64 | | - public Connection getConnection() { |
65 | | - return new Connection(getHostAndPort(), getClientConfigBuilder().build()); |
66 | | - } |
67 | | - |
68 | | - public Connection getConnection(int timeoutMillis) { |
69 | | - return new Connection(getHostAndPort(), getClientConfigBuilder().timeoutMillis(timeoutMillis).build()); |
70 | | - } |
71 | | - |
72 | | - public Jedis getJedis() { |
73 | | - return new Jedis(getHostAndPort(), getClientConfigBuilder().build()); |
74 | | - } |
75 | | - |
76 | | - public Jedis getJedis(int timeoutMillis) { |
77 | | - return new Jedis(getHostAndPort(), getClientConfigBuilder().timeoutMillis(timeoutMillis).build()); |
78 | | - } |
79 | | - |
80 | | - public DefaultJedisClientConfig.Builder getClientConfigBuilder() { |
81 | | - return DefaultJedisClientConfig.builder().user(username).password(password); |
82 | | - } |
83 | | - |
84 | | - public static HashMap<String, EndpointConfig> loadFromJSON(String filePath) throws Exception { |
85 | | - Gson gson = new Gson(); |
86 | | - HashMap<String, EndpointConfig> configs; |
87 | | - try (FileReader reader = new FileReader(filePath)) { |
88 | | - configs = gson.fromJson(reader, new TypeToken<HashMap<String, EndpointConfig>>() { |
89 | | - }.getType()); |
| 13 | + private boolean tls; |
| 14 | + private String username; |
| 15 | + private String password; |
| 16 | + private int bdbId; |
| 17 | + private Object rawEndpoints; |
| 18 | + private List<URI> endpoints; |
| 19 | + |
| 20 | + |
| 21 | + public EndpointConfig(HostAndPort hnp, String username, String password) { |
| 22 | + this.tls = false; |
| 23 | + this.username = username; |
| 24 | + this.password = password; |
| 25 | + this.bdbId = 0; |
| 26 | + this.rawEndpoints = null; |
| 27 | + this.endpoints = Collections.singletonList( |
| 28 | + URI.create("redis://" + hnp.getHost() + ":" + hnp.getPort()) |
| 29 | + ); |
| 30 | + } |
| 31 | + |
| 32 | + public HostAndPort getHostAndPort() { |
| 33 | + return JedisURIHelper.getHostAndPort(endpoints.get(0)); |
| 34 | + } |
| 35 | + |
| 36 | + public String getPassword() { |
| 37 | + return password; |
| 38 | + } |
| 39 | + |
| 40 | + public String getUsername() { |
| 41 | + return username; |
| 42 | + } |
| 43 | + |
| 44 | + public String getHost() { |
| 45 | + return getHostAndPort().getHost(); |
| 46 | + } |
| 47 | + |
| 48 | + public int getPort() { |
| 49 | + return getHostAndPort().getPort(); |
| 50 | + } |
| 51 | + |
| 52 | + public URI getURI() { |
| 53 | + return endpoints.get(0); |
| 54 | + } |
| 55 | + |
| 56 | + public URI getCustomizedURI(boolean withCredentials, String path) { |
| 57 | + return getCustomizedURI(withCredentials ? username : "", withCredentials ? password : "", path); |
| 58 | + } |
| 59 | + |
| 60 | + public URI getCustomizedURI(String u, String p, String path) { |
| 61 | + String userInfo = !(u.isEmpty() && p.isEmpty()) ? u + ":" + p + "@" : ""; |
| 62 | + return URI.create((tls ? "rediss" : "redis") + "://" + userInfo + getHost() + ":" + getPort() + path); |
| 63 | + } |
| 64 | + |
| 65 | + public Connection getConnection() { |
| 66 | + return new Connection(getHostAndPort(), getClientConfigBuilder().build()); |
| 67 | + } |
| 68 | + |
| 69 | + public Connection getConnection(int timeoutMillis) { |
| 70 | + return new Connection(getHostAndPort(), getClientConfigBuilder().timeoutMillis(timeoutMillis).build()); |
| 71 | + } |
| 72 | + |
| 73 | + public Jedis getJedis() { |
| 74 | + return new Jedis(getHostAndPort(), getClientConfigBuilder().build()); |
| 75 | + } |
| 76 | + |
| 77 | + public Jedis getJedis(int timeoutMillis) { |
| 78 | + return new Jedis(getHostAndPort(), getClientConfigBuilder().timeoutMillis(timeoutMillis).build()); |
| 79 | + } |
| 80 | + |
| 81 | + public DefaultJedisClientConfig.Builder getClientConfigBuilder() { |
| 82 | + return DefaultJedisClientConfig.builder().user(username).password(password).ssl(tls); |
| 83 | + } |
| 84 | + |
| 85 | + public static HashMap<String, EndpointConfig> loadFromJSON(String filePath) throws Exception { |
| 86 | + Gson gson = new Gson(); |
| 87 | + HashMap<String, EndpointConfig> configs; |
| 88 | + try (FileReader reader = new FileReader(filePath)) { |
| 89 | + configs = gson.fromJson(reader, new TypeToken<HashMap<String, EndpointConfig>>() { |
| 90 | + }.getType()); |
| 91 | + } |
| 92 | + return configs; |
90 | 93 | } |
91 | | - return configs; |
92 | | - } |
93 | 94 | } |
0 commit comments