diff --git a/pom.xml b/pom.xml index 1f0d73253d..b063505409 100644 --- a/pom.xml +++ b/pom.xml @@ -129,8 +129,8 @@ maven-compiler-plugin 3.8.1 - 1.7 - 1.7 + 1.8 + 1.8 diff --git a/src/main/java/redis/clients/jedis/BinaryClient.java b/src/main/java/redis/clients/jedis/BinaryClient.java index e52c2ca128..cee6768717 100644 --- a/src/main/java/redis/clients/jedis/BinaryClient.java +++ b/src/main/java/redis/clients/jedis/BinaryClient.java @@ -60,16 +60,22 @@ public BinaryClient(final String host, final int port) { super(host, port); } + @Deprecated public BinaryClient(final String host, final int port, final boolean ssl) { super(host, port, ssl); } + @Deprecated public BinaryClient(final String host, final int port, final boolean ssl, final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { super(host, port, ssl, sslSocketFactory, sslParameters, hostnameVerifier); } + public BinaryClient(final String host, final int port, final JedisSocketConfig jedisSocketConfig) { + super(host, port, jedisSocketConfig); + } + public BinaryClient(final JedisSocketFactory jedisSocketFactory) { super(jedisSocketFactory); } @@ -97,7 +103,9 @@ private byte[][] joinParameters(byte[] first, byte[] second, byte[][] rest) { return result; } - public void setUser(final String user) { this.user = user; } + public void setUser(final String user) { + this.user = user; + } public void setPassword(final String password) { this.password = password; diff --git a/src/main/java/redis/clients/jedis/BinaryJedis.java b/src/main/java/redis/clients/jedis/BinaryJedis.java index 6e73d8864a..5feb0d8a55 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryJedis.java @@ -43,7 +43,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands, MultiKeyBinaryCommands, AdvancedBinaryJedisCommands, BinaryScriptingCommands, Closeable { - protected Client client = null; + protected final Client client; protected Transaction transaction = null; protected Pipeline pipeline = null; protected static final byte[][] DUMMY_ARRAY = new byte[0][]; @@ -55,7 +55,8 @@ public BinaryJedis() { public BinaryJedis(final String host) { URI uri = URI.create(host); if (JedisURIHelper.isValid(uri)) { - initializeClientFromURI(uri); + client = createClientFromURI(uri); + initializeFromURI(uri); } else { client = new Client(host); } @@ -65,18 +66,27 @@ public BinaryJedis(final HostAndPort hp) { this(hp.getHost(), hp.getPort()); } + public BinaryJedis(final HostAndPort hp, final JedisSocketConfig config) { + this(hp.getHost(), hp.getPort(), config); + } + public BinaryJedis(final String host, final int port) { client = new Client(host, port); } + public BinaryJedis(final String host, final int port, final JedisSocketConfig config) { + client = new Client(host, port, config); + } + public BinaryJedis(final String host, final int port, final boolean ssl) { - client = new Client(host, port, ssl); + this(host, port, DefaultJedisSocketConfig.builder().withSsl(ssl).build()); } public BinaryJedis(final String host, final int port, final boolean ssl, final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { - client = new Client(host, port, ssl, sslSocketFactory, sslParameters, hostnameVerifier); + this(host, port, DefaultJedisSocketConfig.builder().withSsl(ssl) + .withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters).withHostnameVerifier(hostnameVerifier).build()); } public BinaryJedis(final String host, final int port, final int timeout) { @@ -95,62 +105,62 @@ public BinaryJedis(final String host, final int port, final int timeout, final b public BinaryJedis(final String host, final int port, final int connectionTimeout, final int soTimeout) { - client = new Client(host, port); - client.setConnectionTimeout(connectionTimeout); - client.setSoTimeout(soTimeout); + this(host, port, DefaultJedisSocketConfig.builder() + .withConnectionTimeout(connectionTimeout).withSoTimeout(soTimeout).build()); } public BinaryJedis(final String host, final int port, final int connectionTimeout, final int soTimeout, final int infiniteSoTimeout) { - client = new Client(host, port); - client.setConnectionTimeout(connectionTimeout); - client.setSoTimeout(soTimeout); + this(host, port, connectionTimeout, soTimeout); client.setInfiniteSoTimeout(infiniteSoTimeout); } public BinaryJedis(final String host, final int port, final int connectionTimeout, final int soTimeout, final boolean ssl) { - client = new Client(host, port, ssl); - client.setConnectionTimeout(connectionTimeout); - client.setSoTimeout(soTimeout); + this(host, port, DefaultJedisSocketConfig.builder() + .withConnectionTimeout(connectionTimeout).withSoTimeout(soTimeout).withSsl(ssl).build()); } public BinaryJedis(final String host, final int port, final int connectionTimeout, final int soTimeout, final boolean ssl, final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { - client = new Client(host, port, ssl, sslSocketFactory, sslParameters, hostnameVerifier); - client.setConnectionTimeout(connectionTimeout); - client.setSoTimeout(soTimeout); + this(host, port, DefaultJedisSocketConfig.builder() + .withConnectionTimeout(connectionTimeout).withSoTimeout(soTimeout).withSsl(ssl) + .withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters).withHostnameVerifier(hostnameVerifier).build()); } public BinaryJedis(final String host, final int port, final int connectionTimeout, final int soTimeout, final int infiniteSoTimeout, final boolean ssl, final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { - client = new Client(host, port, ssl, sslSocketFactory, sslParameters, hostnameVerifier); - client.setConnectionTimeout(connectionTimeout); - client.setSoTimeout(soTimeout); + this(host, port, connectionTimeout, soTimeout, ssl, sslSocketFactory, sslParameters, hostnameVerifier); + client.setInfiniteSoTimeout(infiniteSoTimeout); + } + + public BinaryJedis(final HostAndPort hostAndPort, final JedisSocketConfig config, final int infiniteSoTimeout) { + this(hostAndPort, config); client.setInfiniteSoTimeout(infiniteSoTimeout); } public BinaryJedis(final JedisShardInfo shardInfo) { - client = new Client(shardInfo.getHost(), shardInfo.getPort(), shardInfo.getSsl(), - shardInfo.getSslSocketFactory(), shardInfo.getSslParameters(), - shardInfo.getHostnameVerifier()); - client.setConnectionTimeout(shardInfo.getConnectionTimeout()); - client.setSoTimeout(shardInfo.getSoTimeout()); + this(shardInfo.getHost(), shardInfo.getPort(), shardInfo.getConnectionTimeout(), + shardInfo.getSoTimeout(), shardInfo.getSsl(), shardInfo.getSslSocketFactory(), + shardInfo.getSslParameters(), shardInfo.getHostnameVerifier()); client.setUser(shardInfo.getUser()); client.setPassword(shardInfo.getPassword()); client.setDb(shardInfo.getDb()); } public BinaryJedis(URI uri) { - initializeClientFromURI(uri); + client = createClientFromURI(uri); + initializeFromURI(uri); } public BinaryJedis(URI uri, final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { - initializeClientFromURI(uri, sslSocketFactory, sslParameters, hostnameVerifier); + this(uri, DefaultJedisSocketConfig.builder() + .withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters) + .withHostnameVerifier(hostnameVerifier).build()); } public BinaryJedis(final URI uri, final int timeout) { @@ -163,65 +173,66 @@ public BinaryJedis(final URI uri, final int timeout, final SSLSocketFactory sslS } public BinaryJedis(final URI uri, final int connectionTimeout, final int soTimeout) { - initializeClientFromURI(uri); - client.setConnectionTimeout(connectionTimeout); - client.setSoTimeout(soTimeout); + this(uri, DefaultJedisSocketConfig.builder() + .withConnectionTimeout(connectionTimeout).withSoTimeout(soTimeout).build()); } public BinaryJedis(final URI uri, final int connectionTimeout, final int soTimeout, final SSLSocketFactory sslSocketFactory,final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { - initializeClientFromURI(uri, sslSocketFactory, sslParameters, hostnameVerifier); - client.setConnectionTimeout(connectionTimeout); - client.setSoTimeout(soTimeout); + this(uri, DefaultJedisSocketConfig.builder() + .withConnectionTimeout(connectionTimeout).withSoTimeout(soTimeout) + .withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters) + .withHostnameVerifier(hostnameVerifier).build()); } public BinaryJedis(final URI uri, final int connectionTimeout, final int soTimeout, final int infiniteSoTimeout, final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { - initializeClientFromURI(uri, sslSocketFactory, sslParameters, hostnameVerifier); - client.setConnectionTimeout(connectionTimeout); - client.setSoTimeout(soTimeout); + this(uri, connectionTimeout, soTimeout, sslSocketFactory, sslParameters, hostnameVerifier); client.setInfiniteSoTimeout(infiniteSoTimeout); } - public BinaryJedis(final JedisSocketFactory jedisSocketFactory) { - client = new Client(jedisSocketFactory); + public BinaryJedis(final URI uri, JedisSocketConfig config) { + if (!JedisURIHelper.isValid(uri)) { + throw new InvalidURIException(String.format("Cannot open Redis connection due invalid URI \"%s\".", uri.toString())); + } + client = new Client(uri.getHost(), uri.getPort(), + DefaultJedisSocketConfig.withSsl(JedisURIHelper.isRedisSSLScheme(uri), config)); + initializeFromURI(uri); } - private void initializeClientFromURI(URI uri) { - initializeClientFromURI(uri, null, null, null); + public BinaryJedis(final JedisSocketFactory jedisSocketFactory) { + client = new Client(jedisSocketFactory); } - private void initializeClientFromURI(URI uri, final SSLSocketFactory sslSocketFactory, - final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { + private static Client createClientFromURI(URI uri) { if (!JedisURIHelper.isValid(uri)) { - throw new InvalidURIException(String.format( - "Cannot open Redis connection due invalid URI. %s", uri.toString())); + throw new InvalidURIException(String.format("Cannot open Redis connection due invalid URI \"%s\".", uri.toString())); } + return new Client(uri.getHost(), uri.getPort(), + DefaultJedisSocketConfig.builder().withSsl(JedisURIHelper.isRedisSSLScheme(uri)).build()); + } - client = new Client(uri.getHost(), uri.getPort(), JedisURIHelper.isRedisSSLScheme(uri), - sslSocketFactory, sslParameters, hostnameVerifier); - + private void initializeFromURI(URI uri) { String password = JedisURIHelper.getPassword(uri); if (password != null) { String user = JedisURIHelper.getUser(uri); - if (user == null) { - client.auth(password); + if (user != null) { + auth(user, password); } else { - client.auth(user, password); + auth(password); } - client.getStatusCodeReply(); } - int dbIndex = JedisURIHelper.getDBIndex(uri); if (dbIndex > 0) { - client.select(dbIndex); - client.getStatusCodeReply(); - client.setDb(dbIndex); + select(dbIndex); } } + /** + * @return PONG + */ @Override public String ping() { checkIsInMultiOrPipeline(); @@ -230,7 +241,7 @@ public String ping() { } /** - * Works same as ping() but returns argument message instead of PONG. + * Works same as {@link #ping()} but returns argument message instead of PONG. * @param message * @return message */ @@ -1430,6 +1441,7 @@ public Long lpos(final byte[] key, final byte[] element, final LPosParams params * @see #lpos(byte[], byte[], LPosParams, long) * @param key * @param element + * @param params * @param count * @return Returns value will be a list containing position of the matching elements inside the list. */ diff --git a/src/main/java/redis/clients/jedis/BinaryJedisCluster.java b/src/main/java/redis/clients/jedis/BinaryJedisCluster.java index c7973d4c88..db6cabf75c 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedisCluster.java +++ b/src/main/java/redis/clients/jedis/BinaryJedisCluster.java @@ -84,21 +84,23 @@ public BinaryJedisCluster(Set jedisClusterNode, int connectionTimeo this(jedisClusterNode, connectionTimeout, soTimeout, maxAttempts, user, password, clientName, poolConfig, ssl, null, null, null, null); } + @Deprecated public BinaryJedisCluster(Set jedisClusterNode, int connectionTimeout, int soTimeout, int maxAttempts, String password, String clientName, GenericObjectPoolConfig poolConfig, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap hostAndPortMap) { this(jedisClusterNode, connectionTimeout, soTimeout, maxAttempts, null, password, clientName, poolConfig, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); } + @Deprecated public BinaryJedisCluster(Set jedisClusterNode, int connectionTimeout, int soTimeout, int maxAttempts, String user, String password, String clientName, GenericObjectPoolConfig poolConfig, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap hostAndPortMap) { - this.connectionHandler = new JedisSlotBasedConnectionHandler(jedisClusterNode, poolConfig, - connectionTimeout, soTimeout, user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); - this.maxAttempts = maxAttempts; + this(jedisClusterNode, connectionTimeout, soTimeout, 0, maxAttempts, user, password, clientName, poolConfig, + ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); } + @Deprecated public BinaryJedisCluster(Set jedisClusterNode, int connectionTimeout, int soTimeout, int infiniteSoTimeout, int maxAttempts, String user, String password, String clientName, GenericObjectPoolConfig poolConfig, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap hostAndPortMap) { @@ -107,6 +109,14 @@ public BinaryJedisCluster(Set jedisClusterNode, int connectionTimeo this.maxAttempts = maxAttempts; } + public BinaryJedisCluster(Set jedisClusterNode, JedisSocketConfig socketConfig, + int infiniteSoTimeout, int maxAttempts, String user, String password, String clientName, + GenericObjectPoolConfig poolConfig) { + this.connectionHandler = new JedisSlotBasedConnectionHandler(jedisClusterNode, poolConfig, + socketConfig, infiniteSoTimeout, user, password, clientName); + this.maxAttempts = maxAttempts; + } + @Override public void close() { if (connectionHandler != null) { diff --git a/src/main/java/redis/clients/jedis/Client.java b/src/main/java/redis/clients/jedis/Client.java index 5e69b8f4f6..f53d3e1ddf 100644 --- a/src/main/java/redis/clients/jedis/Client.java +++ b/src/main/java/redis/clients/jedis/Client.java @@ -37,16 +37,22 @@ public Client(final String host, final int port) { super(host, port); } + @Deprecated public Client(final String host, final int port, final boolean ssl) { super(host, port, ssl); } + @Deprecated public Client(final String host, final int port, final boolean ssl, final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { super(host, port, ssl, sslSocketFactory, sslParameters, hostnameVerifier); } + public Client(final String host, final int port, final JedisSocketConfig jedisSocketConfig) { + super(host, port, jedisSocketConfig); + } + public Client(final JedisSocketFactory jedisSocketFactory) { super(jedisSocketFactory); } diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index 6d533c549a..4ea766a1a2 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -23,7 +23,7 @@ public class Connection implements Closeable { private static final byte[][] EMPTY_ARGS = new byte[0][]; - private JedisSocketFactory jedisSocketFactory; + private final JedisSocketFactory socketFactory; private Socket socket; private RedisOutputStream outputStream; private RedisInputStream inputStream; @@ -39,22 +39,33 @@ public Connection(final String host) { } public Connection(final String host, final int port) { - this(host, port, false); + this(host, port, DefaultJedisSocketConfig.DEFAULT_SOCKET_CONFIG); } + @Deprecated public Connection(final String host, final int port, final boolean ssl) { - this(host, port, ssl, null, null, null); + this(host, port, DefaultJedisSocketConfig.builder().withSsl(ssl).build()); } + @Deprecated public Connection(final String host, final int port, final boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier) { - this(new DefaultJedisSocketFactory(host, port, Protocol.DEFAULT_TIMEOUT, - Protocol.DEFAULT_TIMEOUT, ssl, sslSocketFactory, sslParameters, hostnameVerifier)); + this(host, port, DefaultJedisSocketConfig.builder().withSsl(ssl) + .withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters) + .withHostnameVerifier(hostnameVerifier).build()); + } + + public Connection(final String host, final int port, final JedisSocketConfig jedisSocketConfig) { + this(new HostAndPort(host, port), jedisSocketConfig); + } + + public Connection(final HostAndPort hostAndPort, final JedisSocketConfig jedisSocketConfig) { + this(new DefaultJedisSocketFactory(hostAndPort, jedisSocketConfig)); } public Connection(final JedisSocketFactory jedisSocketFactory) { - this.jedisSocketFactory = jedisSocketFactory; + this.socketFactory = jedisSocketFactory; } public Socket getSocket() { @@ -62,19 +73,21 @@ public Socket getSocket() { } public int getConnectionTimeout() { - return jedisSocketFactory.getConnectionTimeout(); + return socketFactory.getConnectionTimeout(); } public int getSoTimeout() { - return jedisSocketFactory.getSoTimeout(); + return socketFactory.getSoTimeout(); } + @Deprecated public void setConnectionTimeout(int connectionTimeout) { - jedisSocketFactory.setConnectionTimeout(connectionTimeout); + socketFactory.setConnectionTimeout(connectionTimeout); } + @Deprecated public void setSoTimeout(int soTimeout) { - jedisSocketFactory.setSoTimeout(soTimeout); + socketFactory.setSoTimeout(soTimeout); } public void setInfiniteSoTimeout(int infiniteSoTimeout) { @@ -95,7 +108,7 @@ public void setTimeoutInfinite() { public void rollbackTimeout() { try { - socket.setSoTimeout(jedisSocketFactory.getSoTimeout()); + socket.setSoTimeout(socketFactory.getSoTimeout()); } catch (SocketException ex) { broken = true; throw new JedisConnectionException(ex); @@ -142,32 +155,40 @@ public void sendCommand(final ProtocolCommand cmd, final byte[]... args) { } public String getHost() { - return jedisSocketFactory.getHost(); + return socketFactory.getHost(); } + @Deprecated public void setHost(final String host) { - jedisSocketFactory.setHost(host); + socketFactory.setHost(host); } public int getPort() { - return jedisSocketFactory.getPort(); + return socketFactory.getPort(); } + @Deprecated public void setPort(final int port) { - jedisSocketFactory.setPort(port); + socketFactory.setPort(port); } public void connect() { if (!isConnected()) { try { - socket = jedisSocketFactory.createSocket(); + socket = socketFactory.createSocket(); outputStream = new RedisOutputStream(socket.getOutputStream()); inputStream = new RedisInputStream(socket.getInputStream()); - } catch (IOException ex) { + } catch (IOException ioe) { broken = true; - throw new JedisConnectionException("Failed connecting to " - + jedisSocketFactory.getDescription(), ex); + throw new JedisConnectionException("Failed to create input/output stream", ioe); + } catch (JedisConnectionException jce) { + broken = true; + throw jce; + } finally { + if (broken) { + IOUtils.closeQuietly(socket); + } } } } diff --git a/src/main/java/redis/clients/jedis/DefaultJedisSocketConfig.java b/src/main/java/redis/clients/jedis/DefaultJedisSocketConfig.java new file mode 100644 index 0000000000..9da0f6dbaf --- /dev/null +++ b/src/main/java/redis/clients/jedis/DefaultJedisSocketConfig.java @@ -0,0 +1,165 @@ +package redis.clients.jedis; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLParameters; +import javax.net.ssl.SSLSocketFactory; + +public class DefaultJedisSocketConfig implements JedisSocketConfig { + + public static final DefaultJedisSocketConfig DEFAULT_SOCKET_CONFIG = DefaultJedisSocketConfig.builder().build(); + + private final int connectionTimeout; + private final int soTimeout; + + private final boolean ssl; + private final SSLSocketFactory sslSocketFactory; + private final SSLParameters sslParameters; + private final HostnameVerifier hostnameVerifier; + + private final HostAndPortMapper hostAndPortMapper; + + private DefaultJedisSocketConfig(int connectionTimeout, int soTimeout, boolean ssl, + SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, + HostnameVerifier hostnameVerifier, HostAndPortMapper hostAndPortMapper) { + this.connectionTimeout = connectionTimeout; + this.soTimeout = soTimeout; + this.ssl = ssl; + this.sslSocketFactory = sslSocketFactory; + this.sslParameters = sslParameters; + this.hostnameVerifier = hostnameVerifier; + this.hostAndPortMapper = hostAndPortMapper; + } + + @Override + public int getConnectionTimeout() { + return connectionTimeout; + } + + @Override + public int getSoTimeout() { + return soTimeout; + } + + @Override + public boolean isSSL() { + return ssl; + } + + @Override + public SSLSocketFactory getSSLSocketFactory() { + return sslSocketFactory; + } + + @Override + public SSLParameters getSSLParameters() { + return sslParameters; + } + + @Override + public HostnameVerifier getHostnameVerifier() { + return hostnameVerifier; + } + + @Override + public HostAndPortMapper getHostAndPortMapper() { + return hostAndPortMapper; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + + private int connectionTimeout = Protocol.DEFAULT_TIMEOUT; + private int soTimeout = Protocol.DEFAULT_TIMEOUT; + + private boolean ssl = false; + private SSLSocketFactory sslSocketFactory = null; + private SSLParameters sslParameters = null; + private HostnameVerifier hostnameVerifier = null; + + private HostAndPortMapper hostAndPortMapper = null; + + private Builder() { + } + + public DefaultJedisSocketConfig build() { + return new DefaultJedisSocketConfig(connectionTimeout, soTimeout, ssl, + sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMapper); + } + + public Builder withTimeout(int timeout) { + return withConnectionTimeout(timeout).withSoTimeout(timeout); + } + + public Builder withConnectionTimeout(int connectionTimeout) { + this.connectionTimeout = connectionTimeout; + return this; + } + + public Builder withSoTimeout(int soTimeout) { + this.soTimeout = soTimeout; + return this; + } + + public Builder withSsl(boolean ssl) { + this.ssl = ssl; + return this; + } + + public Builder withSslSocketFactory(SSLSocketFactory sslSocketFactory) { + this.sslSocketFactory = sslSocketFactory; + return this; + } + + public Builder withSslParameters(SSLParameters sslParameters) { + this.sslParameters = sslParameters; + return this; + } + + public Builder withHostnameVerifier(HostnameVerifier hostnameVerifier) { + this.hostnameVerifier = hostnameVerifier; + return this; + } + + public Builder withHostAndPortMapper(HostAndPortMapper hostAndPortMapper) { + this.hostAndPortMapper = hostAndPortMapper; + return this; + } + + public int getConnectionTimeout() { + return connectionTimeout; + } + + public int getSoTimeout() { + return soTimeout; + } + + public boolean isSsl() { + return ssl; + } + + public SSLSocketFactory getSslSocketFactory() { + return sslSocketFactory; + } + + public SSLParameters getSslParameters() { + return sslParameters; + } + + public HostnameVerifier getHostnameVerifier() { + return hostnameVerifier; + } + + public HostAndPortMapper getHostAndPortMapper() { + return hostAndPortMapper; + } + } + + static DefaultJedisSocketConfig withSsl(boolean ssl, JedisSocketConfig copy) { + return new DefaultJedisSocketConfig(copy.getConnectionTimeout(), copy.getSoTimeout(), + ssl, copy.getSSLSocketFactory(), copy.getSSLParameters(), copy.getHostnameVerifier(), + copy.getHostAndPortMapper()); + } +} diff --git a/src/main/java/redis/clients/jedis/DefaultJedisSocketFactory.java b/src/main/java/redis/clients/jedis/DefaultJedisSocketFactory.java index 1c914962a9..fa6a27b3e6 100644 --- a/src/main/java/redis/clients/jedis/DefaultJedisSocketFactory.java +++ b/src/main/java/redis/clients/jedis/DefaultJedisSocketFactory.java @@ -1,124 +1,145 @@ package redis.clients.jedis; -import redis.clients.jedis.exceptions.JedisConnectionException; - +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLParameters; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; -import java.io.IOException; -import java.net.InetSocketAddress; -import java.net.Socket; + +import redis.clients.jedis.exceptions.JedisConnectionException; +import redis.clients.jedis.util.IOUtils; public class DefaultJedisSocketFactory implements JedisSocketFactory { - private String host; - private int port; - private int connectionTimeout; - private int soTimeout; - private boolean ssl; - private SSLSocketFactory sslSocketFactory; - private SSLParameters sslParameters; - private HostnameVerifier hostnameVerifier; + private final HostAndPort hostPort; + private final JedisSocketConfig config; + @Deprecated public DefaultJedisSocketFactory(String host, int port, int connectionTimeout, int soTimeout, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier) { - this.host = host; - this.port = port; - this.connectionTimeout = connectionTimeout; - this.soTimeout = soTimeout; - this.ssl = ssl; - this.sslSocketFactory = sslSocketFactory; - this.sslParameters = sslParameters; - this.hostnameVerifier = hostnameVerifier; + this(new HostAndPort(host, port), + DefaultJedisSocketConfig.builder() + .withConnectionTimeout(connectionTimeout) + .withSoTimeout(soTimeout) + .withSsl(ssl) + .withSslSocketFactory(sslSocketFactory) + .withSslParameters(sslParameters) + .withHostnameVerifier(hostnameVerifier) + .build() + ); + } + + public DefaultJedisSocketFactory(HostAndPort hostAndPort, JedisSocketConfig socketConfig) { + this.hostPort = hostAndPort; + this.config = socketConfig != null ? socketConfig : DefaultJedisSocketConfig.DEFAULT_SOCKET_CONFIG; } @Override - public Socket createSocket() throws IOException { + public Socket createSocket() throws JedisConnectionException { Socket socket = null; try { socket = new Socket(); // ->@wjw_add socket.setReuseAddress(true); - socket.setKeepAlive(true); // Will monitor the TCP connection is - // valid - socket.setTcpNoDelay(true); // Socket buffer Whetherclosed, to - // ensure timely delivery of data - socket.setSoLinger(true, 0); // Control calls close () method, - // the underlying socket is closed - // immediately + socket.setKeepAlive(true); // Will monitor the TCP connection is valid + socket.setTcpNoDelay(true); // Socket buffer Whetherclosed, to ensure timely delivery of data + socket.setSoLinger(true, 0); // Control calls close () method, the underlying socket is closed immediately // <-@wjw_add - socket.connect(new InetSocketAddress(getHost(), getPort()), getConnectionTimeout()); + HostAndPort hostAndPort = getSocketHostAndPort(); + socket.connect(new InetSocketAddress(hostAndPort.getHost(), hostAndPort.getPort()), getConnectionTimeout()); socket.setSoTimeout(getSoTimeout()); - if (ssl) { + if (config.isSSL()) { + SSLSocketFactory sslSocketFactory = config.getSSLSocketFactory(); if (null == sslSocketFactory) { sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); } - socket = sslSocketFactory.createSocket(socket, getHost(), getPort(), true); + socket = sslSocketFactory.createSocket(socket, hostAndPort.getHost(), hostAndPort.getPort(), true); + + SSLParameters sslParameters = config.getSSLParameters(); if (null != sslParameters) { ((SSLSocket) socket).setSSLParameters(sslParameters); } - if ((null != hostnameVerifier) - && (!hostnameVerifier.verify(getHost(), ((SSLSocket) socket).getSession()))) { + + HostnameVerifier hostnameVerifier = config.getHostnameVerifier(); + if (null != hostnameVerifier + && !hostnameVerifier.verify(getHost(), ((SSLSocket) socket).getSession())) { String message = String.format( - "The connection to '%s' failed ssl/tls hostname verification.", getHost()); + "The connection to '%s' failed ssl/tls hostname verification.", getHost()); throw new JedisConnectionException(message); } } + return socket; - } catch (Exception ex) { - if (socket != null) { - socket.close(); - } - throw ex; + + } catch (IOException ex) { + + IOUtils.closeQuietly(socket); + + throw new JedisConnectionException("Failed to create socket.", ex); + } + } + + public HostAndPort getHostAndPort() { + return this.hostPort; + } + + public HostAndPort getSocketHostAndPort() { + HostAndPortMapper mapper = config.getHostAndPortMapper(); + HostAndPort hostAndPort = getHostAndPort(); + if (mapper != null) { + HostAndPort mapped = mapper.getHostAndPort(hostAndPort); + if (mapped != null) return mapped; } + return hostAndPort; } @Override public String getDescription() { - return host + ":" + port; + return this.hostPort.toString(); } @Override public String getHost() { - return host; + return this.hostPort.getHost(); } @Override public void setHost(String host) { - this.host = host; + // throw exception? } @Override public int getPort() { - return port; + return this.hostPort.getPort(); } @Override public void setPort(int port) { - this.port = port; + // throw exception? } @Override public int getConnectionTimeout() { - return connectionTimeout; + return config.getConnectionTimeout(); } @Override public void setConnectionTimeout(int connectionTimeout) { - this.connectionTimeout = connectionTimeout; + // throw exception? } @Override public int getSoTimeout() { - return soTimeout; + return config.getSoTimeout(); } @Override public void setSoTimeout(int soTimeout) { - this.soTimeout = soTimeout; + // throw exception? } } diff --git a/src/main/java/redis/clients/jedis/HostAndPortMapper.java b/src/main/java/redis/clients/jedis/HostAndPortMapper.java new file mode 100644 index 0000000000..68727d9ca6 --- /dev/null +++ b/src/main/java/redis/clients/jedis/HostAndPortMapper.java @@ -0,0 +1,6 @@ +package redis.clients.jedis; + +public interface HostAndPortMapper { + + HostAndPort getHostAndPort(HostAndPort hap); +} diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index 14eb19b723..630400efb9 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -49,6 +49,10 @@ public Jedis(final HostAndPort hp) { super(hp); } + public Jedis(final HostAndPort hp, final JedisSocketConfig config) { + super(hp, config); + } + public Jedis(final String host, final int port) { super(host, port); } @@ -86,6 +90,10 @@ public Jedis(final String host, final int port, final int connectionTimeout, fin super(host, port, connectionTimeout, soTimeout, infiniteSoTimeout); } + public Jedis(final HostAndPort hostAndPort, final JedisSocketConfig config, final int infiniteSoTimeout) { + super(hostAndPort, config, infiniteSoTimeout); + } + public Jedis(final String host, final int port, final int connectionTimeout, final int soTimeout, final boolean ssl) { super(host, port, connectionTimeout, soTimeout, ssl); @@ -143,6 +151,10 @@ public Jedis(final URI uri, final int connectionTimeout, final int soTimeout, super(uri, connectionTimeout, soTimeout, infiniteSoTimeout, sslSocketFactory, sslParameters, hostnameVerifier); } + public Jedis(final URI uri, JedisSocketConfig config) { + super(uri, config); + } + public Jedis(final JedisSocketFactory jedisSocketFactory) { super(jedisSocketFactory); } diff --git a/src/main/java/redis/clients/jedis/JedisCluster.java b/src/main/java/redis/clients/jedis/JedisCluster.java index 7f9d6e3921..8b6c71439f 100644 --- a/src/main/java/redis/clients/jedis/JedisCluster.java +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -193,6 +193,12 @@ public JedisCluster(Set jedisClusterNode, int connectionTimeout, in clientName, poolConfig, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); } + public JedisCluster(Set jedisClusterNode, JedisSocketConfig socketConfig, + int infiniteSoTimeout, int maxAttempts, String user, String password, String clientName, + final GenericObjectPoolConfig poolConfig) { + super(jedisClusterNode, socketConfig, infiniteSoTimeout, maxAttempts, user, password, clientName, poolConfig); + } + @Override public String set(final String key, final String value) { return new JedisClusterCommand(connectionHandler, maxAttempts) { diff --git a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java index 645412e71d..e07a1defb5 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java +++ b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java @@ -34,6 +34,12 @@ public JedisClusterConnectionHandler(Set nodes, final GenericObject this(nodes, poolConfig, connectionTimeout, soTimeout, infiniteSoTimeout, user, password, clientName, false, null, null, null, null); } + public JedisClusterConnectionHandler(Set nodes, final GenericObjectPoolConfig poolConfig, + final JedisSocketConfig socketConfig, String user, String password, String clientName) { + this(nodes, poolConfig, socketConfig, 0, user, password, clientName); + } + + @Deprecated public JedisClusterConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, String password, String clientName, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, @@ -41,6 +47,14 @@ public JedisClusterConnectionHandler(Set nodes, GenericObjectPoolCo this(nodes, poolConfig, connectionTimeout, soTimeout, null, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap); } + public JedisClusterConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, + int connectionTimeout, int soTimeout, String password, String clientName, + boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, + HostnameVerifier hostnameVerifier, HostAndPortMapper portMap) { + this(nodes, poolConfig, connectionTimeout, soTimeout, null, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap); + } + + @Deprecated public JedisClusterConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, String user, String password, String clientName, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, @@ -48,14 +62,52 @@ public JedisClusterConnectionHandler(Set nodes, GenericObjectPoolCo this(nodes, poolConfig, connectionTimeout, soTimeout, 0, user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap); } + public JedisClusterConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, + int connectionTimeout, int soTimeout, String user, String password, String clientName, + boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, + HostnameVerifier hostnameVerifier, HostAndPortMapper portMap) { + this(nodes, poolConfig, connectionTimeout, soTimeout, 0, user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap); + } + + @Deprecated public JedisClusterConnectionHandler(Set nodes, final GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, int infiniteSoTimeout, String user, String password, String clientName, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap portMap) { - this.cache = new JedisClusterInfoCache(poolConfig, connectionTimeout, soTimeout, infiniteSoTimeout, - user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap); - initializeSlotsCache(nodes, connectionTimeout, soTimeout, infiniteSoTimeout, - user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier); + this(nodes, + DefaultJedisSocketConfig.builder().withConnectionTimeout(connectionTimeout).withSoTimeout(soTimeout) + .withSsl(ssl).withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters) + .withHostnameVerifier(hostnameVerifier).build(), + poolConfig, + DefaultJedisSocketConfig.builder().withConnectionTimeout(connectionTimeout).withSoTimeout(soTimeout) + .withSsl(ssl).withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters) + .withHostnameVerifier(hostnameVerifier).withHostAndPortMapper(portMap).build(), + infiniteSoTimeout, user, password, clientName); + } + + public JedisClusterConnectionHandler(Set nodes, final GenericObjectPoolConfig poolConfig, + int connectionTimeout, int soTimeout, int infiniteSoTimeout, String user, String password, String clientName, + boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, + HostnameVerifier hostnameVerifier, HostAndPortMapper portMap) { + this(nodes, poolConfig, + DefaultJedisSocketConfig.builder().withConnectionTimeout(connectionTimeout).withSoTimeout(soTimeout) + .withSsl(ssl).withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters) + .withHostnameVerifier(hostnameVerifier).withHostAndPortMapper(portMap).build(), + infiniteSoTimeout, user, password, clientName); + } + + @Deprecated + public JedisClusterConnectionHandler(Set nodes, final JedisSocketConfig seedNodesSocketConfig, + final GenericObjectPoolConfig poolConfig, final JedisSocketConfig clusterNodesSocketConfig, + int infiniteSoTimeout, String user, String password, String clientName) { + this.cache = new JedisClusterInfoCache(poolConfig, clusterNodesSocketConfig, infiniteSoTimeout, user, password, clientName); + initializeSlotsCache(nodes, seedNodesSocketConfig, infiniteSoTimeout, user, password, clientName); + } + + public JedisClusterConnectionHandler(Set nodes, final GenericObjectPoolConfig poolConfig, + final JedisSocketConfig socketConfig, int infiniteSoTimeout, String user, String password, String clientName) { + this.cache = new JedisClusterInfoCache(poolConfig, socketConfig, infiniteSoTimeout, user, password, clientName); + initializeSlotsCache(nodes, socketConfig, infiniteSoTimeout, user, password, clientName); } abstract Jedis getConnection(); @@ -70,14 +122,11 @@ public Map getNodes() { return cache.getNodes(); } - private void initializeSlotsCache(Set startNodes, - int connectionTimeout, int soTimeout, int infiniteSoTimeout, String user, String password, String clientName, - boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier) { - for (HostAndPort hostAndPort : startNodes) { - - try (Jedis jedis = new Jedis(hostAndPort.getHost(), hostAndPort.getPort(), connectionTimeout, - soTimeout, infiniteSoTimeout, ssl, sslSocketFactory, sslParameters, hostnameVerifier)) { + private void initializeSlotsCache(Set startNodes, final JedisSocketConfig socketConfig, + int infiniteSoTimeout, String user, String password, String clientName) { + for (HostAndPort hostAndPort : startNodes) { + try (Jedis jedis = new Jedis(hostAndPort, socketConfig, infiniteSoTimeout)) { if (user != null) { jedis.auth(user, password); } else if (password != null) { diff --git a/src/main/java/redis/clients/jedis/JedisClusterHostAndPortMap.java b/src/main/java/redis/clients/jedis/JedisClusterHostAndPortMap.java index 9a5a5029cb..79fee84390 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterHostAndPortMap.java +++ b/src/main/java/redis/clients/jedis/JedisClusterHostAndPortMap.java @@ -1,5 +1,15 @@ package redis.clients.jedis; -public interface JedisClusterHostAndPortMap { +/** + * @deprecated Use HostAndPortMapper + */ +@Deprecated +public interface JedisClusterHostAndPortMap extends HostAndPortMapper { + HostAndPort getSSLHostAndPort(String host, int port); + + @Override + default HostAndPort getHostAndPort(HostAndPort hap) { + return getSSLHostAndPort(hap.getHost(), hap.getPort()); + } } diff --git a/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java b/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java index b5242dd1e3..cfdde4bd55 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java +++ b/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java @@ -27,19 +27,12 @@ public class JedisClusterInfoCache { private volatile boolean rediscovering; private final GenericObjectPoolConfig poolConfig; - private int connectionTimeout; - private int soTimeout; + private final JedisSocketConfig socketConfig; private int infiniteSoTimeout; private String user; private String password; private String clientName; - private boolean ssl; - private SSLSocketFactory sslSocketFactory; - private SSLParameters sslParameters; - private HostnameVerifier hostnameVerifier; - private JedisClusterHostAndPortMap hostAndPortMap; - private static final int MASTER_NODE_INDEX = 2; public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, int timeout) { @@ -58,15 +51,16 @@ public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, final String user, final String password, final String clientName) { - this(poolConfig, connectionTimeout, soTimeout, user, password, clientName, false, null, null, null, null); + this(poolConfig, connectionTimeout, soTimeout, user, password, clientName, false, null, null, null, (HostAndPortMapper) null); } public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, final int infiniteSoTimeout, final String user, final String password, final String clientName) { - this(poolConfig, connectionTimeout, soTimeout, infiniteSoTimeout, user, password, clientName, false, null, null, null, null); + this(poolConfig, connectionTimeout, soTimeout, infiniteSoTimeout, user, password, clientName, false, null, null, null, (HostAndPortMapper) null); } + @Deprecated public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, final String password, final String clientName, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, @@ -74,6 +68,14 @@ public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int this(poolConfig, connectionTimeout, soTimeout, null, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); } + public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int connectionTimeout, + final int soTimeout, final String password, final String clientName, + boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, + HostnameVerifier hostnameVerifier, HostAndPortMapper hostAndPortMap) { + this(poolConfig, connectionTimeout, soTimeout, null, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); + } + + @Deprecated public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, final String user, final String password, final String clientName, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, @@ -81,23 +83,43 @@ public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int this(poolConfig, connectionTimeout, soTimeout, 0, user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); } + public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int connectionTimeout, + final int soTimeout, final String user, final String password, final String clientName, + boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, + HostnameVerifier hostnameVerifier, HostAndPortMapper hostAndPortMap) { + this(poolConfig, connectionTimeout, soTimeout, 0, user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); + } + + @Deprecated public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, final int infiniteSoTimeout, final String user, final String password, final String clientName, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap hostAndPortMap) { + this(poolConfig, connectionTimeout, soTimeout, infiniteSoTimeout, user, password, clientName, + ssl, sslSocketFactory, sslParameters, hostnameVerifier, (HostAndPortMapper) hostAndPortMap); + } + + public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, + final int connectionTimeout, final int soTimeout, final int infiniteSoTimeout, + final String user, final String password, final String clientName, boolean ssl, + SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, + HostnameVerifier hostnameVerifier, HostAndPortMapper hostAndPortMap) { + this(poolConfig, DefaultJedisSocketConfig.builder().withConnectionTimeout(connectionTimeout) + .withSoTimeout(soTimeout).withSsl(ssl).withSslSocketFactory(sslSocketFactory) + .withSslParameters(sslParameters).withHostnameVerifier(hostnameVerifier) + .withHostAndPortMapper(hostAndPortMap).build(), infiniteSoTimeout, user, password, clientName); + } + + public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, + final JedisSocketConfig socketConfig, final int infiniteSoTimeout, + final String user, final String password, final String clientName) { this.poolConfig = poolConfig; - this.connectionTimeout = connectionTimeout; - this.soTimeout = soTimeout; + this.socketConfig = socketConfig; this.infiniteSoTimeout = infiniteSoTimeout; this.user = user; this.password = password; this.clientName = clientName; - this.ssl = ssl; - this.sslSocketFactory = sslSocketFactory; - this.sslParameters = sslParameters; - this.hostnameVerifier = hostnameVerifier; - this.hostAndPortMap = hostAndPortMap; } public void discoverClusterNodesAndSlots(Jedis jedis) { @@ -206,25 +228,17 @@ private void discoverClusterSlots(Jedis jedis) { private HostAndPort generateHostAndPort(List hostInfos) { String host = SafeEncoder.encode((byte[]) hostInfos.get(0)); int port = ((Long) hostInfos.get(1)).intValue(); - if (ssl && hostAndPortMap != null) { - HostAndPort hostAndPort = hostAndPortMap.getSSLHostAndPort(host, port); - if (hostAndPort != null) { - return hostAndPort; - } - } return new HostAndPort(host, port); } - public JedisPool setupNodeIfNotExist(HostAndPort node) { + public JedisPool setupNodeIfNotExist(final HostAndPort node) { w.lock(); try { String nodeKey = getNodeKey(node); JedisPool existingPool = nodes.get(nodeKey); if (existingPool != null) return existingPool; - JedisPool nodePool = new JedisPool(poolConfig, node.getHost(), node.getPort(), - connectionTimeout, soTimeout, infiniteSoTimeout, user, password, 0, clientName, - ssl, sslSocketFactory, sslParameters, hostnameVerifier); + JedisPool nodePool = new JedisPool(poolConfig, node, socketConfig, infiniteSoTimeout, user, password, 0, clientName); nodes.put(nodeKey, nodePool); return nodePool; } finally { @@ -318,10 +332,12 @@ public static String getNodeKey(HostAndPort hnp) { return hnp.getHost() + ":" + hnp.getPort(); } + @Deprecated public static String getNodeKey(Client client) { return client.getHost() + ":" + client.getPort(); } + @Deprecated public static String getNodeKey(Jedis jedis) { return getNodeKey(jedis.getClient()); } diff --git a/src/main/java/redis/clients/jedis/JedisFactory.java b/src/main/java/redis/clients/jedis/JedisFactory.java index 5cc0aa283a..48b1fec030 100644 --- a/src/main/java/redis/clients/jedis/JedisFactory.java +++ b/src/main/java/redis/clients/jedis/JedisFactory.java @@ -19,18 +19,15 @@ * PoolableObjectFactory custom impl. */ class JedisFactory implements PooledObjectFactory { + private final AtomicReference hostAndPort = new AtomicReference<>(); - private final int connectionTimeout; - private final int soTimeout; + + private final JedisSocketConfig socketConfig; private final int infiniteSoTimeout; private final String user; private final String password; private final int database; private final String clientName; - private final boolean ssl; - private final SSLSocketFactory sslSocketFactory; - private final SSLParameters sslParameters; - private final HostnameVerifier hostnameVerifier; JedisFactory(final String host, final int port, final int connectionTimeout, final int soTimeout, final String password, final int database, final String clientName) { @@ -65,18 +62,23 @@ class JedisFactory implements PooledObjectFactory { final int infiniteSoTimeout, final String user, final String password, final int database, final String clientName, final boolean ssl, final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { - this.hostAndPort.set(new HostAndPort(host, port)); - this.connectionTimeout = connectionTimeout; - this.soTimeout = soTimeout; + this(new HostAndPort(host, port), DefaultJedisSocketConfig.builder() + .withConnectionTimeout(connectionTimeout).withSoTimeout(soTimeout) + .withSsl(ssl).withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters) + .withHostnameVerifier(hostnameVerifier).build(), infiniteSoTimeout, + user, password, database, clientName); + } + + JedisFactory(final HostAndPort hostAndPort, final JedisSocketConfig socketConfig, + final int infiniteSoTimeout, final String user, final String password, final int database, + final String clientName) { + this.hostAndPort.set(hostAndPort); + this.socketConfig = socketConfig; this.infiniteSoTimeout = infiniteSoTimeout; this.user = user; this.password = password; this.database = database; this.clientName = clientName; - this.ssl = ssl; - this.sslSocketFactory = sslSocketFactory; - this.sslParameters = sslParameters; - this.hostnameVerifier = hostnameVerifier; } JedisFactory(final URI uri, final int connectionTimeout, final int soTimeout, @@ -93,23 +95,25 @@ class JedisFactory implements PooledObjectFactory { JedisFactory(final URI uri, final int connectionTimeout, final int soTimeout, final int infiniteSoTimeout, final String clientName, final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { + this(uri, DefaultJedisSocketConfig.builder() + .withConnectionTimeout(connectionTimeout).withSoTimeout(soTimeout) + .withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters) + .withHostnameVerifier(hostnameVerifier).build(), infiniteSoTimeout, clientName); + } + + JedisFactory(final URI uri, final JedisSocketConfig socketConfig, final int infiniteSoTimeout, final String clientName) { if (!JedisURIHelper.isValid(uri)) { throw new InvalidURIException(String.format( "Cannot open Redis connection due invalid URI. %s", uri.toString())); } this.hostAndPort.set(new HostAndPort(uri.getHost(), uri.getPort())); - this.connectionTimeout = connectionTimeout; - this.soTimeout = soTimeout; + this.socketConfig = DefaultJedisSocketConfig.withSsl(JedisURIHelper.isRedisSSLScheme(uri), socketConfig); this.infiniteSoTimeout = infiniteSoTimeout; this.user = JedisURIHelper.getUser(uri); this.password = JedisURIHelper.getPassword(uri); this.database = JedisURIHelper.getDBIndex(uri); this.clientName = clientName; - this.ssl = JedisURIHelper.isRedisSSLScheme(uri); - this.sslSocketFactory = sslSocketFactory; - this.sslParameters = sslParameters; - this.hostnameVerifier = hostnameVerifier; } public void setHostAndPort(final HostAndPort hostAndPort) { @@ -141,9 +145,8 @@ public void destroyObject(PooledObject pooledJedis) throws Exception { @Override public PooledObject makeObject() throws Exception { - final HostAndPort hp = this.hostAndPort.get(); - final Jedis jedis = new Jedis(hp.getHost(), hp.getPort(), connectionTimeout, soTimeout, - infiniteSoTimeout, ssl, sslSocketFactory, sslParameters, hostnameVerifier); + final HostAndPort hostPort = this.hostAndPort.get(); + final Jedis jedis = new Jedis(hostPort, socketConfig, infiniteSoTimeout); try { jedis.connect(); diff --git a/src/main/java/redis/clients/jedis/JedisPool.java b/src/main/java/redis/clients/jedis/JedisPool.java index 1f962a2d54..0800bed3af 100644 --- a/src/main/java/redis/clients/jedis/JedisPool.java +++ b/src/main/java/redis/clients/jedis/JedisPool.java @@ -234,6 +234,12 @@ public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, in user, password, database, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier)); } + public JedisPool(final GenericObjectPoolConfig poolConfig, final HostAndPort hostAndPort, + final JedisSocketConfig socketConfig, final int infiniteSoTimeout, + final String user, final String password, final int database, final String clientName) { + super(poolConfig, new JedisFactory(hostAndPort, socketConfig, infiniteSoTimeout, user, password, database, clientName)); + } + public JedisPool(final GenericObjectPoolConfig poolConfig) { this(poolConfig, Protocol.DEFAULT_HOST, Protocol.DEFAULT_PORT); } diff --git a/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java index 457fa80d0c..e84c0728d0 100644 --- a/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java +++ b/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java @@ -64,6 +64,17 @@ public JedisSlotBasedConnectionHandler(Set nodes, GenericObjectPool super(nodes, poolConfig, connectionTimeout, soTimeout, infiniteSoTimeout, user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap); } + public JedisSlotBasedConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, + int connectionTimeout, int soTimeout, int infiniteSoTimeout, String user, String password, String clientName, + boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier, HostAndPortMapper portMap) { + super(nodes, poolConfig, connectionTimeout, soTimeout, infiniteSoTimeout, user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap); + } + + public JedisSlotBasedConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, + JedisSocketConfig socketConfig, int infiniteSoTimeout, String user, String password, String clientName) { + super(nodes, poolConfig, socketConfig, infiniteSoTimeout, user, password, clientName); + } + @Override public Jedis getConnection() { // In antirez's redis-rb-cluster implementation, diff --git a/src/main/java/redis/clients/jedis/JedisSocketConfig.java b/src/main/java/redis/clients/jedis/JedisSocketConfig.java new file mode 100644 index 0000000000..1443877181 --- /dev/null +++ b/src/main/java/redis/clients/jedis/JedisSocketConfig.java @@ -0,0 +1,22 @@ +package redis.clients.jedis; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLParameters; +import javax.net.ssl.SSLSocketFactory; + +public interface JedisSocketConfig { + + int getConnectionTimeout(); + + int getSoTimeout(); + + boolean isSSL(); + + SSLSocketFactory getSSLSocketFactory(); + + SSLParameters getSSLParameters(); + + HostnameVerifier getHostnameVerifier(); + + HostAndPortMapper getHostAndPortMapper(); +} diff --git a/src/main/java/redis/clients/jedis/JedisSocketFactory.java b/src/main/java/redis/clients/jedis/JedisSocketFactory.java index 60677847ea..a58f22048c 100644 --- a/src/main/java/redis/clients/jedis/JedisSocketFactory.java +++ b/src/main/java/redis/clients/jedis/JedisSocketFactory.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.net.Socket; +import redis.clients.jedis.exceptions.JedisConnectionException; /** * JedisSocketFactory: responsible for creating socket connections @@ -15,23 +16,30 @@ */ public interface JedisSocketFactory { - Socket createSocket() throws IOException; + /** + * @return Socket + * @throws IOException this will be removed in future + * @throws JedisConnectionException + * @deprecated throwing IOException will not be supported in future + */ + @Deprecated + Socket createSocket() throws IOException, JedisConnectionException; - String getDescription(); + @Deprecated String getDescription(); - String getHost(); + @Deprecated String getHost(); - void setHost(String host); + @Deprecated void setHost(String host); - int getPort(); + @Deprecated int getPort(); - void setPort(int port); + @Deprecated void setPort(int port); int getConnectionTimeout(); - void setConnectionTimeout(int connectionTimeout); + @Deprecated void setConnectionTimeout(int connectionTimeout); int getSoTimeout(); - void setSoTimeout(int soTimeout); + @Deprecated void setSoTimeout(int soTimeout); } diff --git a/src/test/java/redis/clients/jedis/tests/ConnectionTest.java b/src/test/java/redis/clients/jedis/tests/ConnectionTest.java index e6ebe058e1..f3974c1dc9 100644 --- a/src/test/java/redis/clients/jedis/tests/ConnectionTest.java +++ b/src/test/java/redis/clients/jedis/tests/ConnectionTest.java @@ -4,7 +4,6 @@ import static org.junit.Assert.fail; import org.junit.After; -import org.junit.Before; import org.junit.Test; import redis.clients.jedis.Connection; @@ -15,40 +14,34 @@ public class ConnectionTest { private Connection client; - @Before - public void setUp() throws Exception { - client = new Connection(); - } - @After public void tearDown() throws Exception { - client.close(); + if (client != null) { + client.close(); + } } @Test(expected = JedisConnectionException.class) public void checkUnkownHost() { - client.setHost("someunknownhost"); + client = new Connection("someunknownhost"); client.connect(); } @Test(expected = JedisConnectionException.class) public void checkWrongPort() { - client.setHost("localhost"); - client.setPort(55665); + client = new Connection("localhost", 55665); client.connect(); } @Test public void connectIfNotConnectedWhenSettingTimeoutInfinite() { - client.setHost("localhost"); - client.setPort(6379); + client = new Connection("localhost", 6379); client.setTimeoutInfinite(); } @Test public void checkCloseable() { - client.setHost("localhost"); - client.setPort(6379); + client = new Connection("localhost", 6379); client.connect(); client.close(); } diff --git a/src/test/java/redis/clients/jedis/tests/JedisTest.java b/src/test/java/redis/clients/jedis/tests/JedisTest.java index eaccdca09b..490abf2b94 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static redis.clients.jedis.DefaultJedisSocketConfig.DEFAULT_SOCKET_CONFIG; import java.io.IOException; import java.net.URI; @@ -40,11 +41,10 @@ public void checkBinaryData() { for (int b = 0; b < bigdata.length; b++) { bigdata[b] = (byte) ((byte) b % 255); } - Map hash = new HashMap(); + Map hash = new HashMap<>(); hash.put("data", SafeEncoder.encode(bigdata)); - String status = jedis.hmset("foo", hash); - assertEquals("OK", status); + assertEquals("OK", jedis.hmset("foo", hash)); assertEquals(hash, jedis.hgetAll("foo")); } @@ -52,9 +52,17 @@ public void checkBinaryData() { public void connectWithShardInfo() { JedisShardInfo shardInfo = new JedisShardInfo("localhost", Protocol.DEFAULT_PORT); shardInfo.setPassword("foobared"); - Jedis jedis = new Jedis(shardInfo); - jedis.get("foo"); - jedis.close(); + try (Jedis jedis = new Jedis(shardInfo)) { + jedis.get("foo"); + } + } + + @Test + public void connectWithConfig() { + try (Jedis jedis = new Jedis(hnp, DEFAULT_SOCKET_CONFIG)) { + jedis.auth("foobared"); + assertEquals("PONG", jedis.ping()); + } } @Test @@ -79,39 +87,19 @@ public void timeoutConnection() throws Exception { jedis.close(); } - @Test - public void timeoutConnectionWithURI() throws Exception { - Jedis jedis = new Jedis(new URI("redis://:foobared@localhost:6380/2"), 15000); - String timeout = jedis.configGet("timeout").get(1); - jedis.configSet("timeout", "1"); - Thread.sleep(2000); - try { - jedis.hmget("foobar", "foo"); - fail("Operation should throw JedisConnectionException"); - } catch(JedisConnectionException jce) { - // expected - } - jedis.close(); - - // reset config - jedis = new Jedis(new URI("redis://:foobared@localhost:6380/2")); - jedis.configSet("timeout", timeout); - jedis.close(); - } - @Test public void infiniteTimeout() throws Exception { - Jedis jedis = new Jedis("localhost", 6379, 350, 350, 350); - jedis.auth("foobared"); - try { - jedis.blpop(0, "foo"); - fail("SocketTimeoutException should occur"); - } catch(JedisConnectionException jce) { - assertEquals(java.net.SocketTimeoutException.class, jce.getCause().getClass()); - assertEquals("Read timed out", jce.getCause().getMessage()); - assertTrue(jedis.getClient().isBroken()); + try (Jedis jedis = new Jedis("localhost", 6379, 350, 350, 350)) { + jedis.auth("foobared"); + try { + jedis.blpop(0, "foo"); + fail("SocketTimeoutException should occur"); + } catch (JedisConnectionException jce) { + assertEquals(java.net.SocketTimeoutException.class, jce.getCause().getClass()); + assertEquals("Read timed out", jce.getCause().getMessage()); + assertTrue(jedis.getClient().isBroken()); + } } - jedis.close(); } @Test(expected = JedisDataException.class) @@ -134,7 +122,7 @@ public void shouldReconnectToSameDB() throws IOException { } @Test - public void startWithUrlString() { + public void startWithUrl() { try (Jedis j = new Jedis("localhost", 6380)) { j.auth("foobared"); j.select(2); @@ -148,16 +136,17 @@ public void startWithUrlString() { } @Test - public void startWithUrl() throws URISyntaxException { - Jedis j = new Jedis("localhost", 6380); - j.auth("foobared"); - j.select(2); - j.set("foo", "bar"); + public void startWithUri() throws URISyntaxException { + try (Jedis j = new Jedis("localhost", 6380)) { + j.auth("foobared"); + j.select(2); + j.set("foo", "bar"); + } - Jedis jedis = new Jedis(new URI("redis://:foobared@localhost:6380/2")); - assertEquals("PONG", jedis.ping()); - assertEquals("bar", jedis.get("foo")); - jedis.close(); + try (Jedis jedis = new Jedis(new URI("redis://:foobared@localhost:6380/2"))) { + assertEquals("PONG", jedis.ping()); + assertEquals("bar", jedis.get("foo")); + } } @Test @@ -190,14 +179,40 @@ public void allowUrlWithNoDBAndNoPassword() { } } + @Test + public void uriWithDBindexShouldUseTimeout() throws URISyntaxException { + URI uri = new URI("redis://fakehost:6378/1"); + long startTime = System.nanoTime(); + try (Jedis jedis = new Jedis(uri, 5000)) { + jedis.ping(); + } catch (Exception ex) { + assertEquals(JedisConnectionException.class, ex.getClass()); + assertEquals(java.net.UnknownHostException.class, ex.getCause().getClass()); + } + long stopTime = System.nanoTime(); + assertTrue(stopTime - startTime > 3500); + } + @Test public void checkCloseable() { - jedis.close(); - BinaryJedis bj = new BinaryJedis("localhost"); + BinaryJedis bj = new BinaryJedis(); + bj.close(); + } + + @Test + public void checkCloseableAfterConnect() { + BinaryJedis bj = new BinaryJedis(); bj.connect(); bj.close(); } + @Test + public void checkCloseableAfterCommand() { + BinaryJedis bj = new BinaryJedis(); + bj.auth("foobared"); + bj.close(); + } + @Test public void checkDisconnectOnQuit() { jedis.quit(); diff --git a/src/test/java/redis/clients/jedis/tests/JedisWithCompleteCredentialsTest.java b/src/test/java/redis/clients/jedis/tests/JedisWithCompleteCredentialsTest.java index f4e184a362..eea86efb04 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisWithCompleteCredentialsTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisWithCompleteCredentialsTest.java @@ -1,6 +1,7 @@ package redis.clients.jedis.tests; import static org.junit.Assert.assertEquals; +import static redis.clients.jedis.DefaultJedisSocketConfig.DEFAULT_SOCKET_CONFIG; import java.net.URI; import java.net.URISyntaxException; @@ -48,7 +49,15 @@ public void connectWithShardInfo() { } @Test - public void startWithUrlString() { + public void connectWithConfig() { + try (Jedis jedis = new Jedis(hnp, DEFAULT_SOCKET_CONFIG)) { + jedis.auth("acljedis", "fizzbuzz"); + assertEquals("PONG", jedis.ping()); + } + } + + @Test + public void startWithUrl() { try(Jedis j = new Jedis("localhost", 6379)){ assertEquals("OK", j.auth("acljedis", "fizzbuzz")); assertEquals("OK", j.select(2)); @@ -61,7 +70,7 @@ public void startWithUrlString() { } @Test - public void startWithUrl() throws URISyntaxException { + public void startWithUri() throws URISyntaxException { try(Jedis j = new Jedis("localhost", 6379)){ assertEquals("OK", j.auth("acljedis", "fizzbuzz")); assertEquals("OK", j.select(2)); diff --git a/src/test/java/redis/clients/jedis/tests/SSLJedisClusterTest.java b/src/test/java/redis/clients/jedis/tests/SSLJedisClusterTest.java index ab2f41134a..e33f3f06fa 100644 --- a/src/test/java/redis/clients/jedis/tests/SSLJedisClusterTest.java +++ b/src/test/java/redis/clients/jedis/tests/SSLJedisClusterTest.java @@ -62,9 +62,9 @@ public void testSSLDiscoverNodesAutomatically() { "cluster", null, DEFAULT_CONFIG, true, null, null, null, hostAndPortMap)){ Map clusterNodes = jc.getClusterNodes(); assertEquals(3, clusterNodes.size()); - assertTrue(clusterNodes.containsKey("localhost:8379")); - assertTrue(clusterNodes.containsKey("localhost:8380")); - assertTrue(clusterNodes.containsKey("localhost:8381")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7379")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7380")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7381")); jc.get("foo"); } @@ -73,9 +73,9 @@ public void testSSLDiscoverNodesAutomatically() { DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_CONFIG, true, null, null, null, hostAndPortMap)){ Map clusterNodes = jc2.getClusterNodes(); assertEquals(3, clusterNodes.size()); - assertTrue(clusterNodes.containsKey("localhost:8379")); - assertTrue(clusterNodes.containsKey("localhost:8380")); - assertTrue(clusterNodes.containsKey("localhost:8381")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7379")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7380")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7381")); jc2.get("foo"); } } diff --git a/src/test/java/redis/clients/jedis/tests/SSLJedisClusterWithCompleteCredentialsTest.java b/src/test/java/redis/clients/jedis/tests/SSLJedisClusterWithCompleteCredentialsTest.java index 0941bd90ee..7219ff676d 100644 --- a/src/test/java/redis/clients/jedis/tests/SSLJedisClusterWithCompleteCredentialsTest.java +++ b/src/test/java/redis/clients/jedis/tests/SSLJedisClusterWithCompleteCredentialsTest.java @@ -50,9 +50,9 @@ public void testSSLDiscoverNodesAutomatically() { "default","cluster", null, DEFAULT_CONFIG, true, null, null, null, hostAndPortMap); Map clusterNodes = jc.getClusterNodes(); assertEquals(3, clusterNodes.size()); - assertTrue(clusterNodes.containsKey("localhost:8379")); - assertTrue(clusterNodes.containsKey("localhost:8380")); - assertTrue(clusterNodes.containsKey("localhost:8381")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7379")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7380")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7381")); jc.get("foo"); jc.close(); @@ -61,9 +61,9 @@ public void testSSLDiscoverNodesAutomatically() { DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_CONFIG, true, null, null, null, hostAndPortMap); clusterNodes = jc2.getClusterNodes(); assertEquals(3, clusterNodes.size()); - assertTrue(clusterNodes.containsKey("localhost:8379")); - assertTrue(clusterNodes.containsKey("localhost:8380")); - assertTrue(clusterNodes.containsKey("localhost:8381")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7379")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7380")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7381")); jc2.get("foo"); jc2.close(); } diff --git a/src/test/java/redis/clients/jedis/tests/SSLJedisTest.java b/src/test/java/redis/clients/jedis/tests/SSLJedisTest.java index b13edaeeba..a8d5aef4d2 100644 --- a/src/test/java/redis/clients/jedis/tests/SSLJedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/SSLJedisTest.java @@ -29,6 +29,8 @@ import org.junit.BeforeClass; import org.junit.Test; +import redis.clients.jedis.DefaultJedisSocketConfig; +import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.exceptions.JedisConnectionException; @@ -51,6 +53,22 @@ private static void setJvmTrustStore(String trustStoreFilePath, String trustStor System.setProperty("javax.net.ssl.trustStoreType", trustStoreType); } + @Test + public void connectWithSsl() { + try (Jedis jedis = new Jedis("localhost", 6390, true)) { + jedis.auth("foobared"); + assertEquals("PONG", jedis.ping()); + } + } + + @Test + public void connectWithConfig() { + try (Jedis jedis = new Jedis(new HostAndPort("localhost", 6390), DefaultJedisSocketConfig.builder().withSsl(true).build())) { + jedis.auth("foobared"); + assertEquals("PONG", jedis.ping()); + } + } + /** * Tests opening a default SSL/TLS connection to redis using "rediss://" scheme url. */ @@ -67,7 +85,7 @@ public void connectWithUrl() { * Tests opening a default SSL/TLS connection to redis. */ @Test - public void connectWithoutShardInfo() { + public void connectWithUri() { // The "rediss" scheme instructs jedis to open a SSL/TLS connection. try (Jedis jedis = new Jedis(URI.create("rediss://localhost:6390"))) { jedis.auth("foobared"); @@ -94,10 +112,9 @@ public void connectWithShardInfo() throws Exception { JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, null); shardInfo.setPassword("foobared"); - Jedis jedis = new Jedis(shardInfo); - assertEquals("PONG", jedis.ping()); - jedis.disconnect(); - jedis.close(); + try (Jedis jedis = new Jedis(shardInfo)) { + assertEquals("PONG", jedis.ping()); + } } /** @@ -123,8 +140,7 @@ public void connectWithShardInfoByIpAddress() throws Exception { JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, null); shardInfo.setPassword("foobared"); - Jedis jedis = new Jedis(shardInfo); - try { + try (Jedis jedis = new Jedis(shardInfo)) { assertEquals("PONG", jedis.ping()); fail("The code did not throw the expected JedisConnectionException."); } catch (JedisConnectionException e) { @@ -133,12 +149,6 @@ public void connectWithShardInfoByIpAddress() throws Exception { assertEquals("Unexpected second inner exception.", CertificateException.class, e.getCause().getCause().getClass()); } - - try { - jedis.close(); - } catch (Throwable e1) { - // Expected. - } } /** @@ -155,10 +165,9 @@ public void connectWithShardInfoAndCustomHostnameVerifier() { JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier); shardInfo.setPassword("foobared"); - Jedis jedis = new Jedis(shardInfo); - assertEquals("PONG", jedis.ping()); - jedis.disconnect(); - jedis.close(); + try (Jedis jedis = new Jedis(shardInfo)) { + assertEquals("PONG", jedis.ping()); + } } /** @@ -174,10 +183,9 @@ public void connectWithShardInfoAndCustomSocketFactory() throws Exception { JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier); shardInfo.setPassword("foobared"); - Jedis jedis = new Jedis(shardInfo); - assertEquals("PONG", jedis.ping()); - jedis.disconnect(); - jedis.close(); + try (Jedis jedis = new Jedis(shardInfo)) { + assertEquals("PONG", jedis.ping()); + } } /** @@ -196,20 +204,13 @@ public void connectWithShardInfoAndCustomHostnameVerifierByIpAddress() { JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier); shardInfo.setPassword("foobared"); - Jedis jedis = new Jedis(shardInfo); - try { + try (Jedis jedis = new Jedis(shardInfo)) { assertEquals("PONG", jedis.ping()); fail("The code did not throw the expected JedisConnectionException."); } catch (JedisConnectionException e) { assertEquals("The JedisConnectionException does not contain the expected message.", "The connection to '127.0.0.1' failed ssl/tls hostname verification.", e.getMessage()); } - - try { - jedis.close(); - } catch (Throwable e1) { - // Expected. - } } /** @@ -228,8 +229,7 @@ public void connectWithShardInfoAndEmptyTrustStore() throws Exception { JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, null, null); shardInfo.setPassword("foobared"); - Jedis jedis = new Jedis(shardInfo); - try { + try (Jedis jedis = new Jedis(shardInfo)) { assertEquals("PONG", jedis.ping()); fail("The code did not throw the expected JedisConnectionException."); } catch (JedisConnectionException e) { @@ -240,12 +240,6 @@ public void connectWithShardInfoAndEmptyTrustStore() throws Exception { assertEquals("Unexpected third inner exception.", InvalidAlgorithmParameterException.class, e.getCause().getCause().getCause().getClass()); } - - try { - jedis.close(); - } catch (Throwable e1) { - // Expected. - } } /** diff --git a/src/test/java/redis/clients/jedis/tests/SSLJedisWithCompleteCredentialsTest.java b/src/test/java/redis/clients/jedis/tests/SSLJedisWithCompleteCredentialsTest.java index 86b85bbc7a..e2b1814cff 100644 --- a/src/test/java/redis/clients/jedis/tests/SSLJedisWithCompleteCredentialsTest.java +++ b/src/test/java/redis/clients/jedis/tests/SSLJedisWithCompleteCredentialsTest.java @@ -2,6 +2,7 @@ import org.junit.BeforeClass; import org.junit.Test; +import redis.clients.jedis.DefaultJedisSocketConfig; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisShardInfo; @@ -26,7 +27,6 @@ * This test is only executed when the server/cluster is Redis 6. or more. */ public class SSLJedisWithCompleteCredentialsTest { - private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0); @BeforeClass public static void prepare() { @@ -36,9 +36,22 @@ public static void prepare() { SSLJedisTest.setupTrustStore(); } - /** - * Tests opening a default SSL/TLS connection to redis using "rediss://" scheme url. - */ + @Test + public void connectWithSsl() { + try (Jedis jedis = new Jedis("localhost", 6390, true)) { + jedis.auth("acljedis", "fizzbuzz"); + assertEquals("PONG", jedis.ping()); + } + } + + @Test + public void connectWithConfig() { + try (Jedis jedis = new Jedis(new HostAndPort("localhost", 6390), DefaultJedisSocketConfig.builder().withSsl(true).build())) { + jedis.auth("acljedis", "fizzbuzz"); + assertEquals("PONG", jedis.ping()); + } + } + @Test public void connectWithUrl() { // The "rediss" scheme instructs jedis to open a SSL/TLS connection. @@ -52,11 +65,8 @@ public void connectWithUrl() { } } - /** - * Tests opening a default SSL/TLS connection to redis using "rediss://" scheme url. - */ @Test - public void connectWithUrlAndCompleteCredentials() { + public void connectWithCompleteCredentialsUrl() { // The "rediss" scheme instructs jedis to open a SSL/TLS connection. try (Jedis jedis = new Jedis("rediss://default:foobared@localhost:6390")) { assertEquals("PONG", jedis.ping()); @@ -66,12 +76,8 @@ public void connectWithUrlAndCompleteCredentials() { } } - - /** - * Tests opening a default SSL/TLS connection to redis. - */ @Test - public void connectWithoutShardInfo() { + public void connectWithUri() { // The "rediss" scheme instructs jedis to open a SSL/TLS connection. try (Jedis jedis = new Jedis(URI.create("rediss://localhost:6390"))) { jedis.auth("acljedis", "fizzbuzz"); @@ -79,6 +85,17 @@ public void connectWithoutShardInfo() { } } + @Test + public void connectWithCompleteCredentialsUri() { + // The "rediss" scheme instructs jedis to open a SSL/TLS connection. + try (Jedis jedis = new Jedis(URI.create("rediss://default:foobared@localhost:6390"))) { + assertEquals("PONG", jedis.ping()); + } + try (Jedis jedis = new Jedis(URI.create("rediss://acljedis:fizzbuzz@localhost:6390"))) { + assertEquals("PONG", jedis.ping()); + } + } + /** * Tests opening an SSL/TLS connection to redis. * NOTE: This test relies on a feature that is only available as of Java 7 and later. @@ -99,10 +116,9 @@ public void connectWithShardInfo() throws Exception { shardInfo.setUser("acljedis"); shardInfo.setPassword("fizzbuzz"); - Jedis jedis = new Jedis(shardInfo); - assertEquals("PONG", jedis.ping()); - jedis.disconnect(); - jedis.close(); + try (Jedis jedis = new Jedis(shardInfo)) { + assertEquals("PONG", jedis.ping()); + } } /** @@ -129,8 +145,7 @@ public void connectWithShardInfoByIpAddress() throws Exception { shardInfo.setUser("acljedis"); shardInfo.setPassword("fizzbuzz"); - Jedis jedis = new Jedis(shardInfo); - try { + try (Jedis jedis = new Jedis(shardInfo)) { assertEquals("PONG", jedis.ping()); fail("The code did not throw the expected JedisConnectionException."); } catch (JedisConnectionException e) { @@ -139,12 +154,6 @@ public void connectWithShardInfoByIpAddress() throws Exception { assertEquals("Unexpected second inner exception.", CertificateException.class, e.getCause().getCause().getClass()); } - - try { - jedis.close(); - } catch (Throwable e1) { - // Expected. - } } /** @@ -162,10 +171,9 @@ public void connectWithShardInfoAndCustomHostnameVerifier() { shardInfo.setUser("acljedis"); shardInfo.setPassword("fizzbuzz"); - Jedis jedis = new Jedis(shardInfo); - assertEquals("PONG", jedis.ping()); - jedis.disconnect(); - jedis.close(); + try (Jedis jedis = new Jedis(shardInfo)) { + assertEquals("PONG", jedis.ping()); + } } /** @@ -182,10 +190,9 @@ public void connectWithShardInfoAndCustomSocketFactory() throws Exception { shardInfo.setUser("acljedis"); shardInfo.setPassword("fizzbuzz"); - Jedis jedis = new Jedis(shardInfo); - assertEquals("PONG", jedis.ping()); - jedis.disconnect(); - jedis.close(); + try (Jedis jedis = new Jedis(shardInfo)) { + assertEquals("PONG", jedis.ping()); + } } /** @@ -205,19 +212,14 @@ public void connectWithShardInfoAndCustomHostnameVerifierByIpAddress() { shardInfo.setUser("acljedis"); shardInfo.setPassword("fizzbuzz"); - Jedis jedis = new Jedis(shardInfo); - try { - assertEquals("PONG", jedis.ping()); - fail("The code did not throw the expected JedisConnectionException."); - } catch (JedisConnectionException e) { - assertEquals("The JedisConnectionException does not contain the expected message.", - "The connection to '127.0.0.1' failed ssl/tls hostname verification.", e.getMessage()); - } - - try { - jedis.close(); - } catch (Throwable e1) { - // Expected. + try (Jedis jedis = new Jedis(shardInfo)) { + try { + assertEquals("PONG", jedis.ping()); + fail("The code did not throw the expected JedisConnectionException."); + } catch (JedisConnectionException e) { + assertEquals("The JedisConnectionException does not contain the expected message.", + "The connection to '127.0.0.1' failed ssl/tls hostname verification.", e.getMessage()); + } } } @@ -238,8 +240,7 @@ public void connectWithShardInfoAndEmptyTrustStore() throws Exception { shardInfo.setUser("acljedis"); shardInfo.setPassword("fizzbuzz"); - Jedis jedis = new Jedis(shardInfo); - try { + try (Jedis jedis = new Jedis(shardInfo)) { assertEquals("PONG", jedis.ping()); fail("The code did not throw the expected JedisConnectionException."); } catch (JedisConnectionException e) { @@ -250,12 +251,6 @@ public void connectWithShardInfoAndEmptyTrustStore() throws Exception { assertEquals("Unexpected third inner exception.", InvalidAlgorithmParameterException.class, e.getCause().getCause().getCause().getClass()); } - - try { - jedis.close(); - } catch (Throwable e1) { - // Expected. - } } /** diff --git a/src/test/java/redis/clients/jedis/tests/UdsTest.java b/src/test/java/redis/clients/jedis/tests/UdsTest.java index 244fd37623..f95f1d944e 100644 --- a/src/test/java/redis/clients/jedis/tests/UdsTest.java +++ b/src/test/java/redis/clients/jedis/tests/UdsTest.java @@ -1,15 +1,16 @@ package redis.clients.jedis.tests; +import java.io.File; +import java.io.IOException; +import java.net.Socket; import org.junit.Test; import org.newsclub.net.unix.AFUNIXSocket; import org.newsclub.net.unix.AFUNIXSocketAddress; + import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisSocketFactory; import redis.clients.jedis.Protocol; - -import java.io.File; -import java.io.IOException; -import java.net.Socket; +import redis.clients.jedis.exceptions.JedisConnectionException; import static org.junit.Assert.assertEquals; @@ -27,10 +28,14 @@ private static class UdsJedisSocketFactory implements JedisSocketFactory { private static final File UDS_SOCKET = new File("/tmp/redis_uds.sock"); @Override - public Socket createSocket() throws IOException { - Socket socket = AFUNIXSocket.newStrictInstance(); - socket.connect(new AFUNIXSocketAddress(UDS_SOCKET), Protocol.DEFAULT_TIMEOUT); - return socket; + public Socket createSocket() throws JedisConnectionException { + try { + Socket socket = AFUNIXSocket.newStrictInstance(); + socket.connect(new AFUNIXSocketAddress(UDS_SOCKET), Protocol.DEFAULT_TIMEOUT); + return socket; + } catch (IOException ioe) { + throw new JedisConnectionException("Failed to create UDS connection.", ioe); + } } @Override