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.myfaces.tobago.config.TobagoConfig;
021    import org.apache.myfaces.tobago.internal.config.TobagoConfigBuilder;
022    import org.apache.myfaces.tobago.internal.context.ResourceManagerFactory;
023    import org.slf4j.Logger;
024    import org.slf4j.LoggerFactory;
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 = LoggerFactory.getLogger(TobagoServletContextListener.class);
033    
034      public void contextInitialized(ServletContextEvent event) {
035    
036        if (LOG.isInfoEnabled()) {
037          LOG.info("*** contextInitialized ***");
038        }
039    
040        ServletContext servletContext = event.getServletContext();
041    
042        if (servletContext.getAttribute(TobagoConfig.TOBAGO_CONFIG) != null) {
043          LOG.warn("Tobago has been already initialized. Do nothing.");
044          return;
045        }
046    
047        TobagoConfigBuilder.init(servletContext);
048        if (LOG.isInfoEnabled()) {
049          LOG.info("tobago config: " + TobagoConfig.getInstance(servletContext));
050        }
051      }
052    
053      public void contextDestroyed(ServletContextEvent event) {
054        if (LOG.isInfoEnabled()) {
055          LOG.info("*** contextDestroyed ***\n--- snip ---------"
056              + "--------------------------------------------------------------");
057        }
058    
059        ServletContext servletContext = event.getServletContext();
060    
061        servletContext.removeAttribute(TobagoConfig.TOBAGO_CONFIG);
062    
063        ResourceManagerFactory.release(servletContext);
064    
065        //LogFactory.releaseAll();
066    //    LogManager.shutdown();
067      }
068    
069    }