KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > core > util > http > ContextServices


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: ContextServices.java,v 1.6 2004/02/01 05:16:28 christianc Exp $
19  */

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

35 public class ContextServices extends org.enhydra.barracuda.plankton.http.ContextServices {
36
37     /**
38      * get the servlet context from a ViewContext
39      *
40      * @param vc the ControlEventContext object
41      * @return the users session
42      */

43     public static ServletContext getContext(ViewContext vc) {
44         EventContext ec = vc.getEventContext();
45         if (ec!=null && (ec instanceof ControlEventContext)) {
46             return getContext((ControlEventContext) ec);
47         } else {
48             return null;
49         }
50     }
51     
52     /**
53      * get the servlet context from a ControlEventContext
54      *
55      * @param ec the ControlEventContext object
56      * @return the users session
57      */

58     public static ServletContext getContext(ControlEventContext ec) {
59         return ec.getConfig().getServletContext();
60     }
61     
62     /**
63      * This method retrieves the servlet context from a ViewContext,
64      * and then looks for an object in the servlet context based on a given key.
65      * If the object is not present, it will be created using the
66      * ReferenceFactory and cached in servlet context for future use.
67      *
68      * @param vc the ViewContext
69      * @param key the key that identifies this object
70      * @param factory the ReferenceFactory used to create the object
71      * @return the object from the cache
72      */

73     public static Object JavaDoc getObjectFromCache(ViewContext vc, Object JavaDoc key, ReferenceFactory factory) {
74         return getObjectFromCache(getContext(vc), key, factory);
75     }
76     
77     /**
78      * This method retrieves the servlet context from a ControlEventContext,
79      * and then looks for an object in the servlet context based on a given key.
80      * If the object is not present, it will be created using the
81      * ReferenceFactory and cached in servlet context for future use.
82      *
83      * @param ec the ControlEventContext
84      * @param key the key that identifies this object
85      * @param factory the ReferenceFactory used to create the object
86      * @return the object from the cache
87      */

88     public static Object JavaDoc getObjectFromCache(ControlEventContext ec, Object JavaDoc key, ReferenceFactory factory) {
89         return getObjectFromCache(getContext(ec), key, factory);
90     }
91     
92 }
93
Popular Tags