1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.taglibs.velocity;
19
20 import javax.servlet.jsp.PageContext;
21
22 /***
23 * <p>
24 * Simple context tool to allow a template-in-JSP to access
25 * the scopes directly to retrieve objects/beans.
26 * </p>
27 *
28 * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
29 * @version $Id: ScopeTool.java 516448 2007-03-09 16:25:47Z ate $
30 */
31 public class ScopeTool
32 {
33 protected PageContext pageContext = null;
34
35 public ScopeTool( PageContext pageContext )
36 {
37 this.pageContext = pageContext;
38 }
39
40 /***
41 * retrieves an object from the page scope
42 *
43 * @param name Name of object in scope
44 * @return object if found, null otherwise
45 */
46 public Object getPageScope( String name )
47 {
48 return pageContext.getAttribute( name, PageContext.PAGE_SCOPE);
49 }
50
51 /***
52 * retrieves an object from the request scope
53 *
54 * @param name Name of object in scope
55 * @return object if found, null otherwise
56 */
57 public Object getRequestScope( String name )
58 {
59 return pageContext.getAttribute( name, PageContext.REQUEST_SCOPE);
60 }
61
62 /***
63 * retrieves an object from the session scope
64 *
65 * @param name Name of object in scope
66 * @return object if found, null otherwise
67 */
68 public Object getSessionScope( String name )
69 {
70 return pageContext.getAttribute( name, PageContext.SESSION_SCOPE);
71 }
72
73 /***
74 * retrieves an object from the application scope
75 *
76 * @param name Name of object in scope
77 * @return object if found, null otherwise
78 */
79 public Object getApplicationScope( String name )
80 {
81 return pageContext.getAttribute( name, PageContext.APPLICATION_SCOPE);
82 }
83
84 /***
85 * retrieves a named object from anyscope,
86 * working 'upwards':
87 * page - > request - > session - > application
88 *
89 * @param name Name of object in scope
90 * @return object if found, null otherwise
91 */
92 public Object getAnyScope( String name )
93 {
94 Object o = getPageScope( name );
95
96 if (o == null)
97 {
98 o = getRequestScope( name );
99
100 if ( o == null)
101 {
102 o = getSessionScope( name );
103
104 if ( o == null )
105 {
106 o = getApplicationScope( name );
107 }
108 }
109 }
110
111 return o;
112 }
113 }