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    package org.apache.shiro.web.env;
020    
021    import javax.servlet.ServletContextEvent;
022    import javax.servlet.ServletContextListener;
023    
024    /**
025     * Bootstrap listener to startup and shutdown the web application's Shiro
026     * {@link WebEnvironment} at ServletContext startup and shutdown respectively.  This class exists only to
027     * implement the {@link ServletContextListener} interface. All 'real' logic is done in the parent
028     * {@link EnvironmentLoader} class.
029     * <h2>Usage</h2>
030     * Define the following in {@code web.xml}:
031     * <pre>
032     * &lt;listener&gt;
033     *     &lt;listener-class&gt;<code>org.apache.shiro.web.env.EnvironmentLoaderListener</code>&lt;/listener-class&gt;
034     * &lt;/listener&gt;
035     * </pre>
036     * Configuration options, such as the {@code WebEnvironment} class to instantiate as well as Shiro configuration
037     * resource locations are specified as {@code ServletContext} {@code context-param}s and are documented in the
038     * {@link EnvironmentLoader} JavaDoc.
039     * <h2>Shiro Filter</h2>
040     * This listener is almost always defined in conjunction with the
041     * {@link org.apache.shiro.web.servlet.ShiroFilter ShiroFilter} to ensure security operations for web requests.  Please
042     * see the {@link org.apache.shiro.web.servlet.ShiroFilter ShiroFilter} JavaDoc for more.
043     *
044     *
045     * @see EnvironmentLoader
046     * @see org.apache.shiro.web.servlet.ShiroFilter ShiroFilter
047     * @since 1.2
048     */
049    public class EnvironmentLoaderListener extends EnvironmentLoader implements ServletContextListener {
050    
051        /**
052         * Initializes the Shiro {@code WebEnvironment} and binds it to the {@code ServletContext} at application
053         * startup for future reference.
054         *
055         * @param sce the ServletContextEvent triggered upon application startup
056         */
057        public void contextInitialized(ServletContextEvent sce) {
058            initEnvironment(sce.getServletContext());
059        }
060    
061        /**
062         * Destroys any previously created/bound {@code WebEnvironment} instance created by
063         * the {@link #contextInitialized(javax.servlet.ServletContextEvent)} method.
064         *
065         * @param sce the ServletContextEvent triggered upon application shutdown
066         */
067        public void contextDestroyed(ServletContextEvent sce) {
068            destroyEnvironment(sce.getServletContext());
069        }
070    }