java/cewolf-1.0/src/main/java/de/laures/cewolf/storage/FileStorage.java
author František Kučera <franta-hg@frantovo.cz>
Sun, 01 Mar 2009 11:06:49 +0100
changeset 4 249560bcc466
parent 1 639991d0808a
permissions -rw-r--r--
Doplnění serializovatelnosti
     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;
    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;
    34 
    35 import javax.servlet.ServletContext;
    36 import javax.servlet.http.HttpServletRequest;
    37 import javax.servlet.jsp.PageContext;
    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 basePath = null;
    66 	List stored = new ArrayList();
    67 	private boolean deleteOnExit = false;
    68 
    69 	/**
    70 	 * @see de.laures.cewolf.Storage#storeChartImage(ChartImage, PageContext)
    71 	 */
    72 	public String storeChartImage(ChartImage cid, PageContext pageContext) {
    73 		if(contains(cid, pageContext)){
    74 			return getKey(cid);
    75 		}
    76 		String id = getKey(cid);
    77 		ObjectOutputStream oos = null;
    78 		try {
    79 			String fileName = getFileName(id);
    80 			pageContext.getServletContext().log("Storing image to file " + fileName);
    81 			File f = new File(fileName);
    82 			if (deleteOnExit) {
    83 				f.deleteOnExit();			
    84 			}
    85 			oos = new ObjectOutputStream(new FileOutputStream(f));
    86 			oos.writeObject(new SerializableChartImage(cid));
    87 			oos.close();
    88 		} catch(IOException 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 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 id, HttpServletRequest request) {
   108 		ChartImage res = null;
   109 		ObjectInputStream ois = null;
   110 		try {
   111 			ois = new ObjectInputStream(new FileInputStream(getFileName(id)));
   112 			res = (ChartImage)ois.readObject();
   113 			ois.close();
   114 		} catch(Exception ex){
   115 			ex.printStackTrace();
   116 		} finally {
   117 			if(ois != null){
   118 				try {
   119 					ois.close();
   120 				} catch(IOException 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 pageContext) {
   132 		return new File(getFileName(chartImage)).exists();
   133 	}
   134 
   135 	/**
   136 	 * @see de.laures.cewolf.Storage#getKey(ChartImage)
   137 	 */
   138 	public String getKey(ChartImage chartImage) {
   139 		return String.valueOf(KeyGenerator.generateKey((Serializable)chartImage));
   140 	}
   141 
   142 	/**
   143 	 * @see de.laures.cewolf.Storage#init(ServletContext)
   144 	 */
   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);
   150 	}
   151 	
   152 	private String getFileName(ChartImage chartImage){
   153 		return getFileName(getKey(chartImage));
   154 	}
   155 
   156 	private String getFileName(String 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 removeChartImage(String imgKey, HttpServletRequest pageContext) throws CewolfException {
   164 		File file = new File(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 }