java/cewolf-1.0/src/main/java/de/laures/cewolf/storage/FileStorage.java
author František Kučera <franta-hg@frantovo.cz>
Sat, 28 Feb 2009 21:31:02 +0100
changeset 1 639991d0808a
permissions -rw-r--r--
Rozbalená knihovna verze 1.0
franta-hg@1
     1
/* ================================================================
franta-hg@1
     2
 * Cewolf : Chart enabling Web Objects Framework
franta-hg@1
     3
 * ================================================================
franta-hg@1
     4
 *
franta-hg@1
     5
 * Project Info:  http://cewolf.sourceforge.net
franta-hg@1
     6
 * Project Lead:  Guido Laures (guido@laures.de);
franta-hg@1
     7
 *
franta-hg@1
     8
 * (C) Copyright 2002, by Guido Laures
franta-hg@1
     9
 *
franta-hg@1
    10
 * This library is free software; you can redistribute it and/or modify it under the terms
franta-hg@1
    11
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
franta-hg@1
    12
 * either version 2.1 of the License, or (at your option) any later version.
franta-hg@1
    13
 *
franta-hg@1
    14
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
franta-hg@1
    15
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
franta-hg@1
    16
 * See the GNU Lesser General Public License for more details.
franta-hg@1
    17
 *
franta-hg@1
    18
 * You should have received a copy of the GNU Lesser General Public License along with this
franta-hg@1
    19
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
franta-hg@1
    20
 * Boston, MA 02111-1307, USA.
franta-hg@1
    21
 */
franta-hg@1
    22
franta-hg@1
    23
package de.laures.cewolf.storage;
franta-hg@1
    24
franta-hg@1
    25
import java.io.File;
franta-hg@1
    26
import java.io.FileInputStream;
franta-hg@1
    27
import java.io.FileOutputStream;
franta-hg@1
    28
import java.io.IOException;
franta-hg@1
    29
import java.io.ObjectInputStream;
franta-hg@1
    30
import java.io.ObjectOutputStream;
franta-hg@1
    31
import java.io.Serializable;
franta-hg@1
    32
import java.util.ArrayList;
franta-hg@1
    33
import java.util.List;
franta-hg@1
    34
franta-hg@1
    35
import javax.servlet.ServletContext;
franta-hg@1
    36
import javax.servlet.http.HttpServletRequest;
franta-hg@1
    37
import javax.servlet.jsp.PageContext;
franta-hg@1
    38
franta-hg@1
    39
import de.laures.cewolf.CewolfException;
franta-hg@1
    40
import de.laures.cewolf.ChartImage;
franta-hg@1
    41
import de.laures.cewolf.Configuration;
franta-hg@1
    42
import de.laures.cewolf.Storage;
franta-hg@1
    43
import de.laures.cewolf.taglib.util.KeyGenerator;
franta-hg@1
    44
franta-hg@1
    45
/**
franta-hg@1
    46
 * Storage for storing images as files in the web application directory as files _chart-XXXXX.
franta-hg@1
    47
 * Note that by default the files won't ever be removed. To remove saved images on VM exit set
franta-hg@1
    48
 * the <code>FileStorage.deleteOnExit</code> configuration parameter to "true". For example:
franta-hg@1
    49
 * 
franta-hg@1
    50
 * <pre>
franta-hg@1
    51
 *		<init-param>
franta-hg@1
    52
 *			<param-name>storage</param-name>
franta-hg@1
    53
 *			<param-value>de.laures.cewolf.storage.FileStorage</param-value>
franta-hg@1
    54
 *		</init-param>
franta-hg@1
    55
 *		<init-param>
franta-hg@1
    56
 *				<param-name>FileStorage.deleteOnExit</param-name>
franta-hg@1
    57
 *				<param-value>true</param-value>
franta-hg@1
    58
 *		</init-param> 
franta-hg@1
    59
 *	</pre> 
franta-hg@1
    60
 * 
franta-hg@1
    61
 * @author guido
franta-hg@1
    62
 */
franta-hg@1
    63
