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; package org.redisson.misc;
import java.io.IOException; import java.io.IOException;
import java.lang.reflect.Field;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
@ -41,6 +42,15 @@ public class URLBuilder {
return; return;
} }
init = true; init = true;
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() { URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {
@Override @Override
public URLStreamHandler createURLStreamHandler(String protocol) { public URLStreamHandler createURLStreamHandler(String protocol) {
@ -60,12 +70,18 @@ public class URLBuilder {
protected int hashCode(URL u) { protected int hashCode(URL u) {
return u.toString().hashCode(); 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) { public static InetSocketAddress toAddress(String url) {

Loading…
Cancel
Save