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.unified.resolver;
018:
019: import java.beans.FeatureDescriptor;
020: import java.util.ArrayList;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import javax.el.ELContext;
026: import javax.el.ELException;
027: import javax.el.ELResolver;
028: import javax.el.PropertyNotFoundException;
029: import javax.el.PropertyNotWritableException;
030: import javax.faces.context.ExternalContext;
031: import javax.faces.context.FacesContext;
032:
033: /**
034: * See JSF 1.2 spec section 5.6.2.7
035: *
036: * @author Stan Silvert
037: */
038: public class ScopedAttributeResolver extends ELResolver {
039:
040: /**
041: * Creates a new instance of ScopedAttributeResolver
042: */
043: public ScopedAttributeResolver() {
044: }
045:
046: public void setValue(ELContext context, Object base,
047: Object property, Object value) throws NullPointerException,
048: PropertyNotFoundException, PropertyNotWritableException,
049: ELException {
050:
051: if (base != null)
052: return;
053: if (property == null)
054: throw new PropertyNotFoundException();
055:
056: Map<String, Object> scopedMap = findScopedMap(
057: externalContext(context), property);
058: if (scopedMap != null) {
059: scopedMap.put((String) property, value);
060: } else {
061: externalContext(context).getRequestMap().put(
062: (String) property, value);
063: }
064:
065: context.setPropertyResolved(true);
066: }
067:
068: public boolean isReadOnly(ELContext context, Object base,
069: Object property) throws NullPointerException,
070: PropertyNotFoundException, ELException {
071:
072: if (base == null)
073: context.setPropertyResolved(true);
074:
075: return false;
076: }
077:
078: public Object getValue(ELContext context, Object base,
079: Object property) throws NullPointerException,
080: PropertyNotFoundException, ELException {
081:
082: if (base != null)
083: return null;
084: if (property == null)
085: throw new PropertyNotFoundException();
086:
087: Map scopedMap = findScopedMap(externalContext(context),
088: property);
089: if (scopedMap != null) {
090: context.setPropertyResolved(true);
091: return scopedMap.get(property);
092: }
093:
094: return null;
095: }
096:
097: public Class<?> getType(ELContext context, Object base,
098: Object property) throws NullPointerException,
099: PropertyNotFoundException, ELException {
100:
101: if (base != null)
102: return null;
103: if (property == null)
104: throw new PropertyNotFoundException();
105:
106: context.setPropertyResolved(true);
107: return Object.class;
108: }
109:
110: public Iterator<FeatureDescriptor> getFeatureDescriptors(
111: ELContext context, Object base) {
112:
113: if (base != null)
114: return null;
115:
116: List<FeatureDescriptor> descriptorList = new ArrayList<FeatureDescriptor>();
117: ExternalContext extContext = externalContext(context);
118: addDescriptorsToList(descriptorList, extContext.getRequestMap());
119: addDescriptorsToList(descriptorList, extContext.getSessionMap());
120: addDescriptorsToList(descriptorList, extContext
121: .getApplicationMap());
122:
123: return descriptorList.iterator();
124: }
125:
126: public Class<?> getCommonPropertyType(ELContext context, Object base) {
127:
128: if (base != null)
129: return null;
130:
131: return String.class;
132: }
133:
134: // side effect: modifies the list
135: private void addDescriptorsToList(
136: List<FeatureDescriptor> descriptorList, Map scopeMap) {
137: for (Object name : scopeMap.keySet()) {
138: String strName = (String) name;
139: Class runtimeType = scopeMap.get(strName).getClass();
140: descriptorList.add(makeDescriptor(strName, runtimeType));
141: }
142: }
143:
144: private FeatureDescriptor makeDescriptor(String name,
145: Class runtimeType) {
146: FeatureDescriptor fd = new FeatureDescriptor();
147: fd.setValue(ELResolver.RESOLVABLE_AT_DESIGN_TIME, Boolean.TRUE);
148: fd.setValue(ELResolver.TYPE, runtimeType);
149: fd.setName(name);
150: fd.setDisplayName(name);
151: fd.setShortDescription(name);
152: fd.setExpert(false);
153: fd.setHidden(false);
154: fd.setPreferred(true);
155: return fd;
156: }
157:
158: // returns null if not found
159: private Map<String, Object> findScopedMap(
160: ExternalContext extContext, Object property) {
161:
162: if (extContext == null)
163: return null;
164:
165: Map<String, Object> scopedMap = extContext.getRequestMap();
166: if (scopedMap.containsKey(property))
167: return scopedMap;
168:
169: scopedMap = extContext.getSessionMap();
170: if (scopedMap.containsKey(property))
171: return scopedMap;
172:
173: scopedMap = extContext.getApplicationMap();
174: if (scopedMap.containsKey(property))
175: return scopedMap;
176:
177: return null;
178: }
179:
180: // get the FacesContext from the ELContext
181: private FacesContext facesContext(ELContext context) {
182: return (FacesContext) context.getContext(FacesContext.class);
183: }
184:
185: private ExternalContext externalContext(ELContext context) {
186: FacesContext facesContext = facesContext(context);
187: if (facesContext != null) {
188: return facesContext(context).getExternalContext();
189: }
190:
191: return null;
192: }
193:
194: }
|