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.slf4j.Logger;
021    import org.slf4j.LoggerFactory;
022    import org.apache.myfaces.tobago.config.TobagoConfig;
023    import org.apache.myfaces.tobago.config.TobagoConfigParser;
024    import org.apache.myfaces.tobago.context.ResourceManagerFactory;
025    
026    import javax.servlet.ServletContext;
027    import javax.servlet.ServletContextEvent;
028    import javax.servlet.ServletContextListener;
029    
030    public class TobagoServletContextListener implements ServletContextListener {
031    
032      private static final Logger LOG
033          = LoggerFactory.getLogger(TobagoServletContextListener.class);
034    
035      public void contextInitialized(ServletContextEvent event) {
036    
037        if (LOG.isInfoEnabled()) {
038          LOG.info("*** contextInitialized ***");
039        }
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          return;
046        }
047    
048        try {
049    
050          // tobago-config.xml
051          TobagoConfig tobagoConfig
052              = new TobagoConfigParser().parse(servletContext);
053          servletContext.setAttribute(TobagoConfig.TOBAGO_CONFIG, tobagoConfig);
054    
055          // todo: cleanup, use one central TobagoConfig, no singleton ResourceManager
056          // resources
057          tobagoConfig.initProjectState(servletContext);
058          ResourceManagerFactory.init(servletContext, tobagoConfig);
059          // prepare themes
060          tobagoConfig.resolveThemes();
061    
062        } catch (Throwable e) {
063          if (LOG.isErrorEnabled()) {
064            String error = "Error while deploy process. Tobago can't be initialized! Application will not run!";
065            LOG.error(error, e);
066            throw new RuntimeException(error, e);
067          }
068        }
069      }
070    
071      public void contextDestroyed(ServletContextEvent event) {
072        if (LOG.isInfoEnabled()) {
073          LOG.info("*** contextDestroyed ***\n--- snip ---------"
074              + "--------------------------------------------------------------");
075        }
076    
077        ServletContext servletContext = event.getServletContext();
078    
079        servletContext.removeAttribute(TobagoConfig.TOBAGO_CONFIG);
080    
081        ResourceManagerFactory.release(servletContext);
082    
083        //LogFactory.releaseAll();
084    //    LogManager.shutdown();
085      }
086    
087    }