franta-hg@1: /* ================================================================ franta-hg@1: * Cewolf : Chart enabling Web Objects Framework franta-hg@1: * ================================================================ franta-hg@1: * franta-hg@1: * Project Info: http://cewolf.sourceforge.net franta-hg@1: * Project Lead: Guido Laures (guido@laures.de); franta-hg@1: * franta-hg@1: * (C) Copyright 2002, by Guido Laures franta-hg@1: * franta-hg@1: * This library is free software; you can redistribute it and/or modify it under the terms franta-hg@1: * of the GNU Lesser General Public License as published by the Free Software Foundation; franta-hg@1: * either version 2.1 of the License, or (at your option) any later version. franta-hg@1: * franta-hg@1: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; franta-hg@1: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. franta-hg@1: * See the GNU Lesser General Public License for more details. franta-hg@1: * franta-hg@1: * You should have received a copy of the GNU Lesser General Public License along with this franta-hg@1: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, franta-hg@1: * Boston, MA 02111-1307, USA. franta-hg@1: */ franta-hg@1: franta-hg@1: package de.laures.cewolf.storage; franta-hg@1: franta-hg@1: import java.io.File; franta-hg@1: import java.io.FileInputStream; franta-hg@1: import java.io.FileOutputStream; franta-hg@1: import java.io.IOException; franta-hg@1: import java.io.ObjectInputStream; franta-hg@1: import java.io.ObjectOutputStream; franta-hg@1: import java.io.Serializable; franta-hg@1: import java.util.ArrayList; franta-hg@1: import java.util.List; franta-hg@1: franta-hg@1: import javax.servlet.ServletContext; franta-hg@1: import javax.servlet.http.HttpServletRequest; franta-hg@1: import javax.servlet.jsp.PageContext; franta-hg@1: franta-hg@1: import de.laures.cewolf.CewolfException; franta-hg@1: import de.laures.cewolf.ChartImage; franta-hg@1: import de.laures.cewolf.Configuration; franta-hg@1: import de.laures.cewolf.Storage; franta-hg@1: import de.laures.cewolf.taglib.util.KeyGenerator; franta-hg@1: franta-hg@1: /** franta-hg@1: * Storage for storing images as files in the web application directory as files _chart-XXXXX. franta-hg@1: * Note that by default the files won't ever be removed. To remove saved images on VM exit set franta-hg@1: * the FileStorage.deleteOnExit configuration parameter to "true". For example: franta-hg@1: * franta-hg@1: *
franta-hg@1:  *		
franta-hg@1:  *			storage
franta-hg@1:  *			de.laures.cewolf.storage.FileStorage
franta-hg@1:  *		
franta-hg@1:  *		
franta-hg@1:  *				FileStorage.deleteOnExit
franta-hg@1:  *				true
franta-hg@1:  *		 
franta-hg@1:  *	
franta-hg@1: * franta-hg@1: * @author guido franta-hg@1: */ franta-hg@1: public class FileStorage implements Storage { franta-hg@1: franta-hg@1: String basePath = null; franta-hg@1: List stored = new ArrayList(); franta-hg@1: private boolean deleteOnExit = false; franta-hg@1: franta-hg@1: /** franta-hg@1: * @see de.laures.cewolf.Storage#storeChartImage(ChartImage, PageContext) franta-hg@1: */ franta-hg@1: public String storeChartImage(ChartImage cid, PageContext pageContext) { franta-hg@1: if(contains(cid, pageContext)){ franta-hg@1: return getKey(cid); franta-hg@1: } franta-hg@1: String id = getKey(cid); franta-hg@1: ObjectOutputStream oos = null; franta-hg@1: try { franta-hg@1: String fileName = getFileName(id); franta-hg@1: pageContext.getServletContext().log("Storing image to file " + fileName); franta-hg@1: File f = new File(fileName); franta-hg@1: if (deleteOnExit) { franta-hg@1: f.deleteOnExit(); franta-hg@1: } franta-hg@1: oos = new ObjectOutputStream(new FileOutputStream(f)); franta-hg@1: oos.writeObject(new SerializableChartImage(cid)); franta-hg@1: oos.close(); franta-hg@1: } catch(IOException ioex){ franta-hg@1: ioex.printStackTrace(); franta-hg@1: } catch(CewolfException cwex){ franta-hg@1: cwex.printStackTrace(); franta-hg@1: } finally { franta-hg@1: if(oos != null){ franta-hg@1: try { franta-hg@1: oos.close(); franta-hg@1: } catch(IOException ioex){ franta-hg@1: ioex.printStackTrace(); franta-hg@1: } franta-hg@1: } franta-hg@1: } franta-hg@1: return id; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * @see de.laures.cewolf.Storage#getChartImage(String, HttpServletRequest) franta-hg@1: */ franta-hg@1: public ChartImage getChartImage(String id, HttpServletRequest request) { franta-hg@1: ChartImage res = null; franta-hg@1: ObjectInputStream ois = null; franta-hg@1: try { franta-hg@1: ois = new ObjectInputStream(new FileInputStream(getFileName(id))); franta-hg@1: res = (ChartImage)ois.readObject(); franta-hg@1: ois.close(); franta-hg@1: } catch(Exception ex){ franta-hg@1: ex.printStackTrace(); franta-hg@1: } finally { franta-hg@1: if(ois != null){ franta-hg@1: try { franta-hg@1: ois.close(); franta-hg@1: } catch(IOException ioex){ franta-hg@1: ioex.printStackTrace(); franta-hg@1: } franta-hg@1: } franta-hg@1: } franta-hg@1: return res; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * @see de.laures.cewolf.Storage#contains(ChartImage, PageContext) franta-hg@1: */ franta-hg@1: public boolean contains(ChartImage chartImage, PageContext pageContext) { franta-hg@1: return new File(getFileName(chartImage)).exists(); franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * @see de.laures.cewolf.Storage#getKey(ChartImage) franta-hg@1: */ franta-hg@1: public String getKey(ChartImage chartImage) { franta-hg@1: return String.valueOf(KeyGenerator.generateKey((Serializable)chartImage)); franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * @see de.laures.cewolf.Storage#init(ServletContext) franta-hg@1: */ franta-hg@1: public void init(ServletContext servletContext) throws CewolfException { franta-hg@1: basePath = servletContext.getRealPath("/"); franta-hg@1: Configuration config = Configuration.getInstance(servletContext); franta-hg@1: deleteOnExit = "true".equalsIgnoreCase("" + config.getParameters().get("FileStorage.deleteOnExit")); franta-hg@1: servletContext.log("FileStorage initialized, deleteOnExit=" + deleteOnExit); franta-hg@1: } franta-hg@1: franta-hg@1: private String getFileName(ChartImage chartImage){ franta-hg@1: return getFileName(getKey(chartImage)); franta-hg@1: } franta-hg@1: franta-hg@1: private String getFileName(String id){ franta-hg@1: return basePath + "_chart" + id; franta-hg@1: } franta-hg@1: franta-hg@1: /** franta-hg@1: * @see de.laures.cewolf.Storage#removeChartImage(java.lang.String, javax.servlet.jsp.PageContext) franta-hg@1: */ franta-hg@1: public String removeChartImage(String imgKey, HttpServletRequest pageContext) throws CewolfException { franta-hg@1: File file = new File(getFileName(imgKey)); franta-hg@1: if (file.exists()) franta-hg@1: { franta-hg@1: if (!file.delete()) franta-hg@1: { franta-hg@1: throw new CewolfException("Could not delete file " + file.getAbsolutePath()); franta-hg@1: } franta-hg@1: } franta-hg@1: return imgKey; franta-hg@1: } franta-hg@1: franta-hg@1: }