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-2005, 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-2004, by Richard Atkinson and Contributors.
  31:  *
  32:  * Original Author:  Richard Atkinson;
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: ChartDeleter.java,v 1.2.2.1 2005/10/25 20:58:06 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 19-Aug-2002 : Version 1;
  40:  * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  41:  *
  42:  */
  43: package org.jfree.chart.servlet;
  44: 
  45: import java.io.File;
  46: import java.util.Iterator;
  47: import java.util.List;
  48: 
  49: import javax.servlet.http.HttpSessionBindingEvent;
  50: import javax.servlet.http.HttpSessionBindingListener;
  51: 
  52: /**
  53:  * Used for deleting charts from the temporary directory when the users session
  54:  * expires.
  55:  *
  56:  * @author Richard Atkinson
  57:  */
  58: public class ChartDeleter implements HttpSessionBindingListener {
  59: 
  60:     /** The chart names. */
  61:     private List chartNames = new java.util.ArrayList();
  62: 
  63:     /**
  64:      * Blank constructor.
  65:      */
  66:     public ChartDeleter() {
  67:         super();
  68:     }
  69: 
  70:     /**
  71:      * Add a chart to be deleted when the session expires
  72:      *
  73:      * @param filename  the name of the chart in the temporary directory to be 
  74:      *                  deleted.
  75:      */
  76:     public void addChart(String filename) {
  77:         this.chartNames.add(filename);
  78:     }
  79: 
  80:     /**
  81:      * Checks to see if a chart is in the list of charts to be deleted
  82:      *
  83:      * @param filename  the name of the chart in the temporary directory.
  84:      *
  85:      * @return A boolean value indicating whether the chart is present in the 
  86:      *         list.
  87:      */
  88:     public boolean isChartAvailable(String filename) {
  89:         return (this.chartNames.contains(filename));
  90:     }
  91: 
  92:     /**
  93:      * Binding this object to the session has no additional effects.
  94:      *
  95:      * @param event  the session bind event.
  96:      */
  97:     public void valueBound(HttpSessionBindingEvent event) {
  98:         return;
  99:     }
 100: 
 101:     /**
 102:      * When this object is unbound from the session (including upon session
 103:      * expiry) the files that have been added to the ArrayList are iterated
 104:      * and deleted.
 105:      *
 106:      * @param event  the session unbind event.
 107:      */
 108:     public void valueUnbound(HttpSessionBindingEvent event) {
 109: 
 110:         Iterator iter = this.chartNames.listIterator();
 111:         while (iter.hasNext()) {
 112:             String filename = (String) iter.next();
 113:             File file = new File(
 114:                 System.getProperty("java.io.tmpdir"), filename
 115:             );
 116:             if (file.exists()) {
 117:                 file.delete();
 118:             }
 119:         }
 120:         return;
 121: 
 122:     }
 123: 
 124: }