001 package org.apache.myfaces.tobago.webapp; 002 003 /* 004 * Licensed to the Apache Software Foundation (ASF) under one or more 005 * contributor license agreements. See the NOTICE file distributed with 006 * this work for additional information regarding copyright ownership. 007 * The ASF licenses this file to You under the Apache License, Version 2.0 008 * (the "License"); you may not use this file except in compliance with 009 * the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019 020 import org.apache.commons.logging.Log; 021 import org.apache.commons.logging.LogFactory; 022 import org.apache.myfaces.tobago.config.ThemeConfig; 023 import org.apache.myfaces.tobago.config.TobagoConfig; 024 import org.apache.myfaces.tobago.config.TobagoConfigParser; 025 import org.apache.myfaces.tobago.context.ResourceManagerFactory; 026 import org.apache.myfaces.tobago.util.LayoutUtil; 027 028 import javax.servlet.ServletContext; 029 import javax.servlet.ServletContextEvent; 030 import javax.servlet.ServletContextListener; 031 032 public class TobagoServletContextListener implements ServletContextListener { 033 034 private static final Log LOG 035 = LogFactory.getLog(TobagoServletContextListener.class); 036 037 public void contextInitialized(ServletContextEvent event) { 038 039 ServletContext servletContext = event.getServletContext(); 040 041 if (servletContext.getAttribute(TobagoConfig.TOBAGO_CONFIG) != null) { 042 LOG.warn("Tobago has been already initialized. Do nothing. " 043 + "(This may happen when there is a TobagoServletContextListener configured manually.)"); 044 return; 045 } 046 047 if (LOG.isInfoEnabled()) { 048 LOG.info("*** contextInitialized ***"); 049 } 050 051 try { 052 053 // tobago-config.xml 054 TobagoConfig tobagoConfig 055 = new TobagoConfigParser().parse(servletContext); 056 servletContext.setAttribute(TobagoConfig.TOBAGO_CONFIG, tobagoConfig); 057 058 // todo: cleanup, use one central TobagoConfig, no singleton ResourceManager 059 // resources 060 ResourceManagerFactory.init(servletContext, tobagoConfig); 061 062 // apply bugfix 063 LayoutUtil.setFixLayoutTransparency(tobagoConfig.isFixLayoutTransparency()); 064 065 // prepare themes 066 tobagoConfig.resolveThemes(); 067 068 // theme config cache 069 ThemeConfig.init(servletContext); 070 071 } catch (Throwable e) { 072 if (LOG.isFatalEnabled()) { 073 String error = "Error while deploy process. Tobago can't be initialized! " 074 + "Application will not run!"; 075 LOG.fatal(error, e); 076 throw new RuntimeException(error, e); 077 } 078 } 079 } 080 081 public void contextDestroyed(ServletContextEvent event) { 082 083 ServletContext servletContext = event.getServletContext(); 084 085 if (servletContext.getAttribute(TobagoConfig.TOBAGO_CONFIG) == null) { 086 LOG.warn("Tobago is not initialized. Do nothing. " 087 + "(This may happen when there is a TobagoServletContextListener configured manually.)"); 088 return; 089 } 090 091 if (LOG.isInfoEnabled()) { 092 LOG.info("*** contextDestroyed ***\n" 093 + "--- snip -----------------------------------------------------------------------"); 094 } 095 096 servletContext.removeAttribute(TobagoConfig.TOBAGO_CONFIG); 097 ResourceManagerFactory.release(servletContext); 098 099 // theme config cache 100 ThemeConfig.shutdown(servletContext); 101 102 LogFactory.releaseAll(); 103 // LogManager.shutdown(); 104 } 105 106 }