01: /*
02: * Copyright 2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.apache.myfaces.el;
17:
18: import org.apache.myfaces.el.unified.resolver.FacesCompositeELResolver.Scope;
19:
20: import javax.el.ELContext;
21: import javax.el.ELException;
22: import javax.el.ValueExpression;
23: import javax.faces.application.Application;
24: import javax.faces.context.FacesContext;
25: import javax.faces.el.EvaluationException;
26: import javax.faces.el.VariableResolver;
27:
28: /**
29: * This variable resolver will be used for legacy variable resolvers which are registered through the faces config. If
30: * it is invoked through the faces chain it will use the el resolver of {@link Application#getELResolver()}. If it is
31: * invoked through the jsp chain it will create an value expression through {@link Application#getExpressionFactory()}
32: * to resolve the variable.
33: *
34: * @author Manfred Geiler (latest modification by $Author: mbr $)
35: * @author Anton Koinov
36: * @author Mathias Broekelmann
37: * @version $Revision: 511514 $ $Date: 2007-02-25 14:47:48 +0100 (So, 25 Feb 2007) $
38: */
39: @SuppressWarnings("deprecation")
40: public class VariableResolverImpl extends VariableResolver {
41: @Override
42: public Object resolveVariable(FacesContext context, String name)
43: throws EvaluationException {
44: if (context == null) {
45: throw new NullPointerException("context must not be null");
46: }
47: if (name == null) {
48: throw new NullPointerException("name must not be null");
49: }
50:
51: try {
52: Scope scope = getScope(context);
53: ELContext elcontext = context.getELContext();
54: Application application = context.getApplication();
55: if (Scope.Faces.equals(scope)) {
56: return application.getELResolver().getValue(elcontext,
57: null, name);
58: } else if (Scope.JSP.equals(scope)) {
59: ValueExpression expression = application
60: .getExpressionFactory().createValueExpression(
61: elcontext, "#{" + name + "}",
62: Object.class);
63: return expression.getValue(elcontext);
64: } else {
65: throw new IllegalStateException(
66: "unknown scope defined: " + scope);
67: }
68: } catch (ELException e) {
69: throw new EvaluationException(e.getMessage(), e);
70: }
71: }
72:
73: protected Scope getScope(FacesContext context) {
74: return (Scope) context.getExternalContext().getRequestMap()
75: .get(Scope.class.getName());
76: }
77: }
|