Skip to content

Commit

Permalink
normalize
Browse files Browse the repository at this point in the history
  • Loading branch information
hexiaofeng committed Jan 3, 2025
1 parent 33da15b commit 84a9118
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public static Date getDate(final Object value, final Date def) {
} else if (value instanceof Number) {
return new Date(((Number) value).longValue());
} else if (value instanceof CharSequence) {
String text = value.toString();
String text = value.toString().trim();
try {
return new Date((Long.parseLong(text)));
} catch (NumberFormatException e) {
Expand Down Expand Up @@ -138,7 +138,7 @@ public static Date getDate(final Object value, final SimpleDateFormat format, fi
} else if (format == null) {
return def;
} else if (value instanceof CharSequence) {
String text = value.toString();
String text = value.toString().trim();
try {
Date result = format.parse(text);
return result == null ? def : result;
Expand Down Expand Up @@ -172,7 +172,7 @@ public static Float getFloat(final Object value, final Float def) {
} else if (value instanceof Number) {
return ((Number) value).floatValue();
} else if (value instanceof CharSequence || value instanceof Character) {
String text = value.toString();
String text = value.toString().trim();
if (text.isEmpty()) {
return def;
}
Expand Down Expand Up @@ -208,7 +208,7 @@ public static Double getDouble(final Object value, final Double def) {
} else if (value instanceof Number) {
return ((Number) value).doubleValue();
} else if (value instanceof CharSequence || value instanceof Character) {
String text = value.toString();
String text = value.toString().trim();
if (text.isEmpty()) {
return def;
}
Expand Down Expand Up @@ -244,7 +244,7 @@ public static Long getLong(final Object value, final Long def) {
} else if (value instanceof Number) {
return ((Number) value).longValue();
} else if (value instanceof CharSequence || value instanceof Character) {
String text = value.toString();
String text = value.toString().trim();
if (text.isEmpty()) {
return def;
}
Expand Down Expand Up @@ -280,7 +280,7 @@ public static Integer getInteger(final Object value, final Integer def) {
} else if (value instanceof Number) {
return ((Number) value).intValue();
} else if (value instanceof CharSequence || value instanceof Character) {
String text = value.toString();
String text = value.toString().trim();
if (text.isEmpty()) {
return def;
}
Expand Down Expand Up @@ -316,7 +316,7 @@ public static Short getShort(final Object value, final Short def) {
} else if (value instanceof Number) {
return ((Number) value).shortValue();
} else if (value instanceof CharSequence || value instanceof Character) {
String text = value.toString();
String text = value.toString().trim();
if (text.isEmpty()) {
return def;
}
Expand Down Expand Up @@ -352,7 +352,7 @@ public static Byte getByte(final Object value, final Byte def) {
} else if (value instanceof Number) {
return ((Number) value).byteValue();
} else if (value instanceof CharSequence || value instanceof Character) {
String text = value.toString();
String text = value.toString().trim();
if (text.isEmpty()) {
return def;
}
Expand Down Expand Up @@ -392,7 +392,7 @@ public static Boolean getBoolean(final Object value, final Boolean def) {
} else if (value instanceof Character) {
return ((Character) value) != '0';
} else if (value instanceof CharSequence) {
String text = value.toString();
String text = value.toString().trim();
if ("true".equalsIgnoreCase(text)) {
return true;
} else if ("false".equalsIgnoreCase(text)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,29 @@ public interface Option extends ValueSupplier {
*/
String getString(String key, String def);

/**
* Retrieves the string parameter value for the specified key.
*
* @param key The name of the parameter.
* @return The value of the parameter as a string.
*/
default String getTrimmed(String key) {
String value = getString(key);
return value == null ? null : value.trim();
}

/**
* Retrieves the string parameter value for the specified key, with a default value.
*
* @param key The name of the parameter.
* @param def The default value to return if the parameter is not found.
* @return The value of the parameter as a string, or the default value if not found.
*/
default String getTrimmed(String key, String def) {
String value = getString(key, def);
return value == null ? null : value.trim();
}

/**
* Retrieves the date parameter value represented as the number of milliseconds from the epoch.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@

import java.util.Objects;

import static com.jd.live.agent.core.util.StringUtils.join;
import static com.jd.live.agent.core.util.StringUtils.split;

/**
* Configuration class for Redis settings.
*/
public class RedisConfig {

private static final Logger logger = LoggerFactory.getLogger(RedisConfig.class);

private static final String SCHEMA_REDISS = "rediss://";
private static final String SCHEMA_REDIS = "redis://";

private static final String KEY_TYPE = "type";
private static final String KEY_ADDRESS = "address";
private static final String KEY_USER = "user";
Expand Down Expand Up @@ -92,13 +98,13 @@ public class RedisConfig {

public RedisConfig(long id, Option option) {
this.id = id;
type = option.getString(KEY_TYPE);
address = option.getString(KEY_ADDRESS);
user = option.getString(KEY_USER);
password = option.getString(KEY_PASSWORD);
type = option.getTrimmed(KEY_TYPE, RedisType.CLUSTER.name()).toUpperCase();
address = join(resolveAddress(split(option.getTrimmed(KEY_ADDRESS, "").toLowerCase())), ",");
user = option.getTrimmed(KEY_USER);
password = option.getTrimmed(KEY_PASSWORD);
database = option.getNatural(KEY_DATABASE, 0);
sentinelUser = option.getString(KEY_SENTINEL_USER);
sentinelPassword = option.getString(KEY_SENTINEL_PASSWORD);
sentinelUser = option.getTrimmed(KEY_SENTINEL_USER);
sentinelPassword = option.getTrimmed(KEY_SENTINEL_PASSWORD);
timeout = option.getPositive(KEY_TIMEOUT, 5000);
keepAlive = option.getBoolean(KEY_RETRY_ATTEMPTS, false);
connectTimeout = option.getPositive(KEY_CONNECT_TIMEOUT, 10000);
Expand Down Expand Up @@ -169,5 +175,45 @@ public int hashCode() {
retryAttempts, retryInterval, sentinelUser, sentinelPassword);
}

/**
* Resolves a single address by ensuring it has the correct Redis schema.
* If the address is null or empty, it returns the address as is.
* If the address already starts with "rediss://" or "redis://", it returns the address unchanged.
* Otherwise, it prepends "redis://" to the address.
*
* @param address the address to resolve
* @return the resolved address with the correct schema
*/
protected String resolveAddress(String address) {
if (address == null || address.isEmpty()) {
return address;
} else if (address.startsWith(SCHEMA_REDISS)) {
return address;
} else if (address.startsWith(SCHEMA_REDIS)) {
return address;
} else {
return SCHEMA_REDIS + address;
}
}

/**
* Resolves an array of addresses by ensuring each address has the correct Redis schema.
* It uses the {@link #resolveAddress(String)} method for each individual address.
*
* @param addresses the array of addresses to resolve
* @return an array of resolved addresses with the correct schema
*/
protected String[] resolveAddress(String[] addresses) {
String[] result = null;
if (addresses != null) {
result = new String[addresses.length];
for (int i = 0; i < addresses.length; i++) {
result[i] = resolveAddress(addresses[i]);
}
}

return result;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@

public interface RedisConfigurator<T> {

String SCHEMA_REDISS = "rediss://";

String SCHEMA_REDIS = "redis://";

/**
* Configures the given configuration object using settings from the provided {@link RedisConfig}.
*
Expand Down Expand Up @@ -56,46 +52,6 @@ public void configure(T config, RedisConfig redisConfig) {
config.setPingConnectionInterval(redisConfig.pingConnectionInterval);
}

/**
* Resolves a single address by ensuring it has the correct Redis schema.
* If the address is null or empty, it returns the address as is.
* If the address already starts with "rediss://" or "redis://", it returns the address unchanged.
* Otherwise, it prepends "redis://" to the address.
*
* @param address the address to resolve
* @return the resolved address with the correct schema
*/
protected String resolveAddress(String address) {
if (address == null || address.isEmpty()) {
return address;
} else if (address.startsWith(SCHEMA_REDISS)) {
return address;
} else if (address.startsWith(SCHEMA_REDIS)) {
return address;
} else {
return SCHEMA_REDIS + address;
}
}

/**
* Resolves an array of addresses by ensuring each address has the correct Redis schema.
* It uses the {@link #resolveAddress(String)} method for each individual address.
*
* @param addresses the array of addresses to resolve
* @return an array of resolved addresses with the correct schema
*/
protected String[] resolveAddress(String[] addresses) {
String[] result = null;
if (addresses != null) {
result = new String[addresses.length];
for (int i = 0; i < addresses.length; i++) {
result[i] = resolveAddress(addresses[i]);
}
}

return result;
}

}

/**
Expand Down Expand Up @@ -145,7 +101,7 @@ class SentinelConfigurator extends BaseMasterSlaveConfigurator<SentinelServersCo
@Override
public void configure(SentinelServersConfig config, RedisConfig redisConfig) {
super.configure(config, redisConfig);
config.addSentinelAddress(resolveAddress(split(redisConfig.address)))
config.addSentinelAddress(split(redisConfig.address))
.setDatabase(config.getDatabase())
.setSentinelUsername(redisConfig.sentinelUser)
.setSentinelPassword(redisConfig.sentinelPassword);
Expand All @@ -162,7 +118,7 @@ class ClusterConfigurator extends BaseMasterSlaveConfigurator<ClusterServersConf
@Override
public void configure(ClusterServersConfig config, RedisConfig redisConfig) {
super.configure(config, redisConfig);
config.addNodeAddress(resolveAddress(split(redisConfig.address)));
config.addNodeAddress(split(redisConfig.address));
}
}

Expand All @@ -176,7 +132,7 @@ class ReplicatedConfigurator extends BaseMasterSlaveConfigurator<ReplicatedServe
@Override
public void configure(ReplicatedServersConfig config, RedisConfig redisConfig) {
super.configure(config, redisConfig);
config.addNodeAddress(resolveAddress(split(redisConfig.address))).setDatabase(config.getDatabase());
config.addNodeAddress(split(redisConfig.address)).setDatabase(config.getDatabase());
}
}

Expand All @@ -190,7 +146,7 @@ class SingleConfigurator extends BaseConfigurator<SingleServerConfig> {
@Override
public void configure(SingleServerConfig config, RedisConfig redisConfig) {
super.configure(config, redisConfig);
config.setAddress(resolveAddress(redisConfig.address))
config.setAddress(redisConfig.address)
.setDatabase(redisConfig.database)
.setConnectionPoolSize(redisConfig.connectionPoolSize)
.setConnectionMinimumIdleSize(redisConfig.connectionMinimumIdleSize);
Expand Down

0 comments on commit 84a9118

Please sign in to comment.