001/* ======================================================
002 * JFreeChart : a chart library for the Java(tm) platform
003 * ======================================================
004 *
005 * (C) Copyright 2000-present, by David Gilbert and Contributors.
006 *
007 * Project Info:  https://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-present, by Richard Atkinson and Contributors.
031 *
032 * Original Author:  Richard Atkinson;
033 * Contributor(s):   -;
034 *
035 */
036
037package org.jfree.chart.servlet;
038
039import java.io.File;
040import java.io.Serializable;
041import java.util.ArrayList;
042import java.util.List;
043
044import javax.servlet.http.HttpSessionBindingEvent;
045import javax.servlet.http.HttpSessionBindingListener;
046
047/**
048 * Used for deleting charts from the temporary directory when the users session
049 * expires.
050 *
051 * @deprecated To be removed in JFreeChart 2.0
052 */
053public class ChartDeleter implements HttpSessionBindingListener, Serializable {
054
055    /** The chart names. */
056    private final List<String> chartNames = new ArrayList<>();
057
058    /**
059     * Blank constructor.
060     */
061    public ChartDeleter() {
062        super();
063    }
064
065    /**
066     * Add a chart to be deleted when the session expires
067     *
068     * @param filename  the name of the chart in the temporary directory to be
069     *                  deleted.
070     */
071    public void addChart(String filename) {
072        this.chartNames.add(filename);
073    }
074
075    /**
076     * Checks to see if a chart is in the list of charts to be deleted
077     *
078     * @param filename  the name of the chart in the temporary directory.
079     *
080     * @return A boolean value indicating whether the chart is present in the
081     *         list.
082     */
083    public boolean isChartAvailable(String filename) {
084        return (this.chartNames.contains(filename));
085    }
086
087    /**
088     * Binding this object to the session has no additional effects.
089     *
090     * @param event  the session bind event.
091     */
092    @Override
093    public void valueBound(HttpSessionBindingEvent event) {
094        // nothing to do
095    }
096
097    /**
098     * When this object is unbound from the session (including upon session
099     * expiry) the files that have been added to the ArrayList are iterated
100     * and deleted.
101     *
102     * @param event  the session unbind event.
103     */
104    @Override
105    public void valueUnbound(HttpSessionBindingEvent event) {
106        for (String filename : this.chartNames) {
107            File file = new File(System.getProperty("java.io.tmpdir"), filename);
108            if (file.exists()) {
109                file.delete();
110            }
111        }
112    }
113
114}