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 java.util.HashMap;
21  import javax.servlet.jsp.PageContext;
22  
23  import org.apache.velocity.context.AbstractContext;
24  
25  /***
26   *  <p>
27   *  Velocity Context implementationfor use in JSP's,
28   *  where the servlet API 'scope' is used directly.
29   *  </p>
30   *  <p>
31   *  This context will 'search' the scopes looking for an 
32   *  item, working outwards 
33   *  page->request->session->application
34   *  </p>
35   *
36   * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
37   * @version $Id: JSPContext.java 516448 2007-03-09 16:25:47Z ate $ 
38   */
39  public class JSPContext extends AbstractContext
40  {
41      private HashMap context = new HashMap();
42      private PageContext pageContext = null;
43  
44      public JSPContext( PageContext pageContext )
45      {
46          this.pageContext = pageContext;
47      }
48  
49      public Object internalGet( String key )
50      {
51          Object o = context.get( key );
52  
53          if ( o == null)
54          {
55              o = pageContext.getAttribute( key, PageContext.PAGE_SCOPE);
56  
57              if (o == null)
58              {
59                  o = pageContext.getAttribute( key, PageContext.REQUEST_SCOPE);
60  
61                  if ( o == null)
62                  {
63                      o = pageContext.getAttribute( key, PageContext.SESSION_SCOPE);
64                      
65                      if ( o == null )
66                      {
67                          o = pageContext.getAttribute( key, PageContext.APPLICATION_SCOPE);
68                      }
69                  }
70              }
71  
72              if ( o != null)
73                  context.put( key, o );
74          }
75          return o;
76      }        
77  
78      public Object internalPut( String key, Object value )
79      {
80          return context.put( key, value );
81      }
82  
83      public  boolean internalContainsKey(Object key)
84      {
85          return context.containsKey( key );
86      }
87  
88      public  Object[] internalGetKeys()
89      {
90          return context.keySet().toArray();
91      }
92  
93      public  Object internalRemove(Object key)
94      {
95          return context.remove( key );
96      }
97  }