java/cewolf-1.0/src/main/java/de/laures/cewolf/storage/AbstractSessionStorage.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
     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;
    26 
    27 import javax.servlet.ServletContext;
    28 import javax.servlet.http.HttpServletRequest;
    29 import javax.servlet.http.HttpSession;
    30 import javax.servlet.jsp.PageContext;
    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 storeChartImage( ChartImage cid, PageContext pageContext ) throws CewolfException
    52   {
    53     if ( contains(cid, pageContext) )
    54     {
    55       return getKey(cid);
    56     }
    57     log.debug("storing chart " + cid);
    58     final HttpSession 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 id, HttpServletRequest request )
    68   {
    69     HttpSession session = request.getSession();
    70     return (ChartImage) session.getAttribute(id);
    71   }
    72 
    73   public boolean contains( ChartImage cid, PageContext pageContext )
    74   {
    75     return pageContext.getSession().getAttribute(getKey(cid)) != null;
    76   }
    77 
    78   public final String getKey( ChartImage cid )
    79   {
    80     return String.valueOf(KeyGenerator.generateKey((Serializable) cid));
    81   }
    82 
    83   protected String storeChartImage( ChartImage cid, HttpSession session ) throws CewolfException
    84   {
    85     final String sessionKey = getKey(cid);
    86     synchronized (session)
    87     {
    88       session.setAttribute(sessionKey, getCacheObject(cid));
    89     }
    90     return sessionKey;
    91   }
    92   
    93 	/**
    94 	 */
    95 	public String removeChartImage(String imgKey, HttpServletRequest request)
    96 			throws CewolfException {
    97 		final HttpSession 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 removeChartImage(String cid, HttpSession session)
   112 			throws CewolfException {
   113 		synchronized (session) {
   114 			session.removeAttribute(cid);
   115 		}
   116 		return cid;
   117 	}
   118 
   119   protected abstract Object getCacheObject( ChartImage cid ) throws CewolfException;
   120 
   121   /**
   122    * @see de.laures.cewolf.Storage#init(ServletContext)
   123    */
   124   public void init( ServletContext servletContext ) throws CewolfException
   125   {
   126   }
   127   
   128 
   129 
   130 }