public class FileStorage implements Storage {
franta-hg@1
    64
	
franta-hg@1
    65
	String basePath = null;
franta-hg@1
    66
	List stored = new ArrayList();
franta-hg@1
    67
	private boolean deleteOnExit = false;
franta-hg@1
    68
franta-hg@1
    69
	/**
franta-hg@1
    70
	 * @see de.laures.cewolf.Storage#storeChartImage(ChartImage, PageContext)
franta-hg@1
    71
	 */
franta-hg@1
    72
	public String storeChartImage(ChartImage cid, PageContext pageContext) {
franta-hg@1
    73
		if(contains(cid, pageContext)){
franta-hg@1
    74
			return getKey(cid);
franta-hg@1
    75
		}
franta-hg@1
    76
		String id = getKey(cid);
franta-hg@1
    77
		ObjectOutputStream oos = null;
franta-hg@1
    78
		try {
franta-hg@1
    79
			String fileName = getFileName(id);
franta-hg@1
    80
			pageContext.getServletContext().log("Storing image to file " + fileName);
franta-hg@1
    81
			File f = new File(fileName);
franta-hg@1
    82
			if (deleteOnExit) {
franta-hg@1
    83
				f.deleteOnExit();			
franta-hg@1
    84
			}
franta-hg@1
    85
			oos = new ObjectOutputStream(new FileOutputStream(f));
franta-hg@1
    86
			oos.writeObject(new SerializableChartImage(cid));
franta-hg@1
    87
			oos.close();
franta-hg@1
    88
		} catch(IOException ioex){
franta-hg@1
    89
			ioex.printStackTrace();
franta-hg@1
    90
		} catch(CewolfException cwex){
franta-hg@1
    91
			cwex.printStackTrace();
franta-hg@1
    92
		} finally {
franta-hg@1
    93
			if(oos != null){
franta-hg@1
    94
				try {
franta-hg@1
    95
					oos.close();
franta-hg@1
    96
				} catch(IOException ioex){
franta-hg@1
    97
					ioex.printStackTrace();
franta-hg@1
    98
				}
franta-hg@1
    99
			}
franta-hg@1
   100
		}
franta-hg@1
   101
		return id;		
franta-hg@1
   102
	}
franta-hg@1
   103
franta-hg@1
   104
	/**
franta-hg@1
   105
	 * @see de.laures.cewolf.Storage#getChartImage(String, HttpServletRequest)
franta-hg@1
   106
	 */
franta-hg@1
   107
	public ChartImage getChartImage(String id, HttpServletRequest request) {
franta-hg@1
   108
		ChartImage res = null;
franta-hg@1
   109
		ObjectInputStream ois = null;
franta-hg@1
   110
		try {
franta-hg@1
   111
			ois = new ObjectInputStream(new FileInputStream(getFileName(id)));
franta-hg@1
   112
			res = (ChartImage)ois.readObject();
franta-hg@1
   113
			ois.close();
franta-hg@1
   114
		} catch(Exception ex){
franta-hg@1
   115
			ex.printStackTrace();
franta-hg@1
   116
		} finally {
franta-hg@1
   117
			if(ois != null){
franta-hg@1
   118
				try {
franta-hg@1
   119
					ois.close();
franta-hg@1
   120
				} catch(IOException ioex){
franta-hg@1
   121
					ioex.printStackTrace();
franta-hg@1
   122
				}
franta-hg@1
   123
			}
franta-hg@1
   124
		}
franta-hg@1
   125
		return res;
franta-hg@1
   126
	}
franta-hg@1
   127
franta-hg@1
   128
	/**
franta-hg@1
   129
	 * @see de.laures.cewolf.Storage#contains(ChartImage, PageContext)
franta-hg@1
   130
	 */
franta-hg@1
   131
	public boolean contains(ChartImage chartImage, PageContext pageContext) {
franta-hg@1
   132
		return new File(getFileName(chartImage)).exists();
franta-hg@1
   133
	}
franta-hg@1
   134
franta-hg@1
   135
	/**
franta-hg@1
   136
	 * @see de.laures.cewolf.Storage#getKey(ChartImage)
franta-hg@1
   137
	 */
franta-hg@1
   138
	public String getKey(ChartImage chartImage) {
franta-hg@1
   139
		return String.valueOf(KeyGenerator.generateKey((Serializable)chartImage));
franta-hg@1
   140
	}
franta-hg@1
   141
franta-hg@1
   142
	/**
franta-hg@1
   143
	 * @see de.laures.cewolf.Storage#init(ServletContext)
franta-hg@1
   144
	 */
franta-hg@1
   145
	public void init(ServletContext servletContext) throws CewolfException {
franta-hg@1
   146
		basePath = servletContext.getRealPath("/");
franta-hg@1
   147
		Configuration config = Configuration.getInstance(servletContext);
franta-hg@1
   148
		deleteOnExit = "true".equalsIgnoreCase("" + config.getParameters().get("FileStorage.deleteOnExit"));
franta-hg@1
   149
		servletContext.log("FileStorage initialized, deleteOnExit=" + deleteOnExit);
franta-hg@1
   150
	}
franta-hg@1
   151
	
franta-hg@1
   152
	private String getFileName(ChartImage chartImage){
franta-hg@1
   153
		return getFileName(getKey(chartImage));
franta-hg@1
   154
	}
franta-hg@1
   155
franta-hg@1
   156
	private String getFileName(String id){
franta-hg@1
   157
		return basePath + "_chart" + id;
franta-hg@1
   158
	}
franta-hg@1
   159
franta-hg@1
   160
	/**
franta-hg@1
   161
	 * @see de.laures.cewolf.Storage#removeChartImage(java.lang.String, javax.servlet.jsp.PageContext)
franta-hg@1
   162
	 */
franta-hg@1
   163
	public String removeChartImage(String imgKey, HttpServletRequest pageContext) throws CewolfException {
franta-hg@1
   164
		File file = new File(getFileName(imgKey));
franta-hg@1
   165
		if (file.exists())
franta-hg@1
   166
		{
franta-hg@1
   167
			if (!file.delete())
franta-hg@1
   168
			{
franta-hg@1
   169
				throw new CewolfException("Could not delete file " + file.getAbsolutePath());
franta-hg@1
   170
			}
franta-hg@1
   171
		}
franta-hg@1
   172
		return imgKey;
franta-hg@1
   173
	}
franta-hg@1
   174
franta-hg@1
   175
}