2009/05/20 - Apache Shale has been retired.

For more information, please explore the Attic.

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.shale.test.mock;
19  
20  import java.util.Map;
21  
22  import javax.faces.context.ExternalContext;
23  import javax.faces.context.FacesContext;
24  import javax.faces.el.VariableResolver;
25  
26  /***
27   * <p>Mock implementation of <code>VariableResolver</code>.</p>
28   *
29   * <p>This implementation recognizes the standard scope names
30   * <code>applicationScope</code>, <code>facesContext</code>,
31   * <code>RequestScope</code>, and
32   * <code>sessionScope</code>, plus it knows how to search in ascending
33   * scopes for non-reserved names.</p>
34   *
35   * $Id$
36   */
37  
38  public class MockVariableResolver extends VariableResolver {
39  
40  
41      // ------------------------------------------------------------ Constructors
42  
43  
44      /***
45       * <p>Construct a default instance.</p>
46       */
47      public MockVariableResolver() {
48      }
49  
50  
51      // ----------------------------------------------------- Mock Object Methods
52  
53  
54      // ------------------------------------------------------ Instance Variables
55  
56  
57      // ------------------------------------------------ VariableResolver Methods
58  
59  
60      /*** {@inheritDoc} */
61      public Object resolveVariable(FacesContext context, String name) {
62  
63          if ((context == null) || (name == null)) {
64              throw new NullPointerException();
65          }
66  
67          // Check for magic names
68          if ("application".equals(name)) {
69              return external().getContext();
70          } else if ("applicationScope".equals(name)) {
71              return external().getApplicationMap();
72          } else if ("cookie".equals(name)) {
73              return external().getRequestCookieMap();
74          } else if ("facesContext".equals(name)) {
75              return FacesContext.getCurrentInstance();
76          } else if ("header".equals(name)) {
77              return external().getRequestHeaderMap();
78          } else if ("headerValues".equals(name)) {
79              return external().getRequestHeaderValuesMap();
80          } else if ("param".equals(name)) {
81              return external().getRequestParameterMap();
82          } else if ("paramValues".equals(name)) {
83              return external().getRequestParameterValuesMap();
84          } else if ("request".equals(name)) {
85              return external().getRequest();
86          } else if ("requestScope".equals(name)) {
87              return external().getRequestMap();
88          } else if ("response".equals(name)) {
89              return external().getResponse();
90          } else if ("session".equals(name)) {
91              return external().getSession(true);
92          } else if ("sessionScope".equals(name)) {
93              return external().getSessionMap();
94          } else if ("view".equals(name)) {
95              return FacesContext.getCurrentInstance().getViewRoot();
96          }
97  
98          // Search ascending scopes for non-magic names
99          Map map = null;
100         map = external().getRequestMap();
101         if (map.containsKey(name)) {
102             return map.get(name);
103         }
104         map = external().getSessionMap();
105         if ((map != null) && (map.containsKey(name))) {
106             return map.get(name);
107         }
108         map = external().getApplicationMap();
109         if (map.containsKey(name)) {
110             return map.get(name);
111         }
112 
113         // No such variable can be found
114         return null;
115 
116     }
117 
118 
119 
120     // --------------------------------------------------------- Private Methods
121 
122 
123     /***
124      * <p>Return the <code>ExternalContext</code> for this request.</p>
125      */
126     private ExternalContext external() {
127 
128         return FacesContext.getCurrentInstance().getExternalContext();
129 
130     }
131 
132 
133 }