use of java.net.URLStreamHandlerFactory in project fabric8 by jboss-fuse.
the class URLs method setURLStreamHandlerFactory.
/**
* The code barfs if we've already set the factory so we have to clear it via reflection first.
* <p/>
* Hacks FTW :)
*/
static void setURLStreamHandlerFactory(URLStreamHandlerFactory newFactory) {
String fieldName = "factory";
Class<URL> clazz = URL.class;
try {
Field field = clazz.getDeclaredField(fieldName);
field.setAccessible(true);
URLStreamHandlerFactory oldValue = (URLStreamHandlerFactory) field.get(null);
if (oldValue != null) {
field.set(null, null);
}
} catch (NoSuchFieldException e) {
LOG.error("Could not find field " + fieldName + " in class " + clazz.getName() + ". " + e, e);
} catch (IllegalAccessException e) {
LOG.error("Could not access field " + fieldName + " in class " + clazz.getName() + ". " + e, e);
}
if (newFactory != null) {
URL.setURLStreamHandlerFactory(newFactory);
}
}
use of java.net.URLStreamHandlerFactory in project fabric8 by jboss-fuse.
the class URLs method doWithCustomURLHandlerFactory.
/**
* Executes the given block with the given {@link URLStreamHandlerFactory},
* whatever is happening with system properties.
*/
public static <T> T doWithCustomURLHandlerFactory(final URLStreamHandlerFactory customFactory, Callable<T> block) throws Exception {
final URLStreamHandlerFactory oldFactory = getURLStreamHandlerFactory();
try {
URLStreamHandlerFactory newFactory = new URLStreamHandlerFactory() {
@Override
public URLStreamHandler createURLStreamHandler(String protocol) {
URLStreamHandler answer = customFactory.createURLStreamHandler(protocol);
if (answer == null && oldFactory != null) {
answer = oldFactory.createURLStreamHandler(protocol);
}
return answer;
}
};
setURLStreamHandlerFactory(newFactory);
return block.call();
} finally {
setURLStreamHandlerFactory(oldFactory);
}
}
Aggregations