View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.el;
20  
21  import javax.el.ELContext;
22  import javax.el.ELException;
23  import javax.el.ValueExpression;
24  import javax.faces.application.Application;
25  import javax.faces.context.FacesContext;
26  import javax.faces.el.EvaluationException;
27  import javax.faces.el.VariableResolver;
28  
29  import org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver;
30  import org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.Scope;
31  
32  /**
33   * This variable resolver will be used for legacy variable resolvers which are registered through the faces config. If
34   * it is invoked through the faces chain it will use the el resolver of {@link Application#getELResolver()}. If it is
35   * invoked through the jsp chain it will create an value expression through {@link Application#getExpressionFactory()}
36   * to resolve the variable.
37   * 
38   * @author Manfred Geiler (latest modification by $Author$)
39   * @author Anton Koinov
40   * @author Mathias Broekelmann
41   * @version $Revision$ $Date$
42   */
43  @SuppressWarnings("deprecation")
44  public final class VariableResolverImpl extends VariableResolver
45  {
46      @Override
47      public Object resolveVariable(final FacesContext context, final String name) throws EvaluationException
48      {
49          if (context == null)
50          {
51              throw new NullPointerException("context must not be null");
52          }
53          if (name == null)
54          {
55              throw new NullPointerException("name must not be null");
56          }
57  
58          try
59          {
60              final Scope scope = getScope(context);
61              final ELContext elcontext = context.getELContext();
62              final Application application = context.getApplication();
63              if (Scope.Faces.equals(scope))
64              {
65                  return application.getELResolver().getValue(elcontext, null, name);
66              }
67              else if (Scope.JSP.equals(scope))
68              {
69                  ValueExpression expression = application.getExpressionFactory().createValueExpression(elcontext,
70                          "#{" + name + "}", Object.class);
71                  return expression.getValue(elcontext);
72              }
73              else
74              {
75                  throw new IllegalStateException("unknown scope defined: " + scope);
76              }
77          }
78          catch (ELException e)
79          {
80              throw new EvaluationException(e.getMessage(), e);
81          }
82      }
83  
84      protected Scope getScope(final FacesContext context)
85      {
86          return (Scope) context.getAttributes().get(FacesCompositeELResolver.SCOPE);
87      }
88  }