Search in sources :

Example 6 with URLStreamHandlerFactory

use of java.net.URLStreamHandlerFactory in project tomcat70 by apache.

the class WebappLoader method startInternal.

/**
 * Start associated {@link ClassLoader} and implement the requirements
 * of {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
 *
 * @exception LifecycleException if this component detects a fatal error
 *  that prevents this component from being used
 */
@Override
protected void startInternal() throws LifecycleException {
    if (log.isDebugEnabled())
        log.debug(sm.getString("webappLoader.starting"));
    if (container.getResources() == null) {
        log.info("No resources for " + container);
        setState(LifecycleState.STARTING);
        return;
    }
    // Register a stream handler factory for the JNDI protocol
    URLStreamHandlerFactory streamHandlerFactory = DirContextURLStreamHandlerFactory.getInstance();
    if (first) {
        first = false;
        try {
            URL.setURLStreamHandlerFactory(streamHandlerFactory);
        } catch (Exception e) {
            // Log and continue anyway, this is not critical
            log.error("Error registering jndi stream handler", e);
        } catch (Throwable t) {
            ExceptionUtils.handleThrowable(t);
            // This is likely a dual registration
            log.info("Dual registration of jndi stream handler: " + t.getMessage());
        }
    }
    // Construct a class loader based on our current repositories list
    try {
        classLoader = createClassLoader();
        classLoader.setJarOpenInterval(this.jarOpenInterval);
        classLoader.setResources(container.getResources());
        classLoader.setDelegate(this.delegate);
        classLoader.setSearchExternalFirst(searchExternalFirst);
        if (container instanceof StandardContext) {
            classLoader.setAntiJARLocking(((StandardContext) container).getAntiJARLocking());
            classLoader.setClearReferencesRmiTargets(((StandardContext) container).getClearReferencesRmiTargets());
            classLoader.setClearReferencesStatic(((StandardContext) container).getClearReferencesStatic());
            classLoader.setClearReferencesStopThreads(((StandardContext) container).getClearReferencesStopThreads());
            classLoader.setClearReferencesStopTimerThreads(((StandardContext) container).getClearReferencesStopTimerThreads());
            classLoader.setClearReferencesHttpClientKeepAliveThread(((StandardContext) container).getClearReferencesHttpClientKeepAliveThread());
            classLoader.setClearReferencesObjectStreamClassCaches(((StandardContext) container).getClearReferencesObjectStreamClassCaches());
        }
        for (int i = 0; i < repositories.length; i++) {
            classLoader.addRepository(repositories[i]);
        }
        // Configure our repositories
        setRepositories();
        setClassPath();
        setPermissions();
        ((Lifecycle) classLoader).start();
        // Binding the Webapp class loader to the directory context
        DirContextURLStreamHandler.bind(classLoader, this.container.getResources());
        StandardContext ctx = (StandardContext) container;
        String contextName = ctx.getName();
        if (!contextName.startsWith("/")) {
            contextName = "/" + contextName;
        }
        ObjectName cloname = new ObjectName(MBeanUtils.getDomain(ctx) + ":type=WebappClassLoader,context=" + contextName + ",host=" + ctx.getParent().getName());
        Registry.getRegistry(null, null).registerComponent(classLoader, cloname, null);
    } catch (Throwable t) {
        t = ExceptionUtils.unwrapInvocationTargetException(t);
        ExceptionUtils.handleThrowable(t);
        log.error("LifecycleException ", t);
        throw new LifecycleException("start: ", t);
    }
    setState(LifecycleState.STARTING);
}
Also used : LifecycleException(org.apache.catalina.LifecycleException) StandardContext(org.apache.catalina.core.StandardContext) Lifecycle(org.apache.catalina.Lifecycle) URLStreamHandlerFactory(java.net.URLStreamHandlerFactory) DirContextURLStreamHandlerFactory(org.apache.naming.resources.DirContextURLStreamHandlerFactory) NamingException(javax.naming.NamingException) LifecycleException(org.apache.catalina.LifecycleException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ObjectName(javax.management.ObjectName)

Example 7 with URLStreamHandlerFactory

use of java.net.URLStreamHandlerFactory in project agera by google.

the class HttpFunctionsTest method onlyOnce.

@BeforeClass
public static void onlyOnce() throws Throwable {
    mockHttpURLConnection = mock(HttpURLConnection.class);
    URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {

        @Override
        public URLStreamHandler createURLStreamHandler(final String s) {
            return s.equals(TEST_PROTOCOL) ? new URLStreamHandler() {

                @Override
                protected URLConnection openConnection(final URL url) throws IOException {
                    return mockHttpURLConnection;
                }
            } : null;
        }
    });
}
Also used : URLStreamHandler(java.net.URLStreamHandler) HttpURLConnection(java.net.HttpURLConnection) URLStreamHandlerFactory(java.net.URLStreamHandlerFactory) Matchers.anyString(org.mockito.Matchers.anyString) URL(java.net.URL) BeforeClass(org.junit.BeforeClass)

Example 8 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 9 with URLStreamHandlerFactory

use of java.net.URLStreamHandlerFactory in project tomcat by apache.

the class TestTomcatURLStreamHandlerFactory method testUserFactory.

@Test
public void testUserFactory() throws Exception {
    URLStreamHandlerFactory factory = new URLStreamHandlerFactory() {

        @Override
        public URLStreamHandler createURLStreamHandler(String protocol) {
            return null;
        }
    };
    TomcatURLStreamHandlerFactory.getInstance().addUserFactory(factory);
    TomcatURLStreamHandlerFactory.release(factory.getClass().getClassLoader());
}
Also used : URLStreamHandlerFactory(java.net.URLStreamHandlerFactory) Test(org.junit.Test)

Example 10 with URLStreamHandlerFactory

use of java.net.URLStreamHandlerFactory in project kylo by Teradata.

the class Handler method openConnection.

@Nullable
@Override
protected URLConnection openConnection(@Nullable final URL url) throws IOException {
    final URLStreamHandlerFactory handlerFactory = FACTORY.get();
    final String protocol = (url != null) ? url.getProtocol() : null;
    if (handlerFactory != null && protocol != null && protocol.equals("hadoop")) {
        final String file = url.getFile();
        final URLStreamHandler handler = handlerFactory.createURLStreamHandler(URI.create(file).getScheme());
        return (handler != null) ? new URL(null, file, handler).openConnection() : null;
    } else {
        return null;
    }
}
Also used : URLStreamHandler(java.net.URLStreamHandler) URLStreamHandlerFactory(java.net.URLStreamHandlerFactory) URL(java.net.URL) Nullable(javax.annotation.Nullable)

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