Skip to content

Commit ae69c9a

Browse files
committed
copy config params
1 parent 1de3c2c commit ae69c9a

File tree

3 files changed

+140
-68
lines changed

3 files changed

+140
-68
lines changed

src/main/java/redis/clients/jedis/DefaultJedisSocketConfig.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,10 @@ static DefaultJedisSocketConfig withSsl(boolean ssl, JedisSocketConfig copy) {
134134
ssl, copy.getSslSocketFactory(), copy.getSslParameters(), copy.getHostnameVerifier(),
135135
copy.getHostAndPortMapper());
136136
}
137+
138+
public static DefaultJedisSocketConfig copyConfig(JedisSocketConfig copy) {
139+
return new DefaultJedisSocketConfig(copy.getConnectionTimeout(), copy.getSoTimeout(),
140+
copy.isSsl(), copy.getSslSocketFactory(), copy.getSslParameters(), copy.getHostnameVerifier(),
141+
copy.getHostAndPortMapper());
142+
}
137143
}

src/main/java/redis/clients/jedis/DefaultJedisSocketFactory.java

Lines changed: 93 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,44 @@
1313

1414
public class DefaultJedisSocketFactory implements JedisSocketFactory {
1515

16-
private HostAndPort hostPort; // TODO: should be final
17-
private final JedisSocketConfig config;
16+
private String host = Protocol.DEFAULT_HOST;
17+
private int port = Protocol.DEFAULT_PORT;
18+
private int connectionTimeout = Protocol.DEFAULT_TIMEOUT;
19+
private int soTimeout = Protocol.DEFAULT_TIMEOUT;
20+
private boolean ssl = false;
21+
private SSLSocketFactory sslSocketFactory = null;
22+
private SSLParameters sslParameters = null;
23+
private HostnameVerifier hostnameVerifier = null;
24+
private HostAndPortMapper hostAndPortMapper = null;
25+
26+
public DefaultJedisSocketFactory() {
27+
}
1828

1929
@Deprecated
2030
public DefaultJedisSocketFactory(String host, int port, int connectionTimeout, int soTimeout,
2131
boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters,
2232
HostnameVerifier hostnameVerifier) {
23-
this(new HostAndPort(host, port),
24-
DefaultJedisSocketConfig.builder()
25-
.withConnectionTimeout(connectionTimeout)
26-
.withSoTimeout(soTimeout)
27-
.withSsl(ssl)
28-
.withSslSocketFactory(sslSocketFactory)
29-
.withSslParameters(sslParameters)
30-
.withHostnameVerifier(hostnameVerifier)
31-
.build()
32-
);
33+
setHost(host);
34+
setPort(port);
35+
setConnectionTimeout(connectionTimeout);
36+
setSoTimeout(soTimeout);
37+
setSsl(ssl);
38+
setSslSocketFactory(sslSocketFactory);
39+
setSslParameters(sslParameters);
40+
setHostnameVerifier(hostnameVerifier);
3341
}
3442

3543
public DefaultJedisSocketFactory(HostAndPort hostAndPort, JedisSocketConfig socketConfig) {
36-
this.hostPort = hostAndPort;
37-
this.config = socketConfig != null ? socketConfig : DefaultJedisSocketConfig.DEFAULT_SOCKET_CONFIG;
44+
setHostAndPort(hostAndPort);
45+
if (socketConfig != null) {
46+
setConnectionTimeout(socketConfig.getConnectionTimeout());
47+
setSoTimeout(socketConfig.getSoTimeout());
48+
setSsl(socketConfig.isSsl());
49+
setSslSocketFactory(socketConfig.getSslSocketFactory());
50+
setSslParameters(socketConfig.getSslParameters());
51+
setHostnameVerifier(socketConfig.getHostnameVerifier());
52+
setHostAndPortMapper(socketConfig.getHostAndPortMapper());
53+
}
3854
}
3955

4056
@Override
@@ -53,19 +69,19 @@ public Socket createSocket() throws JedisConnectionException {
5369
socket.connect(new InetSocketAddress(hostAndPort.getHost(), hostAndPort.getPort()), getConnectionTimeout());
5470
socket.setSoTimeout(getSoTimeout());
5571

56-
if (config.isSsl()) {
57-
SSLSocketFactory sslSocketFactory = config.getSslSocketFactory();
72+
if (ssl) {
73+
SSLSocketFactory sslSocketFactory = getSslSocketFactory();
5874
if (null == sslSocketFactory) {
5975
sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault();
6076
}
6177
socket = sslSocketFactory.createSocket(socket, hostAndPort.getHost(), hostAndPort.getPort(), true);
6278

63-
SSLParameters sslParameters = config.getSslParameters();
79+
SSLParameters sslParameters = getSslParameters();
6480
if (null != sslParameters) {
6581
((SSLSocket) socket).setSSLParameters(sslParameters);
6682
}
6783

68-
HostnameVerifier hostnameVerifier = config.getHostnameVerifier();
84+
HostnameVerifier hostnameVerifier = getHostnameVerifier();
6985
if (null != hostnameVerifier
7086
&& !hostnameVerifier.verify(getHost(), ((SSLSocket) socket).getSession())) {
7187
String message = String.format(
@@ -84,12 +100,8 @@ public Socket createSocket() throws JedisConnectionException {
84100
}
85101
}
86102

87-
public HostAndPort getHostAndPort() {
88-
return this.hostPort;
89-
}
90-
91103
public HostAndPort getSocketHostAndPort() {
92-
HostAndPortMapper mapper = config.getHostAndPortMapper();
104+
HostAndPortMapper mapper = getHostAndPortMapper();
93105
HostAndPort hostAndPort = getHostAndPort();
94106
if (mapper != null) {
95107
HostAndPort mapped = mapper.getHostAndPort(hostAndPort);
@@ -98,58 +110,97 @@ public HostAndPort getSocketHostAndPort() {
98110
return hostAndPort;
99111
}
100112

113+
public HostAndPort getHostAndPort() {
114+
return new HostAndPort(this.host, this.port);
115+
}
116+
117+
public void setHostAndPort(HostAndPort hostPort) {
118+
this.host = hostPort.getHost();
119+
this.port = hostPort.getPort();
120+
}
121+
101122
@Override
102123
public String getDescription() {
103-
return this.hostPort.toString();
124+
return host + ":" + port;
104125
}
105126

106127
@Override
107128
public String getHost() {
108-
return this.hostPort.getHost();
129+
return this.host;
109130
}
110131

111-
/**
112-
* @param host
113-
* @deprecated This method will be removed in future.
114-
*/
115132
@Override
116-
@Deprecated
117133
public void setHost(String host) {
118-
this.hostPort = new HostAndPort(host, this.hostPort.getPort());
134+
this.host = host;
119135
}
120136

121137
@Override
122138
public int getPort() {
123-
return this.hostPort.getPort();
139+
return this.port;
124140
}
125141

126-
/**
127-
* @param port
128-
* @deprecated This method will be removed in future.
129-
*/
130142
@Override
131-
@Deprecated
132143
public void setPort(int port) {
133-
this.hostPort = new HostAndPort(this.hostPort.getHost(), port);
144+
this.port = port;
134145
}
135146

136147
@Override
137148
public int getConnectionTimeout() {
138-
return config.getConnectionTimeout();
149+
return this.connectionTimeout;
139150
}
140151

141152
@Override
142153
public void setConnectionTimeout(int connectionTimeout) {
143-
// throw exception?
154+
this.connectionTimeout = connectionTimeout;
144155
}
145156

146157
@Override
147158
public int getSoTimeout() {
148-
return config.getSoTimeout();
159+
return this.soTimeout;
149160
}
150161

151162
@Override
152163
public void setSoTimeout(int soTimeout) {
153-
// throw exception?
164+
this.soTimeout = soTimeout;
165+
}
166+
167+
public boolean isSsl() {
168+
return ssl;
169+
}
170+
171+
public void setSsl(boolean ssl) {
172+
this.ssl = ssl;
173+
}
174+
175+
public SSLSocketFactory getSslSocketFactory() {
176+
return sslSocketFactory;
177+
}
178+
179+
public void setSslSocketFactory(SSLSocketFactory sslSocketFactory) {
180+
this.sslSocketFactory = sslSocketFactory;
181+
}
182+
183+
public SSLParameters getSslParameters() {
184+
return sslParameters;
185+
}
186+
187+
public void setSslParameters(SSLParameters sslParameters) {
188+
this.sslParameters = sslParameters;
189+
}
190+
191+
public HostnameVerifier getHostnameVerifier() {
192+
return hostnameVerifier;
193+
}
194+
195+
public void setHostnameVerifier(HostnameVerifier hostnameVerifier) {
196+
this.hostnameVerifier = hostnameVerifier;
197+
}
198+
199+
public HostAndPortMapper getHostAndPortMapper() {
200+
return hostAndPortMapper;
201+
}
202+
203+
public void setHostAndPortMapper(HostAndPortMapper hostAndPortMapper) {
204+
this.hostAndPortMapper = hostAndPortMapper;
154205
}
155206
}

src/main/java/redis/clients/jedis/JedisFactory.java

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,12 @@ class JedisFactory implements PooledObjectFactory<Jedis> {
2222

2323
private final AtomicReference<HostAndPort> hostAndPort = new AtomicReference<>();
2424

25-
private final JedisClientConfig clientConfig;
25+
private final JedisSocketConfig socketConfig;
26+
private final int infiniteSoTimeout;
27+
private final String user;
28+
private final String password;
29+
private final int database;
30+
private final String clientName;
2631

2732
JedisFactory(final String host, final int port, final int connectionTimeout,
2833
final int soTimeout, final String password, final int database, final String clientName) {
@@ -55,20 +60,27 @@ class JedisFactory implements PooledObjectFactory<Jedis> {
5560

5661
JedisFactory(final HostAndPort hostAndPort, final JedisClientConfig clientConfig) {
5762
this.hostAndPort.set(hostAndPort);
58-
this.clientConfig = clientConfig;
63+
this.socketConfig = DefaultJedisSocketConfig.copyConfig(clientConfig);
64+
this.infiniteSoTimeout = clientConfig.getInfiniteSoTimeout();
65+
this.user = clientConfig.getUser();
66+
this.password = clientConfig.getPassword();
67+
this.database = clientConfig.getDatabase();
68+
this.clientName = clientConfig.getClientName();
5969
}
6070

6171
JedisFactory(final String host, final int port, final int connectionTimeout, final int soTimeout,
6272
final int infiniteSoTimeout, final String user, final String password, final int database,
6373
final String clientName, final boolean ssl, final SSLSocketFactory sslSocketFactory,
6474
final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) {
65-
this(new HostAndPort(host, port),
66-
DefaultJedisClientConfig.builder().withConnectionTimeout(connectionTimeout)
67-
.withSoTimeout(soTimeout).withInfiniteSoTimeout(infiniteSoTimeout)
68-
.withUser(user).withPassword(password).withDatabse(database).withClientName(clientName)
69-
.withSsl(ssl).withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters)
70-
.withHostnameVerifier(hostnameVerifier).build()
71-
);
75+
this.hostAndPort.set(new HostAndPort(host, port));
76+
this.socketConfig = DefaultJedisSocketConfig.builder().withConnectionTimeout(connectionTimeout)
77+
.withSoTimeout(soTimeout).withSsl(ssl).withSslSocketFactory(sslSocketFactory)
78+
.withSslParameters(sslParameters).withHostnameVerifier(hostnameVerifier).build();
79+
this.infiniteSoTimeout = infiniteSoTimeout;
80+
this.user = user;
81+
this.password = password;
82+
this.database = database;
83+
this.clientName = clientName;
7284
}
7385

7486
JedisFactory(final URI uri, final int connectionTimeout, final int soTimeout,
@@ -90,12 +102,15 @@ class JedisFactory implements PooledObjectFactory<Jedis> {
90102
"Cannot open Redis connection due invalid URI. %s", uri.toString()));
91103
}
92104
this.hostAndPort.set(new HostAndPort(uri.getHost(), uri.getPort()));
93-
this.clientConfig = DefaultJedisClientConfig.builder().withConnectionTimeout(connectionTimeout)
94-
.withSoTimeout(soTimeout).withInfiniteSoTimeout(infiniteSoTimeout)
95-
.withUser(JedisURIHelper.getUser(uri)).withPassword(JedisURIHelper.getPassword(uri))
96-
.withDatabse(JedisURIHelper.getDBIndex(uri)).withClientName(clientName)
97-
.withSsl(JedisURIHelper.isRedisSSLScheme(uri)).withSslSocketFactory(sslSocketFactory)
98-
.withSslParameters(sslParameters).withHostnameVerifier(hostnameVerifier).build();
105+
this.socketConfig = DefaultJedisSocketConfig.builder().withConnectionTimeout(connectionTimeout)
106+
.withSoTimeout(soTimeout).withSsl(JedisURIHelper.isRedisSSLScheme(uri))
107+
.withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters)
108+
.withHostnameVerifier(hostnameVerifier).build();
109+
this.infiniteSoTimeout = infiniteSoTimeout;
110+
this.user = JedisURIHelper.getUser(uri);
111+
this.password = JedisURIHelper.getPassword(uri);
112+
this.database = JedisURIHelper.getDBIndex(uri);
113+
this.clientName = clientName;
99114
}
100115

101116
public void setHostAndPort(final HostAndPort hostAndPort) {
@@ -105,8 +120,8 @@ public void setHostAndPort(final HostAndPort hostAndPort) {
105120
@Override
106121
public void activateObject(PooledObject<Jedis> pooledJedis) throws Exception {
107122
final BinaryJedis jedis = pooledJedis.getObject();
108-
if (jedis.getDB() != clientConfig.getDatabase()) {
109-
jedis.select(clientConfig.getDatabase());
123+
if (jedis.getDB() != database) {
124+
jedis.select(database);
110125
}
111126
}
112127

@@ -128,20 +143,20 @@ public void destroyObject(PooledObject<Jedis> pooledJedis) throws Exception {
128143
@Override
129144
public PooledObject<Jedis> makeObject() throws Exception {
130145
final HostAndPort hostPort = this.hostAndPort.get();
131-
final Jedis jedis = new Jedis(hostPort, (JedisSocketConfig) clientConfig, clientConfig.getInfiniteSoTimeout());
146+
final Jedis jedis = new Jedis(hostPort, (JedisSocketConfig) socketConfig, infiniteSoTimeout);
132147

133148
try {
134149
jedis.connect();
135-
if (clientConfig.getUser() != null) {
136-
jedis.auth(clientConfig.getUser(), clientConfig.getPassword());
137-
} else if (clientConfig.getPassword() != null) {
138-
jedis.auth(clientConfig.getPassword());
150+
if (user != null) {
151+
jedis.auth(user, password);
152+
} else if (password != null) {
153+
jedis.auth(password);
139154
}
140-
if (clientConfig.getDatabase() != 0) {
141-
jedis.select(clientConfig.getDatabase());
155+
if (database != 0) {
156+
jedis.select(database);
142157
}
143-
if (clientConfig.getClientName() != null) {
144-
jedis.clientSetname(clientConfig.getClientName());
158+
if (clientName != null) {
159+
jedis.clientSetname(clientName);
145160
}
146161
} catch (JedisException je) {
147162
try {

0 commit comments

Comments
 (0)