1 /* ================================================================
2 * Cewolf : Chart enabling Web Objects Framework
3 * ================================================================
5 * Project Info: http://cewolf.sourceforge.net
6 * Project Lead: Guido Laures (guido@laures.de);
8 * (C) Copyright 2002, by Guido Laures
10 * This library is free software; you can redistribute it and/or modify it under the terms
11 * of the GNU Lesser General Public License as published by the Free Software Foundation;
12 * either version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License along with this
19 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20 * Boston, MA 02111-1307, USA.
23 package de.laures.cewolf.storage;
26 import java.io.FileInputStream;
27 import java.io.FileOutputStream;
28 import java.io.IOException;
29 import java.io.ObjectInputStream;
30 import java.io.ObjectOutputStream;
31 import java.io.Serializable;
32 import java.util.ArrayList;
33 import java.util.List;
35 import javax.servlet.ServletContext;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.servlet.jsp.PageContext;
39 import de.laures.cewolf.CewolfException;
40 import de.laures.cewolf.ChartImage;
41 import de.laures.cewolf.Configuration;
42 import de.laures.cewolf.Storage;
43 import de.laures.cewolf.taglib.util.KeyGenerator;
46 * Storage for storing images as files in the web application directory as files _chart-XXXXX.
47 * Note that by default the files won't ever be removed. To remove saved images on VM exit set
48 * the <code>FileStorage.deleteOnExit</code> configuration parameter to "true". For example:
52 * <param-name>storage</param-name>
53 * <param-value>de.laures.cewolf.storage.FileStorage</param-value>
56 * <param-name>FileStorage.deleteOnExit</param-name>
57 * <param-value>true</param-value>
63 public class FileStorage implements Storage {
65 String basePath = null;
66 List stored = new ArrayList();
67 private boolean deleteOnExit = false;
70 * @see de.laures.cewolf.Storage#storeChartImage(ChartImage, PageContext)
72 public String storeChartImage(ChartImage cid, PageContext pageContext) {
73 if(contains(cid, pageContext)){
76 String id = getKey(cid);
77 ObjectOutputStream oos = null;
79 String fileName = getFileName(id);
80 pageContext.getServletContext().log("Storing image to file " + fileName);
81 File f = new File(fileName);
85 oos = new ObjectOutputStream(new FileOutputStream(f));
86 oos.writeObject(new SerializableChartImage(cid));
88 } catch(IOException ioex){
89 ioex.printStackTrace();
90 } catch(CewolfException cwex){
91 cwex.printStackTrace();
96 } catch(IOException ioex){
97 ioex.printStackTrace();
105 * @see de.laures.cewolf.Storage#getChartImage(String, HttpServletRequest)
107 public ChartImage getChartImage(String id, HttpServletRequest request) {
108 ChartImage res = null;
109 ObjectInputStream ois = null;
111 ois = new ObjectInputStream(new FileInputStream(getFileName(id)));
112 res = (ChartImage)ois.readObject();
114 } catch(Exception ex){
115 ex.printStackTrace();
120 } catch(IOException ioex){
121 ioex.printStackTrace();
129 * @see de.laures.cewolf.Storage#contains(ChartImage, PageContext)
131 public boolean contains(ChartImage chartImage, PageContext pageContext) {
132 return new File(getFileName(chartImage)).exists();
136 * @see de.laures.cewolf.Storage#getKey(ChartImage)
138 public String getKey(ChartImage chartImage) {
139 return String.valueOf(KeyGenerator.generateKey((Serializable)chartImage));
143 * @see de.laures.cewolf.Storage#init(ServletContext)
145 public void init(ServletContext servletContext) throws CewolfException {
146 basePath = servletContext.getRealPath("/");
147 Configuration config = Configuration.getInstance(servletContext);
148 deleteOnExit = "true".equalsIgnoreCase("" + config.getParameters().get("FileStorage.deleteOnExit"));
149 servletContext.log("FileStorage initialized, deleteOnExit=" + deleteOnExit);
152 private String getFileName(ChartImage chartImage){
153 return getFileName(getKey(chartImage));
156 private String getFileName(String id){
157 return basePath + "_chart" + id;
161 * @see de.laures.cewolf.Storage#removeChartImage(java.lang.String, javax.servlet.jsp.PageContext)
163 public String removeChartImage(String imgKey, HttpServletRequest pageContext) throws CewolfException {
164 File file = new File(getFileName(imgKey));
169 throw new CewolfException("Could not delete file " + file.getAbsolutePath());