Search in sources :

Example 1 with ObjectFactoryDestroyable

use of org.apache.struts2.util.ObjectFactoryDestroyable in project struts by apache.

the class Dispatcher method cleanup.

/**
 * Releases all instances bound to this dispatcher instance.
 */
public void cleanup() {
    // clean up ObjectFactory
    ObjectFactory objectFactory = getContainer().getInstance(ObjectFactory.class);
    if (objectFactory == null) {
        LOG.warn("Object Factory is null, something is seriously wrong, no clean up will be performed");
    }
    if (objectFactory instanceof ObjectFactoryDestroyable) {
        try {
            ((ObjectFactoryDestroyable) objectFactory).destroy();
        } catch (Exception e) {
            // catch any exception that may occurred during destroy() and log it
            LOG.error("Exception occurred while destroying ObjectFactory [{}]", objectFactory.toString(), e);
        }
    }
    // clean up Dispatcher itself for this thread
    instance.set(null);
    servletContext.setAttribute(StrutsStatics.SERVLET_DISPATCHER, null);
    // clean up DispatcherListeners
    if (!dispatcherListeners.isEmpty()) {
        for (DispatcherListener l : dispatcherListeners) {
            l.dispatcherDestroyed(this);
        }
    }
    // clean up all interceptors by calling their destroy() method
    Set<Interceptor> interceptors = new HashSet<>();
    Collection<PackageConfig> packageConfigs = configurationManager.getConfiguration().getPackageConfigs().values();
    for (PackageConfig packageConfig : packageConfigs) {
        for (Object config : packageConfig.getAllInterceptorConfigs().values()) {
            if (config instanceof InterceptorStackConfig) {
                for (InterceptorMapping interceptorMapping : ((InterceptorStackConfig) config).getInterceptors()) {
                    interceptors.add(interceptorMapping.getInterceptor());
                }
            }
        }
    }
    for (Interceptor interceptor : interceptors) {
        interceptor.destroy();
    }
    // Clear container holder when application is unloaded / server shutdown
    ContainerHolder.clear();
    // cleanup action context
    ActionContext.clear();
    // clean up configuration
    configurationManager.destroyConfiguration();
    configurationManager = null;
}
Also used : InterceptorStackConfig(com.opensymphony.xwork2.config.entities.InterceptorStackConfig) ObjectFactory(com.opensymphony.xwork2.ObjectFactory) ObjectFactoryDestroyable(org.apache.struts2.util.ObjectFactoryDestroyable) InterceptorMapping(com.opensymphony.xwork2.config.entities.InterceptorMapping) Interceptor(com.opensymphony.xwork2.interceptor.Interceptor) ServletException(javax.servlet.ServletException) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) StrutsException(org.apache.struts2.StrutsException) IOException(java.io.IOException) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig) HashSet(java.util.HashSet)

Aggregations

ObjectFactory (com.opensymphony.xwork2.ObjectFactory)1 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)1 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)1 InterceptorStackConfig (com.opensymphony.xwork2.config.entities.InterceptorStackConfig)1 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)1 Interceptor (com.opensymphony.xwork2.interceptor.Interceptor)1 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 ServletException (javax.servlet.ServletException)1 StrutsException (org.apache.struts2.StrutsException)1 ObjectFactoryDestroyable (org.apache.struts2.util.ObjectFactoryDestroyable)1