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.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.Reader; 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.URI;
import java.net.URL; import java.net.URL;
import java.util.List; import java.util.List;
@ -121,62 +124,101 @@ public class ConfigSupport {
private ObjectMapper jsonMapper = createMapper(null, null); private ObjectMapper jsonMapper = createMapper(null, null);
private ObjectMapper yamlMapper = createMapper(new YAMLFactory(), 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 { public <T> T fromJSON(String content, Class<T> configType) throws IOException {
patchUriObject();
return jsonMapper.readValue(content, configType); return jsonMapper.readValue(content, configType);
} }
public <T> T fromJSON(File file, Class<T> configType) throws IOException { public <T> T fromJSON(File file, Class<T> configType) throws IOException {
patchUriObject();
return fromJSON(file, configType, null); return fromJSON(file, configType, null);
} }
public <T> T fromJSON(File file, Class<T> configType, ClassLoader classLoader) throws IOException { public <T> T fromJSON(File file, Class<T> configType, ClassLoader classLoader) throws IOException {
patchUriObject();
jsonMapper = createMapper(null, classLoader); jsonMapper = createMapper(null, classLoader);
return jsonMapper.readValue(file, configType); return jsonMapper.readValue(file, configType);
} }
public <T> T fromJSON(URL url, Class<T> configType) throws IOException { public <T> T fromJSON(URL url, Class<T> configType) throws IOException {
patchUriObject();
return jsonMapper.readValue(url, configType); return jsonMapper.readValue(url, configType);
} }
public <T> T fromJSON(Reader reader, Class<T> configType) throws IOException { public <T> T fromJSON(Reader reader, Class<T> configType) throws IOException {
patchUriObject();
return jsonMapper.readValue(reader, configType); return jsonMapper.readValue(reader, configType);
} }
public <T> T fromJSON(InputStream inputStream, Class<T> configType) throws IOException { public <T> T fromJSON(InputStream inputStream, Class<T> configType) throws IOException {
patchUriObject();
return jsonMapper.readValue(inputStream, configType); return jsonMapper.readValue(inputStream, configType);
} }
public String toJSON(Config config) throws IOException { public String toJSON(Config config) throws IOException {
patchUriObject();
return jsonMapper.writeValueAsString(config); return jsonMapper.writeValueAsString(config);
} }
public <T> T fromYAML(String content, Class<T> configType) throws IOException { public <T> T fromYAML(String content, Class<T> configType) throws IOException {
patchUriObject();
return yamlMapper.readValue(content, configType); return yamlMapper.readValue(content, configType);
} }
public <T> T fromYAML(File file, Class<T> configType) throws IOException { public <T> T fromYAML(File file, Class<T> configType) throws IOException {
patchUriObject();
return yamlMapper.readValue(file, configType); return yamlMapper.readValue(file, configType);
} }
public <T> T fromYAML(File file, Class<T> configType, ClassLoader classLoader) throws IOException { public <T> T fromYAML(File file, Class<T> configType, ClassLoader classLoader) throws IOException {
patchUriObject();
yamlMapper = createMapper(new YAMLFactory(), classLoader); yamlMapper = createMapper(new YAMLFactory(), classLoader);
return yamlMapper.readValue(file, configType); return yamlMapper.readValue(file, configType);
} }
public <T> T fromYAML(URL url, Class<T> configType) throws IOException { public <T> T fromYAML(URL url, Class<T> configType) throws IOException {
patchUriObject();
return yamlMapper.readValue(url, configType); return yamlMapper.readValue(url, configType);
} }
public <T> T fromYAML(Reader reader, Class<T> configType) throws IOException { public <T> T fromYAML(Reader reader, Class<T> configType) throws IOException {
patchUriObject();
return yamlMapper.readValue(reader, configType); return yamlMapper.readValue(reader, configType);
} }
public <T> T fromYAML(InputStream inputStream, Class<T> configType) throws IOException { public <T> T fromYAML(InputStream inputStream, Class<T> configType) throws IOException {
patchUriObject();
return yamlMapper.readValue(inputStream, configType); return yamlMapper.readValue(inputStream, configType);
} }
public String toYAML(Config config) throws IOException { public String toYAML(Config config) throws IOException {
patchUriObject();
return yamlMapper.writeValueAsString(config); return yamlMapper.writeValueAsString(config);
} }

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

Loading…
Cancel
Save