001/* ===========================================================
002 * JFreeChart : a free chart library for the Java(tm) platform
003 * ===========================================================
004 *
005 * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors.
006 *
007 * Project Info:  http://www.jfree.org/jfreechart/index.html
008 *
009 * This library is free software; you can redistribute it and/or modify it
010 * under the terms of the GNU Lesser General Public License as published by
011 * the Free Software Foundation; either version 2.1 of the License, or
012 * (at your option) any later version.
013 *
014 * This library is distributed in the hope that it will be useful, but
015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017 * License for more details.
018 *
019 * You should have received a copy of the GNU Lesser General Public
020 * License along with this library; if not, write to the Free Software
021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
022 * USA.
023 *
024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 
025 * Other names may be trademarks of their respective owners.]
026 *
027 * -----------------
028 * ChartDeleter.java
029 * -----------------
030  * (C) Copyright 2002-2013, by Richard Atkinson and Contributors.
031 *
032 * Original Author:  Richard Atkinson;
033 * Contributor(s):   -;
034 *
035 * Changes
036 * -------
037 * 19-Aug-2002 : Version 1;
038 * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
039 * ------------- JFREECHART 1.0.x ---------------------------------------------
040 * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
041 *
042 */
043
044package org.jfree.chart.servlet;
045
046import java.io.File;
047import java.io.Serializable;
048import java.util.Iterator;
049import java.util.List;
050
051import javax.servlet.http.HttpSessionBindingEvent;
052import javax.servlet.http.HttpSessionBindingListener;
053
054/**
055 * Used for deleting charts from the temporary directory when the users session
056 * expires.
057 */
058public class ChartDeleter implements HttpSessionBindingListener, Serializable {
059
060    /** The chart names. */
061    private List chartNames = new java.util.ArrayList();
062
063    /**
064     * Blank constructor.
065     */
066    public ChartDeleter() {
067        super();
068    }
069
070    /**
071     * Add a chart to be deleted when the session expires
072     *
073     * @param filename  the name of the chart in the temporary directory to be
074     *                  deleted.
075     */
076    public void addChart(String filename) {
077        this.chartNames.add(filename);
078    }
079
080    /**
081     * Checks to see if a chart is in the list of charts to be deleted
082     *
083     * @param filename  the name of the chart in the temporary directory.
084     *
085     * @return A boolean value indicating whether the chart is present in the
086     *         list.
087     */
088    public boolean isChartAvailable(String filename) {
089        return (this.chartNames.contains(filename));
090    }
091
092    /**
093     * Binding this object to the session has no additional effects.
094     *
095     * @param event  the session bind event.
096     */
097    @Override
098    public void valueBound(HttpSessionBindingEvent event) {
099        // nothing to do
100    }
101
102    /**
103     * When this object is unbound from the session (including upon session
104     * expiry) the files that have been added to the ArrayList are iterated
105     * and deleted.
106     *
107     * @param event  the session unbind event.
108     */
109    @Override
110    public void valueUnbound(HttpSessionBindingEvent event) {
111        Iterator iter = this.chartNames.listIterator();
112        while (iter.hasNext()) {
113            String filename = (String) iter.next();
114            File file = new File(
115                System.getProperty("java.io.tmpdir"), filename
116            );
117            if (file.exists()) {
118                file.delete();
119            }
120        }
121    }
122
123}