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
027 import javax.servlet.ServletContext;
028 import javax.servlet.ServletContextEvent;
029 import javax.servlet.ServletContextListener;
030 import java.util.HashMap;
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 if (LOG.isInfoEnabled()) {
040 LOG.info("*** contextInitialized ***");
041 }
042
043 ServletContext servletContext = event.getServletContext();
044
045 if (servletContext.getAttribute(TobagoConfig.TOBAGO_CONFIG) != null) {
046 LOG.warn("Tobago has been already initialized. Do nothing.");
047 return;
048 }
049
050 try {
051
052 // tobago-config.xml
053 TobagoConfig tobagoConfig
054 = new TobagoConfigParser().parse(servletContext);
055 servletContext.setAttribute(TobagoConfig.TOBAGO_CONFIG, tobagoConfig);
056
057 // todo: cleanup, use one central TobagoConfig, no singleton ResourceManager
058 // resources
059 ResourceManagerFactory.init(servletContext, tobagoConfig);
060
061 // prepare themes
062 tobagoConfig.resolveThemes();
063
064 // theme config cache
065 servletContext.setAttribute(ThemeConfig.THEME_CONFIG_CACHE, new HashMap());
066
067 } catch (Throwable e) {
068 if (LOG.isFatalEnabled()) {
069 String error = "Error while deploy process. Tobago can't be initialized! "
070 + "Application will not run!";
071 LOG.fatal(error, e);
072 throw new RuntimeException(error, e);
073 }
074 }
075 }
076
077 public void contextDestroyed(ServletContextEvent event) {
078 if (LOG.isInfoEnabled()) {
079 LOG.info("*** contextDestroyed ***\n--- snip ---------"
080 + "--------------------------------------------------------------");
081 }
082
083 ServletContext servletContext = event.getServletContext();
084
085 servletContext.removeAttribute(TobagoConfig.TOBAGO_CONFIG);
086 ResourceManagerFactory.release(servletContext);
087 servletContext.removeAttribute(ThemeConfig.THEME_CONFIG_CACHE);
088
089 LogFactory.releaseAll();
090 // LogManager.shutdown();
091 }
092
093 }