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.Serializable;
026:
027: import javax.servlet.ServletContext;
028: import javax.servlet.http.HttpServletRequest;
029: import javax.servlet.http.HttpSession;
030: import javax.servlet.jsp.PageContext;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: import de.laures.cewolf.CewolfException;
036: import de.laures.cewolf.ChartImage;
037: import de.laures.cewolf.Storage;
038: import de.laures.cewolf.taglib.util.KeyGenerator;
039:
040: /**
041: * @author glaures
042: */
043: public abstract class AbstractSessionStorage implements Storage {
044:
045: private static final Log log = LogFactory
046: .getLog(AbstractSessionStorage.class);
047:
048: /**
049: * @see de.laures.cewolf.Storage#storeChartImage(ChartImage, ServletContext)
050: */
051: public String storeChartImage(ChartImage cid,
052: PageContext pageContext) throws CewolfException {
053: if (contains(cid, pageContext)) {
054: return getKey(cid);
055: }
056: log.debug("storing chart " + cid);
057: final HttpSession session = pageContext.getSession();
058: //String key = getKey(cid);
059: return storeChartImage(cid, session);
060: }
061:
062: /**
063: * @see de.laures.cewolf.Storage#getChartImage(String)
064: */
065: public ChartImage getChartImage(String id,
066: HttpServletRequest request) {
067: HttpSession session = request.getSession();
068: return (ChartImage) session.getAttribute(id);
069: }
070:
071: public boolean contains(ChartImage cid, PageContext pageContext) {
072: return pageContext.getSession().getAttribute(getKey(cid)) != null;
073: }
074:
075: public final String getKey(ChartImage cid) {
076: return String.valueOf(KeyGenerator
077: .generateKey((Serializable) cid));
078: }
079:
080: protected String storeChartImage(ChartImage cid, HttpSession session)
081: throws CewolfException {
082: final String sessionKey = getKey(cid);
083: synchronized (session) {
084: session.setAttribute(sessionKey, getCacheObject(cid));
085: }
086: return sessionKey;
087: }
088:
089: /**
090: */
091: public String removeChartImage(String imgKey,
092: HttpServletRequest request) throws CewolfException {
093: final HttpSession session = request.getSession();
094: if (session == null) {
095: return imgKey;
096: }
097: return removeChartImage(imgKey, session);
098: }
099:
100: /**
101: * @param cid
102: * @param session
103: * @return
104: * @throws CewolfException
105: */
106: protected String removeChartImage(String cid, HttpSession session)
107: throws CewolfException {
108: synchronized (session) {
109: session.removeAttribute(cid);
110: }
111: return cid;
112: }
113:
114: protected abstract Object getCacheObject(ChartImage cid)
115: throws CewolfException;
116:
117: /**
118: * @see de.laures.cewolf.Storage#init(ServletContext)
119: */
120: public void init(ServletContext servletContext)
121: throws CewolfException {
122: }
123:
124: }
|