View Javadoc

    1   /*
    2    * $Id: ServletContextHolderListener.java 471756 2006-11-06 15:01:43Z husted $
    3    *
    4    * Licensed to the Apache Software Foundation (ASF) under one
    5    * or more contributor license agreements.  See the NOTICE file
    6    * distributed with this work for additional information
    7    * regarding copyright ownership.  The ASF licenses this file
    8    * to you under the Apache License, Version 2.0 (the
    9    * "License"); you may not use this file except in compliance
    10   * with the License.  You may obtain a copy of the License at
    11   *
    12   *  http://www.apache.org/licenses/LICENSE-2.0
    13   *
    14   * Unless required by applicable law or agreed to in writing,
    15   * software distributed under the License is distributed on an
    16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    17   * KIND, either express or implied.  See the License for the
    18   * specific language governing permissions and limitations
    19   * under the License.
    20   */
    21  package org.apache.struts2.portlet.context;
    22
    23  import javax.servlet.ServletContext;
    24  import javax.servlet.ServletContextEvent;
    25  import javax.servlet.ServletContextListener;
    26
    27  /***
    28   * Some of the factory/managers (e.g. the ObjectFactory) need access to
    29   * the {@link org.apache.struts2.ServletActionContext} object when initializing.
    30   * This {@link javax.servlet.ServletContextListener} keeps a reference to the
    31   * {@link javax.servlet.ServletContext} and exposes it through a <code>public static</code>
    32   * method.
    33   *
    34   */
    35  public class ServletContextHolderListener implements ServletContextListener {
    36
    37      private static ServletContext context = null;
    38
    39      /***
    40       * @return The current servlet context
    41       */
    42      public static ServletContext getServletContext() {
    43          return context;
    44      }
    45
    46      /***
    47       * Stores the reference to the {@link ServletContext}.
    48       *
    49       * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)
    50       */
    51      public void contextInitialized(ServletContextEvent event) {
    52          context = event.getServletContext();
    53
    54      }
    55
    56      /***
    57       * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)
    58       */
    59      public void contextDestroyed(ServletContextEvent event) {
    60          context = null;
    61      }
    62
    63  }