View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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 }