java/cewolf-1.0/src/main/java/de/laures/cewolf/storage/AbstractSessionStorage.java
changeset 1 639991d0808a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/java/cewolf-1.0/src/main/java/de/laures/cewolf/storage/AbstractSessionStorage.java	Sat Feb 28 21:31:02 2009 +0100
     1.3 @@ -0,0 +1,130 @@
     1.4 +/* ================================================================
     1.5 + * Cewolf : Chart enabling Web Objects Framework
     1.6 + * ================================================================
     1.7 + *
     1.8 + * Project Info:  http://cewolf.sourceforge.net
     1.9 + * Project Lead:  Guido Laures (guido@laures.de);
    1.10 + *
    1.11 + * (C) Copyright 2002, by Guido Laures
    1.12 + *
    1.13 + * This library is free software; you can redistribute it and/or modify it under the terms
    1.14 + * of the GNU Lesser General Public License as published by the Free Software Foundation;
    1.15 + * either version 2.1 of the License, or (at your option) any later version.
    1.16 + *
    1.17 + * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
    1.18 + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    1.19 + * See the GNU Lesser General Public License for more details.
    1.20 + *
    1.21 + * You should have received a copy of the GNU Lesser General Public License along with this
    1.22 + * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
    1.23 + * Boston, MA 02111-1307, USA.
    1.24 + */
    1.25 +
    1.26 +package de.laures.cewolf.storage;
    1.27 +
    1.28 +import java.io.Serializable;
    1.29 +
    1.30 +import javax.servlet.ServletContext;
    1.31 +import javax.servlet.http.HttpServletRequest;
    1.32 +import javax.servlet.http.HttpSession;
    1.33 +import javax.servlet.jsp.PageContext;
    1.34 +
    1.35 +import org.apache.commons.logging.Log;
    1.36 +import org.apache.commons.logging.LogFactory;
    1.37 +
    1.38 +import de.laures.cewolf.CewolfException;
    1.39 +import de.laures.cewolf.ChartImage;
    1.40 +import de.laures.cewolf.Storage;
    1.41 +import de.laures.cewolf.taglib.util.KeyGenerator;
    1.42 +
    1.43 +/**
    1.44 + * @author glaures
    1.45 + */
    1.46 +public abstract class AbstractSessionStorage implements Storage
    1.47 +{
    1.48 +
    1.49 +  private static final Log log = LogFactory.getLog(AbstractSessionStorage.class);
    1.50 +
    1.51 +  /**
    1.52 +   * @see de.laures.cewolf.Storage#storeChartImage(ChartImage, ServletContext)
    1.53 +   */
    1.54 +  public String storeChartImage( ChartImage cid, PageContext pageContext ) throws CewolfException
    1.55 +  {
    1.56 +    if ( contains(cid, pageContext) )
    1.57 +    {
    1.58 +      return getKey(cid);
    1.59 +    }
    1.60 +    log.debug("storing chart " + cid);
    1.61 +    final HttpSession session = pageContext.getSession();
    1.62 +    //String key = getKey(cid);
    1.63 +    return storeChartImage(cid, session);
    1.64 +  }
    1.65 +
    1.66 +
    1.67 +  /**
    1.68 +   * @see de.laures.cewolf.Storage#getChartImage(String)
    1.69 +   */
    1.70 +  public ChartImage getChartImage( String id, HttpServletRequest request )
    1.71 +  {
    1.72 +    HttpSession session = request.getSession();
    1.73 +    return (ChartImage) session.getAttribute(id);
    1.74 +  }
    1.75 +
    1.76 +  public boolean contains( ChartImage cid, PageContext pageContext )
    1.77 +  {
    1.78 +    return pageContext.getSession().getAttribute(getKey(cid)) != null;
    1.79 +  }
    1.80 +
    1.81 +  public final String getKey( ChartImage cid )
    1.82 +  {
    1.83 +    return String.valueOf(KeyGenerator.generateKey((Serializable) cid));
    1.84 +  }
    1.85 +
    1.86 +  protected String storeChartImage( ChartImage cid, HttpSession session ) throws CewolfException
    1.87 +  {
    1.88 +    final String sessionKey = getKey(cid);
    1.89 +    synchronized (session)
    1.90 +    {
    1.91 +      session.setAttribute(sessionKey, getCacheObject(cid));
    1.92 +    }
    1.93 +    return sessionKey;
    1.94 +  }
    1.95 +  
    1.96 +	/**
    1.97 +	 */
    1.98 +	public String removeChartImage(String imgKey, HttpServletRequest request)
    1.99 +			throws CewolfException {
   1.100 +		final HttpSession session = request.getSession();
   1.101 +		if (session == null)
   1.102 +		{
   1.103 +			return imgKey;
   1.104 +		}
   1.105 +		return removeChartImage(imgKey, session);		
   1.106 +	}
   1.107 +
   1.108 +	/**
   1.109 +	 * @param cid
   1.110 +	 * @param session
   1.111 +	 * @return
   1.112 +	 * @throws CewolfException
   1.113 +	 */
   1.114 +	protected String removeChartImage(String cid, HttpSession session)
   1.115 +			throws CewolfException {
   1.116 +		synchronized (session) {
   1.117 +			session.removeAttribute(cid);
   1.118 +		}
   1.119 +		return cid;
   1.120 +	}
   1.121 +
   1.122 +  protected abstract Object getCacheObject( ChartImage cid ) throws CewolfException;
   1.123 +
   1.124 +  /**
   1.125 +   * @see de.laures.cewolf.Storage#init(ServletContext)
   1.126 +   */
   1.127 +  public void init( ServletContext servletContext ) throws CewolfException
   1.128 +  {
   1.129 +  }
   1.130 +  
   1.131 +
   1.132 +
   1.133 +}
   1.134 \ No newline at end of file