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 */ 019package org.apache.shiro.web.env; 020 021import javax.servlet.ServletContextEvent; 022import 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 * <listener> 033 * <listener-class><code>org.apache.shiro.web.env.EnvironmentLoaderListener</code></listener-class> 034 * </listener> 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 * @see EnvironmentLoader 045 * @see org.apache.shiro.web.servlet.ShiroFilter ShiroFilter 046 * @since 1.2 047 */ 048public class EnvironmentLoaderListener extends EnvironmentLoader implements ServletContextListener { 049 050 /** 051 * Initializes the Shiro {@code WebEnvironment} and binds it to the {@code ServletContext} at application 052 * startup for future reference. 053 * 054 * @param sce the ServletContextEvent triggered upon application startup 055 */ 056 public void contextInitialized(ServletContextEvent sce) { 057 initEnvironment(sce.getServletContext()); 058 } 059 060 /** 061 * Destroys any previously created/bound {@code WebEnvironment} instance created by 062 * the {@link #contextInitialized(javax.servlet.ServletContextEvent)} method. 063 * 064 * @param sce the ServletContextEvent triggered upon application shutdown 065 */ 066 public void contextDestroyed(ServletContextEvent sce) { 067 destroyEnvironment(sce.getServletContext()); 068 } 069}