View Javadoc

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