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: package de.laures.cewolf.storage;
023:
024: import java.io.Serializable;
025:
026: import javax.servlet.ServletContext;
027: import javax.servlet.http.HttpServletRequest;
028: import javax.servlet.http.HttpSession;
029: import javax.servlet.jsp.PageContext;
030:
031: import de.laures.cewolf.CewolfException;
032: import de.laures.cewolf.ChartImage;
033: import de.laures.cewolf.Storage;
034: import de.laures.cewolf.taglib.util.KeyGenerator;
035:
036: /**
037: * Storage stores images in session, but expires them after a certain time.
038: * This expiration time defaults to 300 seconds, and can be changed by adding
039: * the timeout="xxx" parameter to <cewolf:img> and <cewolf:legend> tags.
040: *
041: * @author brianf
042: */
043: public class LongTermSessionStorage implements Storage {
044:
045: public final String getKey(ChartImage cid) {
046: return String.valueOf(KeyGenerator
047: .generateKey((Serializable) cid));
048: }
049:
050: /*
051: * (non-Javadoc)
052: *
053: * @see de.laures.cewolf.Storage#storeChartImage(de.laures.cewolf.ChartImage,
054: * javax.servlet.jsp.PageContext)
055: */
056: public String storeChartImage(ChartImage chartImage,
057: PageContext pageContext) throws CewolfException {
058: HttpSession session = pageContext.getSession();
059: SessionStorageGroup ssg = (SessionStorageGroup) session
060: .getAttribute("CewolfCharts");
061: if (ssg == null) {
062: ssg = new SessionStorageGroup();
063: session.setAttribute("CewolfCharts", ssg);
064: }
065: String cid = getKey(chartImage);
066: SessionStorageItem ssi = new SessionStorageItem(chartImage,
067: cid, chartImage.getTimeoutTime());
068: ssg.put(cid, ssi);
069:
070: return cid;
071: }
072:
073: /*
074: * (non-Javadoc)
075: *
076: * @see de.laures.cewolf.Storage#getChartImage(java.lang.String,
077: * javax.servlet.http.HttpServletRequest)
078: */
079: public ChartImage getChartImage(String id,
080: HttpServletRequest request) {
081: HttpSession session = request.getSession();
082: ChartImage chart = null;
083: SessionStorageGroup ssg = (SessionStorageGroup) session
084: .getAttribute("CewolfCharts");
085: if (ssg != null) {
086: SessionStorageItem ssi = (SessionStorageItem) ssg.get(id);
087: if (ssi != null) {
088: chart = ssi.getChart();
089: }
090: }
091:
092: return chart;
093: }
094:
095: /*
096: * (non-Javadoc)
097: *
098: * @see de.laures.cewolf.Storage#init(javax.servlet.ServletContext)
099: */
100: public void init(ServletContext servletContext)
101: throws CewolfException {
102: }
103:
104: /**
105: * @see de.laures.cewolf.Storage#removeChartImage(java.lang.String, javax.servlet.jsp.PageContext)
106: */
107: public String removeChartImage(String cid,
108: HttpServletRequest request) throws CewolfException {
109: HttpSession session = request.getSession();
110: // No session exit
111: if (session == null) {
112: return cid;
113: }
114: SessionStorageGroup ssg = (SessionStorageGroup) session
115: .getAttribute("CewolfCharts");
116: if (ssg == null) {
117: // No group exit
118: return cid;
119: }
120: ssg.remove(cid);
121: return cid;
122: }
123: }
|