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  /**
    27   * A servlet context listener, which ensures that the
    28   * {@link org.apache.commons.io.FileCleaner FileCleaner's}
    29   * reaper thread is terminated,
    30   * when the web application is destroyed.
    31   */
    32  public class FileCleanerCleanup implements ServletContextListener {
    33      /**
    34       * Attribute name, which is used for storing an instance of
    35       * {@link FileCleaningTracker} in the web application.
    36       */
    37      public static final String FILE_CLEANING_TRACKER_ATTRIBUTE
    38          = FileCleanerCleanup.class.getName() + ".FileCleaningTracker";
    39
    40      /**
    41       * Returns the instance of {@link FileCleaningTracker}, which is
    42       * associated with the given {@link ServletContext}.
    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       * @param pServletContext The servlet context to modify
    56       * @param pTracker The tracker to set
    57       */
    58      public static void setFileCleaningTracker(ServletContext pServletContext,
    59              FileCleaningTracker pTracker) {
    60          pServletContext.setAttribute(FILE_CLEANING_TRACKER_ATTRIBUTE, pTracker);
    61      }
    62
    63      /**
    64       * Called when the web application is initialized. Does
    65       * nothing.
    66       * @param sce The servlet context, used for calling
    67       *   {@link #setFileCleaningTracker(ServletContext, FileCleaningTracker)}.
    68       */
    69      public void contextInitialized(ServletContextEvent sce) {
    70          setFileCleaningTracker(sce.getServletContext(),
    71                  new FileCleaningTracker());
    72      }
    73
    74      /**
    75       * Called when the web application is being destroyed.
    76       * Calls {@link FileCleaningTracker#exitWhenFinished()}.
    77       * @param sce The servlet context, used for calling
    78       *     {@link #getFileCleaningTracker(ServletContext)}.
    79       */
    80      public void contextDestroyed(ServletContextEvent sce) {
    81          getFileCleaningTracker(sce.getServletContext()).exitWhenFinished();
    82      }
    83  }