Search in sources :

Example 11 with URLStreamHandlerFactory

use of java.net.URLStreamHandlerFactory in project fabric8 by jboss-fuse.

the class URLs method getURLStreamHandlerFactory.

/**
 * For some odd reason this is private so lets use lovely reflection as a hack!
 */
static URLStreamHandlerFactory getURLStreamHandlerFactory() {
    String fieldName = "factory";
    Class<URL> clazz = URL.class;
    try {
        Field field = clazz.getDeclaredField(fieldName);
        field.setAccessible(true);
        return (URLStreamHandlerFactory) field.get(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);
    }
    return null;
}
Also used : Field(java.lang.reflect.Field) URLStreamHandlerFactory(java.net.URLStreamHandlerFactory) URL(java.net.URL)

Example 12 with URLStreamHandlerFactory

use of java.net.URLStreamHandlerFactory in project logging-log4j2 by apache.

the class URLStreamHandlerFactoryRule method apply.

@Override
public Statement apply(final Statement base, final Description description) {
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            Field factoryField = null;
            int matches = 0;
            URLStreamHandlerFactory oldFactory = null;
            for (final Field field : URL.class.getDeclaredFields()) {
                if (URLStreamHandlerFactory.class.equals(field.getType())) {
                    factoryField = field;
                    matches++;
                    factoryField.setAccessible(true);
                    oldFactory = (URLStreamHandlerFactory) factoryField.get(null);
                    break;
                }
            }
            Assert.assertNotNull("java.net URL does not declare a java.net.URLStreamHandlerFactory field", factoryField);
            Assert.assertEquals("java.net.URL declares multiple java.net.URLStreamHandlerFactory fields.", 1, // FIXME There is a break in the loop so always 0 or 1
            matches);
            URL.setURLStreamHandlerFactory(newURLStreamHandlerFactory);
            try {
                base.evaluate();
            } finally {
                clearURLHandlers();
                factoryField.set(null, null);
                URL.setURLStreamHandlerFactory(oldFactory);
            }
        }
    };
}
Also used : Field(java.lang.reflect.Field) Statement(org.junit.runners.model.Statement) URLStreamHandlerFactory(java.net.URLStreamHandlerFactory)

Example 13 with URLStreamHandlerFactory

use of java.net.URLStreamHandlerFactory in project core by weld.

the class AdditionalBeanArchiveHandlerTest method testAdditionalBeanArchiveHandlerUsed.

@Test
public void testAdditionalBeanArchiveHandlerUsed() {
    URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {

        @Override
        public URLStreamHandler createURLStreamHandler(String protocol) {
            return new TestURLStreamHandler();
        }
    });
    try (WeldContainer container = new Weld().setResourceLoader(new TestResourceLoader()).initialize()) {
        // Bar is available, Foo is not (TestBeanArchiveHandler2 has higher priority)
        assertTrue(container.select(Bar.class).isResolvable());
        assertFalse(container.select(Foo.class).isResolvable());
    }
}
Also used : URLStreamHandler(java.net.URLStreamHandler) WeldContainer(org.jboss.weld.environment.se.WeldContainer) URLStreamHandlerFactory(java.net.URLStreamHandlerFactory) Weld(org.jboss.weld.environment.se.Weld) Test(org.junit.Test)

Example 14 with URLStreamHandlerFactory

use of java.net.URLStreamHandlerFactory in project hibernate-orm by hibernate.

the class JarVisitorTest method testJarVisitorFactory.

@Test
@TestForIssue(jiraKey = "HHH-6806")
public void testJarVisitorFactory() throws Exception {
    final File explodedPar = buildExplodedPar();
    final File defaultPar = buildDefaultPar();
    addPackageToClasspath(explodedPar, defaultPar);
    // setting URL to accept vfs based protocol
    URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {

        public URLStreamHandler createURLStreamHandler(String protocol) {
            if ("vfszip".equals(protocol) || "vfsfile".equals(protocol))
                return new URLStreamHandler() {

                    protected URLConnection openConnection(URL u) throws IOException {
                        return null;
                    }
                };
            return null;
        }
    });
    URL jarUrl = defaultPar.toURL();
    ArchiveDescriptor descriptor = StandardArchiveDescriptorFactory.INSTANCE.buildArchiveDescriptor(jarUrl);
    assertEquals(JarFileBasedArchiveDescriptor.class.getName(), descriptor.getClass().getName());
    jarUrl = explodedPar.toURL();
    descriptor = StandardArchiveDescriptorFactory.INSTANCE.buildArchiveDescriptor(jarUrl);
    assertEquals(ExplodedArchiveDescriptor.class.getName(), descriptor.getClass().getName());
    jarUrl = new URL(defaultPar.toURL().toExternalForm().replace("file:", "vfszip:"));
    descriptor = StandardArchiveDescriptorFactory.INSTANCE.buildArchiveDescriptor(jarUrl);
    assertEquals(JarFileBasedArchiveDescriptor.class.getName(), descriptor.getClass().getName());
    jarUrl = new URL(explodedPar.toURL().toExternalForm().replace("file:", "vfsfile:"));
    descriptor = StandardArchiveDescriptorFactory.INSTANCE.buildArchiveDescriptor(jarUrl);
    assertEquals(ExplodedArchiveDescriptor.class.getName(), descriptor.getClass().getName());
}
Also used : URLStreamHandler(java.net.URLStreamHandler) JarFileBasedArchiveDescriptor(org.hibernate.boot.archive.internal.JarFileBasedArchiveDescriptor) URLStreamHandlerFactory(java.net.URLStreamHandlerFactory) ExplodedArchiveDescriptor(org.hibernate.boot.archive.internal.ExplodedArchiveDescriptor) JarFileBasedArchiveDescriptor(org.hibernate.boot.archive.internal.JarFileBasedArchiveDescriptor) JarProtocolArchiveDescriptor(org.hibernate.boot.archive.internal.JarProtocolArchiveDescriptor) ExplodedArchiveDescriptor(org.hibernate.boot.archive.internal.ExplodedArchiveDescriptor) ArchiveDescriptor(org.hibernate.boot.archive.spi.ArchiveDescriptor) File(java.io.File) URL(java.net.URL) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 15 with URLStreamHandlerFactory

use of java.net.URLStreamHandlerFactory in project fabric8 by fabric8io.

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