View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.commons.fileupload.servlet;
    18
    19  import javax.servlet.ServletContext;
    20  import javax.servlet.ServletContextListener;
    21  import javax.servlet.ServletContextEvent;
    22
    23  import org.apache.commons.io.FileCleaningTracker;
    24
    25  /**
    26   * A servlet context listener, which ensures that the
    27   * {@link FileCleaningTracker}'s reaper thread is terminated,
    28   * when the web application is destroyed.
    29   */
    30  public class FileCleanerCleanup implements ServletContextListener {
    31
    32      /**
    33       * Attribute name, which is used for storing an instance of
    34       * {@link FileCleaningTracker} in the web application.
    35       */
    36      public static final String FILE_CLEANING_TRACKER_ATTRIBUTE
    37          = FileCleanerCleanup.class.getName() + ".FileCleaningTracker";
    38
    39      /**
    40       * Returns the instance of {@link FileCleaningTracker}, which is
    41       * associated with the given {@link ServletContext}.
    42       *
    43       * @param pServletContext The servlet context to query
    44       * @return The contexts tracker
    45       */
    46      public static FileCleaningTracker
    47              getFileCleaningTracker(ServletContext pServletContext) {
    48          return (FileCleaningTracker)
    49              pServletContext.getAttribute(FILE_CLEANING_TRACKER_ATTRIBUTE);
    50      }
    51
    52      /**
    53       * Sets the instance of {@link FileCleaningTracker}, which is
    54       * associated with the given {@link ServletContext}.
    55       *
    56       * @param pServletContext The servlet context to modify
    57       * @param pTracker The tracker to set
    58       */
    59      public static void setFileCleaningTracker(ServletContext pServletContext,
    60              FileCleaningTracker pTracker) {
    61          pServletContext.setAttribute(FILE_CLEANING_TRACKER_ATTRIBUTE, pTracker);
    62      }
    63
    64      /**
    65       * Called when the web application is initialized. Does
    66       * nothing.
    67       *
    68       * @param sce The servlet context, used for calling
    69       *   {@link #setFileCleaningTracker(ServletContext, FileCleaningTracker)}.
    70       */
    71      public void contextInitialized(ServletContextEvent sce) {
    72          setFileCleaningTracker(sce.getServletContext(),
    73                  new FileCleaningTracker());
    74      }
    75
    76      /**
    77       * Called when the web application is being destroyed.
    78       * Calls {@link FileCleaningTracker#exitWhenFinished()}.
    79       *
    80       * @param sce The servlet context, used for calling
    81       *     {@link #getFileCleaningTracker(ServletContext)}.
    82       */
    83      public void contextDestroyed(ServletContextEvent sce) {
    84          getFileCleaningTracker(sce.getServletContext()).exitWhenFinished();
    85      }
    86
    87  }