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:  * $Id: ChartDeleter.java,v 1.2.2.3 2007/02/02 15:03:19 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 19-Aug-2002 : Version 1;
  40:  * 17-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  41:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  42:  * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
  43:  *
  44:  */
  45: package org.jfree.chart.servlet;
  46: 
  47: import java.io.File;
  48: import java.io.Serializable;
  49: import java.util.Iterator;
  50: import java.util.List;
  51: 
  52: import javax.servlet.http.HttpSessionBindingEvent;
  53: import javax.servlet.http.HttpSessionBindingListener;
  54: 
  55: /**
  56:  * Used for deleting charts from the temporary directory when the users session
  57:  * expires.
  58:  */
  59: public class ChartDeleter implements HttpSessionBindingListener, Serializable {
  60: 
  61:     /** The chart names. */
  62:     private List chartNames = new java.util.ArrayList();
  63: 
  64:     /**
  65:      * Blank constructor.
  66:      */
  67:     public ChartDeleter() {
  68:         super();
  69:     }
  70: 
  71:     /**
  72:      * Add a chart to be deleted when the session expires
  73:      *
  74:      * @param filename  the name of the chart in the temporary directory to be 
  75:      *                  deleted.
  76:      */
  77:     public void addChart(String filename) {
  78:         this.chartNames.add(filename);
  79:     }
  80: 
  81:     /**
  82:      * Checks to see if a chart is in the list of charts to be deleted
  83:      *
  84:      * @param filename  the name of the chart in the temporary directory.
  85:      *
  86:      * @return A boolean value indicating whether the chart is present in the 
  87:      *         list.
  88:      */
  89:     public boolean isChartAvailable(String filename) {
  90:         return (this.chartNames.contains(filename));
  91:     }
  92: 
  93:     /**
  94:      * Binding this object to the session has no additional effects.
  95:      *
  96:      * @param event  the session bind event.
  97:      */
  98:     public void valueBound(HttpSessionBindingEvent event) {
  99:         return;
 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:     public void valueUnbound(HttpSessionBindingEvent event) {
 110: 
 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:         return;
 122: 
 123:     }
 124: 
 125: }