View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.web.env;
20  
21  import javax.servlet.ServletContextEvent;
22  import javax.servlet.ServletContextListener;
23  
24  /**
25   * Bootstrap listener to startup and shutdown the web application's Shiro
26   * {@link WebEnvironment} at ServletContext startup and shutdown respectively.  This class exists only to
27   * implement the {@link ServletContextListener} interface. All 'real' logic is done in the parent
28   * {@link EnvironmentLoader} class.
29   * <h2>Usage</h2>
30   * Define the following in {@code web.xml}:
31   * <pre>
32   * &lt;listener&gt;
33   *     &lt;listener-class&gt;<code>org.apache.shiro.web.env.EnvironmentLoaderListener</code>&lt;/listener-class&gt;
34   * &lt;/listener&gt;
35   * </pre>
36   * Configuration options, such as the {@code WebEnvironment} class to instantiate as well as Shiro configuration
37   * resource locations are specified as {@code ServletContext} {@code context-param}s and are documented in the
38   * {@link EnvironmentLoader} JavaDoc.
39   * <h2>Shiro Filter</h2>
40   * This listener is almost always defined in conjunction with the
41   * {@link org.apache.shiro.web.servlet.ShiroFilter ShiroFilter} to ensure security operations for web requests.  Please
42   * see the {@link org.apache.shiro.web.servlet.ShiroFilter ShiroFilter} JavaDoc for more.
43   *
44   *
45   * @see EnvironmentLoader
46   * @see org.apache.shiro.web.servlet.ShiroFilter ShiroFilter
47   * @since 1.2
48   */
49  public class EnvironmentLoaderListener extends EnvironmentLoader implements ServletContextListener {
50  
51      /**
52       * Initializes the Shiro {@code WebEnvironment} and binds it to the {@code ServletContext} at application
53       * startup for future reference.
54       *
55       * @param sce the ServletContextEvent triggered upon application startup
56       */
57      public void contextInitialized(ServletContextEvent sce) {
58          initEnvironment(sce.getServletContext());
59      }
60  
61      /**
62       * Destroys any previously created/bound {@code WebEnvironment} instance created by
63       * the {@link #contextInitialized(javax.servlet.ServletContextEvent)} method.
64       *
65       * @param sce the ServletContextEvent triggered upon application shutdown
66       */
67      public void contextDestroyed(ServletContextEvent sce) {
68          destroyEnvironment(sce.getServletContext());
69      }
70  }