underscore support for Uri object

pull/903/head
Nikita 8 years ago
parent 89689b4473
commit 51fc7ad6c0

@ -19,6 +19,9 @@ import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URI;
import java.net.URL;
import java.util.List;
@ -121,62 +124,101 @@ public class ConfigSupport {
private ObjectMapper jsonMapper = createMapper(null, null);
private ObjectMapper yamlMapper = createMapper(new YAMLFactory(), null);
private void patchUriObject() throws IOException {
patchUriField("lowMask", "L_DASH");
patchUriField("highMask", "H_DASH");
}
private void patchUriField(String methodName, String fieldName)
throws IOException {
try {
Method lowMask = URI.class.getDeclaredMethod(methodName, String.class);
lowMask.setAccessible(true);
long lowMaskValue = (long) lowMask.invoke(null, "-_");
Field lowDash = URI.class.getDeclaredField(fieldName);
Field modifiers = Field.class.getDeclaredField("modifiers");
modifiers.setAccessible(true);
modifiers.setInt(lowDash, lowDash.getModifiers() & ~Modifier.FINAL);
lowDash.setAccessible(true);
lowDash.setLong(null, lowMaskValue);
} catch (Exception e) {
throw new IOException(e);
}
}
public <T> T fromJSON(String content, Class<T> configType) throws IOException {
patchUriObject();
return jsonMapper.readValue(content, configType);
}
public <T> T fromJSON(File file, Class<T> configType) throws IOException {
patchUriObject();
return fromJSON(file, configType, null);
}
public <T> T fromJSON(File file, Class<T> configType, ClassLoader classLoader) throws IOException {
patchUriObject();
jsonMapper = createMapper(null, classLoader);
return jsonMapper.readValue(file, configType);
}
public <T> T fromJSON(URL url, Class<T> configType) throws IOException {
patchUriObject();
return jsonMapper.readValue(url, configType);
}
public <T> T fromJSON(Reader reader, Class<T> configType) throws IOException {
patchUriObject();
return jsonMapper.readValue(reader, configType);
}
public <T> T fromJSON(InputStream inputStream, Class<T> configType) throws IOException {
patchUriObject();
return jsonMapper.readValue(inputStream, configType);
}
public String toJSON(Config config) throws IOException {
patchUriObject();
return jsonMapper.writeValueAsString(config);
}
public <T> T fromYAML(String content, Class<T> configType) throws IOException {
patchUriObject();
return yamlMapper.readValue(content, configType);
}
public <T> T fromYAML(File file, Class<T> configType) throws IOException {
patchUriObject();
return yamlMapper.readValue(file, configType);
}
public <T> T fromYAML(File file, Class<T> configType, ClassLoader classLoader) throws IOException {
patchUriObject();
yamlMapper = createMapper(new YAMLFactory(), classLoader);
return yamlMapper.readValue(file, configType);
}
public <T> T fromYAML(URL url, Class<T> configType) throws IOException {
patchUriObject();
return yamlMapper.readValue(url, configType);
}
public <T> T fromYAML(Reader reader, Class<T> configType) throws IOException {
patchUriObject();
return yamlMapper.readValue(reader, configType);
}
public <T> T fromYAML(InputStream inputStream, Class<T> configType) throws IOException {
patchUriObject();
return yamlMapper.readValue(inputStream, configType);
}
public String toYAML(Config config) throws IOException {
patchUriObject();
return yamlMapper.writeValueAsString(config);
}

@ -80,7 +80,7 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager {
// TODO async
List<String> master = connection.sync(RedisCommands.SENTINEL_GET_MASTER_ADDR_BY_NAME, cfg.getMasterName());
String masterHost = "redis://" + master.get(0) + ":" + master.get(1);
String masterHost = createAddress(master.get(0), master.get(1));
this.config.setMasterAddress(masterHost);
currentMaster.set(masterHost);
log.info("master: {} added", masterHost);
@ -96,7 +96,7 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager {
String port = map.get("port");
String flags = map.get("flags");
String host = "redis://" + ip + ":" + port;
String host = createAddress(ip, port);
this.config.addSlaveAddress(host);
slaves.put(host, true);
@ -132,6 +132,13 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager {
future.awaitUninterruptibly();
}
}
private String createAddress(String host, Object port) {
if (host.contains(":")) {
host = "[" + host + "]";
}
return "redis://" + host + ":" + port;
}
@Override
protected MasterSlaveEntry createMasterSlaveEntry(MasterSlaveServersConfig config,
@ -207,7 +214,7 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager {
String ip = parts[2];
String port = parts[3];
String addr = "redis://" + ip + ":" + port;
String addr = createAddress(ip, port);
URI uri = URI.create(addr);
registerSentinel(cfg, uri, c);
}
@ -221,7 +228,7 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager {
final String ip = parts[2];
final String port = parts[3];
final String slaveAddr = "redis://" + ip + ":" + port;
final String slaveAddr = createAddress(ip, port);
if (!isUseSameMaster(parts)) {
return;
@ -309,7 +316,7 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager {
String slaveAddr = ip + ":" + port;
String master = currentMaster.get();
String slaveMaster = "redis://" + parts[6] + ":" + parts[7];
String slaveMaster = createAddress(parts[6], parts[7]);
if (!master.equals(slaveMaster)) {
log.warn("Skipped slave up {} for master {} differs from current {}", slaveAddr, slaveMaster, master);
return false;
@ -369,7 +376,7 @@ public class SentinelConnectionManager extends MasterSlaveConnectionManager {
String port = parts[4];
String current = currentMaster.get();
String newMaster = "redis://" + ip + ":" + port;
String newMaster = createAddress(ip, port);
if (!newMaster.equals(current)
&& currentMaster.compareAndSet(current, newMaster)) {
changeMaster(singleSlotRange.getStartSlot(), URI.create(newMaster));

Loading…
Cancel
Save