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.ServletContextEvent;
    21  import javax.servlet.ServletContextListener;
    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 servletContext The servlet context to query
    44       * @return The contexts tracker
    45       */
    46      public static FileCleaningTracker getFileCleaningTracker(final ServletContext servletContext) {
    47          return (FileCleaningTracker) servletContext.getAttribute(FILE_CLEANING_TRACKER_ATTRIBUTE);
    48      }
    49
    50      /**
    51       * Sets the instance of {@link FileCleaningTracker}, which is
    52       * associated with the given {@link ServletContext}.
    53       *
    54       * @param servletContext The servlet context to modify
    55       * @param tracker The tracker to set
    56       */
    57      public static void setFileCleaningTracker(final ServletContext servletContext, final FileCleaningTracker tracker) {
    58          servletContext.setAttribute(FILE_CLEANING_TRACKER_ATTRIBUTE, tracker);
    59      }
    60
    61      /**
    62       * Constructs a new instance.
    63       */
    64      public FileCleanerCleanup() {
    65          // empty
    66      }
    67
    68      /**
    69       * Called when the web application is being destroyed.
    70       * Calls {@link FileCleaningTracker#exitWhenFinished()}.
    71       *
    72       * @param sce The servlet context, used for calling
    73       *     {@link #getFileCleaningTracker(ServletContext)}.
    74       */
    75      @Override
    76      public void contextDestroyed(final ServletContextEvent sce) {
    77          getFileCleaningTracker(sce.getServletContext()).exitWhenFinished();
    78      }
    79
    80      /**
    81       * Called when the web application is initialized. Does
    82       * nothing.
    83       *
    84       * @param sce The servlet context, used for calling
    85       *   {@link #setFileCleaningTracker(ServletContext, FileCleaningTracker)}.
    86       */
    87      @Override
    88      public void contextInitialized(final ServletContextEvent sce) {
    89          setFileCleaningTracker(sce.getServletContext(),
    90                  new FileCleaningTracker());
    91      }
    92
    93  }