1 5 package com.opensymphony.webwork.lifecycle; 6 7 import com.opensymphony.xwork.interceptor.component.ComponentConfiguration; 8 import com.opensymphony.xwork.interceptor.component.ComponentManager; 9 import com.opensymphony.xwork.interceptor.component.DefaultComponentManager; 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 import org.xml.sax.SAXException ; 13 14 import javax.servlet.ServletContext ; 15 import javax.servlet.ServletContextEvent ; 16 import javax.servlet.ServletContextListener ; 17 import java.io.IOException ; 18 import java.io.InputStream ; 19 import java.beans.Introspector ; 20 21 22 29 public class ApplicationLifecycleListener implements ServletContextListener { 30 32 private static final Log log = LogFactory.getLog(ApplicationLifecycleListener.class); 33 34 36 41 public void contextDestroyed(ServletContextEvent event) { 42 ServletContext application = event.getServletContext(); 43 ComponentManager container = (ComponentManager) application.getAttribute(ComponentManager.COMPONENT_MANAGER_KEY); 44 45 if (container != null) { 46 container.dispose(); 47 } 48 49 51 Introspector.flushCaches(); 57 LogFactory.releaseAll(); 58 } 59 60 66 public void contextInitialized(ServletContextEvent event) { 67 ServletContext application = event.getServletContext(); 68 ComponentManager container = createComponentManager(); 69 ComponentConfiguration config = loadConfiguration(); 70 71 if (config != null) { 72 config.configure(container, "application"); 73 application.setAttribute(ComponentManager.COMPONENT_MANAGER_KEY, container); 74 application.setAttribute("ComponentConfiguration", config); 75 } 76 } 77 78 84 protected DefaultComponentManager createComponentManager() { 85 return new DefaultComponentManager(); 86 } 87 88 private ComponentConfiguration loadConfiguration() { 89 ComponentConfiguration config = new ComponentConfiguration(); 90 InputStream configXml = Thread.currentThread().getContextClassLoader().getResourceAsStream("components.xml"); 91 92 if (configXml == null) { 93 final String message = "Unable to find the file components.xml in the classpath, XWork IoC *not* initialized."; 94 log.warn(message); 95 return null; 96 } 97 98 try { 99 config.loadFromXml(configXml); 100 } catch (IOException ioe) { 101 log.error(ioe); 102 throw new RuntimeException ("Unable to load component configuration"); 103 } catch (SAXException sae) { 104 log.error(sae); 105 throw new RuntimeException ("Unable to load component configuration"); 106 } 107 108 return config; 109 } 110 } 111 | Popular Tags |