001: /*
002: * Copyright 2006 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.myfaces.el.convert;
018:
019: import javax.el.ELContext;
020: import javax.el.ELException;
021: import javax.el.ELResolver;
022: import javax.el.PropertyNotFoundException;
023: import javax.el.PropertyNotWritableException;
024: import javax.faces.context.FacesContext;
025: import javax.faces.el.EvaluationException;
026: import javax.faces.el.VariableResolver;
027:
028: import java.beans.FeatureDescriptor;
029: import java.util.Collection;
030: import java.util.HashSet;
031: import java.util.Iterator;
032:
033: /**
034: * Wrapper that converts a VariableResolver into an ELResolver. See JSF 1.2 spec section 5.6.1.5
035: *
036: * @author Stan Silvert (latest modification by $Author$)
037: * @author Mathias Broekelmann
038: * @version $Revision$ $Date$
039: */
040: @SuppressWarnings("deprecation")
041: public class VariableResolverToELResolver extends ELResolver {
042:
043: // holds a flag to check if this instance is already called in current thread
044: private static final ThreadLocal<Collection<String>> propertyGuard = new ThreadLocal<Collection<String>>() {
045: @Override
046: protected Collection<String> initialValue() {
047: return new HashSet<String>();
048: }
049: };
050:
051: private VariableResolver variableResolver;
052:
053: /**
054: * Creates a new instance of VariableResolverToELResolver
055: */
056: public VariableResolverToELResolver(
057: VariableResolver variableResolver) {
058: this .variableResolver = variableResolver;
059: }
060:
061: /**
062: * @return the variableResolver
063: */
064: public VariableResolver getVariableResolver() {
065: return variableResolver;
066: }
067:
068: public Object getValue(ELContext context, Object base,
069: Object property) throws NullPointerException,
070: PropertyNotFoundException, ELException {
071:
072: if (base != null)
073: return null;
074: if (property == null)
075: throw new PropertyNotFoundException();
076:
077: context.setPropertyResolved(true);
078:
079: if (!(property instanceof String))
080: return null;
081:
082: String strProperty = (String) property;
083:
084: Object result = null;
085: try {
086: // only call the resolver if we haven't done it in current stack
087: if (!propertyGuard.get().contains(strProperty)) {
088: propertyGuard.get().add(strProperty);
089: result = variableResolver.resolveVariable(
090: facesContext(context), strProperty);
091: }
092: } catch (javax.faces.el.PropertyNotFoundException e) {
093: context.setPropertyResolved(false);
094: throw new PropertyNotFoundException(e.getMessage(), e);
095: } catch (EvaluationException e) {
096: context.setPropertyResolved(false);
097: throw new ELException(e.getMessage(), e);
098: } catch (RuntimeException e) {
099: context.setPropertyResolved(false);
100: throw e;
101: } finally {
102: propertyGuard.get().remove(strProperty);
103: // set property resolved to false in any case if result is null
104: context.setPropertyResolved(result != null);
105: }
106:
107: return result;
108: }
109:
110: // get the FacesContext from the ELContext
111: private FacesContext facesContext(ELContext context) {
112: return (FacesContext) context.getContext(FacesContext.class);
113: }
114:
115: public Class<?> getCommonPropertyType(ELContext context, Object base) {
116: if (base != null)
117: return null;
118:
119: return String.class;
120: }
121:
122: public void setValue(ELContext context, Object base,
123: Object property, Object value) throws NullPointerException,
124: PropertyNotFoundException, PropertyNotWritableException,
125: ELException {
126:
127: if ((base == null) && (property == null))
128: throw new PropertyNotFoundException();
129: }
130:
131: public boolean isReadOnly(ELContext context, Object base,
132: Object property) throws NullPointerException,
133: PropertyNotFoundException, ELException {
134:
135: if ((base == null) && (property == null))
136: throw new PropertyNotFoundException();
137:
138: return false;
139: }
140:
141: public Class<?> getType(ELContext context, Object base,
142: Object property) throws NullPointerException,
143: PropertyNotFoundException, ELException {
144:
145: if ((base == null) && (property == null))
146: throw new PropertyNotFoundException();
147:
148: return null;
149: }
150:
151: public Iterator<FeatureDescriptor> getFeatureDescriptors(
152: ELContext context, Object base) {
153: return null;
154: }
155:
156: }
|