001: /* ================================================================
002: * Cewolf : Chart enabling Web Objects Framework
003: * ================================================================
004: *
005: * Project Info: http://cewolf.sourceforge.net
006: * Project Lead: Guido Laures (guido@laures.de);
007: *
008: * (C) Copyright 2002, by Guido Laures
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package de.laures.cewolf.storage;
024:
025: import java.io.File;
026: import java.io.FileInputStream;
027: import java.io.FileOutputStream;
028: import java.io.IOException;
029: import java.io.ObjectInputStream;
030: import java.io.ObjectOutputStream;
031: import java.io.Serializable;
032: import java.util.ArrayList;
033: import java.util.List;
034:
035: import javax.servlet.ServletContext;
036: import javax.servlet.http.HttpServletRequest;
037: import javax.servlet.jsp.PageContext;
038:
039: import de.laures.cewolf.CewolfException;
040: import de.laures.cewolf.ChartImage;
041: import de.laures.cewolf.Configuration;
042: import de.laures.cewolf.Storage;
043: import de.laures.cewolf.taglib.util.KeyGenerator;
044:
045: /**
046: * Storage for storing images as files in the web application directory as files _chart-XXXXX.
047: * Note that by default the files won't ever be removed. To remove saved images on VM exit set
048: * the <code>FileStorage.deleteOnExit</code> configuration parameter to "true". For example:
049: *
050: * <pre>
051: * <init-param>
052: * <param-name>storage</param-name>
053: * <param-value>de.laures.cewolf.storage.FileStorage</param-value>
054: * </init-param>
055: * <init-param>
056: * <param-name>FileStorage.deleteOnExit</param-name>
057: * <param-value>true</param-value>
058: * </init-param>
059: * </pre>
060: *
061: * @author guido
062: */
063: public class FileStorage implements Storage {
064:
065: String basePath = null;
066: List stored = new ArrayList();
067: private boolean deleteOnExit = false;
068:
069: /**
070: * @see de.laures.cewolf.Storage#storeChartImage(ChartImage, PageContext)
071: */
072: public String storeChartImage(ChartImage cid,
073: PageContext pageContext) {
074: if (contains(cid, pageContext)) {
075: return getKey(cid);
076: }
077: String id = getKey(cid);
078: ObjectOutputStream oos = null;
079: try {
080: String fileName = getFileName(id);
081: pageContext.getServletContext().log(
082: "Storing image to file " + fileName);
083: File f = new File(fileName);
084: if (deleteOnExit) {
085: f.deleteOnExit();
086: }
087: oos = new ObjectOutputStream(new FileOutputStream(f));
088: oos.writeObject(new SerializableChartImage(cid));
089: oos.close();
090: } catch (IOException ioex) {
091: ioex.printStackTrace();
092: } catch (CewolfException cwex) {
093: cwex.printStackTrace();
094: } finally {
095: if (oos != null) {
096: try {
097: oos.close();
098: } catch (IOException ioex) {
099: ioex.printStackTrace();
100: }
101: }
102: }
103: return id;
104: }
105:
106: /**
107: * @see de.laures.cewolf.Storage#getChartImage(String, HttpServletRequest)
108: */
109: public ChartImage getChartImage(String id,
110: HttpServletRequest request) {
111: ChartImage res = null;
112: ObjectInputStream ois = null;
113: try {
114: ois = new ObjectInputStream(new FileInputStream(
115: getFileName(id)));
116: res = (ChartImage) ois.readObject();
117: ois.close();
118: } catch (Exception ex) {
119: ex.printStackTrace();
120: } finally {
121: if (ois != null) {
122: try {
123: ois.close();
124: } catch (IOException ioex) {
125: ioex.printStackTrace();
126: }
127: }
128: }
129: return res;
130: }
131:
132: /**
133: * @see de.laures.cewolf.Storage#contains(ChartImage, PageContext)
134: */
135: public boolean contains(ChartImage chartImage,
136: PageContext pageContext) {
137: return new File(getFileName(chartImage)).exists();
138: }
139:
140: /**
141: * @see de.laures.cewolf.Storage#getKey(ChartImage)
142: */
143: public String getKey(ChartImage chartImage) {
144: return String.valueOf(KeyGenerator
145: .generateKey((Serializable) chartImage));
146: }
147:
148: /**
149: * @see de.laures.cewolf.Storage#init(ServletContext)
150: */
151: public void init(ServletContext servletContext)
152: throws CewolfException {
153: basePath = servletContext.getRealPath("/");
154: Configuration config = Configuration
155: .getInstance(servletContext);
156: deleteOnExit = "true".equalsIgnoreCase(""
157: + config.getParameters()
158: .get("FileStorage.deleteOnExit"));
159: servletContext.log("FileStorage initialized, deleteOnExit="
160: + deleteOnExit);
161: }
162:
163: private String getFileName(ChartImage chartImage) {
164: return getFileName(getKey(chartImage));
165: }
166:
167: private String getFileName(String id) {
168: return basePath + "_chart" + id;
169: }
170:
171: /**
172: * @see de.laures.cewolf.Storage#removeChartImage(java.lang.String, javax.servlet.jsp.PageContext)
173: */
174: public String removeChartImage(String imgKey,
175: HttpServletRequest pageContext) throws CewolfException {
176: File file = new File(getFileName(imgKey));
177: if (file.exists()) {
178: if (!file.delete()) {
179: throw new CewolfException("Could not delete file "
180: + file.getAbsolutePath());
181: }
182: }
183: return imgKey;
184: }
185:
186: }
|