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 as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jsf.el;
031:
032: import com.caucho.el.AbstractVariableResolver;
033: import com.caucho.jsf.application.*;
034: import com.caucho.jsf.cfg.*;
035: import com.caucho.jsp.el.*;
036: import com.caucho.webbeans.el.*;
037:
038: import javax.el.*;
039: import javax.faces.component.*;
040: import javax.faces.context.*;
041: import java.beans.FeatureDescriptor;
042: import java.util.*;
043:
044: /**
045: * Variable resolution for JSF variables
046: */
047: public class FacesJspELResolver extends ELResolver {
048: private static final ArrayList<FeatureDescriptor> _implicitFeatureDescriptors = new ArrayList<FeatureDescriptor>();
049:
050: private static HashMap<String, Type> _typeMap = new HashMap<String, Type>();
051:
052: private ApplicationImpl _app;
053: private ELResolver _managedBeanResolver;
054: private ELResolver _webBeansResolver;
055: private ELResolver _resourceBundleResolver;
056:
057: public FacesJspELResolver(ApplicationImpl app) {
058: _app = app;
059:
060: FacesContextELResolver facesResolver = (FacesContextELResolver) _app
061: .getELResolver();
062:
063: _managedBeanResolver = facesResolver.getManagedBeanResolver();
064: _webBeansResolver = new WebBeansELResolver();
065: _resourceBundleResolver = facesResolver
066: .getResourceBundleResolver();
067: }
068:
069: @Override
070: public Class<?> getCommonPropertyType(ELContext env, Object base) {
071: return null;
072: }
073:
074: private static Class common(Class a, Class b) {
075: if (a == null)
076: return b;
077: else if (b == null)
078: return a;
079: else if (a.isAssignableFrom(b))
080: return a;
081: else if (b.isAssignableFrom(a))
082: return b;
083: else
084: // XXX:
085: return Object.class;
086: }
087:
088: @Override
089: public Iterator<FeatureDescriptor> getFeatureDescriptors(
090: ELContext env, Object base) {
091: ArrayList<FeatureDescriptor> descriptors = new ArrayList<FeatureDescriptor>();
092:
093: descriptors.addAll(_implicitFeatureDescriptors);
094:
095: addDescriptors(descriptors, _managedBeanResolver
096: .getFeatureDescriptors(env, base));
097:
098: addDescriptors(descriptors, _webBeansResolver
099: .getFeatureDescriptors(env, base));
100:
101: addDescriptors(descriptors, _resourceBundleResolver
102: .getFeatureDescriptors(env, base));
103:
104: return descriptors.iterator();
105: }
106:
107: private void addDescriptors(
108: ArrayList<FeatureDescriptor> descriptors,
109: Iterator<FeatureDescriptor> iter) {
110: if (iter == null)
111: return;
112:
113: while (iter.hasNext()) {
114: FeatureDescriptor desc = iter.next();
115:
116: descriptors.add(desc);
117: }
118: }
119:
120: @Override
121: public Class getType(ELContext env, Object base, Object property) {
122: if (base == null && property instanceof String) {
123: Type type = _typeMap.get((String) property);
124:
125: if (type != null) {
126: env.setPropertyResolved(true);
127:
128: return null;
129: }
130: }
131:
132: return null;
133: }
134:
135: @Override
136: public Object getValue(ELContext env, Object base, Object property) {
137: env.setPropertyResolved(false);
138:
139: if (base == null && property instanceof String) {
140: Type type = _typeMap.get((String) property);
141:
142: if (type != null) {
143: env.setPropertyResolved(true);
144:
145: FacesContext facesContext = FacesContext
146: .getCurrentInstance();
147:
148: if (facesContext != null) {
149: switch (type) {
150: case FACES_CONTEXT:
151: return facesContext;
152: case VIEW:
153: return facesContext.getViewRoot();
154: }
155: }
156: }
157:
158: Object value = _webBeansResolver.getValue(env, base,
159: property);
160:
161: if (env.isPropertyResolved())
162: return value;
163:
164: value = _managedBeanResolver.getValue(env, base, property);
165:
166: if (env.isPropertyResolved())
167: return value;
168:
169: value = _resourceBundleResolver.getValue(env, base,
170: property);
171:
172: if (env.isPropertyResolved())
173: return value;
174: }
175:
176: return null;
177: }
178:
179: @Override
180: public boolean isReadOnly(ELContext env, Object base,
181: Object property) {
182: env.setPropertyResolved(false);
183:
184: if (base == null && property instanceof String) {
185: Type type = _typeMap.get((String) property);
186:
187: if (type != null) {
188: env.setPropertyResolved(true);
189:
190: return true;
191: }
192: }
193:
194: return false;
195: }
196:
197: @Override
198: public void setValue(ELContext env, Object base, Object property,
199: Object value) {
200: if (base == null && property instanceof String) {
201: String key = (String) property;
202:
203: Type type = _typeMap.get(key);
204:
205: if (type != null) {
206: throw new PropertyNotWritableException(key);
207: }
208: }
209: }
210:
211: enum Type {
212: FACES_CONTEXT, VIEW
213: };
214:
215: private static void addDescriptor(String name, Class type) {
216: FeatureDescriptor desc = new FeatureDescriptor();
217: desc.setName(name);
218: desc.setDisplayName(name);
219: desc.setExpert(false);
220: desc.setHidden(false);
221: desc.setPreferred(true);
222: desc.setValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME,
223: Boolean.TRUE);
224: desc.setValue(ELResolver.TYPE, type);
225:
226: _implicitFeatureDescriptors.add(desc);
227: }
228:
229: static {
230: _typeMap.put("facesContext", Type.FACES_CONTEXT);
231: _typeMap.put("view", Type.VIEW);
232:
233: addDescriptor("facesContext", FacesContext.class);
234: addDescriptor("view", UIViewRoot.class);
235: }
236: }
|