Skip to content

Commit

Permalink
update with config pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 committed Mar 14, 2021
1 parent 5b6163f commit 16bcd37
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
18 changes: 17 additions & 1 deletion src/main/java/redis/clients/jedis/DefaultJedisClientConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package redis.clients.jedis;

import java.util.Objects;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocketFactory;
Expand All @@ -11,7 +12,7 @@ public final class DefaultJedisClientConfig implements JedisClientConfig {
private final int infiniteSoTimeoutMillis;

private final String user;
private final String password;
private volatile String password;
private final int database;
private final String clientName;

Expand Down Expand Up @@ -65,6 +66,21 @@ public String getPassword() {
return password;
}

/**
* @param user has to be the same one with which the factory is created
* @param password
* @throws IllegalArgumentException if the user is not the original user
*/
@Override
public synchronized void updatePassword(String user, String password) {
if (!Objects.equals(this.user, user)) {
throw new IllegalArgumentException();
}
if (!Objects.equals(this.password, password)) {
this.password = password;
}
}

@Override
public int getDatabase() {
return database;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/redis/clients/jedis/JedisClientConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ default String getPassword() {
return null;
}

default void updatePassword(String user, String password) {
}

default int getDatabase() {
return Protocol.DEFAULT_DATABASE;
}
Expand Down
15 changes: 1 addition & 14 deletions src/main/java/redis/clients/jedis/JedisFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,25 +122,12 @@ public void setHostAndPort(final HostAndPort hostAndPort) {
this.hostAndPort.set(hostAndPort);
}

/**
* @param password
* @throws IllegalArgumentException
* @see #setPassword(java.lang.String, java.lang.String)
*/
public void setPassword(final String password) throws IllegalArgumentException {
setPassword(null, password);
}

/**
* @param user has to be the same one with which the factory is created
* @param password
* @throws IllegalArgumentException if the user is not the original user
*/
public void setPassword(final String user, final String password) throws IllegalArgumentException {
if (!java.util.Objects.equals(this.user, user)) {
throw new IllegalArgumentException();
}
this.password = password;
this.config.updatePassword(user, password);
}

@Override
Expand Down

0 comments on commit 16bcd37

Please sign in to comment.