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,31 +42,46 @@ public class URLBuilder {
return; return;
} }
init = true; 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();
};
@Override try {
protected boolean equals(URL u1, URL u2) { Field field = URL.class.getDeclaredField("factory");
return u1.toString().equals(u2.toString()); 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 @Override
protected int hashCode(URL u) { protected boolean equals(URL u1, URL u2) {
return u.toString().hashCode(); 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) { public static InetSocketAddress toAddress(String url) {

Loading…
Cancel
Save