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