Fixed - `factory already defined` error during Redisson initialization. #734

pull/748/head
Nikita 8 years ago
parent 3dbf0488fd
commit 8bd1d868c5

@ -16,6 +16,7 @@
package org.redisson.misc;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.URL;
@ -41,31 +42,46 @@ public class URLBuilder {
return;
}
init = true;
URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
if ("redis".equals(protocol)) {
return new URLStreamHandler() {
@Override
protected URLConnection openConnection(URL u) throws IOException {
throw new UnsupportedOperationException();
try {
Field field = URL.class.getDeclaredField("factory");
field.setAccessible(true);
final URLStreamHandlerFactory currentFactory = (URLStreamHandlerFactory) field.get(null);
if (currentFactory != null) {
field.set(null, null);
}
URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
if ("redis".equals(protocol)) {
return new URLStreamHandler() {
@Override
protected URLConnection openConnection(URL u) throws IOException {
throw new UnsupportedOperationException();
};
@Override
protected boolean equals(URL u1, URL u2) {
return u1.toString().equals(u2.toString());
}
@Override
protected int hashCode(URL u) {
return u.toString().hashCode();
}
};
@Override
protected boolean equals(URL u1, URL u2) {
return u1.toString().equals(u2.toString());
}
@Override
protected int hashCode(URL u) {
return u.toString().hashCode();
}
};
}
if (currentFactory != null) {
return currentFactory.createURLStreamHandler(protocol);
}
return null;
}
return null;
}
});
});
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
public static InetSocketAddress toAddress(String url) {

Loading…
Cancel
Save