001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License version 2
011: * as published by the Free Software Foundation.
012: *
013: * Resin Open Source is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
016: * of NON-INFRINGEMENT. See the GNU General Public License for more
017: * details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with Resin Open Source; if not, write to the
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.jsf.cfg;
030:
031: import java.lang.reflect.*;
032: import java.util.*;
033:
034: import javax.el.*;
035:
036: import javax.faces.*;
037: import javax.faces.application.*;
038: import javax.faces.component.*;
039: import javax.faces.component.html.*;
040: import javax.faces.context.*;
041: import javax.faces.convert.*;
042: import javax.faces.el.*;
043: import javax.faces.event.*;
044: import javax.faces.validator.*;
045:
046: import javax.xml.bind.annotation.*;
047:
048: import com.caucho.config.*;
049: import com.caucho.config.types.*;
050: import com.caucho.jsf.el.*;
051: import com.caucho.util.*;
052:
053: public class ELValue implements AbstractValue {
054: private static final L10N L = new L10N(ELValue.class);
055:
056: private static final HashMap<String, Integer> _implicitMap = new HashMap<String, Integer>();
057:
058: private final String _exprString;
059: private final Class _type;
060:
061: private ValueExpression _expr;
062:
063: private int _scope = Integer.MAX_VALUE;
064:
065: ELValue(String exprString, Class type) {
066: _exprString = exprString;
067: _type = type;
068: }
069:
070: public Object getValue(FacesContext context) {
071: ELContext elContext = context.getELContext();
072:
073: if (_expr == null) {
074: Application app = context.getApplication();
075: ExpressionFactory factory = app.getExpressionFactory();
076:
077: factory.createValueExpression(
078: new ScopeELContext(elContext), _exprString, _type);
079:
080: _expr = factory.createValueExpression(elContext,
081: _exprString, _type);
082: }
083:
084: if (_scope < Integer.MAX_VALUE) {
085: ManagedBeanELResolver.Scope scope;
086: scope = (ManagedBeanELResolver.Scope) elContext
087: .getContext(ManagedBeanELResolver.Scope.class);
088:
089: if (scope != null && scope.getScope() < _scope)
090: throw new ELException(L
091: .l("implicit scope is too short."));
092: }
093:
094: Object value = _expr.getValue(elContext);
095:
096: if (elContext.isPropertyResolved())
097: return value;
098: else
099: return null;
100: }
101:
102: class ScopeELContext extends ELContext {
103: private ELContext _elContext;
104:
105: ScopeELContext(ELContext elContext) {
106: _elContext = elContext;
107: }
108:
109: public ELResolver getELResolver() {
110: return _elContext.getELResolver();
111: }
112:
113: public javax.el.FunctionMapper getFunctionMapper() {
114: return _elContext.getFunctionMapper();
115: }
116:
117: public javax.el.VariableMapper getVariableMapper() {
118: return new ImplicitVariableMapper();
119: }
120: }
121:
122: class ImplicitVariableMapper extends VariableMapper {
123: public ValueExpression resolveVariable(String variable) {
124: Integer objValue = _implicitMap.get(variable);
125:
126: if (objValue != null) {
127: int value = objValue;
128:
129: if (value < _scope)
130: _scope = value;
131: }
132:
133: return null;
134: }
135:
136: public ValueExpression setVariable(String variable,
137: ValueExpression expr) {
138: return expr;
139: }
140: }
141:
142: static {
143: _implicitMap.put("application", 1);
144: _implicitMap.put("applicationScope", 1);
145:
146: _implicitMap.put("session", 2);
147: _implicitMap.put("sessionScope", 2);
148:
149: _implicitMap.put("request", 3);
150: _implicitMap.put("requestScope", 3);
151: _implicitMap.put("view", 3);
152: _implicitMap.put("cookie", 3);
153: _implicitMap.put("param", 3);
154: _implicitMap.put("paramValues", 3);
155: _implicitMap.put("header", 3);
156: _implicitMap.put("headerValues", 3);
157: }
158: }
|