1 16 package org.apache.myfaces.webapp; 17 18 import javax.faces.FactoryFinder; 19 import javax.faces.context.ExternalContext; 20 import javax.servlet.ServletContext ; 21 import javax.servlet.ServletContextEvent ; 22 import javax.servlet.ServletContextListener ; 23 24 import org.apache.myfaces.config.FacesConfigurator; 25 import org.apache.myfaces.context.servlet.ServletExternalContextImpl; 26 import org.apache.myfaces.webapp.webxml.WebXml; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 78 public class StartupServletContextListener 79 implements ServletContextListener 80 { 81 private static final Log log = LogFactory.getLog(StartupServletContextListener.class); 82 83 static final String FACES_INIT_DONE 84 = StartupServletContextListener.class.getName() + ".FACES_INIT_DONE"; 85 86 public void contextInitialized(ServletContextEvent event) 87 { 88 initFaces(event.getServletContext()); 89 } 90 91 public static void initFaces(ServletContext servletContext) 92 { 93 try 94 { 95 Boolean b = (Boolean )servletContext.getAttribute(FACES_INIT_DONE); 96 97 if (b == null || b.booleanValue() == false) 98 { 99 log.trace("Initializing MyFaces"); 100 101 ExternalContext externalContext = new ServletExternalContextImpl(servletContext, null, null); 103 104 new FacesConfigurator(externalContext).configure(); 106 107 WebXml.init(externalContext); 109 110 servletContext.setAttribute(FACES_INIT_DONE, Boolean.TRUE); 111 } 112 else 113 { 114 log.info("MyFaces already initialized"); 115 } 116 } 117 catch (Exception ex) 118 { 119 log.error("Error initializing ServletContext", ex); 120 ex.printStackTrace(); 121 } 122 log.info("ServletContext '" + servletContext.getRealPath("/") + "' initialized."); 123 } 124 125 126 public void contextDestroyed(ServletContextEvent e) 127 { 128 FactoryFinder.releaseFactories(); 129 } 130 } 131 | Popular Tags |