1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/cewolf-1.0/src/main/java/de/laures/cewolf/storage/FileStorage.java Sat Feb 28 21:31:02 2009 +0100
1.3 @@ -0,0 +1,175 @@
1.4 +/* ================================================================
1.5 + * Cewolf : Chart enabling Web Objects Framework
1.6 + * ================================================================
1.7 + *
1.8 + * Project Info: http://cewolf.sourceforge.net
1.9 + * Project Lead: Guido Laures (guido@laures.de);
1.10 + *
1.11 + * (C) Copyright 2002, by Guido Laures
1.12 + *
1.13 + * This library is free software; you can redistribute it and/or modify it under the terms
1.14 + * of the GNU Lesser General Public License as published by the Free Software Foundation;
1.15 + * either version 2.1 of the License, or (at your option) any later version.
1.16 + *
1.17 + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
1.18 + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
1.19 + * See the GNU Lesser General Public License for more details.
1.20 + *
1.21 + * You should have received a copy of the GNU Lesser General Public License along with this
1.22 + * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
1.23 + * Boston, MA 02111-1307, USA.
1.24 + */
1.25 +
1.26 +package de.laures.cewolf.storage;
1.27 +
1.28 +import java.io.File;
1.29 +import java.io.FileInputStream;
1.30 +import java.io.FileOutputStream;
1.31 +import java.io.IOException;
1.32 +import java.io.ObjectInputStream;
1.33 +import java.io.ObjectOutputStream;
1.34 +import java.io.Serializable;
1.35 +import java.util.ArrayList;
1.36 +import java.util.List;
1.37 +
1.38 +import javax.servlet.ServletContext;
1.39 +import javax.servlet.http.HttpServletRequest;
1.40 +import javax.servlet.jsp.PageContext;
1.41 +
1.42 +import de.laures.cewolf.CewolfException;
1.43 +import de.laures.cewolf.ChartImage;
1.44 +import de.laures.cewolf.Configuration;
1.45 +import de.laures.cewolf.Storage;
1.46 +import de.laures.cewolf.taglib.util.KeyGenerator;
1.47 +
1.48 +/**
1.49 + * Storage for storing images as files in the web application directory as files _chart-XXXXX.
1.50 + * Note that by default the files won't ever be removed. To remove saved images on VM exit set
1.51 + * the <code>FileStorage.deleteOnExit</code> configuration parameter to "true". For example:
1.52 + *
1.53 + * <pre>
1.54 + * <init-param>
1.55 + * <param-name>storage</param-name>
1.56 + * <param-value>de.laures.cewolf.storage.FileStorage</param-value>
1.57 + * </init-param>
1.58 + * <init-param>
1.59 + * <param-name>FileStorage.deleteOnExit</param-name>
1.60 + * <param-value>true</param-value>
1.61 + * </init-param>
1.62 + * </pre>
1.63 + *
1.64 + * @author guido
1.65 + */
1.66 +public class FileStorage implements Storage {
1.67 +
1.68 + String basePath = null;
1.69 + List stored = new ArrayList();
1.70 + private boolean deleteOnExit = false;
1.71 +
1.72 + /**
1.73 + * @see de.laures.cewolf.Storage#storeChartImage(ChartImage, PageContext)
1.74 + */
1.75 + public String storeChartImage(ChartImage cid, PageContext pageContext) {
1.76 + if(contains(cid, pageContext)){
1.77 + return getKey(cid);
1.78 + }
1.79 + String id = getKey(cid);
1.80 + ObjectOutputStream oos = null;
1.81 + try {
1.82 + String fileName = getFileName(id);
1.83 + pageContext.getServletContext().log("Storing image to file " + fileName);
1.84 + File f = new File(fileName);
1.85 + if (deleteOnExit) {
1.86 + f.deleteOnExit();
1.87 + }
1.88 + oos = new ObjectOutputStream(new FileOutputStream(f));
1.89 + oos.writeObject(new SerializableChartImage(cid));
1.90 + oos.close();
1.91 + } catch(IOException ioex){
1.92 + ioex.printStackTrace();
1.93 + } catch(CewolfException cwex){
1.94 + cwex.printStackTrace();
1.95 + } finally {
1.96 + if(oos != null){
1.97 + try {
1.98 + oos.close();
1.99 + } catch(IOException ioex){
1.100 + ioex.printStackTrace();
1.101 + }
1.102 + }
1.103 + }
1.104 + return id;
1.105 + }
1.106 +
1.107 + /**
1.108 + * @see de.laures.cewolf.Storage#getChartImage(String, HttpServletRequest)
1.109 + */
1.110 + public ChartImage getChartImage(String id, HttpServletRequest request) {
1.111 + ChartImage res = null;
1.112 + ObjectInputStream ois = null;
1.113 + try {
1.114 + ois = new ObjectInputStream(new FileInputStream(getFileName(id)));
1.115 + res = (ChartImage)ois.readObject();
1.116 + ois.close();
1.117 + } catch(Exception ex){
1.118 + ex.printStackTrace();
1.119 + } finally {
1.120 + if(ois != null){
1.121 + try {
1.122 + ois.close();
1.123 + } catch(IOException ioex){
1.124 + ioex.printStackTrace();
1.125 + }
1.126 + }
1.127 + }
1.128 + return res;
1.129 + }
1.130 +
1.131 + /**
1.132 + * @see de.laures.cewolf.Storage#contains(ChartImage, PageContext)
1.133 + */
1.134 + public boolean contains(ChartImage chartImage, PageContext pageContext) {
1.135 + return new File(getFileName(chartImage)).exists();
1.136 + }
1.137 +
1.138 + /**
1.139 + * @see de.laures.cewolf.Storage#getKey(ChartImage)
1.140 + */
1.141 + public String getKey(ChartImage chartImage) {
1.142 + return String.valueOf(KeyGenerator.generateKey((Serializable)chartImage));
1.143 + }
1.144 +
1.145 + /**
1.146 + * @see de.laures.cewolf.Storage#init(ServletContext)
1.147 + */
1.148 + public void init(ServletContext servletContext) throws CewolfException {
1.149 + basePath = servletContext.getRealPath("/");
1.150 + Configuration config = Configuration.getInstance(servletContext);
1.151 + deleteOnExit = "true".equalsIgnoreCase("" + config.getParameters().get("FileStorage.deleteOnExit"));
1.152 + servletContext.log("FileStorage initialized, deleteOnExit=" + deleteOnExit);
1.153 + }
1.154 +
1.155 + private String getFileName(ChartImage chartImage){
1.156 + return getFileName(getKey(chartImage));
1.157 + }
1.158 +
1.159 + private String getFileName(String id){
1.160 + return basePath + "_chart" + id;
1.161 + }
1.162 +
1.163 + /**
1.164 + * @see de.laures.cewolf.Storage#removeChartImage(java.lang.String, javax.servlet.jsp.PageContext)
1.165 + */
1.166 + public String removeChartImage(String imgKey, HttpServletRequest pageContext) throws CewolfException {
1.167 + File file = new File(getFileName(imgKey));
1.168 + if (file.exists())
1.169 + {
1.170 + if (!file.delete())
1.171 + {
1.172 + throw new CewolfException("Could not delete file " + file.getAbsolutePath());
1.173 + }
1.174 + }
1.175 + return imgKey;
1.176 + }
1.177 +
1.178 +}