KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > plankton > http > SessionServices


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: SessionServices.java,v 1.5 2004/02/01 05:16:28 christianc Exp $
19  */

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 /**
32  * This class defines a convenience method to get the session
33  * and set the timeout at the same time. It also provides a
34  * mechanism to easily cache objects in the session by Reference
35  * (which allows them to automatically be removed from the session
36  * if the system starts running low on memory)
37  */

38 public class SessionServices {
39
40     //constants
41
// public static int DEFAULT_TIMEOUT = 600; //in seconds (10 minutes)
42
public static final String JavaDoc KEY = SessionServices.class.getName()+".Key";
43
44     protected static final Logger logger = Logger.getLogger(SessionServices.class.getName());
45
46     /**
47      * get the session from a request. If no session exists it will
48      * create it for us. Ensures that the timeout is set to DEFAULT_TIMEOUT.
49      *
50      * @param req the ServletRequest object
51      * @return the users session
52      */

53     public static HttpSession getSession(HttpServletRequest req) {
54 // return getSession(req, true, DEFAULT_TIMEOUT);
55
return getSession(req, true, null);
56     }
57     
58     /**
59      * get the session from a request (allowing you to specify
60      * whether or not to create it)
61      *
62      * @param req the ServletRequest object
63      * @param create if true, the session will be created if it does not
64      * already exist
65      * @return the users session (may be null if the session does not yet
66      * exist and create is false)
67      */

68     public static HttpSession getSession(HttpServletRequest req, boolean create) {
69         return getSession(req, create, null);
70     }
71     
72     /**
73      * get the session from a request (allowing you to specify
74      * whether or not to create it). If the session exists, it
75      * will set the default timeout for us.
76      *
77      * @param req the ServletRequest object
78      * @param create if true, the session will be created if it does not
79      * already exist
80      * @param timeout the default timeout value (null indicates do not set)
81      * @return the users session (may be null if the session does not yet
82      * exist and create is false)
83      */

84     public static HttpSession getSession(HttpServletRequest req, boolean create, Integer JavaDoc 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     /**
94      * This method looks for an object in the session based on a given key.
95      * If the object is not present, it will be created using the ReferenceFactory
96      * and cached in session for future use.
97      *
98      * @param session the HttpSession
99      * @param key the key that identifies this object
100      * @param factory the ReferenceFactory used to create the object
101      * @return the object from the cache
102      */

103     public static Object JavaDoc getObjectFromCache(HttpSession session, Object JavaDoc key, ReferenceFactory factory) {
104         Reference r = (Reference) session.getAttribute(KEY+key);
105         Object JavaDoc 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