1 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 43 public abstract class AbstractSessionStorage implements Storage 44 { 45 46 private static final Log log = LogFactory.getLog(AbstractSessionStorage.class); 47 48 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 return storeChartImage(cid, session); 61 } 62 63 64 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 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 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 124 public void init( ServletContext servletContext ) throws CewolfException 125 { 126 } 127 128 129 130 } | Popular Tags |