KickJava   Java API By Example, From Geeks To Geeks.

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


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.Serializable JavaDoc;
26
27 import javax.servlet.ServletContext JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpSession JavaDoc;
30 import javax.servlet.jsp.PageContext JavaDoc;
31
32 import org.apache.commons.logging.Log;
33 import org.apache.commons.logging.LogFactory;
34
35 import de.laures.cewolf.CewolfException;
36 import de.laures.cewolf.ChartImage;
37 import de.laures.cewolf.Storage;
38 import de.laures.cewolf.taglib.util.KeyGenerator;
39
40 /**
41  * @author glaures
42  */

43 public abstract class AbstractSessionStorage implements Storage
44 {
45
46   private static final Log log = LogFactory.getLog(AbstractSessionStorage.class);
47
48   /**
49    * @see de.laures.cewolf.Storage#storeChartImage(ChartImage, ServletContext)
50    */

51   public String JavaDoc storeChartImage( ChartImage cid, PageContext JavaDoc pageContext ) throws CewolfException
52   {
53     if ( contains(cid, pageContext) )
54     {
55       return getKey(cid);
56     }
57     log.debug("storing chart " + cid);
58     final HttpSession JavaDoc session = pageContext.getSession();
59     //String key = getKey(cid);
60
return storeChartImage(cid, session);
61   }
62
63
64   /**
65    * @see de.laures.cewolf.Storage#getChartImage(String)
66    */

67   public ChartImage getChartImage( String JavaDoc id, HttpServletRequest JavaDoc request )
68   {
69     HttpSession JavaDoc session = request.getSession();
70     return (ChartImage) session.getAttribute(id);
71   }
72
73   public boolean contains( ChartImage cid, PageContext JavaDoc pageContext )
74   {
75     return pageContext.getSession().getAttribute(getKey(cid)) != null;
76   }
77
78   public final String JavaDoc getKey( ChartImage cid )
79   {
80     return String.valueOf(KeyGenerator.generateKey((Serializable JavaDoc) cid));
81   }
82
83   protected String JavaDoc storeChartImage( ChartImage cid, HttpSession JavaDoc session ) throws CewolfException
84   {
85     final String JavaDoc sessionKey = getKey(cid);
86     synchronized (session)
87     {
88       session.setAttribute(sessionKey, getCacheObject(cid));
89     }
90     return sessionKey;
91   }
92   
93     /**
94      */

95     public String JavaDoc removeChartImage(String JavaDoc imgKey, HttpServletRequest JavaDoc request)
96             throws CewolfException {
97         final HttpSession JavaDoc session = request.getSession();
98         if (session == null)
99         {
100             return imgKey;
101         }
102         return removeChartImage(imgKey, session);
103     }
104
105     /**
106      * @param cid
107      * @param session
108      * @return
109      * @throws CewolfException
110      */

111     protected String JavaDoc removeChartImage(String JavaDoc cid, HttpSession JavaDoc session)
112             throws CewolfException {
113         synchronized (session) {
114             session.removeAttribute(cid);
115         }
116         return cid;
117     }
118
119   protected abstract Object JavaDoc getCacheObject( ChartImage cid ) throws CewolfException;
120
121   /**
122    * @see de.laures.cewolf.Storage#init(ServletContext)
123    */

124   public void init( ServletContext JavaDoc servletContext ) throws CewolfException
125   {
126   }
127   
128
129
130 }
Popular Tags