Source for org.jfree.chart.servlet.ChartDeleter

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * -----------------
  28:  * ChartDeleter.java
  29:  * -----------------
  30:   * (C) Copyright 2002-2007, by Richard Atkinson and Contributors.
  31:  *
  32:  * Original Author:  Richard Atkinson;
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 19-Aug-2002 : Version 1;
  38:  * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  39:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  40:  * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
  41:  *
  42:  */
  43: package org.jfree.chart.servlet;
  44: 
  45: import java.io.File;
  46: import java.io.Serializable;
  47: import java.util.Iterator;
  48: import java.util.List;
  49: 
  50: import javax.servlet.http.HttpSessionBindingEvent;
  51: import javax.servlet.http.HttpSessionBindingListener;
  52: 
  53: /**
  54:  * Used for deleting charts from the temporary directory when the users session
  55:  * expires.
  56:  */
  57: public class ChartDeleter implements HttpSessionBindingListener, Serializable {
  58: 
  59:     /** The chart names. */
  60:     private List chartNames = new java.util.ArrayList();
  61: 
  62:     /**
  63:      * Blank constructor.
  64:      */
  65:     public ChartDeleter() {
  66:         super();
  67:     }
  68: 
  69:     /**
  70:      * Add a chart to be deleted when the session expires
  71:      *
  72:      * @param filename  the name of the chart in the temporary directory to be 
  73:      *                  deleted.
  74:      */
  75:     public void addChart(String filename) {
  76:         this.chartNames.add(filename);
  77:     }
  78: 
  79:     /**
  80:      * Checks to see if a chart is in the list of charts to be deleted
  81:      *
  82:      * @param filename  the name of the chart in the temporary directory.
  83:      *
  84:      * @return A boolean value indicating whether the chart is present in the 
  85:      *         list.
  86:      */
  87:     public boolean isChartAvailable(String filename) {
  88:         return (this.chartNames.contains(filename));
  89:     }
  90: 
  91:     /**
  92:      * Binding this object to the session has no additional effects.
  93:      *
  94:      * @param event  the session bind event.
  95:      */
  96:     public void valueBound(HttpSessionBindingEvent event) {
  97:         return;
  98:     }
  99: 
 100:     /**
 101:      * When this object is unbound from the session (including upon session
 102:      * expiry) the files that have been added to the ArrayList are iterated
 103:      * and deleted.
 104:      *
 105:      * @param event  the session unbind event.
 106:      */
 107:     public void valueUnbound(HttpSessionBindingEvent event) {
 108: 
 109:         Iterator iter = this.chartNames.listIterator();
 110:         while (iter.hasNext()) {
 111:             String filename = (String) iter.next();
 112:             File file = new File(
 113:                 System.getProperty("java.io.tmpdir"), filename
 114:             );
 115:             if (file.exists()) {
 116:                 file.delete();
 117:             }
 118:         }
 119:         return;
 120: 
 121:     }
 122: 
 123: }