JavaxFileCleaner.java

  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.fileupload2.javax;

  18. import javax.servlet.ServletContext;
  19. import javax.servlet.ServletContextEvent;
  20. import javax.servlet.ServletContextListener;

  21. import org.apache.commons.io.FileCleaningTracker;

  22. /**
  23.  * A servlet context listener, which ensures that the {@link FileCleaningTracker}'s reaper thread is terminated, when the web application is destroyed.
  24.  */
  25. public class JavaxFileCleaner implements ServletContextListener {

  26.     /**
  27.      * Attribute name, which is used for storing an instance of {@link FileCleaningTracker} in the web application.
  28.      */
  29.     public static final String FILE_CLEANING_TRACKER_ATTRIBUTE = JavaxFileCleaner.class.getName() + ".FileCleaningTracker";

  30.     /**
  31.      * Gets the instance of {@link FileCleaningTracker}, which is associated with the given {@link ServletContext}.
  32.      *
  33.      * @param servletContext The servlet context to query
  34.      * @return The contexts tracker
  35.      */
  36.     public static FileCleaningTracker getFileCleaningTracker(final ServletContext servletContext) {
  37.         return (FileCleaningTracker) servletContext.getAttribute(FILE_CLEANING_TRACKER_ATTRIBUTE);
  38.     }

  39.     /**
  40.      * Sets the instance of {@link FileCleaningTracker}, which is associated with the given {@link ServletContext}.
  41.      *
  42.      * @param servletContext The servlet context to modify
  43.      * @param tracker        The tracker to set
  44.      */
  45.     public static void setFileCleaningTracker(final ServletContext servletContext, final FileCleaningTracker tracker) {
  46.         servletContext.setAttribute(FILE_CLEANING_TRACKER_ATTRIBUTE, tracker);
  47.     }

  48.     /**
  49.      * Constructs a new instance.
  50.      */
  51.     public JavaxFileCleaner() {
  52.         // empty
  53.     }

  54.     /**
  55.      * Called when the web application is being destroyed. Calls {@link FileCleaningTracker#exitWhenFinished()}.
  56.      *
  57.      * @param sce The servlet context, used for calling {@link #getFileCleaningTracker(ServletContext)}.
  58.      */
  59.     @Override
  60.     public void contextDestroyed(final ServletContextEvent sce) {
  61.         getFileCleaningTracker(sce.getServletContext()).exitWhenFinished();
  62.     }

  63.     /**
  64.      * Called when the web application is initialized. Does nothing.
  65.      *
  66.      * @param sce The servlet context, used for calling {@link #setFileCleaningTracker(ServletContext, FileCleaningTracker)}.
  67.      */
  68.     @Override
  69.     public void contextInitialized(final ServletContextEvent sce) {
  70.         setFileCleaningTracker(sce.getServletContext(), new FileCleaningTracker());
  71.     }

  72. }