KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > util > 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.17 2004/02/01 05:16:28 christianc Exp $
19  */

20 package org.enhydra.barracuda.core.util.http;
21
22 import javax.servlet.*;
23 import javax.servlet.http.*;
24
25 import org.enhydra.barracuda.core.comp.ViewContext;
26 import org.enhydra.barracuda.core.event.ControlEventContext;
27 import org.enhydra.barracuda.plankton.data.ReferenceFactory;
28
29
30 /**
31  * This class defines a convenience method to get the session
32  * and set the timeout at the same time. It also provides a
33  * mechanism to easily cache objects in the session by Reference
34  * (which allows them to automatically be removed from the session
35  * if the system starts running low on memory)
36  */

37 public class SessionServices extends org.enhydra.barracuda.plankton.http.SessionServices {
38
39     /**
40      * get the session from the view context (it will automatically extract
41      * the event context and cast it to ControlEventContext for you, then
42      * retrieve the request from there and use that to get the session info).
43      * If no session exists it will create it for us. Ensures that the timeout
44      * is set to DEFAULT_TIMEOUT.
45      *
46      * @param vc the ViewContext
47      * @return the users session
48      */

49     public static HttpSession getSession(ViewContext vc) {
50         return getSession(((ControlEventContext) vc.getEventContext()).getRequest());
51     }
52     
53     /**
54      * get the session from the view context (allowing you to specify whether or
55      * not to create it)
56      *
57      * @param vc the ViewContext
58      * @param create if true, the session will be created if it does not
59      * already exist
60      * @return the users session (may be null if the session does not yet
61      * exist and create is false)
62      */

63     public static HttpSession getSession(ViewContext vc, boolean create) {
64         return getSession(((ControlEventContext) vc.getEventContext()).getRequest(), create);
65     }
66     
67     /**
68      * get the session from the view context (allowing you to specify
69      * whether or not to create it). If the session exists, it
70      * will set the default timeout for us.
71      *
72      * @param vc the ViewContext
73      * @param create if true, the session will be created if it does not
74      * already exist
75      * @param timeout the default timeout value (null indicates do not set)
76      * @return the users session (may be null if the session does not yet
77      * exist and create is false)
78      */

79     public static HttpSession getSession(ViewContext vc, boolean create, Integer JavaDoc timeout) {
80         return getSession(((ControlEventContext) vc.getEventContext()).getRequest(), create, timeout);
81     }
82
83
84     /**
85      * This method retrieves the session from the Context, and then looks for an
86      * object in the session based on a given key. If the object is not present,
87      * it will be created using the ReferenceFactory and cached in session for
88      * future use.
89      *
90      * @param context the ControlEventContext
91      * @param key the key that identifies this object
92      * @param factory the ReferenceFactory used to create the object
93      * @return the object from the cache
94      */

95     public static Object JavaDoc getObjectFromCache(ControlEventContext context, Object JavaDoc key, ReferenceFactory factory) {
96         HttpSession session = SessionServices.getSession(context.getRequest());
97         return getObjectFromCache(session, key, factory);
98     }
99     
100 }
101
Popular Tags