public final class SafeContextLoaderListener extends Object implements javax.servlet.ServletContextListener
ServletContextListener which wraps Spring's
ContextLoaderListener and catches anything that delegate throws so as
to prevent its having thrown a Throwable from aborting the initialization of
our entire web application context. Use of this ContextListener will not be
appropriate for all deployments of a web application. Sometimes, a context
listener's aborting context initialization is exactly the desired behavior.
This might be because the resulting application inavailability is acceptable
or because another layer (Apache, a proxy, a balancer, etc.) detects the
inavailability of the context and provides an appropriate user experience. Or
because a root context handles all requests that aren't otherwise handled.
There are many fine alternatives to this approach. However, when using the
bare Tomcat container with CAS as the root context, if your desired behavior
is that a failure at context initialization results in a dummy CAS context
that presents a user-friendly "CAS is unavailable at this time" message,
using this ContextListener in place of Spring's ContextLoaderListener
should do the trick. The error page associated with this deployment failure
is configured in the web.xml via the standard error handling mechanism.
Rather than being a generic ContextListener wrapper that will make safe any
ContextListener, instead this implementation specifically wraps and delegates
to a Spring ContextLoaderListener. As such, mapping this listener in
web.xml is a one for one replacement for ContextLoaderListener.
The exception thrown is exposed in the Servlet Context under the key "exceptionCaughtByListener".
ContextLoaderListener| Modifier and Type | Field and Description |
|---|---|
static String |
CAUGHT_THROWABLE_KEY
The name of the ServletContext attribute whereat we will place a List of
Throwables that we caught from our delegate context listeners.
|
| Constructor and Description |
|---|
SafeContextLoaderListener() |
| Modifier and Type | Method and Description |
|---|---|
void |
contextDestroyed(javax.servlet.ServletContextEvent sce) |
void |
contextInitialized(javax.servlet.ServletContextEvent sce) |
public static final String CAUGHT_THROWABLE_KEY
public void contextInitialized(javax.servlet.ServletContextEvent sce)
contextInitialized in interface javax.servlet.ServletContextListenerpublic void contextDestroyed(javax.servlet.ServletContextEvent sce)
contextDestroyed in interface javax.servlet.ServletContextListenerCopyright © 2004-2015 Apereo. All Rights Reserved.