View Javadoc

1   /*
2    * $Id: StrutsVariableResolver.java 440597 2006-09-06 03:34:39Z wsmoak $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.jsf;
19  
20  import javax.faces.context.FacesContext;
21  import javax.faces.el.EvaluationException;
22  import javax.faces.el.VariableResolver;
23  
24  import com.opensymphony.xwork2.util.OgnlValueStack;
25  import com.opensymphony.xwork2.ActionContext;
26  
27  /***
28   * Will return a reference to the current action if the action name matches the
29   * requested variable name. Otherwise it will attempt to resolve the name from
30   * the value stack. Otherwise it will delegate to the original jsf resolver.
31   */
32  public class StrutsVariableResolver extends VariableResolver {
33  
34      /*** The original <code>VariableResolver</code> passed to our constructor. */
35      private VariableResolver original = null;
36  
37      /*** The variable name of our Struts action */
38      private static final String STRUTS_VARIABLE_NAME = "action";
39  
40      /***
41       * Constructor
42       * 
43       * @param original
44       *            Original resolver to delegate to.
45       */
46      public StrutsVariableResolver(VariableResolver original) {
47  
48          this.original = original;
49  
50      }
51  
52      /***
53       * <p>
54       * Will return a reference to the current action if the action name matches
55       * the requested variable name. Otherwise it will attempt to resolve the
56       * name from the value stack. Otherwise it will delegate to the original jsf
57       * resolver.
58       * </p>
59       * 
60       * @param name
61       *            Variable name to be resolved
62       */
63      public Object resolveVariable(FacesContext context, String name)
64              throws EvaluationException {
65  
66          if (STRUTS_VARIABLE_NAME.equals(name)) {
67              return ActionContext.getContext().getActionInvocation().getAction();
68          }
69          
70          Object obj = ActionContext.getContext().getValueStack().findValue(name);
71          if (obj != null) {
72              return obj;
73          } else {
74              return original.resolveVariable(context, name);
75          }
76  
77      }
78  
79  }