Search in sources :

Example 16 with URLStreamHandlerFactory

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);
    }
}
Also used : Field(java.lang.reflect.Field) URLStreamHandlerFactory(java.net.URLStreamHandlerFactory) URL(java.net.URL)

Example 17 with URLStreamHandlerFactory

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);
    }
}
Also used : URLStreamHandler(java.net.URLStreamHandler) URLStreamHandlerFactory(java.net.URLStreamHandlerFactory)

Aggregations

URLStreamHandlerFactory (java.net.URLStreamHandlerFactory)17 URL (java.net.URL)9 Field (java.lang.reflect.Field)7 URLStreamHandler (java.net.URLStreamHandler)7 Test (org.junit.Test)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 IOException (java.io.IOException)2 URI (java.net.URI)2 Statement (org.junit.runners.model.Statement)2 BufferedReader (java.io.BufferedReader)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HttpURLConnection (java.net.HttpURLConnection)1 MalformedURLException (java.net.MalformedURLException)1 URLConnection (java.net.URLConnection)1 Nullable (javax.annotation.Nullable)1 ObjectName (javax.management.ObjectName)1 NamingException (javax.naming.NamingException)1