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.fileupload2.javax;
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 {@link FileCleaningTracker}'s reaper thread is terminated, when the web application is destroyed.
27   */
28  public class JavaxFileCleaner implements ServletContextListener {
29  
30      /**
31       * Attribute name, which is used for storing an instance of {@link FileCleaningTracker} in the web application.
32       */
33      public static final String FILE_CLEANING_TRACKER_ATTRIBUTE = JavaxFileCleaner.class.getName() + ".FileCleaningTracker";
34  
35      /**
36       * Gets the instance of {@link FileCleaningTracker}, which is associated with the given {@link ServletContext}.
37       *
38       * @param servletContext The servlet context to query
39       * @return The contexts tracker
40       */
41      public static FileCleaningTracker getFileCleaningTracker(final ServletContext servletContext) {
42          return (FileCleaningTracker) servletContext.getAttribute(FILE_CLEANING_TRACKER_ATTRIBUTE);
43      }
44  
45      /**
46       * Sets the instance of {@link FileCleaningTracker}, which is associated with the given {@link ServletContext}.
47       *
48       * @param servletContext The servlet context to modify
49       * @param tracker        The tracker to set
50       */
51      public static void setFileCleaningTracker(final ServletContext servletContext, final FileCleaningTracker tracker) {
52          servletContext.setAttribute(FILE_CLEANING_TRACKER_ATTRIBUTE, tracker);
53      }
54  
55      /**
56       * Called when the web application is being destroyed. Calls {@link FileCleaningTracker#exitWhenFinished()}.
57       *
58       * @param sce The servlet context, used for calling {@link #getFileCleaningTracker(ServletContext)}.
59       */
60      @Override
61      public void contextDestroyed(final ServletContextEvent sce) {
62          getFileCleaningTracker(sce.getServletContext()).exitWhenFinished();
63      }
64  
65      /**
66       * Called when the web application is initialized. Does nothing.
67       *
68       * @param sce The servlet context, used for calling {@link #setFileCleaningTracker(ServletContext, FileCleaningTracker)}.
69       */
70      @Override
71      public void contextInitialized(final ServletContextEvent sce) {
72          setFileCleaningTracker(sce.getServletContext(), new FileCleaningTracker());
73      }
74  
75  }