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);
}
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;
}
});
}
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);
}
}
};
}
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());
}
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;
}
}
Aggregations