1 20 package org.enhydra.barracuda.plankton.http; 21 22 import java.lang.ref.*; 23 import javax.servlet.*; 24 import javax.servlet.http.*; 25 26 import org.apache.log4j.*; 27 28 import org.enhydra.barracuda.plankton.data.ReferenceFactory; 29 30 31 38 public class SessionServices { 39 40 public static final String KEY = SessionServices.class.getName()+".Key"; 43 44 protected static final Logger logger = Logger.getLogger(SessionServices.class.getName()); 45 46 53 public static HttpSession getSession(HttpServletRequest req) { 54 return getSession(req, true, null); 56 } 57 58 68 public static HttpSession getSession(HttpServletRequest req, boolean create) { 69 return getSession(req, create, null); 70 } 71 72 84 public static HttpSession getSession(HttpServletRequest req, boolean create, Integer timeout) { 85 HttpSession session = req.getSession(false); 86 if (session==null && create) { 87 session = req.getSession(create); 88 if (timeout!=null) session.setMaxInactiveInterval(timeout.intValue()); 89 } 90 return session; 91 } 92 93 103 public static Object getObjectFromCache(HttpSession session, Object key, ReferenceFactory factory) { 104 Reference r = (Reference) session.getAttribute(KEY+key); 105 Object obj = null; 106 if (r!=null) obj = r.get(); 107 if (r==null || obj==null) { 108 r = factory.getObjectReference(); 109 obj = r.get(); 110 session.setAttribute(KEY+key, r); 111 if (logger.isDebugEnabled()) logger.debug("Created reference:"+r); 112 } 113 if (logger.isDebugEnabled()) logger.debug("Returning object from cache:"+obj); 114 return obj; 115 } 116 117 118 119 } 120 | Popular Tags |