KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > laures > cewolf > storage > FileStorage


1 /* ================================================================
2  * Cewolf : Chart enabling Web Objects Framework
3  * ================================================================
4  *
5  * Project Info: http://cewolf.sourceforge.net
6  * Project Lead: Guido Laures (guido@laures.de);
7  *
8  * (C) Copyright 2002, by Guido Laures
9  *
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.
13  *
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.
17  *
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.
21  */

22
23 package de.laures.cewolf.storage;
24
25 import java.io.File JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.ObjectInputStream JavaDoc;
30 import java.io.ObjectOutputStream JavaDoc;
31 import java.io.Serializable JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.List JavaDoc;
34
35 import javax.servlet.ServletContext JavaDoc;
36 import javax.servlet.http.HttpServletRequest JavaDoc;
37 import javax.servlet.jsp.PageContext JavaDoc;
38
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;
44
45 /**
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:
49  *
50  * <pre>
51  * <init-param>
52  * <param-name>storage</param-name>
53  * <param-value>de.laures.cewolf.storage.FileStorage</param-value>
54  * </init-param>
55  * <init-param>
56  * <param-name>FileStorage.deleteOnExit</param-name>
57  * <param-value>true</param-value>
58  * </init-param>
59  * </pre>
60  *
61  * @author guido
62  */

63 public class FileStorage implements Storage {
64     
65     String JavaDoc basePath = null;
66     List JavaDoc stored = new ArrayList JavaDoc();
67     private boolean deleteOnExit = false;
68
69     /**
70      * @see de.laures.cewolf.Storage#storeChartImage(ChartImage, PageContext)
71      */

72     public String JavaDoc storeChartImage(ChartImage cid, PageContext JavaDoc pageContext) {
73         if(contains(cid, pageContext)){
74             return getKey(cid);
75         }
76         String JavaDoc id = getKey(cid);
77         ObjectOutputStream JavaDoc oos = null;
78         try {
79             String JavaDoc fileName = getFileName(id);
80             pageContext.getServletContext().log("Storing image to file " + fileName);
81             File JavaDoc f = new File JavaDoc(fileName);
82             if (deleteOnExit) {
83                 f.deleteOnExit();
84             }
85             oos = new ObjectOutputStream JavaDoc(new FileOutputStream JavaDoc(f));
86             oos.writeObject(new SerializableChartImage(cid));
87             oos.close();
88         } catch(IOException JavaDoc ioex){
89             ioex.printStackTrace();
90         } catch(CewolfException cwex){
91             cwex.printStackTrace();
92         } finally {
93             if(oos != null){
94                 try {
95                     oos.close();
96                 } catch(IOException JavaDoc ioex){
97                     ioex.printStackTrace();
98                 }
99             }
100         }
101         return id;
102     }
103
104     /**
105      * @see de.laures.cewolf.Storage#getChartImage(String, HttpServletRequest)
106      */

107     public ChartImage getChartImage(String JavaDoc id, HttpServletRequest JavaDoc request) {
108         ChartImage res = null;
109         ObjectInputStream JavaDoc ois = null;
110         try {
111             ois = new ObjectInputStream JavaDoc(new FileInputStream JavaDoc(getFileName(id)));
112             res = (ChartImage)ois.readObject();
113             ois.close();
114         } catch(Exception JavaDoc ex){
115             ex.printStackTrace();
116         } finally {
117             if(ois != null){
118                 try {
119                     ois.close();
120                 } catch(IOException JavaDoc ioex){
121                     ioex.printStackTrace();
122                 }
123             }
124         }
125         return res;
126     }
127
128     /**
129      * @see de.laures.cewolf.Storage#contains(ChartImage, PageContext)
130      */

131     public boolean contains(ChartImage chartImage, PageContext JavaDoc pageContext) {
132         return new File JavaDoc(getFileName(chartImage)).exists();
133     }
134
135     /**
136      * @see de.laures.cewolf.Storage#getKey(ChartImage)
137      */

138     public String JavaDoc getKey(ChartImage chartImage) {
139         return String.valueOf(KeyGenerator.generateKey((Serializable JavaDoc)chartImage));
140     }
141
142     /**
143      * @see de.laures.cewolf.Storage#init(ServletContext)
144      */

145     public void init(ServletContext JavaDoc 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);
150     }
151     
152     private String JavaDoc getFileName(ChartImage chartImage){
153         return getFileName(getKey(chartImage));
154     }
155
156     private String JavaDoc getFileName(String JavaDoc id){
157         return basePath + "_chart" + id;
158     }
159
160     /**
161      * @see de.laures.cewolf.Storage#removeChartImage(java.lang.String, javax.servlet.jsp.PageContext)
162      */

163     public String JavaDoc removeChartImage(String JavaDoc imgKey, HttpServletRequest JavaDoc pageContext) throws CewolfException {
164         File JavaDoc file = new File JavaDoc(getFileName(imgKey));
165         if (file.exists())
166         {
167             if (!file.delete())
168             {
169                 throw new CewolfException("Could not delete file " + file.getAbsolutePath());
170             }
171         }
172         return imgKey;
173     }
174
175 }
176
Popular Tags